Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft PR on how to report in the same time metrics recorded with oc and otel #6296

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 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 @@ -111,13 +110,15 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
return err
}

var pe http.Handler
var err error
// Always need this
tel.ocRegistry = ocmetric.NewRegistry()
registry := prometheus.NewRegistry()
if tel.registry.IsEnabled(obsreportconfig.UseOtelForInternalMetricsfeatureGateID) {
pe, err = tel.initOpenTelemetry(telAttrs)
} else {
pe, err = tel.initOpenCensus(cfg, telAttrs)
if err := tel.initOpenTelemetry(telAttrs, registry); err != nil {
return err
}
}
pe, err := tel.initOpenCensus(cfg, telAttrs, registry)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +177,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, registry *prometheus.Registry) (http.Handler, error) {
tel.ocRegistry = ocmetric.NewRegistry()
metricproducer.GlobalManager().AddProducer(tel.ocRegistry)

Expand All @@ -190,13 +191,12 @@ func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs m
return nil, err
}

// Until we can use a generic metrics exporter, default to Prometheus.
opts := ocprom.Options{
Namespace: "otelcol",
Registry: registry,
}

opts.ConstLabels = make(map[string]string)

for k, v := range telAttrs {
opts.ConstLabels[sanitizePrometheusKey(k)] = v
}
Expand All @@ -210,7 +210,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, registry *prometheus.Registry) error {
// Initialize the ocRegistry, still used by the process metrics.
tel.ocRegistry = ocmetric.NewRegistry()

Expand All @@ -221,7 +221,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 +230,11 @@ func (tel *telemetryInitializer) initOpenTelemetry(attrs map[string]string) (htt
sdkmetric.WithReader(exporter),
)

registry := prometheus.NewRegistry()

wrappedRegisterer := prometheus.WrapRegistererWithPrefix("otelcol_", registry)
if err := wrappedRegisterer.Register(exporter.Collector); err != nil {
return nil, fmt.Errorf("failed to register prometheus collector: %w", err)
if err = prometheus.WrapRegistererWithPrefix("otelcol_", registry).Register(exporter.Collector); err != nil {
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