Debian Codegraph @

instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv/event_monitor.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package semconv provides semantic convention types and functionality.
5 package semconv // import "go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/internal/semconv"
6
7 import (
8 "net"
9 "os"
10 "strconv"
11 "strings"
12
13 "go.mongodb.org/mongo-driver/bson"
14 "go.mongodb.org/mongo-driver/event"
15 "go.opentelemetry.io/otel/attribute"
16 semconv1210 "go.opentelemetry.io/otel/semconv/v1.21.0"
17 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
18 )
19
20 // Constants for environment variable keys and versions.
21 const (
22 semconvOptIn = "OTEL_SEMCONV_STABILITY_OPT_IN"
23 semconvOptInDup = "database/dup"
24 )
25
26 // EventMonitor is responsible for monitoring events with a specified semantic
27 // version.
28 type EventMonitor struct {
29 version string
30 }
31
32 // NewEventMonitor creates an EventMonitor with the version set based on the
33 // OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
34 func NewEventMonitor() EventMonitor {
35 return EventMonitor{
36 version: strings.ToLower(os.Getenv(semconvOptIn)),
37 }
38 }
39
40 // AttributeOptions represents options for tracing attributes.
41 type AttributeOptions struct {
42 collectionName string
43 commandAttributeDisabled bool
44 }
45
46 // AttributeOption is a function type that modifies AttributeOptions.
47 type AttributeOption func(*AttributeOptions)
48
49 // WithCollectionName is a functional option to set the collection name in
50 // AttributeOptions.
51 func WithCollectionName(collName string) AttributeOption {
52 return func(opts *AttributeOptions) {
53 opts.collectionName = collName
54 }
55 }
56
57 // WithCommandAttributeDisabled is a functional option to enable or disable
58 // command attributes.
59 func WithCommandAttributeDisabled(disabled bool) AttributeOption {
60 return func(opts *AttributeOptions) {
61 opts.commandAttributeDisabled = disabled
62 }
63 }
64
65 // hasOptIn returns true if the comma-separated version string contains the
66 // exact optIn value.
67 func hasOptIn(version, optIn string) bool {
68 for v := range strings.SplitSeq(version, ",") {
69 if strings.TrimSpace(v) == optIn {
70 return true
71 }
72 }
73 return false
74 }
75
76 // CommandStartedTraceAttrs generates trace attributes for a CommandStartedEvent
77 // based on the EventMonitor version.
78 func (m EventMonitor) CommandStartedTraceAttrs(
79 evt *event.CommandStartedEvent,
80 opts ...AttributeOption,
81 ) []attribute.KeyValue {
82 // Dup implies both v1.26.0 and v1.21.0
83 if hasOptIn(m.version, semconvOptInDup) {
84 return append(
85 commandStartedTraceAttrs(evt, opts...),
86 commandStartedTraceAttrsV1210(evt, opts...)...,
87 )
88 }
89
90 return commandStartedTraceAttrs(evt, opts...)
91 }
92
93 // peerInfo extracts the hostname and port from a CommandStartedEvent.
94 func peerInfo(evt *event.CommandStartedEvent) (hostname string, port int) {
95 hostname = evt.ConnectionID
96 port = 27017 // Default MongoDB port
97
98 host, portStr, err := net.SplitHostPort(hostname)
99 if err != nil {
100 // If there's an error (likely because there's no port), assume default port
101 // and use ConnectionID as hostname
102 return hostname, port
103 }
104
105 if parsedPort, err := strconv.Atoi(portStr); err == nil {
106 port = parsedPort
107 }
108
109 return host, port
110 }
111
112 // sanitizeCommand converts a BSON command to a sanitized JSON string.
113 // TODO: Sanitize values where possible.
114 // TODO: Limit maximum size.
115 func sanitizeCommand(command bson.Raw) string {
116 b, _ := bson.MarshalExtJSON(command, false, false)
117
118 return string(b)
119 }
120
121 // commandStartedTraceAttrs generates trace attributes for the latest semantic
122 // version.
123 func commandStartedTraceAttrs(evt *event.CommandStartedEvent, setters ...AttributeOption) []attribute.KeyValue {
124 opts := &AttributeOptions{}
125 for _, set := range setters {
126 set(opts)
127 }
128
129 attrs := []attribute.KeyValue{semconv.DBSystemNameMongoDB}
130
131 attrs = append(
132 attrs,
133 semconv.DBOperationName(evt.CommandName),
134 semconv.DBNamespace(evt.DatabaseName),
135 semconv.NetworkTransportTCP,
136 )
137
138 hostname, port := peerInfo(evt)
139 attrs = append(
140 attrs,
141 semconv.NetworkPeerPort(port),
142 semconv.NetworkPeerAddress(net.JoinHostPort(hostname, strconv.Itoa(port))),
143 )
144
145 if !opts.commandAttributeDisabled {
146 attrs = append(attrs, semconv.DBQueryText(sanitizeCommand(evt.Command)))
147 }
148
149 if opts.collectionName != "" {
150 attrs = append(attrs, semconv.DBCollectionName(opts.collectionName))
151 }
152
153 return attrs
154 }
155
156 // commandStartedTraceAttrsV1210 generates trace attributes for semantic version
157 // 1.21.0.
158 func commandStartedTraceAttrsV1210(evt *event.CommandStartedEvent, setters ...AttributeOption) []attribute.KeyValue {
159 opts := &AttributeOptions{}
160 for _, set := range setters {
161 set(opts)
162 }
163
164 attrs := []attribute.KeyValue{semconv1210.DBSystemMongoDB}
165
166 attrs = append(
167 attrs,
168 semconv1210.DBOperation(evt.CommandName),
169 semconv1210.DBName(evt.DatabaseName),
170 semconv1210.NetTransportTCP,
171 )
172
173 hostname, port := peerInfo(evt)
174 attrs = append(
175 attrs,
176 semconv1210.NetPeerPort(port),
177 semconv1210.NetPeerName(hostname),
178 )
179
180 if !opts.commandAttributeDisabled {
181 attrs = append(attrs, semconv1210.DBStatement(sanitizeCommand(evt.Command)))
182 }
183
184 if opts.collectionName != "" {
185 attrs = append(attrs, semconv1210.DBMongoDBCollection(opts.collectionName))
186 }
187
188 return attrs
189 }