Debian Codegraph @

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

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package semconv
5
6 import (
7 "net"
8 "strconv"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "go.mongodb.org/mongo-driver/bson"
13 "go.mongodb.org/mongo-driver/event"
14 "go.opentelemetry.io/otel/attribute"
15 semconv1210 "go.opentelemetry.io/otel/semconv/v1.21.0"
16 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
17 )
18
19 func TestNewEventMonitor(t *testing.T) {
20 tests := []struct {
21 name string
22 version string
23 want string
24 }{
25 {
26 name: "Default Version",
27 version: "",
28 want: "",
29 },
30 {
31 name: "Duplicate Version",
32 version: semconvOptInDup,
33 want: "database/dup",
34 },
35 }
36
37 for _, test := range tests {
38 t.Run(test.name, func(t *testing.T) {
39 t.Setenv(semconvOptIn, test.version)
40
41 monitor := NewEventMonitor()
42 assert.Equal(t, test.want, monitor.version, "Expected version does not match")
43 })
44 }
45 }
46
47 func TestPeerInfo(t *testing.T) {
48 // Test cases for peerInfo
49 tests := []struct {
50 name string
51 connectionID string
52 wantHostname string
53 wantPort int
54 }{
55 {"No Port", "localhost", "localhost", 27017},
56 {"With Port", "localhost:12345", "localhost", 12345},
57 }
58
59 for _, tt := range tests {
60 t.Run(tt.name, func(t *testing.T) {
61 evt := &event.CommandStartedEvent{ConnectionID: tt.connectionID}
62 hostname, port := peerInfo(evt)
63 assert.Equal(t, tt.wantHostname, hostname, "Hostname does not match")
64 assert.Equal(t, tt.wantPort, port, "Port does not match")
65 })
66 }
67 }
68
69 func TestCommandStartedTraceAttrs(t *testing.T) {
70 const (
71 opName = "opName"
72 dbNamespace = "dbNamespace"
73 port = 1
74 host = "host"
75 address = "host:1"
76 stmt = `{"insert":"users"}`
77 coll = "coll"
78 )
79
80 v1210 := []attribute.KeyValue{
81 semconv1210.DBSystemMongoDB,
82 {Key: "db.operation", Value: attribute.StringValue(opName)},
83 {Key: "db.name", Value: attribute.StringValue(dbNamespace)},
84 {Key: "db.statement", Value: attribute.StringValue(stmt)},
85 {Key: "net.peer.port", Value: attribute.IntValue(port)},
86 {Key: "net.peer.name", Value: attribute.StringValue(host)},
87 {Key: "net.transport", Value: attribute.StringValue("ip_tcp")},
88 {Key: "db.mongodb.collection", Value: attribute.StringValue("coll")},
89 }
90
91 v1260 := []attribute.KeyValue{
92 semconv.DBSystemNameMongoDB,
93 {Key: "db.operation.name", Value: attribute.StringValue(opName)},
94 {Key: "db.namespace", Value: attribute.StringValue(dbNamespace)},
95 {Key: "db.query.text", Value: attribute.StringValue(stmt)},
96 {Key: "network.peer.port", Value: attribute.IntValue(port)},
97 {Key: "network.peer.address", Value: attribute.StringValue(address)},
98 {Key: "network.transport", Value: attribute.StringValue("tcp")},
99 {Key: "db.collection.name", Value: attribute.StringValue("coll")},
100 }
101
102 tests := []struct {
103 name string
104 initAttrs []attribute.KeyValue
105 version string
106 want []attribute.KeyValue
107 }{
108 {
109 name: "no version",
110 initAttrs: []attribute.KeyValue{},
111 version: "",
112 want: v1260,
113 },
114 {
115 name: "unsupported version",
116 initAttrs: []attribute.KeyValue{},
117 version: "database/foo",
118 want: v1260,
119 },
120 {
121 name: "database/dup",
122 initAttrs: []attribute.KeyValue{},
123 version: "database/dup",
124 want: append(v1210, v1260...),
125 },
126 {
127 name: "mixed categories",
128 initAttrs: []attribute.KeyValue{},
129 version: "database/dup,http",
130 want: append(v1210, v1260...),
131 },
132 }
133
134 for _, test := range tests {
135 t.Run(test.name, func(t *testing.T) {
136 t.Setenv(semconvOptIn, test.version)
137
138 stmtBytes, err := bson.Marshal(bson.D{{Key: "insert", Value: "users"}})
139 assert.NoError(t, err)
140
141 monitor := NewEventMonitor()
142 attrs := monitor.CommandStartedTraceAttrs(&event.CommandStartedEvent{
143 DatabaseName: dbNamespace,
144 CommandName: opName,
145 Command: bson.Raw(stmtBytes),
146 ConnectionID: net.JoinHostPort(host, strconv.FormatInt(int64(port), 10)),
147 }, WithCollectionName(coll))
148
149 assert.ElementsMatch(t, test.want, attrs)
150 })
151 }
152 }