-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDBTests.cs
97 lines (80 loc) · 4.02 KB
/
MongoDBTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Common.V1;
using OpenTelemetry.Proto.Trace.V1;
using Xunit.Abstractions;
namespace IntegrationTests;
[Collection(MongoDBCollection.Name)]
public class MongoDBTests : TestHelper
{
private const string MongoDBInstrumentationScopeName = "OpenTelemetry.AutoInstrumentation.MongoDB";
private const string MongoDbNamespace = "test-db";
private const string MongoDbCollectionName = "employees";
private const string MongoDbSystem = "mongodb";
private const string DbSystemAttributeName = "db.system";
private const string DbCollectionNameAttributeName = "db.collection.name";
private const string DbNamespaceAttributeName = "db.namespace";
private const string DbOperationNameAttributeName = "db.operation.name";
private const string ServerAddressAttributeName = "server.address";
private const string ServerPortAttributeName = "server.port";
private const string NetworkPeerAddressAttributeName = "network.peer.address";
private const string NetworkPeerPortAttributeName = "network.peer.port";
private readonly MongoDBFixture _mongoDB;
public MongoDBTests(ITestOutputHelper output, MongoDBFixture mongoDB)
: base("MongoDB", output)
{
_mongoDB = mongoDB;
}
[Theory]
[Trait("Category", "EndToEnd")]
[Trait("Containers", "Any")]
[MemberData(nameof(LibraryVersion.MongoDB), MemberType = typeof(LibraryVersion))]
public void SubmitsTraces(string packageVersion)
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
const int spanCount = 3;
for (int i = 0; i < spanCount; i++)
{
collector.Expect(MongoDBInstrumentationScopeName);
}
collector.Expect(MongoDBInstrumentationScopeName, span => ValidateSpan(span));
EnableBytecodeInstrumentation();
RunTestApplication(new()
{
#if NET462
Framework = string.IsNullOrEmpty(packageVersion) || new Version(packageVersion) >= new Version(3, 0, 0) ? "net472" : "net462",
#endif
Arguments = $"--mongo-db {_mongoDB.Port} {MongoDbNamespace} {MongoDbCollectionName}",
PackageVersion = packageVersion
});
collector.AssertExpectations();
}
private bool ValidateSpan(Span span)
{
return span.Kind == Span.Types.SpanKind.Client && ValidateDatabaseAttributes(span.Attributes) && ValidateNetworkAttributes(span.Attributes);
}
private bool ValidateNetworkAttributes(IReadOnlyCollection<KeyValue> spanAttributes)
{
var serverAddress = spanAttributes.Single(kv => kv.Key == ServerAddressAttributeName).Value.StringValue;
var serverPort = spanAttributes.Single(kv => kv.Key == ServerPortAttributeName).Value.IntValue;
var networkPeerAddress = spanAttributes.Single(kv => kv.Key == NetworkPeerAddressAttributeName).Value.StringValue;
var networkPeerPort = spanAttributes.Single(kv => kv.Key == NetworkPeerPortAttributeName).Value.IntValue;
return serverAddress == "localhost" &&
serverPort == _mongoDB.Port &&
networkPeerAddress is "127.0.0.1" or "::1" &&
networkPeerPort == _mongoDB.Port;
}
private bool ValidateDatabaseAttributes(IReadOnlyCollection<KeyValue> spanAttributes)
{
var collectionName = spanAttributes.Single(kv => kv.Key == DbCollectionNameAttributeName).Value.StringValue;
var dbNamespace = spanAttributes.Single(kv => kv.Key == DbNamespaceAttributeName).Value.StringValue;
var dbSystem = spanAttributes.Single(kv => kv.Key == DbSystemAttributeName).Value.StringValue;
var dbOperationName = spanAttributes.Single(kv => kv.Key == DbOperationNameAttributeName).Value.StringValue;
return collectionName == MongoDbCollectionName &&
dbNamespace == MongoDbNamespace &&
dbSystem == MongoDbSystem &&
!string.IsNullOrWhiteSpace(dbOperationName);
}
}