Debian Codegraph @

instrumentation/net/http/otelhttp/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
5
6 import (
7 "context"
8 "net/http"
9 "net/http/httptrace"
10
11 "go.opentelemetry.io/otel"
12 "go.opentelemetry.io/otel/attribute"
13 "go.opentelemetry.io/otel/metric"
14 "go.opentelemetry.io/otel/propagation"
15 "go.opentelemetry.io/otel/trace"
16 )
17
18 // ScopeName is the instrumentation scope name.
19 const ScopeName = "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
20
21 // config represents the configuration options available for the http.Handler
22 // and http.Transport types.
23 type config struct {
24 ServerName string
25 Tracer trace.Tracer
26 Meter metric.Meter
27 Propagators propagation.TextMapPropagator
28 SpanStartOptions []trace.SpanStartOption
29 PublicEndpointFn func(*http.Request) bool
30 ReadEvent bool
31 WriteEvent bool
32 Filters []Filter
33 SpanNameFormatter func(string, *http.Request) string
34 ClientTrace func(context.Context) *httptrace.ClientTrace
35
36 TracerProvider trace.TracerProvider
37 MeterProvider metric.MeterProvider
38 MetricAttributesFn func(*http.Request) []attribute.KeyValue
39 }
40
41 // Option interface used for setting optional config properties.
42 type Option interface {
43 apply(*config)
44 }
45
46 type optionFunc func(*config)
47
48 func (o optionFunc) apply(c *config) {
49 o(c)
50 }
51
52 // newConfig creates a new config struct and applies opts to it.
53 func newConfig(opts ...Option) *config {
54 c := &config{
55 Propagators: otel.GetTextMapPropagator(),
56 MeterProvider: otel.GetMeterProvider(),
57 }
58 for _, opt := range opts {
59 opt.apply(c)
60 }
61
62 // Tracer is only initialized if manually specified. Otherwise, can be passed with the tracing context.
63 if c.TracerProvider != nil {
64 c.Tracer = newTracer(c.TracerProvider)
65 }
66
67 c.Meter = c.MeterProvider.Meter(
68 ScopeName,
69 metric.WithInstrumentationVersion(Version),
70 )
71
72 return c
73 }
74
75 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
76 // If none is specified, the global provider is used.
77 func WithTracerProvider(provider trace.TracerProvider) Option {
78 return optionFunc(func(cfg *config) {
79 if provider != nil {
80 cfg.TracerProvider = provider
81 }
82 })
83 }
84
85 // WithMeterProvider specifies a meter provider to use for creating a meter.
86 // If none is specified, the global provider is used.
87 func WithMeterProvider(provider metric.MeterProvider) Option {
88 return optionFunc(func(cfg *config) {
89 if provider != nil {
90 cfg.MeterProvider = provider
91 }
92 })
93 }
94
95 // WithPublicEndpointFn runs with every request, and allows conditionally
96 // configuring the Handler to link the span with an incoming span context. If
97 // this option is not provided or returns false, then the association is a
98 // child association instead of a link.
99 func WithPublicEndpointFn(fn func(*http.Request) bool) Option {
100 return optionFunc(func(c *config) {
101 c.PublicEndpointFn = fn
102 })
103 }
104
105 // WithPropagators configures specific propagators. If this
106 // option isn't specified, then the global TextMapPropagator is used.
107 func WithPropagators(ps propagation.TextMapPropagator) Option {
108 return optionFunc(func(c *config) {
109 if ps != nil {
110 c.Propagators = ps
111 }
112 })
113 }
114
115 // WithSpanOptions configures an additional set of
116 // trace.SpanOptions, which are applied to each new span.
117 func WithSpanOptions(opts ...trace.SpanStartOption) Option {
118 return optionFunc(func(c *config) {
119 c.SpanStartOptions = append(c.SpanStartOptions, opts...)
120 })
121 }
122
123 // WithFilter adds a filter to the list of filters used by the handler.
124 // If any filter indicates to exclude a request then the request will not be
125 // traced. All filters must allow a request to be traced for a Span to be created.
126 // If no filters are provided then all requests are traced.
127 // Filters will be invoked for each processed request, it is advised to make them
128 // simple and fast.
129 func WithFilter(f Filter) Option {
130 return optionFunc(func(c *config) {
131 c.Filters = append(c.Filters, f)
132 })
133 }
134
135 // Event represents message event types for [WithMessageEvents].
136 type Event int
137
138 // Different types of events that can be recorded, see WithMessageEvents.
139 const (
140 unspecifiedEvents Event = iota
141 ReadEvents
142 WriteEvents
143 )
144
145 // WithMessageEvents configures the Handler to record the specified events
146 // (span.AddEvent) on spans. By default only summary attributes are added at the
147 // end of the request.
148 //
149 // Valid events are:
150 // - ReadEvents: Record the number of bytes read after every http.Request.Body.Read
151 // using the ReadBytesKey
152 // - WriteEvents: Record the number of bytes written after every http.ResponeWriter.Write
153 // using the WriteBytesKey
154 func WithMessageEvents(events ...Event) Option {
155 return optionFunc(func(c *config) {
156 for _, e := range events {
157 switch e {
158 case ReadEvents:
159 c.ReadEvent = true
160 case WriteEvents:
161 c.WriteEvent = true
162 }
163 }
164 })
165 }
166
167 // WithSpanNameFormatter takes a function that will be called on every
168 // request and the returned string will become the Span Name.
169 //
170 // When using [http.ServeMux] (or any middleware that sets the Pattern of [http.Request]),
171 // the span name formatter will run twice. Once when the span is created, and
172 // second time after the middleware, so the pattern can be used.
173 func WithSpanNameFormatter(f func(operation string, r *http.Request) string) Option {
174 return optionFunc(func(c *config) {
175 c.SpanNameFormatter = f
176 })
177 }
178
179 // WithClientTrace takes a function that returns client trace instance that will be
180 // applied to the requests sent through the otelhttp Transport.
181 func WithClientTrace(f func(context.Context) *httptrace.ClientTrace) Option {
182 return optionFunc(func(c *config) {
183 c.ClientTrace = f
184 })
185 }
186
187 // WithServerName returns an Option that sets the name of the (virtual) server
188 // handling requests.
189 func WithServerName(server string) Option {
190 return optionFunc(func(c *config) {
191 c.ServerName = server
192 })
193 }
194
195 // WithMetricAttributesFn returns an Option to set a function that maps an HTTP request to a slice of attribute.KeyValue.
196 // These attributes will be included in metrics for every request.
197 //
198 // Deprecated: WithMetricAttributesFn is deprecated and will be removed in a
199 // future release. Use [Labeler] instead.
200 func WithMetricAttributesFn(metricAttributesFn func(r *http.Request) []attribute.KeyValue) Option {
201 return optionFunc(func(c *config) {
202 c.MetricAttributesFn = metricAttributesFn
203 })
204 }