| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package 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 |
|
| 21 |
const ( |
| 22 |
semconvOptIn = "OTEL_SEMCONV_STABILITY_OPT_IN" |
| 23 |
semconvOptInDup = "database/dup" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
type EventMonitor struct { |
| 29 |
version string |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func NewEventMonitor() EventMonitor { |
| 35 |
return EventMonitor{ |
| 36 |
version: strings.ToLower(os.Getenv(semconvOptIn)), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type AttributeOptions struct { |
| 42 |
collectionName string |
| 43 |
commandAttributeDisabled bool |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
type AttributeOption func(*AttributeOptions) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func WithCollectionName(collName string) AttributeOption { |
| 52 |
return func(opts *AttributeOptions) { |
| 53 |
opts.collectionName = collName |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
func WithCommandAttributeDisabled(disabled bool) AttributeOption { |
| 60 |
return func(opts *AttributeOptions) { |
| 61 |
opts.commandAttributeDisabled = disabled |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 77 |
|
| 78 |
func (m EventMonitor) CommandStartedTraceAttrs( |
| 79 |
evt *event.CommandStartedEvent, |
| 80 |
opts ...AttributeOption, |
| 81 |
) []attribute.KeyValue { |
| 82 |
|
| 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 |
|
| 94 |
func peerInfo(evt *event.CommandStartedEvent) (hostname string, port int) { |
| 95 |
hostname = evt.ConnectionID |
| 96 |
port = 27017 |
| 97 |
|
| 98 |
host, portStr, err := net.SplitHostPort(hostname) |
| 99 |
if err != nil { |
| 100 |
|
| 101 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
func sanitizeCommand(command bson.Raw) string { |
| 116 |
b, _ := bson.MarshalExtJSON(command, false, false) |
| 117 |
|
| 118 |
return string(b) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 157 |
|
| 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 |
} |