Skip to content

Commit

Permalink
Add dynamic elps filtering option
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-at-luther committed Dec 4, 2024
1 parent a55dac3 commit 68093d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 31 additions & 3 deletions opttrace/opttrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import (
"go.opentelemetry.io/otel/trace/noop"
)

const tracerName = "opttrace"
const (
tracerName = "opttrace"
disableElpsFilteringTraceState = "disable_elps_filtering"
)

var noopTracerProvider = noop.NewTracerProvider()

// Tracer provides a tracing interface that generates traces only if configured
// with a trace exporter
type Tracer struct {
exportTP *sdktrace.TracerProvider
exportTP *sdktrace.TracerProvider
disableELPSFilter bool

Check failure on line 30 in opttrace/opttrace.go

View workflow job for this annotation

GitHub Actions / lint

field `disableELPSFilter` is unused (unused)
}

// Option provides Tracer configuration options
Expand Down Expand Up @@ -145,13 +149,37 @@ func otlpExporter(ctx context.Context, traceURI string) (*otlptrace.Exporter, er
return otlptracegrpc.New(ctx, otlpOpts...)
}

// TraceContextWithoutELPSFilter takes adds trace state into the propagated
// context to signify that elps filtering should be disabled for the request.
// NOTE: this results in very large traces.
func TraceContextWithoutELPSFilter(ctx context.Context) (context.Context, error) {
spanCtx := trace.SpanContextFromContext(ctx)
if !spanCtx.IsValid() {
return ctx, fmt.Errorf("not trace context")
}

traceState := spanCtx.TraceState()
if value := traceState.Get(disableElpsFilteringTraceState); value == "true" {
return ctx, nil
}

newTraceState, err := traceState.Insert(disableElpsFilteringTraceState, "true")
if err != nil {
return ctx, fmt.Errorf("state insert: %w", err)
}

newSpanCtx := spanCtx.WithTraceState(newTraceState)
return trace.ContextWithSpanContext(ctx, newSpanCtx), nil
}

// Span creates a new trace span and returns the supplied context with span
// added. The returned span must be ended to avoid leaking resources.
func (t Tracer) Span(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
if t.exportTP == nil {
return noopTracerProvider.Tracer(tracerName).Start(ctx, spanName, opts...)
}
return t.exportTP.Tracer(tracerName).Start(ctx, spanName, opts...)
tracer := t.exportTP.Tracer(tracerName)
return tracer.Start(ctx, spanName, opts...)
}

// Shutdown releases all resources allocated by the tracing provider.
Expand Down
12 changes: 12 additions & 0 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/luthersystems/svc/grpclogging"
"github.com/luthersystems/svc/opttrace"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -374,6 +375,17 @@ func (orc *Oracle) close() error {
return orc.phylum.Close()
}

// TraceContextDefer is a fuction that must be called to stop the span.
type TraceContextDefer func()

// TraceContext adds tracing to a request context.
func (orc *Oracle) TraceContext(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, TraceContextDefer) {
ctx, span := orc.tracer.Span(ctx, spanName, opts...)
return ctx, func() {
span.End()
}
}

// Call calls the phylum.
func Call[K proto.Message, R proto.Message](s *Oracle, ctx context.Context, methodName string, req K, resp R, config ...shiroclient.Config) (R, error) {
configs := s.txConfigs(ctx)
Expand Down

0 comments on commit 68093d8

Please sign in to comment.