forked from envoyproxy/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add control plane metrics library
Signed-off-by: bitliu <[email protected]>
- Loading branch information
Showing
22 changed files
with
971 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package metrics | ||
|
||
// A Metric collects numerical observations. | ||
type Metric interface { | ||
// Name returns the name value of a Metric. | ||
Name() string | ||
|
||
// Record makes an observation of the provided value for the given measure. | ||
Record(value float64) | ||
|
||
// RecordInt makes an observation of the provided value for the measure. | ||
RecordInt(value int64) | ||
|
||
// Increment records a value of 1 for the current measure. | ||
// For Counters, this is equivalent to adding 1 to the current value. | ||
// For Gauges, this is equivalent to setting the value to 1. | ||
// For Histograms, this is equivalent to making an observation of value 1. | ||
Increment() | ||
|
||
// Decrement records a value of -1 for the current measure. | ||
// For Counters, this is equivalent to subtracting -1 to the current value. | ||
// For Gauges, this is equivalent to setting the value to -1. | ||
// For Histograms, this is equivalent to making an observation of value -1. | ||
Decrement() | ||
|
||
// With creates a new Metric, with the LabelValues provided. | ||
// This allows creating a set of pre-dimensioned data for recording purposes. | ||
// This is primarily used for documentation and convenience. | ||
// Metrics created with this method do not need to be registered (they share the registration of their parent Metric). | ||
With(labelValues ...LabelValue) Metric | ||
} | ||
|
||
// Label holds a metric dimension which can be operated on using the interface | ||
// methods. | ||
type Label interface { | ||
// Value will set the provided value for the Label. | ||
Value(value string) LabelValue | ||
} | ||
|
||
// LabelValue holds an action to take on a metric dimension's value. | ||
type LabelValue interface { | ||
// Key will get the key of the Label. | ||
Key() Label | ||
// Value will get the value of the Label. | ||
Value() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package metrics | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"go.opentelemetry.io/otel" | ||
api "go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
|
||
"github.com/envoyproxy/gateway/api/v1alpha1" | ||
log "github.com/envoyproxy/gateway/internal/logging" | ||
) | ||
|
||
var ( | ||
meter = func() api.Meter { | ||
return otel.GetMeterProvider().Meter("envoy-gateway") | ||
} | ||
|
||
metricsLogger = log.DefaultLogger(v1alpha1.LogLevelInfo).WithName("metrics") | ||
) | ||
|
||
func init() { | ||
otel.SetLogger(metricsLogger.Logger) | ||
} | ||
|
||
// MetricType is the type of a metric. | ||
type MetricType string | ||
|
||
// Metric type supports: | ||
// * Counter: A Counter is a simple metric that only goes up (increments). | ||
// | ||
// * Gauge: A Gauge is a metric that represent | ||
// a single numerical value that can arbitrarily go up and down. | ||
// | ||
// * Histogram: A Histogram samples observations and counts them in configurable buckets. | ||
// It also provides a sum of all observed values. | ||
// It's used to visualize the statistical distribution of these observations. | ||
|
||
const ( | ||
CounterType MetricType = "Counter" | ||
GaugeType MetricType = "Gauge" | ||
HistogramType MetricType = "Histogram" | ||
) | ||
|
||
// Metadata records a metric's metadata. | ||
type Metadata struct { | ||
Name string | ||
Type MetricType | ||
Description string | ||
Bounds []float64 | ||
} | ||
|
||
// metrics stores stores metrics | ||
type metricstore struct { | ||
started bool | ||
mu sync.Mutex | ||
stores map[string]Metadata | ||
} | ||
|
||
// stores is a global that stores all registered metrics | ||
var stores = metricstore{ | ||
stores: map[string]Metadata{}, | ||
} | ||
|
||
// register records a newly defined metric. Only valid before an exporter is set. | ||
func (d *metricstore) register(metricstore Metadata) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if d.started { | ||
metricsLogger.Error(errors.New("cannot initialize metric after metric has started"), "metric", metricstore.Name) | ||
} | ||
d.stores[metricstore.Name] = metricstore | ||
} | ||
|
||
// preAddOptions runs pre-run steps before adding to meter provider. | ||
func (d *metricstore) preAddOptions() []metric.Option { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
d.started = true | ||
opts := []metric.Option{} | ||
for name, metricstore := range d.stores { | ||
if metricstore.Bounds == nil { | ||
continue | ||
} | ||
// for each histogram metric (i.e. those with bounds), set up a view explicitly defining those buckets. | ||
v := metric.WithView(metric.NewView( | ||
metric.Instrument{Name: name}, | ||
metric.Stream{ | ||
Aggregation: metric.AggregationExplicitBucketHistogram{ | ||
Boundaries: metricstore.Bounds, | ||
}}, | ||
)) | ||
opts = append(opts, v) | ||
} | ||
return opts | ||
} |
Oops, something went wrong.