Skip to content

Commit

Permalink
Add prometheus metric prefix and constant service attributes to otel-…
Browse files Browse the repository at this point in the history
…go prometheus exporter (#6223)

* add prometheus metric prefix and constant service attributes to otel-go prometheus exporter

* changelog

* add telemetry attributes as resource

* context and comments

* remove constant labels wrapper
  • Loading branch information
paivagustavo authored Oct 4, 2022
1 parent b1e1614 commit 00ec3d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Add config marshaler (#5566)
- Add semantic conventions for specification v1.10-v1.13 (#6213)
- `receiver/otlp`: Make logs related to gRCPC and HTTP server startup clearer (#6174)
- Add prometheus metric prefix and constant service attributes to Collector's own telemetry when using OpenTelemetry for internal telemetry (#6223)

## v0.61.0 Beta

Expand Down
26 changes: 22 additions & 4 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package service // import "go.opentelemetry.io/collector/service"

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -31,10 +32,12 @@ import (
"go.opencensus.io/stats/view"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelprom "go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -129,7 +132,7 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
var pe http.Handler
var err error
if tel.registry.IsEnabled(useOtelForInternalMetricsfeatureGateID) {
pe, err = tel.initOpenTelemetry()
pe, err = tel.initOpenTelemetry(telAttrs)
} else {
pe, err = tel.initOpenCensus(cfg, telAttrs)
}
Expand Down Expand Up @@ -225,15 +228,30 @@ func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs m
return pe, nil
}

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

var resAttrs []attribute.KeyValue
for k, v := range attrs {
resAttrs = append(resAttrs, attribute.String(k, v))
}

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

exporter := otelprom.New()
tel.mp = sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter))
tel.mp = sdkmetric.NewMeterProvider(
sdkmetric.WithResource(res),
sdkmetric.WithReader(exporter),
)

registry := prometheus.NewRegistry()
if err := registry.Register(exporter.Collector); err != nil {

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

Expand Down

0 comments on commit 00ec3d0

Please sign in to comment.