Skip to content

Commit

Permalink
fix: disable metrics if using zipkin traces endpoint but empty metric…
Browse files Browse the repository at this point in the history
…s endpoint (#203)

* fix: disable metrics if using zipkin traces endpoint but empty metrics endpoint

* fix comment
  • Loading branch information
tim-mwangi authored Feb 1, 2023
1 parent 9dc499c commit 6204322
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
}

func initializeMetrics(cfg *config.AgentConfig) func() {
if cfg.GetTelemetry() == nil || !cfg.GetTelemetry().GetMetricsEnabled().GetValue() {
if shouldDisableMetrics(cfg) {
return func() {}
}

Expand Down Expand Up @@ -428,6 +428,18 @@ func initializeMetrics(cfg *config.AgentConfig) func() {
}
}

func shouldDisableMetrics(cfg *config.AgentConfig) bool {
// Disable metrics if the tracing exporter is not OTLP(grpc) and the metrics endpoint is not explicitly set.
// This is because we use the traces OTLP endpoint for metrics if the metrics endpoint is not set.
// By default the traces endpoint is zipkin which does not have support for metrics.
if cfg.GetReporting() != nil && cfg.GetReporting().GetTraceReporterType() != config.TraceReporterType_OTLP &&
len(cfg.GetReporting().GetMetricEndpoint().GetValue()) == 0 {
return true
}

return cfg.GetTelemetry() == nil || !cfg.GetTelemetry().GetMetricsEnabled().GetValue()
}

// SpanProcessorWrapper wraps otel span processor
// and is responsible to delegate calls to the wrapped processor
type SpanProcessorWrapper interface {
Expand Down
19 changes: 19 additions & 0 deletions instrumentation/opentelemetry/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,22 @@ func TestInitWithSpanProcessorWrapper(t *testing.T) {
assert.Equal(t, 3, wrapper.onStartCount)
assert.Equal(t, 3, wrapper.onEndCount)
}

func TestShouldDisableMetrics(t *testing.T) {
// Using default values: since zipkin is the default traces exporter turn off metrics
cfg := config.Load()
assert.True(t, shouldDisableMetrics(cfg))

// For OTLP reporting endpoint, turn it on
cfg.Reporting.TraceReporterType = config.TraceReporterType_OTLP
assert.False(t, shouldDisableMetrics(cfg))

cfg = config.Load()
cfg.Telemetry.MetricsEnabled = config.Bool(false)
assert.True(t, shouldDisableMetrics(cfg))

// Set a metrics endpoint
cfg = config.Load()
cfg.Reporting.MetricEndpoint = config.String("localhost:4317")
assert.False(t, shouldDisableMetrics(cfg))
}

0 comments on commit 6204322

Please sign in to comment.