| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"go.mongodb.org/mongo-driver/bson" |
| 13 |
"go.mongodb.org/mongo-driver/mongo" |
| 14 |
"go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 15 |
"go.mongodb.org/mongo-driver/mongo/options" |
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/codes" |
| 18 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 19 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 20 |
"go.opentelemetry.io/otel/trace" |
| 21 |
|
| 22 |
"go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo" |
| 23 |
) |
| 24 |
|
| 25 |
type validator func(sdktrace.ReadOnlySpan) bool |
| 26 |
|
| 27 |
func TestDBCrudOperation(t *testing.T) { |
| 28 |
commonValidators := []validator{ |
| 29 |
func(s sdktrace.ReadOnlySpan) bool { |
| 30 |
return assert.Equal(t, "test-collection.insert", s.Name(), "expected %s", s.Name()) |
| 31 |
}, |
| 32 |
func(s sdktrace.ReadOnlySpan) bool { |
| 33 |
return assert.Contains(t, s.Attributes(), attribute.String("db.operation.name", "insert")) |
| 34 |
}, |
| 35 |
func(s sdktrace.ReadOnlySpan) bool { |
| 36 |
return assert.Contains(t, s.Attributes(), attribute.String("db.collection.name", "test-collection")) |
| 37 |
}, |
| 38 |
func(s sdktrace.ReadOnlySpan) bool { |
| 39 |
return assert.Equal(t, codes.Unset, s.Status().Code) |
| 40 |
}, |
| 41 |
} |
| 42 |
|
| 43 |
tt := []struct { |
| 44 |
title string |
| 45 |
operation func(context.Context, *mongo.Database) (any, error) |
| 46 |
mockResponses []bson.D |
| 47 |
excludeCommand bool |
| 48 |
validators []validator |
| 49 |
}{ |
| 50 |
{ |
| 51 |
title: "insert", |
| 52 |
operation: func(ctx context.Context, db *mongo.Database) (any, error) { |
| 53 |
return db.Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}}) |
| 54 |
}, |
| 55 |
mockResponses: []bson.D{{{Key: "ok", Value: 1}}}, |
| 56 |
excludeCommand: false, |
| 57 |
validators: append(commonValidators, func(s sdktrace.ReadOnlySpan) bool { |
| 58 |
for _, attr := range s.Attributes() { |
| 59 |
if attr.Key == "db.query.text" { |
| 60 |
return assert.Contains(t, attr.Value.AsString(), `"test-item":"test-value"`) |
| 61 |
} |
| 62 |
} |
| 63 |
return false |
| 64 |
}), |
| 65 |
}, |
| 66 |
{ |
| 67 |
title: "insert", |
| 68 |
operation: func(ctx context.Context, db *mongo.Database) (any, error) { |
| 69 |
return db.Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}}) |
| 70 |
}, |
| 71 |
mockResponses: []bson.D{{{Key: "ok", Value: 1}}}, |
| 72 |
excludeCommand: true, |
| 73 |
validators: append(commonValidators, func(s sdktrace.ReadOnlySpan) bool { |
| 74 |
for _, attr := range s.Attributes() { |
| 75 |
if attr.Key == "db.query.text" { |
| 76 |
return false |
| 77 |
} |
| 78 |
} |
| 79 |
return true |
| 80 |
}), |
| 81 |
}, |
| 82 |
} |
| 83 |
for _, tc := range tt { |
| 84 |
title := tc.title |
| 85 |
if tc.excludeCommand { |
| 86 |
title += "/excludeCommand" |
| 87 |
} else { |
| 88 |
title += "/includeCommand" |
| 89 |
} |
| 90 |
|
| 91 |
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) |
| 92 |
mt.Run(title, func(mt *mtest.T) { |
| 93 |
sr := tracetest.NewSpanRecorder() |
| 94 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 95 |
|
| 96 |
ctx, cancel := context.WithTimeout(t.Context(), time.Second*3) |
| 97 |
defer cancel() |
| 98 |
|
| 99 |
ctx, span := provider.Tracer("test").Start(ctx, "mongodb-test") |
| 100 |
|
| 101 |
addr := "mongodb://localhost:27017/?connect=direct" |
| 102 |
opts := options.Client() |
| 103 |
opts.Monitor = otelmongo.NewMonitor( |
| 104 |
otelmongo.WithTracerProvider(provider), |
| 105 |
otelmongo.WithCommandAttributeDisabled(tc.excludeCommand), |
| 106 |
) |
| 107 |
opts.ApplyURI(addr) |
| 108 |
|
| 109 |
mt.ResetClient(opts) |
| 110 |
mt.AddMockResponses(tc.mockResponses...) |
| 111 |
|
| 112 |
_, err := tc.operation(ctx, mt.Client.Database("test-database")) |
| 113 |
if err != nil { |
| 114 |
mt.Error(err) |
| 115 |
} |
| 116 |
|
| 117 |
span.End() |
| 118 |
|
| 119 |
spans := sr.Ended() |
| 120 |
if !assert.Len(mt, spans, 2, "expected 2 spans, received %d", len(spans)) { |
| 121 |
mt.FailNow() |
| 122 |
} |
| 123 |
assert.Len(mt, spans, 2) |
| 124 |
assert.Equal(mt, spans[0].SpanContext().TraceID(), spans[1].SpanContext().TraceID()) |
| 125 |
assert.Equal(mt, spans[0].Parent().SpanID(), spans[1].SpanContext().SpanID()) |
| 126 |
assert.Equal(mt, span.SpanContext().SpanID(), spans[1].SpanContext().SpanID()) |
| 127 |
|
| 128 |
s := spans[0] |
| 129 |
assert.Equal(mt, trace.SpanKindClient, s.SpanKind()) |
| 130 |
attrs := s.Attributes() |
| 131 |
assert.Contains(mt, attrs, attribute.String("db.system.name", "mongodb")) |
| 132 |
assert.Contains(mt, attrs, attribute.String("network.peer.address", "<mock_connection>:27017")) |
| 133 |
assert.Contains(mt, attrs, attribute.Int64("network.peer.port", int64(27017))) |
| 134 |
assert.Contains(mt, attrs, attribute.String("network.transport", "tcp")) |
| 135 |
assert.Contains(mt, attrs, attribute.String("db.namespace", "test-database")) |
| 136 |
for _, v := range tc.validators { |
| 137 |
assert.True(mt, v(s)) |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestDBCollectionAttribute(t *testing.T) { |
| 144 |
tt := []struct { |
| 145 |
title string |
| 146 |
operation func(context.Context, *mongo.Database) (any, error) |
| 147 |
mockResponses []bson.D |
| 148 |
validators []validator |
| 149 |
}{ |
| 150 |
{ |
| 151 |
title: "delete", |
| 152 |
operation: func(ctx context.Context, db *mongo.Database) (any, error) { |
| 153 |
return db.Collection("test-collection").DeleteOne(ctx, bson.D{{Key: "test-item"}}) |
| 154 |
}, |
| 155 |
mockResponses: []bson.D{{{Key: "ok", Value: 1}}}, |
| 156 |
validators: []validator{ |
| 157 |
func(s sdktrace.ReadOnlySpan) bool { |
| 158 |
return assert.Equal(t, "test-collection.delete", s.Name()) |
| 159 |
}, |
| 160 |
func(s sdktrace.ReadOnlySpan) bool { |
| 161 |
return assert.Contains(t, s.Attributes(), attribute.String("db.operation.name", "delete")) |
| 162 |
}, |
| 163 |
func(s sdktrace.ReadOnlySpan) bool { |
| 164 |
return assert.Contains(t, s.Attributes(), attribute.String("db.collection.name", "test-collection")) |
| 165 |
}, |
| 166 |
func(s sdktrace.ReadOnlySpan) bool { |
| 167 |
return assert.Equal(t, codes.Unset, s.Status().Code) |
| 168 |
}, |
| 169 |
}, |
| 170 |
}, |
| 171 |
{ |
| 172 |
title: "listCollectionNames", |
| 173 |
operation: func(ctx context.Context, db *mongo.Database) (any, error) { |
| 174 |
return db.ListCollectionNames(ctx, bson.D{}) |
| 175 |
}, |
| 176 |
mockResponses: []bson.D{ |
| 177 |
{ |
| 178 |
{Key: "ok", Value: 1}, |
| 179 |
{Key: "cursor", Value: bson.D{{Key: "firstBatch", Value: bson.A{}}}}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
validators: []validator{ |
| 183 |
func(s sdktrace.ReadOnlySpan) bool { |
| 184 |
return assert.Equal(t, "listCollections", s.Name()) |
| 185 |
}, |
| 186 |
func(s sdktrace.ReadOnlySpan) bool { |
| 187 |
return assert.Contains(t, s.Attributes(), attribute.String("db.operation.name", "listCollections")) |
| 188 |
}, |
| 189 |
func(s sdktrace.ReadOnlySpan) bool { |
| 190 |
return assert.Equal(t, codes.Unset, s.Status().Code) |
| 191 |
}, |
| 192 |
}, |
| 193 |
}, |
| 194 |
} |
| 195 |
for _, tc := range tt { |
| 196 |
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) |
| 197 |
mt.Run(tc.title, func(mt *mtest.T) { |
| 198 |
sr := tracetest.NewSpanRecorder() |
| 199 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 200 |
|
| 201 |
ctx, cancel := context.WithTimeout(t.Context(), time.Second*3) |
| 202 |
defer cancel() |
| 203 |
|
| 204 |
ctx, span := provider.Tracer("test").Start(ctx, "mongodb-test") |
| 205 |
|
| 206 |
addr := "mongodb://localhost:27017/?connect=direct" |
| 207 |
opts := options.Client() |
| 208 |
opts.Monitor = otelmongo.NewMonitor( |
| 209 |
otelmongo.WithTracerProvider(provider), |
| 210 |
otelmongo.WithCommandAttributeDisabled(true), |
| 211 |
) |
| 212 |
opts.ApplyURI(addr) |
| 213 |
|
| 214 |
mt.ResetClient(opts) |
| 215 |
mt.AddMockResponses(tc.mockResponses...) |
| 216 |
|
| 217 |
_, err := tc.operation(ctx, mt.Client.Database("test-database")) |
| 218 |
if err != nil { |
| 219 |
mt.Error(err) |
| 220 |
} |
| 221 |
|
| 222 |
span.End() |
| 223 |
|
| 224 |
spans := sr.Ended() |
| 225 |
if !assert.Len(mt, spans, 2, "expected 2 spans, received %d", len(spans)) { |
| 226 |
mt.FailNow() |
| 227 |
} |
| 228 |
assert.Len(mt, spans, 2) |
| 229 |
assert.Equal(mt, spans[0].SpanContext().TraceID(), spans[1].SpanContext().TraceID()) |
| 230 |
assert.Equal(mt, spans[0].Parent().SpanID(), spans[1].SpanContext().SpanID()) |
| 231 |
assert.Equal(mt, span.SpanContext().SpanID(), spans[1].SpanContext().SpanID()) |
| 232 |
|
| 233 |
s := spans[0] |
| 234 |
assert.Equal(mt, trace.SpanKindClient, s.SpanKind()) |
| 235 |
attrs := s.Attributes() |
| 236 |
assert.Contains(mt, attrs, attribute.String("db.system.name", "mongodb")) |
| 237 |
assert.Contains(mt, attrs, attribute.String("network.peer.address", "<mock_connection>:27017")) |
| 238 |
assert.Contains(mt, attrs, attribute.Int64("network.peer.port", int64(27017))) |
| 239 |
assert.Contains(mt, attrs, attribute.String("network.transport", "tcp")) |
| 240 |
assert.Contains(mt, attrs, attribute.String("db.namespace", "test-database")) |
| 241 |
for _, v := range tc.validators { |
| 242 |
assert.True(mt, v(s)) |
| 243 |
} |
| 244 |
}) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func assertSemconv(mt *mtest.T, attrs []attribute.KeyValue) { |
| 249 |
mt.Helper() |
| 250 |
|
| 251 |
assert.Contains(mt, attrs, attribute.String("db.system.name", "mongodb")) |
| 252 |
assert.Contains(mt, attrs, attribute.String("network.peer.address", "<mock_connection>:27017")) |
| 253 |
assert.Contains(mt, attrs, attribute.Int64("network.peer.port", int64(27017))) |
| 254 |
assert.Contains(mt, attrs, attribute.String("network.transport", "tcp")) |
| 255 |
assert.Contains(mt, attrs, attribute.String("db.namespace", "test-database")) |
| 256 |
} |
| 257 |
|
| 258 |
func TestSemanticConventionOptIn(t *testing.T) { |
| 259 |
tt := []struct { |
| 260 |
name string |
| 261 |
semconvOptIn string |
| 262 |
assert func(*mtest.T, []attribute.KeyValue) |
| 263 |
}{ |
| 264 |
{ |
| 265 |
name: "default", |
| 266 |
semconvOptIn: "", |
| 267 |
assert: assertSemconv, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "database", |
| 271 |
semconvOptIn: "database", |
| 272 |
assert: assertSemconv, |
| 273 |
}, |
| 274 |
} |
| 275 |
for _, tc := range tt { |
| 276 |
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) |
| 277 |
|
| 278 |
mt.Run(tc.name, func(mt *mtest.T) { |
| 279 |
mt.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tc.semconvOptIn) |
| 280 |
|
| 281 |
sr := tracetest.NewSpanRecorder() |
| 282 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 283 |
|
| 284 |
ctx, cancel := context.WithTimeout(t.Context(), time.Second*3) |
| 285 |
defer cancel() |
| 286 |
|
| 287 |
ctx, span := provider.Tracer("test").Start(ctx, "mongodb-test") |
| 288 |
|
| 289 |
addr := "mongodb://localhost:27017/?connect=direct" |
| 290 |
opts := options.Client() |
| 291 |
opts.Monitor = otelmongo.NewMonitor( |
| 292 |
otelmongo.WithTracerProvider(provider), |
| 293 |
otelmongo.WithCommandAttributeDisabled(true), |
| 294 |
) |
| 295 |
opts.ApplyURI(addr) |
| 296 |
|
| 297 |
mt.ResetClient(opts) |
| 298 |
mt.AddMockResponses(bson.D{{Key: "ok", Value: 1}}) |
| 299 |
|
| 300 |
db := mt.Client.Database("test-database") |
| 301 |
|
| 302 |
_, _ = db.Collection("test-collection").InsertOne(ctx, bson.D{{Key: "test-item", Value: "test-value"}}) |
| 303 |
|
| 304 |
span.End() |
| 305 |
|
| 306 |
spans := sr.Ended() |
| 307 |
if !assert.Len(mt, spans, 2, "expected 2 spans, received %d", len(spans)) { |
| 308 |
mt.FailNow() |
| 309 |
} |
| 310 |
assert.Len(mt, spans, 2) |
| 311 |
assert.Equal(mt, spans[0].SpanContext().TraceID(), spans[1].SpanContext().TraceID()) |
| 312 |
assert.Equal(mt, spans[0].Parent().SpanID(), spans[1].SpanContext().SpanID()) |
| 313 |
assert.Equal(mt, span.SpanContext().SpanID(), spans[1].SpanContext().SpanID()) |
| 314 |
|
| 315 |
s := spans[0] |
| 316 |
assert.Equal(mt, trace.SpanKindClient, s.SpanKind()) |
| 317 |
|
| 318 |
tc.assert(mt, s.Attributes()) |
| 319 |
}) |
| 320 |
} |
| 321 |
} |