Skip to content

Commit

Permalink
Open Telemetry minor fix (#1212)
Browse files Browse the repository at this point in the history
* move IsEnabled func

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

* apply suggestions

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik authored Jan 13, 2022
1 parent b112919 commit 5d3e278
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pkg/tools/jaeger/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 0 additions & 19 deletions pkg/tools/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,15 @@ package log

import (
"context"
"os"
"sync/atomic"
)

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
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/tools/log/spanlogger/spanlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/opentelemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
16 changes: 15 additions & 1 deletion pkg/tools/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package opentelemetry
import (
"context"
"io"
"os"
"strconv"
"time"

"go.opentelemetry.io/otel"
Expand All @@ -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

Expand Down Expand Up @@ -70,7 +84,7 @@ func Init(ctx context.Context, spanExporter sdktrace.SpanExporter, metricExporte
o := &opentelemetry{
ctx: ctx,
}
if !log.IsOpentelemetryEnabled() {
if !IsEnabled() {
return o
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/tracing/grpcoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pkg/tools/zipkin/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 5d3e278

Please sign in to comment.