This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
forked from connectrpc/otelconnect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
211 lines (177 loc) · 6 KB
/
option.go
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
// Copyright 2022-2023 Buf Technologies, Inc.
//
// 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"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
)
// 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 global.MeterProvider().
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, *Request) bool) Option {
return &filterOption{filter}
}
// WithoutTracing disables tracing.
func WithoutTracing() Option {
return WithTracerProvider(trace.NewNoopTracerProvider())
}
// WithoutMetrics disables metrics.
func WithoutMetrics() Option {
return WithMeterProvider(noop.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(request *Request, value attribute.KeyValue) bool {
if request.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{}
}
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, *Request) 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
}