Skip to content

Commit

Permalink
Merge pull request #201 from heroku/aa-upgrade-opentelemetry-go
Browse files Browse the repository at this point in the history
Upgrade to latest opentelemetry-go and use exponential histograms by default
  • Loading branch information
alexmarnell authored Nov 9, 2023
2 parents 0850add + 4f98043 commit 9d25edf
Show file tree
Hide file tree
Showing 12 changed files with 718 additions and 821 deletions.
3 changes: 3 additions & 0 deletions cmdutil/metrics/otel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ type Config struct {
CollectorURL *url.URL `env:"OTEL_COLLECTOR_URL"`
MetricsDestinations []string `env:"OTEL_METRICS_DESTINATIONS,default=honeycomb;argus"`
Honeycomb honeycomb.Config

// EndpointURL maps to the official opentelemetry environment variable for configuring the endpoint
EndpointURL *url.URL `env:"OTEL_EXPORTER_OTLP_ENDPOINT"`
}
50 changes: 29 additions & 21 deletions cmdutil/metrics/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"strings"

"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
"go.opentelemetry.io/otel/sdk/export/metric"

"github.com/heroku/x/go-kit/metrics"
"github.com/heroku/x/go-kit/metrics/provider/otel"
Expand All @@ -19,28 +16,43 @@ func MustProvider(ctx context.Context, logger logrus.FieldLogger, cfg Config, se
// This provider is used for metrics reporting to the collector.
logger.WithField("metrics_destinations", strings.Join(cfg.MetricsDestinations, ",")).Info("setting up provider")

if cfg.CollectorURL == nil {
logger.Fatal("provider collectorURL cannot be nil")
// to allow transitioning between endpoints first check if the newer env is present
endpoint := cfg.EndpointURL
if endpoint == nil {
endpoint = cfg.CollectorURL
}

client := otel.NewHTTPClient(*cfg.CollectorURL)
expOpts := otlpmetric.WithMetricExportKindSelector(metric.DeltaExportKindSelector())
exporter := otlpmetric.NewUnstarted(client, expOpts)
if endpoint == nil {
logger.Fatal("provider collectorURL cannot be nil")
}

attrs := []attribute.KeyValue{}
// configure some optional resource attributes
attrs := otel.MetricsDestinations(cfg.MetricsDestinations)
if cfg.Honeycomb.MetricsDataset != "" {
attrs = append(attrs, attribute.String("dataset", cfg.Honeycomb.MetricsDataset))
}
for _, md := range cfg.MetricsDestinations {
attrs = append(attrs, attribute.String(md, "true"))
attrs = append(attrs, otel.HoneycombDataset(cfg.Honeycomb.MetricsDataset))
}

allOpts := []otel.Option{
otel.WithExporter(exporter),
// ensure we have service.id, service.namespace, and service.instance.id attributes
otel.WithOpenTelemetryStandardService(service, serviceNamespace, serviceInstanceID),

// ensure we have _service and component attributes
otel.WithServiceStandard(service),

// ensure we have stage and _subservice attributes
otel.WithEnvironmentStandard(stage),

// if set, ensure we have honeycomb dataset and metrics destination attributes set
otel.WithAttributes(attrs...),
otel.WithServiceNamespaceAttribute(serviceNamespace),
otel.WithServiceInstanceIDAttribute(serviceInstanceID),
otel.WithStageAttribute(stage),

// use the exponential aggregation selector, exponential histograms are generally easier to use than explicit
otel.WithExponentialAggregation(),

// use Delta Temporality by default
otel.WithDeltaTemporality(),

// ensure we use the http exporter
otel.WithHTTPEndpointExporter(endpoint.String()),
}
allOpts = append(allOpts, opts...)

Expand All @@ -49,9 +61,5 @@ func MustProvider(ctx context.Context, logger logrus.FieldLogger, cfg Config, se
logger.Fatal(err)
}

if err := otelProvider.(*otel.Provider).Start(); err != nil {
logger.WithError(err).Fatal("failed to start metrics provider")
}

return otelProvider
}
32 changes: 0 additions & 32 deletions go-kit/metrics/provider/otel/client.go

This file was deleted.

Loading

0 comments on commit 9d25edf

Please sign in to comment.