| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelmongo |
| 5 |
|
| 6 |
import ( |
| 7 |
"go.opentelemetry.io/otel" |
| 8 |
"go.opentelemetry.io/otel/trace" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo" |
| 13 |
|
| 14 |
|
| 15 |
type config struct { |
| 16 |
TracerProvider trace.TracerProvider |
| 17 |
|
| 18 |
Tracer trace.Tracer |
| 19 |
|
| 20 |
CommandAttributeDisabled bool |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func newConfig(opts ...Option) config { |
| 25 |
cfg := config{ |
| 26 |
TracerProvider: otel.GetTracerProvider(), |
| 27 |
CommandAttributeDisabled: true, |
| 28 |
} |
| 29 |
for _, opt := range opts { |
| 30 |
opt.apply(&cfg) |
| 31 |
} |
| 32 |
|
| 33 |
cfg.Tracer = cfg.TracerProvider.Tracer( |
| 34 |
ScopeName, |
| 35 |
trace.WithInstrumentationVersion(Version), |
| 36 |
) |
| 37 |
return cfg |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type Option interface { |
| 42 |
apply(*config) |
| 43 |
} |
| 44 |
|
| 45 |
type optionFunc func(*config) |
| 46 |
|
| 47 |
func (o optionFunc) apply(c *config) { |
| 48 |
o(c) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func WithTracerProvider(provider trace.TracerProvider) Option { |
| 54 |
return optionFunc(func(cfg *config) { |
| 55 |
if provider != nil { |
| 56 |
cfg.TracerProvider = provider |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func WithCommandAttributeDisabled(disabled bool) Option { |
| 65 |
return optionFunc(func(cfg *config) { |
| 66 |
cfg.CommandAttributeDisabled = disabled |
| 67 |
}) |
| 68 |
} |