Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openTelemetry as optional #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using a global variable here is the right approach.

)

// 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