Debian Codegraph @

instrumentation/google.golang.org/grpc/otelgrpc/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelgrpc // import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
5
6 import (
7 "context"
8 "os"
9 "strings"
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 "google.golang.org/grpc/stats"
17 )
18
19 // ScopeName is the instrumentation scope name.
20 const ScopeName = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
21
22 // InterceptorFilter is a predicate used to determine whether a given request in
23 // interceptor info should be instrumented. A InterceptorFilter must return true if
24 // the request should be traced.
25 //
26 // Deprecated: Use stats handlers instead.
27 type InterceptorFilter func(*InterceptorInfo) bool
28
29 // Filter is a predicate used to determine whether a given request in
30 // should be instrumented by the attached RPC tag info.
31 // A Filter must return true if the request should be instrumented.
32 type Filter func(*stats.RPCTagInfo) bool
33
34 type semconvMode int
35
36 const (
37 semconvModeNew semconvMode = iota // Default
38 semconvModeOld
39 semconvModeDup
40 )
41
42 // config is a group of options for this instrumentation.
43 type config struct {
44 Filter Filter
45 InterceptorFilter InterceptorFilter
46 Propagators propagation.TextMapPropagator
47 TracerProvider trace.TracerProvider
48 MeterProvider metric.MeterProvider
49 SpanKind trace.SpanKind
50 SpanStartOptions []trace.SpanStartOption
51 SpanAttributes []attribute.KeyValue
52 MetricAttributes []attribute.KeyValue
53 MetricAttributesFn func(ctx context.Context) []attribute.KeyValue
54
55 PublicEndpoint bool
56 PublicEndpointFn func(ctx context.Context, info *stats.RPCTagInfo) bool
57
58 ReceivedEvent bool
59 SentEvent bool
60
61 semconvMode semconvMode
62 }
63
64 // Option applies an option value for a config.
65 type Option interface {
66 apply(*config)
67 }
68
69 type optionFunc func(*config)
70
71 func (f optionFunc) apply(c *config) {
72 f(c)
73 }
74
75 // newConfig returns a config configured with all the passed Options.
76 func newConfig(opts []Option) *config {
77 c := &config{
78 Propagators: otel.GetTextMapPropagator(),
79 TracerProvider: otel.GetTracerProvider(),
80 MeterProvider: otel.GetMeterProvider(),
81 semconvMode: parseSemconvMode(),
82 }
83 for _, o := range opts {
84 o.apply(c)
85 }
86
87 return c
88 }
89
90 func parseSemconvMode() semconvMode {
91 val := os.Getenv("OTEL_SEMCONV_STABILITY_OPT_IN")
92 if val == "" {
93 return semconvModeNew
94 }
95 parts := strings.SplitSeq(val, ",")
96 for p := range parts {
97 p = strings.TrimSpace(p)
98 if p == "rpc/dup" {
99 return semconvModeDup
100 }
101 if p == "rpc/old" {
102 return semconvModeOld
103 }
104 }
105 return semconvModeNew
106 }
107
108 // WithPublicEndpoint configures the Handler to link the span with an incoming
109 // span context. If this option is not provided, then the association is a child
110 // association instead of a link.
111 func WithPublicEndpoint() Option {
112 return optionFunc(func(c *config) {
113 c.PublicEndpoint = true
114 })
115 }
116
117 // WithPublicEndpointFn runs with every request, and allows conditionally
118 // configuring the Handler to link the span with an incoming span context. If
119 // this option is not provided or returns false, then the association is a
120 // child association instead of a link.
121 // Note: WithPublicEndpoint takes precedence over WithPublicEndpointFn.
122 func WithPublicEndpointFn(fn func(context.Context, *stats.RPCTagInfo) bool) Option {
123 return optionFunc(func(c *config) {
124 c.PublicEndpointFn = fn
125 })
126 }
127
128 // WithPropagators returns an Option to use the Propagators when extracting
129 // and injecting trace context from requests.
130 func WithPropagators(p propagation.TextMapPropagator) Option {
131 return optionFunc(func(c *config) {
132 if p != nil {
133 c.Propagators = p
134 }
135 })
136 }
137
138 // WithInterceptorFilter returns an Option to use the request filter.
139 //
140 // Deprecated: Use stats handlers instead.
141 func WithInterceptorFilter(f InterceptorFilter) Option {
142 return optionFunc(func(c *config) {
143 if f != nil {
144 c.InterceptorFilter = f
145 }
146 })
147 }
148
149 // WithFilter returns an Option to use the request filter.
150 func WithFilter(f Filter) Option {
151 return optionFunc(func(c *config) {
152 if f != nil {
153 c.Filter = f
154 }
155 })
156 }
157
158 // WithTracerProvider returns an Option to use the TracerProvider when
159 // creating a Tracer.
160 func WithTracerProvider(tp trace.TracerProvider) Option {
161 return optionFunc(func(c *config) {
162 if tp != nil {
163 c.TracerProvider = tp
164 }
165 })
166 }
167
168 // WithMeterProvider returns an Option to use the MeterProvider when
169 // creating a Meter. If this option is not provide the global MeterProvider will be used.
170 func WithMeterProvider(mp metric.MeterProvider) Option {
171 return optionFunc(func(c *config) {
172 if mp != nil {
173 c.MeterProvider = mp
174 }
175 })
176 }
177
178 // Event type that can be recorded, see WithMessageEvents.
179 type Event int
180
181 // Different types of events that can be recorded, see WithMessageEvents.
182 const (
183 ReceivedEvents Event = iota
184 SentEvents
185 )
186
187 // WithMessageEvents configures the Handler to record the specified events
188 // (span.AddEvent) on spans. By default only summary attributes are added at the
189 // end of the request.
190 //
191 // Valid events are:
192 // - ReceivedEvents: Record the number of bytes read after every gRPC read operation.
193 // - SentEvents: Record the number of bytes written after every gRPC write operation.
194 func WithMessageEvents(events ...Event) Option {
195 return optionFunc(func(c *config) {
196 for _, e := range events {
197 switch e {
198 case ReceivedEvents:
199 c.ReceivedEvent = true
200 case SentEvents:
201 c.SentEvent = true
202 }
203 }
204 })
205 }
206
207 // WithSpanOptions configures an additional set of
208 // trace.SpanOptions, which are applied to each new span.
209 //
210 // Deprecated: It is only used by the deprecated interceptor, and is unused by [NewClientHandler] and [NewServerHandler].
211 func WithSpanOptions(opts ...trace.SpanStartOption) Option {
212 return optionFunc(func(c *config) {
213 c.SpanStartOptions = append(c.SpanStartOptions, opts...)
214 })
215 }
216
217 // WithSpanKind returns an Option to set the span kind for spans created by
218 // the handler.
219 //
220 // By default, [NewServerHandler] creates spans with
221 // [trace.SpanKindServer] and [NewClientHandler] creates spans with
222 // [trace.SpanKindClient].
223 func WithSpanKind(sk trace.SpanKind) Option {
224 return optionFunc(func(c *config) {
225 c.SpanKind = sk
226 })
227 }
228
229 // WithSpanAttributes returns an Option to add custom attributes to the spans.
230 func WithSpanAttributes(a ...attribute.KeyValue) Option {
231 return optionFunc(func(c *config) {
232 if a != nil {
233 c.SpanAttributes = a
234 }
235 })
236 }
237
238 // WithMetricAttributes returns an Option to add custom attributes to the metrics.
239 func WithMetricAttributes(a ...attribute.KeyValue) Option {
240 return optionFunc(func(c *config) {
241 if a != nil {
242 c.MetricAttributes = append(c.MetricAttributes, a...)
243 }
244 })
245 }
246
247 // WithMetricAttributesFn returns an Option to add dynamic custom attributes to the handler's metrics.
248 // The function is called once per RPC and the returned attributes are applied to all metrics recorded by this handler.
249 //
250 // The context parameter is the standard gRPC request context and provides access to request-scoped data.
251 func WithMetricAttributesFn(fn func(ctx context.Context) []attribute.KeyValue) Option {
252 return optionFunc(func(c *config) {
253 if fn != nil {
254 c.MetricAttributesFn = fn
255 }
256 })
257 }