| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelmux // import "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
"go.opentelemetry.io/otel/metric" |
| 11 |
"go.opentelemetry.io/otel/propagation" |
| 12 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 13 |
) |
| 14 |
|
| 15 |
// config is used to configure the mux middleware. |
| 16 |
type config struct { |
| 17 |
TracerProvider oteltrace.TracerProvider |
| 18 |
Propagators propagation.TextMapPropagator |
| 19 |
spanNameFormatter func(string, *http.Request) string |
| 20 |
PublicEndpoint bool |
| 21 |
PublicEndpointFn func(*http.Request) bool |
| 22 |
Filters []Filter |
| 23 |
MeterProvider metric.MeterProvider |
| 24 |
MetricAttributesFn func(*http.Request) []attribute.KeyValue |
| 25 |
} |
| 26 |
|
| 27 |
// Option specifies instrumentation configuration options. |
| 28 |
type Option interface { |
| 29 |
apply(*config) |
| 30 |
} |
| 31 |
|
| 32 |
type optionFunc func(*config) |
| 33 |
|
| 34 |
func (o optionFunc) apply(c *config) { |
| 35 |
o(c) |
| 36 |
} |
| 37 |
|
| 38 |
// Filter is a predicate used to determine whether a given http.request should |
| 39 |
// be traced. A Filter must return true if the request should be traced. |
| 40 |
type Filter func(*http.Request) bool |
| 41 |
|
| 42 |
// WithPublicEndpoint configures the Handler to link the span with an incoming |
| 43 |
// span context. If this option is not provided, then the association is a child |
| 44 |
// association instead of a link. |
| 45 |
func WithPublicEndpoint() Option { |
| 46 |
return optionFunc(func(c *config) { |
| 47 |
c.PublicEndpoint = true |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
// WithPublicEndpointFn runs with every request, and allows conditionally |
| 52 |
// configuring the Handler to link the span with an incoming span context. If |
| 53 |
// this option is not provided or returns false, then the association is a |
| 54 |
// child association instead of a link. |
| 55 |
// Note: WithPublicEndpoint takes precedence over WithPublicEndpointFn. |
| 56 |
func WithPublicEndpointFn(fn func(*http.Request) bool) Option { |
| 57 |
return optionFunc(func(c *config) { |
| 58 |
c.PublicEndpointFn = fn |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
// WithPropagators specifies propagators to use for extracting |
| 63 |
// information from the HTTP requests. If none are specified, global |
| 64 |
// ones will be used. |
| 65 |
func WithPropagators(propagators propagation.TextMapPropagator) Option { |
| 66 |
return optionFunc(func(cfg *config) { |
| 67 |
if propagators != nil { |
| 68 |
cfg.Propagators = propagators |
| 69 |
} |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
// WithTracerProvider specifies a tracer provider to use for creating a tracer. |
| 74 |
// If none is specified, the global provider is used. |
| 75 |
func WithTracerProvider(provider oteltrace.TracerProvider) Option { |
| 76 |
return optionFunc(func(cfg *config) { |
| 77 |
if provider != nil { |
| 78 |
cfg.TracerProvider = provider |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
// WithSpanNameFormatter specifies a function to use for generating a custom span |
| 84 |
// name. By default, the route name (path template or regexp) is used. The route |
| 85 |
// name is provided so you can use it in the span name without needing to |
| 86 |
// duplicate the logic for extracting it from the request. |
| 87 |
func WithSpanNameFormatter(fn func(routeName string, r *http.Request) string) Option { |
| 88 |
return optionFunc(func(cfg *config) { |
| 89 |
cfg.spanNameFormatter = fn |
| 90 |
}) |
| 91 |
} |
| 92 |
|
| 93 |
// WithFilter adds a filter to the list of filters used by the handler. |
| 94 |
// If any filter indicates to exclude a request then the request will not be |
| 95 |
// traced. All filters must allow a request to be traced for a Span to be created. |
| 96 |
// If no filters are provided then all requests are traced. |
| 97 |
// Filters will be invoked for each processed request, it is advised to make them |
| 98 |
// simple and fast. |
| 99 |
func WithFilter(f Filter) Option { |
| 100 |
return optionFunc(func(c *config) { |
| 101 |
c.Filters = append(c.Filters, f) |
| 102 |
}) |
| 103 |
} |
| 104 |
|
| 105 |
// WithMeterProvider specifies a meter provider to use for creating a metric. |
| 106 |
// If none is specified, the global provider is used. |
| 107 |
func WithMeterProvider(provider metric.MeterProvider) Option { |
| 108 |
return optionFunc(func(cfg *config) { |
| 109 |
if provider != nil { |
| 110 |
cfg.MeterProvider = provider |
| 111 |
} |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
// WithMetricAttributesFn returns an Option to set a function that maps an HTTP request to a slice of attribute.KeyValue. |
| 116 |
// These attributes will be included in metrics for every request. |
| 117 |
func WithMetricAttributesFn(metricAttributesFn func(r *http.Request) []attribute.KeyValue) Option { |
| 118 |
return optionFunc(func(c *config) { |
| 119 |
c.MetricAttributesFn = metricAttributesFn |
| 120 |
}) |
| 121 |
} |