Skip to content

Commit

Permalink
share prometheus registry between oc and otel exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
paivagustavo committed Oct 13, 2022
1 parent a9f41a2 commit d70fae4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 💡 Enhancements 💡

- Instrument `obsreport.Receiver` metrics with otel-go (#6222)
- Share the same `prometheus.Registerer` for the OpenCensus and OpenTelemetry Go prometheus exporters to act as a bridge for internal telemetry (#6297)

## v0.62.0 Beta

Expand Down
30 changes: 18 additions & 12 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
ocprom "contrib.go.opencensus.io/exporter/prometheus"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
ocmetric "go.opencensus.io/metric"
"go.opencensus.io/metric/metricproducer"
"go.opencensus.io/stats/view"
Expand Down Expand Up @@ -113,11 +112,19 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger

var pe http.Handler
var err error
// This prometheus registry is shared between OpenCensus and OpenTelemetry exporters,
// acting as a bridge between OC and Otel.
// This is used as a path to migrate the existing OpenCensus instrumentation
// to the OpenTelemetry Go SDK without breaking existing metrics.
promRegistry := prometheus.NewRegistry()
if tel.registry.IsEnabled(obsreportconfig.UseOtelForInternalMetricsfeatureGateID) {
pe, err = tel.initOpenTelemetry(telAttrs)
} else {
pe, err = tel.initOpenCensus(cfg, telAttrs)
err = tel.initOpenTelemetry(telAttrs, promRegistry)
if err != nil {
return err
}
}

pe, err = tel.initOpenCensus(cfg, telAttrs, promRegistry)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +183,7 @@ func buildTelAttrs(buildInfo component.BuildInfo, cfg telemetry.Config) map[stri
return telAttrs
}

func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs map[string]string) (http.Handler, error) {
func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs map[string]string, promRegistry *prometheus.Registry) (http.Handler, error) {
tel.ocRegistry = ocmetric.NewRegistry()
metricproducer.GlobalManager().AddProducer(tel.ocRegistry)

Expand All @@ -193,6 +200,7 @@ func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs m
// Until we can use a generic metrics exporter, default to Prometheus.
opts := ocprom.Options{
Namespace: "otelcol",
Registry: promRegistry,
}

opts.ConstLabels = make(map[string]string)
Expand All @@ -210,7 +218,7 @@ func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs m
return pe, nil
}

func (tel *telemetryInitializer) initOpenTelemetry(attrs map[string]string) (http.Handler, error) {
func (tel *telemetryInitializer) initOpenTelemetry(attrs map[string]string, promRegistry prometheus.Registerer) error {
// Initialize the ocRegistry, still used by the process metrics.
tel.ocRegistry = ocmetric.NewRegistry()

Expand All @@ -221,7 +229,7 @@ func (tel *telemetryInitializer) initOpenTelemetry(attrs map[string]string) (htt

res, err := resource.New(context.Background(), resource.WithAttributes(resAttrs...))
if err != nil {
return nil, fmt.Errorf("error creating otel resources: %w", err)
return fmt.Errorf("error creating otel resources: %w", err)
}

exporter := otelprom.New()
Expand All @@ -230,14 +238,12 @@ func (tel *telemetryInitializer) initOpenTelemetry(attrs map[string]string) (htt
sdkmetric.WithReader(exporter),
)

registry := prometheus.NewRegistry()

wrappedRegisterer := prometheus.WrapRegistererWithPrefix("otelcol_", registry)
wrappedRegisterer := prometheus.WrapRegistererWithPrefix("otelcol_", promRegistry)
if err := wrappedRegisterer.Register(exporter.Collector); err != nil {
return nil, fmt.Errorf("failed to register prometheus collector: %w", err)
return fmt.Errorf("failed to register prometheus collector: %w", err)
}

return promhttp.HandlerFor(registry, promhttp.HandlerOpts{}), nil
return nil
}

func (tel *telemetryInitializer) shutdown() error {
Expand Down

0 comments on commit d70fae4

Please sign in to comment.