package httptraces

Import Path
	go.pact.im/x/traces/httptraces (on go.dev)

Dependency Relation
	imports 10 packages, and imported by one package

Involved Source Files Package httptraces provides [http.Handler] and [http.RoundTripper] instrumentation using [traces.Tracer]. handler.go transport.go
Code Examples package main import ( "net/http" "go.pact.im/x/traces" "go.pact.im/x/traces/httptraces" "go.pact.im/x/traces/traceparent" ) func main() { tr := traces.New(traces.DiscardHandler()).WithSampler(traces.ParentBased(traces.Ratio(0.1))) mux := http.NewServeMux() mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) handler := httptraces.NewHandler(tr, mux).WithExtract(traceparent.Extract) _ = handler } package main import ( "net/http" "go.pact.im/x/traces" "go.pact.im/x/traces/httptraces" ) func main() { tr := traces.New(traces.DiscardHandler()).WithSampler(traces.ParentBased(traces.Ratio(0.1))) mux := http.NewServeMux() mux.HandleFunc("GET /public/health", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }) handler := httptraces.NewHandler(tr, mux) _ = handler } package main import ( "net/http" "go.pact.im/x/traces" "go.pact.im/x/traces/httptraces" ) func main() { tr := traces.New(traces.DiscardHandler()) client := &http.Client{ Transport: httptraces.NewTransport(tr, nil), } _ = client } package main import ( "net/http" "go.pact.im/x/traces" "go.pact.im/x/traces/httptraces" "go.pact.im/x/traces/traceparent" ) func main() { tr := traces.New(traces.DiscardHandler()) client := &http.Client{ Transport: httptraces.NewTransport(tr, nil).WithInject(traceparent.Inject), } _ = client }
Package-Level Type Names (total 3, in which 2 are exported)
/* sort exporteds by: | */
Handler is an [http.Handler] that traces incoming HTTP requests using [traces.Tracer]. By default, incoming trace context headers are ignored so untrusted clients cannot forge trace IDs or force sampling; call [Handler.WithExtract] to opt in to header extraction on internal or trusted endpoints. The server span reference is propagated via [traces.NewContext], and the span is renamed once the route pattern is known after the wrapped handler returns. Response writing is wrapped with [httpsnoop.Wrap] so optional [http.ResponseWriter] interfaces are preserved. ServeHTTP implements the [http.Handler] interface. WithAttrs returns a [Handler] using f to attach additional start-time attributes for each request. If f is nil, only default attributes are added. WithExtract returns a [Handler] using f to extract the parent [traces.SpanRef] from incoming request headers (for example [traceparent.Extract]). If f is nil, incoming headers are ignored. WithFilter returns a [Handler] that only traces requests for which f returns true. Filtered-out requests are passed directly to the wrapped handler without creating a span. If f is nil, all requests are traced. WithRoute returns a [Handler] using f to extract the low-cardinality route pattern after the inner handler runs. If f is nil, it restores deriving the route from [http.Request.Pattern]. WithSpanName returns a [Handler] using f to compute the final span name from the request and resolved route. If f is nil, it restores the default "METHOD route" (or "METHOD") naming. *Handler : net/http.Handler func NewHandler(tr *traces.Tracer, next http.Handler) *Handler func (*Handler).WithAttrs(f func(*http.Request) []traces.Attr) *Handler func (*Handler).WithExtract(f func(get func(key string) []string) traces.SpanRef) *Handler func (*Handler).WithFilter(f func(*http.Request) bool) *Handler func (*Handler).WithRoute(f func(*http.Request) string) *Handler func (*Handler).WithSpanName(f func(r *http.Request, route string) string) *Handler
Transport is an [http.RoundTripper] that traces outgoing HTTP requests. By default, trace context headers are not injected so internal trace IDs are not leaked to third-party endpoints; call [Transport.WithInject] to opt in for internal or trusted downstream services. Client spans end when the response body reaches EOF or is closed, or at response headers for nil/empty bodies, HEAD requests, errors, and 101 Switching Protocols responses. RoundTrip implements the [http.RoundTripper] interface. WithAttrs returns a [Transport] using f to attach additional start-time attributes for each outgoing request. If f is nil, only default attributes are added. WithFilter returns a [Transport] that only traces requests for which f returns true. Filtered-out requests are passed directly to the wrapped transport without creating a span or injecting trace headers. If f is nil, all requests are traced. WithInject returns a [Transport] using f to inject [traces.SpanRef] into outgoing request headers (for example [traceparent.Inject]). If f is nil, no trace headers are injected. WithSpanName returns a [Transport] using f to compute the span name for each outgoing request. If f is nil, it restores using the HTTP request method. *Transport : net/http.RoundTripper func NewTransport(tr *traces.Tracer, base http.RoundTripper) *Transport func (*Transport).WithAttrs(f func(*http.Request) []traces.Attr) *Transport func (*Transport).WithFilter(f func(*http.Request) bool) *Transport func (*Transport).WithInject(f func(ref traces.SpanRef, set func(key, value string))) *Transport func (*Transport).WithSpanName(f func(*http.Request) string) *Transport
Package-Level Functions (total 9, in which 2 are exported)
NewHandler returns a new [Handler] instance wrapping next with tr.
NewTransport returns a new [Transport] instance wrapping base using tr. If base is nil, [http.DefaultTransport] is used.
Package-Level Variables (only one, which is unexported)
Package-Level Constants (total 16, none are exported)