Debian Codegraph @

instrumentation/go.mongodb.org/mongo-driver/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/mongo/otelmongo"
5
6 import (
7 "go.opentelemetry.io/otel"
8 "go.opentelemetry.io/otel/trace"
9 )
10
11 // ScopeName is the instrumentation scope name.
12 const ScopeName = "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo"
13
14 // config is used to configure the mongo tracer.
15 type config struct {
16 TracerProvider trace.TracerProvider
17
18 Tracer trace.Tracer
19
20 CommandAttributeDisabled bool
21 }
22
23 // newConfig returns a config with all Options set.
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 // Option specifies instrumentation configuration options.
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 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
52 // If none is specified, the global provider is used.
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 // WithCommandAttributeDisabled specifies if the MongoDB command is added as an attribute to Spans or not.
62 // This is disabled by default and the MongoDB command will not be added as an attribute
63 // to Spans if this option is not provided.
64 func WithCommandAttributeDisabled(disabled bool) Option {
65 return optionFunc(func(cfg *config) {
66 cfg.CommandAttributeDisabled = disabled
67 })
68 }