Debian Codegraph @

instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo/config.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelmongo // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo"
5
6 import (
7 "go.mongodb.org/mongo-driver/v2/event"
8 "go.opentelemetry.io/otel"
9 "go.opentelemetry.io/otel/metric"
10 "go.opentelemetry.io/otel/trace"
11 )
12
13 // ScopeName is the instrumentation scope name.
14 const ScopeName = "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo"
15
16 // config is used to configure the mongo tracer.
17 type config struct {
18 MeterProvider metric.MeterProvider
19 TracerProvider trace.TracerProvider
20
21 Meter metric.Meter
22 Tracer trace.Tracer
23
24 CommandAttributeDisabled bool
25
26 SpanNameFormatter SpanNameFormatterFunc
27 }
28
29 // newConfig returns a config with all Options set.
30 func newConfig(opts ...Option) config {
31 cfg := config{
32 MeterProvider: otel.GetMeterProvider(),
33 TracerProvider: otel.GetTracerProvider(),
34 CommandAttributeDisabled: true,
35 }
36
37 cfg.SpanNameFormatter = func(event *event.CommandStartedEvent) string {
38 collection, _ := extractCollection(event)
39 if collection != "" {
40 return collection + "." + event.CommandName
41 }
42
43 return event.CommandName
44 }
45
46 for _, opt := range opts {
47 opt.apply(&cfg)
48 }
49
50 cfg.Meter = cfg.MeterProvider.Meter(
51 ScopeName,
52 metric.WithInstrumentationVersion(Version),
53 )
54
55 cfg.Tracer = cfg.TracerProvider.Tracer(
56 ScopeName,
57 trace.WithInstrumentationVersion(Version),
58 )
59 return cfg
60 }
61
62 // Option specifies instrumentation configuration options.
63 type Option interface {
64 apply(*config)
65 }
66
67 type optionFunc func(*config)
68
69 func (o optionFunc) apply(c *config) {
70 o(c)
71 }
72
73 // WithMeterProvider specifies a [metric.MeterProvider] to use for creating a Meter.
74 // If none is specified, the global MeterProvider is used.
75 func WithMeterProvider(provider metric.MeterProvider) Option {
76 return optionFunc(func(cfg *config) {
77 if provider != nil {
78 cfg.MeterProvider = provider
79 }
80 })
81 }
82
83 // SpanNameFormatterFunc is a function that resolves the span name given an
84 // *event.CommandStartedEvent.
85 type SpanNameFormatterFunc func(e *event.CommandStartedEvent) string
86
87 // WithSpanNameFormatter specifies a function that resolves the span name given an
88 // *event.CommandStartedEvent. If none is specified, the default resolver is used,
89 // which returns "<collection>.<command>" if the collection is non-empty,
90 // and just "<command>" otherwise.
91 func WithSpanNameFormatter(resolver SpanNameFormatterFunc) Option {
92 return optionFunc(func(cfg *config) {
93 if resolver != nil {
94 cfg.SpanNameFormatter = resolver
95 }
96 })
97 }
98
99 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
100 // If none is specified, the global provider is used.
101 func WithTracerProvider(provider trace.TracerProvider) Option {
102 return optionFunc(func(cfg *config) {
103 if provider != nil {
104 cfg.TracerProvider = provider
105 }
106 })
107 }
108
109 // WithCommandAttributeDisabled specifies if the MongoDB command is added as an attribute to Spans or not.
110 // This is disabled by default and the MongoDB command will not be added as an attribute
111 // to Spans if this option is not provided.
112 func WithCommandAttributeDisabled(disabled bool) Option {
113 return optionFunc(func(cfg *config) {
114 cfg.CommandAttributeDisabled = disabled
115 })
116 }