From 6fd5b547ea650e880b7aef1509100dab47cac914 Mon Sep 17 00:00:00 2001 From: Pablo Aguilar Date: Thu, 2 May 2024 10:16:47 -0300 Subject: [PATCH] Improve otlp metrics (#3040) * feat: put resource attributes to metric exporter Signed-off-by: Pablo Aguilar * chore: add meter provider shutdown method to be called Signed-off-by: Pablo Aguilar * feat: add runtime metrics when OLTP is enabled Signed-off-by: Pablo Aguilar * chore: go mod tidy Signed-off-by: Pablo Aguilar --------- Signed-off-by: Pablo Aguilar Co-authored-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --- go.mod | 1 + go.sum | 2 ++ internal/cmd/grpc.go | 22 +++++++++++++++++++++- internal/metrics/metrics.go | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b35abd5d3f..154815122b 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( go.flipt.io/flipt/rpc/flipt v1.38.0 go.flipt.io/flipt/sdk/go v0.11.0 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 + go.opentelemetry.io/contrib/instrumentation/runtime v0.51.0 go.opentelemetry.io/contrib/propagators/autoprop v0.50.0 go.opentelemetry.io/otel v1.26.0 go.opentelemetry.io/otel/exporters/jaeger v1.17.0 diff --git a/go.sum b/go.sum index 6d46072be4..0556df805e 100644 --- a/go.sum +++ b/go.sum @@ -736,6 +736,8 @@ go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.5 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0/go.mod h1:27iA5uvhuRNmalO+iEUdVn5ZMj2qy10Mm+XRIpRmyuU= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/contrib/instrumentation/runtime v0.51.0 h1:1tBjncp/Rr5iuV0WfdKGGynrzIJ8bMm5z7Zl6jMjfIE= +go.opentelemetry.io/contrib/instrumentation/runtime v0.51.0/go.mod h1:6MqTuVXkhmzrIc7SFHYVTo7N6OFvVpDH5eq5xXKpAZQ= go.opentelemetry.io/contrib/propagators/autoprop v0.50.0 h1:tK1hZrY9rV784YPGAUACvqBjvFCim2rrJZR/09giyrA= go.opentelemetry.io/contrib/propagators/autoprop v0.50.0/go.mod h1:oTzb+geTS8mHaeIOYd/1AShfxdovU3AA1/m+IrheAvc= go.opentelemetry.io/contrib/propagators/aws v1.25.0 h1:LYKyPhf1q+1ok4UUxcmQ2sERvWcUylg4v8MK+h8nCcA= diff --git a/internal/cmd/grpc.go b/internal/cmd/grpc.go index 40624b1e7b..280706c150 100644 --- a/internal/cmd/grpc.go +++ b/internal/cmd/grpc.go @@ -10,6 +10,8 @@ import ( "sync" "time" + otlpRuntime "go.opentelemetry.io/contrib/instrumentation/runtime" + "go.opentelemetry.io/contrib/propagators/autoprop" sq "github.com/Masterminds/squirrel" @@ -154,6 +156,11 @@ func NewGRPCServer( // Initialize metrics exporter if enabled if cfg.Metrics.Enabled { + metricsResource, err := metrics.GetResources(ctx) + if err != nil { + return nil, fmt.Errorf("creating metrics resource: %w", err) + } + metricExp, metricExpShutdown, err := metrics.GetExporter(ctx, &cfg.Metrics) if err != nil { return nil, fmt.Errorf("creating metrics exporter: %w", err) @@ -161,8 +168,21 @@ func NewGRPCServer( server.onShutdown(metricExpShutdown) - meterProvider := metricsdk.NewMeterProvider(metricsdk.WithReader(metricExp)) + meterProvider := metricsdk.NewMeterProvider( + metricsdk.WithResource(metricsResource), + metricsdk.WithReader(metricExp), + ) otel.SetMeterProvider(meterProvider) + server.onShutdown(meterProvider.Shutdown) + + // We only want to start the runtime metrics by open telemetry if the user have chosen + // to use OTLP because the Prometheus endpoint already exposes those metrics. + if cfg.Metrics.Exporter == config.MetricsOTLP { + err = otlpRuntime.Start(otlpRuntime.WithMeterProvider(meterProvider)) + if err != nil { + return nil, fmt.Errorf("starting runtime metric exporter: %w", err) + } + } logger.Debug("otel metrics enabled", zap.String("exporter", string(cfg.Metrics.Exporter))) } diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 42e60bfe65..e8cc466e03 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -6,6 +6,9 @@ import ( "net/url" "sync" + "go.opentelemetry.io/otel/sdk/resource" + semconv "go.opentelemetry.io/otel/semconv/v1.24.0" + "go.flipt.io/flipt/internal/config" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" @@ -222,3 +225,18 @@ func GetExporter(ctx context.Context, cfg *config.MetricsConfig) (sdkmetric.Read return metricExp, metricExpFunc, metricExpErr } + +func GetResources(ctx context.Context) (*resource.Resource, error) { + return resource.New( + ctx, + resource.WithSchemaURL(semconv.SchemaURL), + resource.WithAttributes( + semconv.ServiceName("flipt"), + ), + resource.WithFromEnv(), + resource.WithTelemetrySDK(), + resource.WithHost(), + resource.WithProcessRuntimeVersion(), + resource.WithProcessRuntimeName(), + ) +}