diff --git a/pkg/tools/jaeger/exporter.go b/pkg/tools/jaeger/exporter.go index cd430872b..131001fd3 100644 --- a/pkg/tools/jaeger/exporter.go +++ b/pkg/tools/jaeger/exporter.go @@ -26,11 +26,12 @@ import ( "go.opentelemetry.io/otel/sdk/trace" "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) // InitExporter - returns an instance of Jaeger Exporter. func InitExporter(ctx context.Context, exporterURL string) trace.SpanExporter { - if !log.IsOpentelemetryEnabled() { + if !opentelemetry.IsEnabled() { return nil } diff --git a/pkg/tools/log/logger.go b/pkg/tools/log/logger.go index 30bf968be..bbc20ce4a 100644 --- a/pkg/tools/log/logger.go +++ b/pkg/tools/log/logger.go @@ -19,7 +19,6 @@ package log import ( "context" - "os" "sync/atomic" ) @@ -27,26 +26,8 @@ type contextKeyType string const ( logKey contextKeyType = "Logger" - - telemetryEnv = "TELEMETRY" - telemetryOTel = "enabled" - - telemetryDefault = telemetryOTel ) -// IsOpentelemetryEnabled returns true if opentelemetry enabled -func IsOpentelemetryEnabled() bool { - return telemetryOTel == getTelemetryEnv() -} - -func getTelemetryEnv() string { - val := os.Getenv(telemetryEnv) - if val == "" { - return telemetryDefault - } - return val -} - var ( isTracingEnabled int32 = 0 ) diff --git a/pkg/tools/log/spanlogger/spanlogger.go b/pkg/tools/log/spanlogger/spanlogger.go index c668f8cb8..d4aaf0e7b 100644 --- a/pkg/tools/log/spanlogger/spanlogger.go +++ b/pkg/tools/log/spanlogger/spanlogger.go @@ -24,6 +24,7 @@ import ( "sync" "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) // spanlogger - provides a way to log via opentelemetry spans @@ -129,7 +130,7 @@ func (s *spanLogger) logf(level, format string, v ...interface{}) { // FromContext - creates a new spanLogger from context and operation func FromContext(ctx context.Context, operation string, fields map[string]interface{}) (context.Context, log.Logger, Span, func()) { var span Span - if log.IsOpentelemetryEnabled() { + if opentelemetry.IsEnabled() { ctx, span = newOTELSpan(ctx, operation, fields) } newLog := &spanLogger{ diff --git a/pkg/tools/opentelemetry/exporter.go b/pkg/tools/opentelemetry/exporter.go index 30db81cc5..7ce023444 100644 --- a/pkg/tools/opentelemetry/exporter.go +++ b/pkg/tools/opentelemetry/exporter.go @@ -32,7 +32,7 @@ import ( // InitSpanExporter - returns an instance of OpenTelemetry Span Exporter. func InitSpanExporter(ctx context.Context, exporterURL string) trace.SpanExporter { - if !log.IsOpentelemetryEnabled() { + if !IsEnabled() { return nil } @@ -52,7 +52,7 @@ func InitSpanExporter(ctx context.Context, exporterURL string) trace.SpanExporte // InitMetricExporter - returns an instance of OpenTelemetry Metric Exporter. func InitMetricExporter(ctx context.Context, exporterURL string) *otlpmetric.Exporter { - if !log.IsOpentelemetryEnabled() { + if !IsEnabled() { return nil } diff --git a/pkg/tools/opentelemetry/opentelemetry.go b/pkg/tools/opentelemetry/opentelemetry.go index 025cff0c4..639c2e349 100644 --- a/pkg/tools/opentelemetry/opentelemetry.go +++ b/pkg/tools/opentelemetry/opentelemetry.go @@ -20,6 +20,8 @@ package opentelemetry import ( "context" "io" + "os" + "strconv" "time" "go.opentelemetry.io/otel" @@ -35,6 +37,18 @@ import ( "github.com/networkservicemesh/sdk/pkg/tools/log" ) +const ( + telemetryEnv = "TELEMETRY" +) + +// IsEnabled returns true if opentelemetry enabled +func IsEnabled() bool { + if v, err := strconv.ParseBool(os.Getenv(telemetryEnv)); err == nil { + return v + } + return false +} + type opentelemetry struct { io.Closer @@ -70,7 +84,7 @@ func Init(ctx context.Context, spanExporter sdktrace.SpanExporter, metricExporte o := &opentelemetry{ ctx: ctx, } - if !log.IsOpentelemetryEnabled() { + if !IsEnabled() { return o } diff --git a/pkg/tools/tracing/grpcoptions.go b/pkg/tools/tracing/grpcoptions.go index 4986f6401..01a6e5436 100644 --- a/pkg/tools/tracing/grpcoptions.go +++ b/pkg/tools/tracing/grpcoptions.go @@ -27,12 +27,12 @@ import ( "google.golang.org/grpc" "google.golang.org/protobuf/proto" - "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) // WithTracing - returns array of grpc.ServerOption that should be passed to grpc.Dial to enable opentelemetry tracing func WithTracing() []grpc.ServerOption { - if log.IsOpentelemetryEnabled() { + if opentelemetry.IsEnabled() { interceptor := func( ctx context.Context, req interface{}, @@ -55,7 +55,7 @@ func WithTracing() []grpc.ServerOption { // WithTracingDial returns array of grpc.DialOption that should be passed to grpc.Dial to enable opentelemetry tracing func WithTracingDial() []grpc.DialOption { - if log.IsOpentelemetryEnabled() { + if opentelemetry.IsEnabled() { interceptor := func( ctx context.Context, method string, diff --git a/pkg/tools/zipkin/exporter.go b/pkg/tools/zipkin/exporter.go index 4d7890be6..f83e446cb 100644 --- a/pkg/tools/zipkin/exporter.go +++ b/pkg/tools/zipkin/exporter.go @@ -26,11 +26,12 @@ import ( "go.opentelemetry.io/otel/sdk/trace" "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" ) // InitExporter - returns an instance of Zipkin Exporter. func InitExporter(ctx context.Context, exporterURL string) trace.SpanExporter { - if !log.IsOpentelemetryEnabled() { + if !opentelemetry.IsEnabled() { return nil }