Skip to content

Commit

Permalink
feat(trace): propagate WithPublicEndpointFn to otel NewHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
antonaudzei-outreach committed Aug 5, 2024
1 parent 0a37620 commit b08997e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
23 changes: 19 additions & 4 deletions pkg/trace/propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ func (t *otelTracer) newTransport(old http.RoundTripper) http.RoundTripper {
}

type Handler struct {
handler http.Handler
operation string
handler http.Handler
operation string
publicEndpointFn func(*http.Request) bool
}

type HandlerOption func(*Handler)

func WithPublicEndpointFn(fn func(*http.Request) bool) HandlerOption {
return func(h *Handler) {
h.publicEndpointFn = fn
}
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -52,18 +61,24 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
force := r.Header.Get(HeaderForceTracing)
if force != "" {
startOptions = oteltrace.WithAttributes(attribute.String(fieldForceTrace, force))
handler = otelhttp.NewHandler(h.handler, h.operation, otelhttp.WithSpanOptions(startOptions))
handler = otelhttp.NewHandler(h.handler, h.operation,
otelhttp.WithSpanOptions(startOptions),
otelhttp.WithPublicEndpointFn(h.publicEndpointFn), // passing nil function is equivalent to "not configured"
)
r = r.WithContext(forceTracing(r.Context()))
}

handler.ServeHTTP(w, r)
}

func (t *otelTracer) newHandler(handler http.Handler, operation string) http.Handler {
func (t *otelTracer) newHandler(handler http.Handler, operation string, opts ...HandlerOption) http.Handler {
h := Handler{
handler: handler,
operation: operation,
}
for _, opt := range opts {
opt(&h)
}

return &h
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/trace/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ func NewTransport(old http.RoundTripper) http.RoundTripper {
// NewHandler creates a new handlers which reads propagated headers
// from the current trace context.
//
// Supported options are:
// - WithPublicEndpointFn conditionally configure the Handler to link the span with an incoming span context
// instead of child association. Useful for public facing endpoints that don't need to attach to externally
// defined traces
//
// Usage:
//
// trace.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) *roundtripperState {
// trace.StartSpan(r.Context(), "my endpoint")
// defer trace.End(r.Context())
// ... do actual request handling ...
// }), "my endpoint")
func NewHandler(handler http.Handler, operation string) http.Handler {
func NewHandler(handler http.Handler, operation string, opts ...HandlerOption) http.Handler {
if defaultTracer == nil {
return handler
}

return defaultTracer.newHandler(handler, operation)
return defaultTracer.newHandler(handler, operation, opts...)
}
2 changes: 1 addition & 1 deletion pkg/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type tracer interface {

newTransport(http.RoundTripper) http.RoundTripper

newHandler(handler http.Handler, operation string) http.Handler
newHandler(handler http.Handler, operation string, opts ...HandlerOption) http.Handler

toHeaders(ctx context.Context) map[string][]string

Expand Down

0 comments on commit b08997e

Please sign in to comment.