Documentation
¶
Index ¶
- Constants
- func Middleware(serverName string, opts ...Option) func(next http.Handler) http.Handler
- type Filter
- type Option
- func WithChiRoutes(routes chi.Routes) Option
- func WithFilter(filter Filter) Option
- func WithPropagators(propagators propagation.TextMapPropagator) Option
- func WithPublicEndpoint() Option
- func WithPublicEndpointFn(fn func(r *http.Request) bool) Option
- func WithRequestMethodInSpanName(isActive bool) Option
- func WithTraceIDResponseHeader(headerKeyFunc func() string) Optiondeprecated
- func WithTraceResponseHeaders(cfg TraceHeaderConfig) Option
- func WithTracerProvider(provider oteltrace.TracerProvider) Option
- type TraceHeaderConfig
Constants ¶
const ( DefaultTraceIDResponseHeaderKey = "X-Trace-Id" DefaultTraceSampledResponseHeaderKey = "X-Trace-Sampled" )
These defaults are used in `TraceHeaderConfig`.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Filter ¶ added in v0.9.0
Filter is a predicate used to determine whether a given http.Request should be traced. A Filter must return true if the request should be traced.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option specifies instrumentation configuration options.
func WithChiRoutes ¶ added in v0.3.0
func WithChiRoutes(routes chi.Routes) Option
WithChiRoutes specified the routes that being used by application. Its main purpose is to provide route pattern as span name during span creation. If this option is not set, by default the span will be given name at the end of span execution. For some people, this behavior is not desirable since they want to override the span name on underlying handler. By setting this option, it is possible for them to override the span name.
func WithFilter ¶ added in v0.5.0
WithFilter adds a filter to the list of filters used by the handler. If any filter indicates to exclude a request then the request will not be traced. All filters must allow a request to be traced for a Span to be created. If no filters are provided then all requests are traced. Filters will be invoked for each processed request, it is advised to make them simple and fast.
func WithPropagators ¶
func WithPropagators(propagators propagation.TextMapPropagator) Option
WithPropagators specifies propagators to use for extracting information from the HTTP requests. If none are specified, global ones will be used.
func WithPublicEndpoint ¶ added in v0.8.0
func WithPublicEndpoint() Option
WithPublicEndpoint is used for marking every endpoint as public endpoint. This means if the incoming request has span context, it won't be used as parent span by the span generated by this middleware, instead the generated span will be the root span (new trace) and then linked to the span from the incoming request.
Let say we have the following scenario:
- We have 2 systems: `SysA` & `SysB`.
- `SysA` has the following services: `SvcA.1` & `SvcA.2`.
- `SysB` has the following services: `SvcB.1` & `SvcB.2`.
- `SvcA.2` is used internally only by `SvcA.1`.
- `SvcB.2` is used internally only by `SvcB.1`.
- All of these services already instrumented otelchi & using the same collector (e.g Jaeger).
- In `SvcA.1` we should set `WithPublicEndpoint()` since it is the entry point (a.k.a "public endpoint") for entering `SysA`.
- In `SvcA.2` we should not set `WithPublicEndpoint()` since it is only used internally by `SvcA.1` inside `SysA`.
- Point 7 & 8 also applies to both services in `SysB`.
Now, whenever `SvcA.1` calls `SvcA.2` there will be only a single trace generated. This trace will contain 2 spans: root span from `SvcA.1` & child span from `SvcA.2`.
But if let say `SvcA.2` calls `SvcB.1`, then there will be 2 traces generated: trace from `SysA` & trace from `SysB`. But in trace generated in `SysB` there will be like a marking that this trace is actually related to trace in `SysA` (a.k.a linked with the trace from `SysA`).
func WithPublicEndpointFn ¶ added in v0.8.0
WithPublicEndpointFn runs with every request, and allows conditionally configuring the Handler to link the generated span with an incoming span context.
If the function return `true` the generated span will be linked with the incoming span context. Otherwise, the generated span will be set as the child span of the incoming span context.
Essentially it has the same functionality as `WithPublicEndpoint` but with more flexibility.
func WithRequestMethodInSpanName ¶ added in v0.4.0
WithRequestMethodInSpanName is used for adding http request method to span name. While this is not necessary for vendors that properly implemented the tracing specs (e.g Jaeger, AWS X-Ray, etc...), but for other vendors such as Elastic and New Relic this might be helpful.
See following threads for details:
- https://github.com/riandyrn/otelchi/pull/3#issuecomment-1005883910 - https://github.com/riandyrn/otelchi/issues/6#issuecomment-1034461912
func WithTraceIDResponseHeader
deprecated
added in
v0.6.0
WithTraceIDResponseHeader enables adding trace id into response header. It accepts a function that generates the header key name. If this parameter function set to `nil` the default header key which is `X-Trace-Id` will be used.
Deprecated: use `WithTraceResponseHeaders` instead.
func WithTraceResponseHeaders ¶ added in v0.10.0
func WithTraceResponseHeaders(cfg TraceHeaderConfig) Option
WithTraceResponseHeaders configures the response headers for trace information. It accepts a TraceHeaderConfig struct that contains the keys for the Trace ID and Trace Sampled headers. If the provided keys are empty, default values will be used for the respective headers.
func WithTracerProvider ¶
func WithTracerProvider(provider oteltrace.TracerProvider) Option
WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.
type TraceHeaderConfig ¶ added in v0.10.0
type TraceHeaderConfig struct { TraceIDHeader string // if non-empty overrides the default of X-Trace-ID TraceSampledHeader string // if non-empty overrides the default of X-Trace-Sampled }
TraceHeaderConfig is configuration for trace headers in the response.