diff --git a/internal/observability/noop_span.go b/internal/observability/noop_span.go new file mode 100644 index 00000000..e951ddfe --- /dev/null +++ b/internal/observability/noop_span.go @@ -0,0 +1,71 @@ +package observability + +import ( + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" +) + +// noopTraceSpan to represent a span with no operation (do nothing) +type noopTraceSpan struct{} + +// AddEvent implements trace.Span +func (noopTraceSpan) AddEvent(name string, options ...trace.EventOption) { + // do nothing +} + +// End implements trace.Span +func (noopTraceSpan) End(options ...trace.SpanEndOption) { + // do nothing +} + +// IsRecording implements trace.Span +func (noopTraceSpan) IsRecording() bool { + return false +} + +// RecordError implements trace.Span +func (noopTraceSpan) RecordError(err error, options ...trace.EventOption) { + // do nothing +} + +// SetAttributes implements trace.Span +func (noopTraceSpan) SetAttributes(kv ...attribute.KeyValue) { + // do nothing +} + +// SetName implements trace.Span +func (noopTraceSpan) SetName(name string) { + // do nothing +} + +// SetStatus implements trace.Span +func (noopTraceSpan) SetStatus(code codes.Code, description string) { + // do nothing +} + +// SpanContext implements trace.Span +func (noopTraceSpan) SpanContext() trace.SpanContext { + // do nothing + return trace.SpanContext{} +} + +// TracerProvider implements trace.Span +func (noopTraceSpan) TracerProvider() trace.TracerProvider { + // noop + return trace.NewNoopTracerProvider() +} + +func newNoopSpan() trace.Span { + return noopTraceSpan{} +} + +var ( + isNoopTracing = false +) + +// WithNoopTracing enable noop tracing operation +// it is used when you don't want to observability +func WithNoopTracing() { + isNoopTracing = true +} diff --git a/internal/observability/observability.go b/internal/observability/observability.go index 7f2a1def..bcb0e40a 100644 --- a/internal/observability/observability.go +++ b/internal/observability/observability.go @@ -27,6 +27,10 @@ func StartSpan( name string, opts ...trace.SpanStartOption, ) (context.Context, trace.Span) { + if isNoopTracing { + return ctx, newNoopSpan() + } + tracer := otel.GetTracerProvider().Tracer("gohbase") return tracer.Start(ctx, name, opts...) }