diff --git a/bin/runner.go b/bin/runner.go index 50fb600..5318f47 100644 --- a/bin/runner.go +++ b/bin/runner.go @@ -3,6 +3,7 @@ package main import ( "context" "errors" + "os" "github.com/litmuschaos/chaos-runner/pkg/log" "github.com/litmuschaos/chaos-runner/pkg/telemetry" @@ -24,15 +25,16 @@ func init() { func main() { ctx := context.Background() // Set up Observability. - shutdown, err := telemetry.InitOTelSDK(ctx) - if err != nil { - return + if otelExporterEndpoint := os.Getenv(telemetry.OTELExporterOTLPEndpoint); otelExporterEndpoint != "" { + shutdown, err := telemetry.InitOTelSDK(ctx, otelExporterEndpoint) + if err != nil { + return + } + defer func() { + err = errors.Join(err, shutdown(ctx)) + }() + ctx = telemetry.GetTraceParentContext() } - // Handle shutdown properly so nothing leaks. - defer func() { - err = errors.Join(err, shutdown(ctx)) - }() - ctx = telemetry.GetTraceParentContext() engineDetails := utils.EngineDetails{} clients := utils.ClientSets{} diff --git a/pkg/telemetry/otel.go b/pkg/telemetry/otel.go index 2b5c8d6..8f679d8 100644 --- a/pkg/telemetry/otel.go +++ b/pkg/telemetry/otel.go @@ -3,7 +3,6 @@ package telemetry import ( "context" "errors" - "os" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel" @@ -20,7 +19,7 @@ import ( const OTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT" const OTELServiceName = "chaos_runner" -func InitOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) { +func InitOTelSDK(ctx context.Context, endpoint string) (shutdown func(context.Context) error, err error) { var shutdownFuncs []func(context.Context) error shutdown = func(ctx context.Context) error { @@ -36,7 +35,7 @@ func InitOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err err = errors.Join(inErr, shutdown(ctx)) } - tracerProvider, err := newTracerProvider(ctx) + tracerProvider, err := newTracerProvider(ctx, endpoint) if err != nil { handleErr(err) return @@ -61,8 +60,7 @@ func newPropagator() propagation.TextMapPropagator { ) } -func newTracerProvider(ctx context.Context) (*trace.TracerProvider, error) { - endpoint := os.Getenv(OTELExporterOTLPEndpoint) +func newTracerProvider(ctx context.Context, endpoint string) (*trace.TracerProvider, error) { res, err := resource.New(ctx, resource.WithAttributes( semconv.ServiceNameKey.String(OTELServiceName),