Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions internal/observability/noop_span.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions internal/observability/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down