From f21ee8eb6fb5a7ae18dc357b667720aa7ffe5337 Mon Sep 17 00:00:00 2001 From: bitliu Date: Thu, 26 Oct 2023 10:27:05 +0800 Subject: [PATCH] update Signed-off-by: bitliu --- api/v1alpha1/shared_types.go | 4 ++++ internal/metrics/metadata.go | 18 +++++++++--------- internal/metrics/otel_metric_counter.go | 2 +- internal/metrics/register.go | 9 +++++---- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/api/v1alpha1/shared_types.go b/api/v1alpha1/shared_types.go index 588eefebb47e..bbda8a16ec32 100644 --- a/api/v1alpha1/shared_types.go +++ b/api/v1alpha1/shared_types.go @@ -21,6 +21,10 @@ const ( DefaultEnvoyProxyImage = "envoyproxy/envoy-dev:latest" // DefaultRateLimitImage is the default image used by ratelimit. DefaultRateLimitImage = "envoyproxy/ratelimit:master" + // HTTPProtocol is the common-used http protocol. + HTTPProtocol = "http" + // GRPCProtocol is the common-used grpc protocol. + GRPCProtocol = "grpc" ) // GroupVersionKind unambiguously identifies a Kind. diff --git a/internal/metrics/metadata.go b/internal/metrics/metadata.go index 59d8d37323ca..f2ab84984070 100644 --- a/internal/metrics/metadata.go +++ b/internal/metrics/metadata.go @@ -57,35 +57,35 @@ type Metadata struct { } // metrics stores stores metrics -type metricstore struct { +type store struct { started bool mu sync.Mutex stores map[string]Metadata } // stores is a global that stores all registered metrics -var stores = metricstore{ +var stores = store{ stores: map[string]Metadata{}, } // register records a newly defined metric. Only valid before an exporter is set. -func (d *metricstore) register(metricstore Metadata) { +func (d *store) register(store 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) + metricsLogger.Error(errors.New("cannot initialize metric after metric has started"), "metric", store.Name) } - d.stores[metricstore.Name] = metricstore + d.stores[store.Name] = store } // preAddOptions runs pre-run steps before adding to meter provider. -func (d *metricstore) preAddOptions() []metric.Option { +func (d *store) 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 { + for name, store := range d.stores { + if store.Bounds == nil { continue } // for each histogram metric (i.e. those with bounds), set up a view explicitly defining those buckets. @@ -93,7 +93,7 @@ func (d *metricstore) preAddOptions() []metric.Option { metric.Instrument{Name: name}, metric.Stream{ Aggregation: metric.AggregationExplicitBucketHistogram{ - Boundaries: metricstore.Bounds, + Boundaries: store.Bounds, }}, )) opts = append(opts, v) diff --git a/internal/metrics/otel_metric_counter.go b/internal/metrics/otel_metric_counter.go index b5695e410a7e..93dcaa136503 100644 --- a/internal/metrics/otel_metric_counter.go +++ b/internal/metrics/otel_metric_counter.go @@ -32,7 +32,7 @@ func (f *Counter) Increment() { } func (f *Counter) Decrement() { - f.Add(1) + f.Add(-1) } func (f *Counter) With(labelValues ...LabelValue) *Counter { diff --git a/internal/metrics/register.go b/internal/metrics/register.go index f83f876c75e8..627a49355521 100644 --- a/internal/metrics/register.go +++ b/internal/metrics/register.go @@ -8,6 +8,7 @@ package metrics import ( "context" "fmt" + "net" "net/http" "time" @@ -73,7 +74,7 @@ func start(address string, handler http.Handler) error { func newOptions(svr *config.Server) registerOptions { newOpts := registerOptions{} - newOpts.address = fmt.Sprintf("%s:%d", v1alpha1.GatewayMetricsHost, v1alpha1.GatewayMetricsPort) + newOpts.address = net.JoinHostPort(v1alpha1.GatewayMetricsHost, fmt.Sprint(v1alpha1.GatewayMetricsPort)) if !svr.EnvoyGateway.IfDisablePrometheus() { newOpts.pullOptions.disable = true @@ -142,7 +143,7 @@ func registerOTELPromExporter(otelOpts *[]metric.Option, opts registerOptions) e // registerOTELHTTPexporter registers OTEL HTTP metrics exporter (PUSH mode). func registerOTELHTTPexporter(otelOpts *[]metric.Option, opts registerOptions) error { for _, sink := range opts.pushOptions.sinks { - if sink.protocol == "http" { + if sink.protocol == v1alpha1.HTTPProtocol { address := fmt.Sprintf("%s:%d", sink.host, sink.port) httpexporter, err := otlpmetrichttp.New( context.Background(), @@ -165,8 +166,8 @@ func registerOTELHTTPexporter(otelOpts *[]metric.Option, opts registerOptions) e // registerOTELgRPCexporter registers OTEL gRPC metrics exporter (PUSH mode). func registerOTELgRPCexporter(otelOpts *[]metric.Option, opts registerOptions) error { for _, sink := range opts.pushOptions.sinks { - if sink.protocol == "grpc" { - address := fmt.Sprintf("%s:%d", sink.host, sink.port) + if sink.protocol == v1alpha1.GRPCProtocol { + address := net.JoinHostPort(sink.host, fmt.Sprint(sink.port)) httpexporter, err := otlpmetricgrpc.New( context.Background(), otlpmetricgrpc.WithEndpoint(address),