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