Skip to content

Commit

Permalink
Improve otlp metrics (#3040)
Browse files Browse the repository at this point in the history
* feat: put resource attributes to metric exporter

Signed-off-by: Pablo Aguilar <[email protected]>

* chore: add meter provider shutdown method to be called

Signed-off-by: Pablo Aguilar <[email protected]>

* feat: add runtime metrics when OLTP is enabled

Signed-off-by: Pablo Aguilar <[email protected]>

* chore: go mod tidy

Signed-off-by: Pablo Aguilar <[email protected]>

---------

Signed-off-by: Pablo Aguilar <[email protected]>
Co-authored-by: Mark Phelps <[email protected]>
  • Loading branch information
thepabloaguilar and markphelps authored May 2, 2024
1 parent 4b3520e commit 6fd5b54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 21 additions & 1 deletion internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -154,15 +156,33 @@ 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)
}

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)))
}
Expand Down
18 changes: 18 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
)
}

0 comments on commit 6fd5b54

Please sign in to comment.