From 31531acfe6bf3671acb90863ecdea6837ccfd55e Mon Sep 17 00:00:00 2001 From: Dhruv Goel Date: Sat, 10 Jan 2026 00:35:59 +0530 Subject: [PATCH] fix(tracing): disable tracing when no collector is configured Signed-off-by: Dhruv Goel --- tracing/tracing.go | 7 +++++++ tracing/tracing_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tracing/tracing.go b/tracing/tracing.go index 491706f7..71a6916a 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -25,6 +25,9 @@ type Config struct { Endpoint string `yaml:"endpoint" json:"endpoint"` // Insecure determines whether to use an insecure connection (no TLS) Insecure bool `yaml:"insecure" json:"insecure"` + // Adding extra flag to check if tracing is enabled + Enabled *bool `yaml:"enabled" json:"enabled"` + } func InitTracerFromYamlConfig(ctx context.Context, config string) (*sdktrace.TracerProvider, error) { @@ -43,6 +46,10 @@ func InitTracerFromYamlConfig(ctx context.Context, config string) (*sdktrace.Tra // It sets up OTLP gRPC exporter, resource attributes, and W3C trace context propagation func InitTracer(ctx context.Context, cfg Config) (*sdktrace.TracerProvider, error) { // Validate configuration + if cfg.Enabled != nil && !*cfg.Enabled { + return nil, nil + } + if cfg.ServiceName == "" { return nil, fmt.Errorf("service name is required") } diff --git a/tracing/tracing_test.go b/tracing/tracing_test.go index b4ef7fcd..f4ff4940 100644 --- a/tracing/tracing_test.go +++ b/tracing/tracing_test.go @@ -232,3 +232,20 @@ func TestNewTransport(t *testing.T) { t.Error("NewTransport(customTransport) returned nil") } } +func TestInitTracerDisabled(t *testing.T) { + ctx := context.Background() + enabled := false + + cfg := Config{ + Enabled: &enabled, + } + + tp, err := InitTracer(ctx, cfg) + if err != nil { + t.Fatalf("expected no error when tracing is disabled, got %v", err) + } + + if tp != nil { + t.Fatalf("expected nil tracer provider when tracing is disabled") + } +}