| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package 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 |
|
| 20 |
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
type InterceptorFilter func(*InterceptorInfo) bool |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type Filter func(*stats.RPCTagInfo) bool |
| 33 |
|
| 34 |
type semconvMode int |
| 35 |
|
| 36 |
const ( |
| 37 |
semconvModeNew semconvMode = iota |
| 38 |
semconvModeOld |
| 39 |
semconvModeDup |
| 40 |
) |
| 41 |
|
| 42 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
func WithPublicEndpoint() Option { |
| 112 |
return optionFunc(func(c *config) { |
| 113 |
c.PublicEndpoint = true |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 129 |
|
| 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 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 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 |
|
| 159 |
|
| 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 |
|
| 169 |
|
| 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 |
|
| 179 |
type Event int |
| 180 |
|
| 181 |
|
| 182 |
const ( |
| 183 |
ReceivedEvents Event = iota |
| 184 |
SentEvents |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func WithSpanOptions(opts ...trace.SpanStartOption) Option { |
| 212 |
return optionFunc(func(c *config) { |
| 213 |
c.SpanStartOptions = append(c.SpanStartOptions, opts...) |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
func WithSpanKind(sk trace.SpanKind) Option { |
| 224 |
return optionFunc(func(c *config) { |
| 225 |
c.SpanKind = sk |
| 226 |
}) |
| 227 |
} |
| 228 |
|
| 229 |
|
| 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 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 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 |
} |