Skip to content

Latest commit

 

History

History
65 lines (50 loc) · 2.64 KB

README.md

File metadata and controls

65 lines (50 loc) · 2.64 KB

OpenTelemetry extensions - otelmiddleware

Home Related
Home None

Go CodeQL Dependency Review Go Reference

Open Telemetry http middleware. This package provides an instrumentation for middleware that can be used to trace HTTP requests.

Package otelmiddleware provides middleware for wrapping http.Server handlers with Open Telemetry tracing support.

The trace.Span is decorated with standard metadata extracted from the http.Request injected into the middleware. the basic information is extracted using the OpenTelemetry semconv package.

When a span gets initialized it uses the following slice of trace.SpanStartOption

opts := []trace.SpanStartOption{
trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", r)...),
trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(r)...),
trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(r.Host, extractRoute(r.RequestURI), r)...),
trace.WithAttributes(semconv.HTTPClientAttributesFromHTTPRequest(r)...),
trace.WithAttributes(semconv.TelemetrySDKLanguageGo),
trace.WithSpanKind(trace.SpanKindServer),
}

The slice can be extended using the WithAttributes TraceOption function.

After these options are applied a new span is created and the middleware will pass the http.ResponseWriter and http.Request to the next http.Handler.

Functions

func TraceWithOptions(opt ...TraceOption) func (next http.Handler) http.Handler
func Trace(next http.Handler) http.Handler
func WithAttributes(attributes ...attribute.KeyValue) TraceOption
func WithPropagator(p propagation.TextMapPropagator) TraceOption
func WithServiceName(serviceName string) TraceOption
func WithTracer(tracer trace.Tracer) TraceOption

Types

type TraceOption func (*traceConfig)

Structs

type traceConfig struct {
tracer trace.Tracer
propagator propagation.TextMapPropagator
attributes []attribute.KeyValue
serviceName string
}