Skip to content

Commit 6204322

Browse files
authored
fix: disable metrics if using zipkin traces endpoint but empty metrics endpoint (#203)
* fix: disable metrics if using zipkin traces endpoint but empty metrics endpoint * fix comment
1 parent 9dc499c commit 6204322

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

instrumentation/opentelemetry/init.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
390390
}
391391

392392
func initializeMetrics(cfg *config.AgentConfig) func() {
393-
if cfg.GetTelemetry() == nil || !cfg.GetTelemetry().GetMetricsEnabled().GetValue() {
393+
if shouldDisableMetrics(cfg) {
394394
return func() {}
395395
}
396396

@@ -428,6 +428,18 @@ func initializeMetrics(cfg *config.AgentConfig) func() {
428428
}
429429
}
430430

431+
func shouldDisableMetrics(cfg *config.AgentConfig) bool {
432+
// Disable metrics if the tracing exporter is not OTLP(grpc) and the metrics endpoint is not explicitly set.
433+
// This is because we use the traces OTLP endpoint for metrics if the metrics endpoint is not set.
434+
// By default the traces endpoint is zipkin which does not have support for metrics.
435+
if cfg.GetReporting() != nil && cfg.GetReporting().GetTraceReporterType() != config.TraceReporterType_OTLP &&
436+
len(cfg.GetReporting().GetMetricEndpoint().GetValue()) == 0 {
437+
return true
438+
}
439+
440+
return cfg.GetTelemetry() == nil || !cfg.GetTelemetry().GetMetricsEnabled().GetValue()
441+
}
442+
431443
// SpanProcessorWrapper wraps otel span processor
432444
// and is responsible to delegate calls to the wrapped processor
433445
type SpanProcessorWrapper interface {

instrumentation/opentelemetry/init_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,22 @@ func TestInitWithSpanProcessorWrapper(t *testing.T) {
395395
assert.Equal(t, 3, wrapper.onStartCount)
396396
assert.Equal(t, 3, wrapper.onEndCount)
397397
}
398+
399+
func TestShouldDisableMetrics(t *testing.T) {
400+
// Using default values: since zipkin is the default traces exporter turn off metrics
401+
cfg := config.Load()
402+
assert.True(t, shouldDisableMetrics(cfg))
403+
404+
// For OTLP reporting endpoint, turn it on
405+
cfg.Reporting.TraceReporterType = config.TraceReporterType_OTLP
406+
assert.False(t, shouldDisableMetrics(cfg))
407+
408+
cfg = config.Load()
409+
cfg.Telemetry.MetricsEnabled = config.Bool(false)
410+
assert.True(t, shouldDisableMetrics(cfg))
411+
412+
// Set a metrics endpoint
413+
cfg = config.Load()
414+
cfg.Reporting.MetricEndpoint = config.String("localhost:4317")
415+
assert.False(t, shouldDisableMetrics(cfg))
416+
}

0 commit comments

Comments
 (0)