-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathoption.go
More file actions
281 lines (235 loc) · 8.36 KB
/
Copy pathoption.go
File metadata and controls
281 lines (235 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2022-2025 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otelconnect
import (
"context"
"net/http"
"connectrpc.com/connect"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
metricnoop "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
tracenoop "go.opentelemetry.io/otel/trace/noop"
)
var (
// ConnectRPCSystem indicates that the semantic conventions for
// ConnectRPC should be used.
ConnectRPCSystem = connectRPCSystem{} //nolint:gochecknoglobals
// GRPCSystem indicates that the semantic conventions for gRPC
// should be used.
GRPCSystem = gRPCSystem{} //nolint:gochecknoglobals
)
// An Option configures the OpenTelemetry instrumentation.
type Option interface {
apply(*config)
}
// WithPropagator configures the instrumentation to use the supplied propagator
// when extracting and injecting trace context. By default, the instrumentation
// uses [otel.GetTextMapPropagator].
func WithPropagator(propagator propagation.TextMapPropagator) Option {
return &propagatorOption{propagator}
}
// WithMeterProvider configures the instrumentation to use the supplied [metric.MeterProvider]
// when extracting and injecting trace context. By default, the instrumentation
// uses [otel.GetMeterProvider].
func WithMeterProvider(provider metric.MeterProvider) Option {
return &meterProviderOption{provider: provider}
}
// WithTracerProvider configures the instrumentation to use the supplied
// provider when creating a tracer. By default, the instrumentation
// uses [otel.GetTracerProvider].
func WithTracerProvider(provider trace.TracerProvider) Option {
return &tracerProviderOption{provider}
}
// WithFilter configures the instrumentation to emit traces and metrics only
// when the filter function returns true. Filter functions must be safe to call concurrently.
func WithFilter(filter func(context.Context, connect.Spec) bool) Option {
return &filterOption{filter}
}
// WithoutTracing disables tracing.
func WithoutTracing() Option {
return WithTracerProvider(tracenoop.NewTracerProvider())
}
// WithoutMetrics disables metrics.
func WithoutMetrics() Option {
return WithMeterProvider(metricnoop.NewMeterProvider())
}
// WithAttributeFilter sets the attribute filter for all metrics and trace attributes.
func WithAttributeFilter(filter AttributeFilter) Option {
return &attributeFilterOption{filterAttribute: filter}
}
// WithoutServerPeerAttributes removes net.peer.port and net.peer.name
// attributes from server trace and span attributes. The default behavior
// follows the OpenTelemetry semantic conventions for RPC, but produces very
// high-cardinality data; this option significantly reduces cardinality in most
// environments.
func WithoutServerPeerAttributes() Option {
return WithAttributeFilter(func(spec connect.Spec, value attribute.KeyValue) bool {
if spec.IsClient {
return true
}
if value.Key == semconv.NetPeerPortKey {
return false
}
if value.Key == semconv.NetPeerNameKey {
return false
}
return true
})
}
// WithTrustRemote sets the Interceptor to trust remote spans.
// By default, all incoming server spans are untrusted and will be linked
// with a [trace.Link] and will not be a child span.
// By default, all client spans are trusted and no change occurs when WithTrustRemote is used.
func WithTrustRemote() Option {
return &trustRemoteOption{}
}
// WithTraceRequestHeader enables header attributes for the request header keys provided.
// Attributes will be added as Trace attributes only.
func WithTraceRequestHeader(keys ...string) Option {
return &traceRequestHeaderOption{
keys: keys,
}
}
// WithTraceResponseHeader enables header attributes for the response header keys provided.
// Attributes will be added as Trace attributes only.
func WithTraceResponseHeader(keys ...string) Option {
return &traceResponseHeaderOption{
keys: keys,
}
}
// WithoutTraceEvents disables trace events for both unary and streaming
// interceptors. This reduces the quantity of data sent to your tracing system
// by omitting per-message information like message size.
func WithoutTraceEvents() Option {
return &omitTraceEventsOption{}
}
// WithPropagateResponseHeader enables injecting the traceparent header
// into response headers for server-side interceptors. This allows clients
// to correlate their requests with server-side traces.
func WithPropagateResponseHeader() Option {
return &propagateResponseHeaderOption{}
}
// WithRPCSystem forces the use of the semantic conventions for the given
// RPC system. By default, the conventions used vary based on the actual
// protocol of a request: so requests that a client sends or a server
// receives that use the gRPC or gRPC-Web protocols use the gRPC semantic
// conventions; requests that use ConnectRPC use the ConnectRPC semantic
// conventions.
//
// In a system where a server handles requests for the same service but
// from clients that use multiple protocols, this causes the telemetry
// data to be partitioned by the RPC system conventions. But it is often
// desirable to instead emit uniform telemetry, to allow optics and
// aggregation across RPC systems.
//
// So this option can be used to force uniform metrics and spans, using
// the given RPC system conventions, regardless of the actual protocol.
func WithRPCSystem(system RPCSystem) Option {
return &rpcSystemOption{system: system}
}
// RPCSystem represents an RPC system, like ConnectRPC or gRPC. Different
// systems have different semantic conventions for how metrics and spans are
// defined.
//
// https://opentelemetry.io/docs/specs/semconv/rpc/
//
// Valid values currently are ConnectRPCSystem and GRPCSystem.
type RPCSystem interface {
protocol() string
}
type attributeFilterOption struct {
filterAttribute AttributeFilter
}
func (o *attributeFilterOption) apply(c *config) {
if o.filterAttribute != nil {
c.filterAttribute = o.filterAttribute
}
}
type propagatorOption struct {
propagator propagation.TextMapPropagator
}
func (o *propagatorOption) apply(c *config) {
if o.propagator != nil {
c.propagator = o.propagator
}
}
type tracerProviderOption struct {
provider trace.TracerProvider
}
func (o *tracerProviderOption) apply(c *config) {
if o.provider != nil {
c.tracer = o.provider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(semanticVersion),
)
}
}
type filterOption struct {
filter func(context.Context, connect.Spec) bool
}
func (o *filterOption) apply(c *config) {
if o.filter != nil {
c.filter = o.filter
}
}
type meterProviderOption struct {
provider metric.MeterProvider
}
func (m meterProviderOption) apply(c *config) {
c.meter = m.provider.Meter(
instrumentationName,
metric.WithInstrumentationVersion(semanticVersion),
)
}
type trustRemoteOption struct{}
func (o *trustRemoteOption) apply(c *config) {
c.trustRemote = true
}
type traceRequestHeaderOption struct {
keys []string
}
func (o *traceRequestHeaderOption) apply(c *config) {
for _, key := range o.keys {
c.requestHeaderKeys = append(c.requestHeaderKeys, http.CanonicalHeaderKey(key))
}
}
type traceResponseHeaderOption struct {
keys []string
}
func (o *traceResponseHeaderOption) apply(c *config) {
for _, key := range o.keys {
c.responseHeaderKeys = append(c.responseHeaderKeys, http.CanonicalHeaderKey(key))
}
}
type omitTraceEventsOption struct{}
func (o *omitTraceEventsOption) apply(c *config) {
c.omitTraceEvents = true
}
type propagateResponseHeaderOption struct{}
func (o *propagateResponseHeaderOption) apply(c *config) {
c.propagateResponseHeader = true
}
type rpcSystemOption struct {
system RPCSystem
}
func (o *rpcSystemOption) apply(c *config) {
c.rpcSystem = o.system
}
type connectRPCSystem struct{}
func (connectRPCSystem) protocol() string { return connectProtocol }
type gRPCSystem struct{}
func (gRPCSystem) protocol() string { return grpcProtocol }