Skip to content

Commit d05eb4c

Browse files
committed
Add unit tests
1 parent 5469c4f commit d05eb4c

File tree

5 files changed

+316
-0
lines changed

5 files changed

+316
-0
lines changed

tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<assembly fullname="amqmdnetstd" />
55
<assembly fullname="AWSSDK.Core" />
66
<assembly fullname="AWSSDK.DynamoDBv2" />
7+
<assembly fullname="AWSSDK.EventBridge" />
78
<assembly fullname="AWSSDK.Kinesis" />
89
<assembly fullname="AWSSDK.SimpleNotificationService" />
910
<assembly fullname="AWSSDK.SQS" />
@@ -214,6 +215,7 @@
214215
<assembly fullname="System.Data.SqlClient" />
215216
<assembly fullname="System.Data.SQLite" />
216217
<assembly fullname="System.Diagnostics.Debug">
218+
<type fullname="System.Diagnostics.Debug" />
217219
<type fullname="System.Diagnostics.Debugger" />
218220
<type fullname="System.Diagnostics.DebuggerBrowsableAttribute" />
219221
<type fullname="System.Diagnostics.DebuggerBrowsableState" />
@@ -546,6 +548,8 @@
546548
<type fullname="System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute" />
547549
<type fullname="System.Diagnostics.CodeAnalysis.NotNullWhenAttribute" />
548550
<type fullname="System.Diagnostics.ConditionalAttribute" />
551+
<type fullname="System.Diagnostics.Debug" />
552+
<type fullname="System.Diagnostics.Debug/AssertInterpolatedStringHandler" />
549553
<type fullname="System.Diagnostics.DebuggableAttribute" />
550554
<type fullname="System.Diagnostics.DebuggableAttribute/DebuggingModes" />
551555
<type fullname="System.Diagnostics.Debugger" />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <copyright file="AwsEventBridgeCommonTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
using System.Collections.Generic;
7+
using System.Collections.Specialized;
8+
using Amazon.EventBridge.Model;
9+
using Datadog.Trace.Agent;
10+
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.EventBridge;
11+
using Datadog.Trace.Configuration;
12+
using Datadog.Trace.Sampling;
13+
using FluentAssertions;
14+
using Moq;
15+
using Xunit;
16+
17+
namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.AWS.EventBridge;
18+
19+
public class AwsEventBridgeCommonTests
20+
{
21+
[Fact]
22+
public void GetCorrectBusName()
23+
{
24+
var entries = new List<PutEventsRequestEntry>
25+
{
26+
new() { EventBusName = "test-bus-1" },
27+
new() { EventBusName = "test-bus-2" },
28+
new() { EventBusName = string.Empty },
29+
new() { EventBusName = null }
30+
};
31+
32+
var result = AwsEventBridgeCommon.GetBusName(entries);
33+
result.Should().Be("test-bus-1");
34+
35+
AwsEventBridgeCommon.GetBusName(null).Should().BeNull();
36+
AwsEventBridgeCommon.GetBusName(new List<PutEventsRequestEntry>()).Should().BeNull();
37+
38+
var emptyEntries = new List<PutEventsRequestEntry>
39+
{
40+
new() { EventBusName = string.Empty },
41+
new() { EventBusName = null }
42+
};
43+
AwsEventBridgeCommon.GetBusName(emptyEntries).Should().BeNull();
44+
}
45+
46+
[Fact]
47+
public void GetCorrectOperationName()
48+
{
49+
var tracerV0 = GetTracer("v0");
50+
AwsEventBridgeCommon.GetOperationName(tracerV0).Should().Be("eventbridge.request");
51+
52+
var tracerV1 = GetTracer("v1");
53+
AwsEventBridgeCommon.GetOperationName(tracerV1).Should().Be("aws.eventbridge.send");
54+
}
55+
56+
private static Tracer GetTracer(string schemaVersion)
57+
{
58+
var collection = new NameValueCollection { { ConfigurationKeys.MetadataSchemaVersion, schemaVersion } };
59+
IConfigurationSource source = new NameValueConfigurationSource(collection);
60+
var settings = new TracerSettings(source);
61+
var writerMock = new Mock<IAgentWriter>();
62+
var samplerMock = new Mock<ITraceSampler>();
63+
64+
return new Tracer(settings, writerMock.Object, samplerMock.Object, scopeManager: null, statsd: null);
65+
}
66+
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// <copyright file="ContextPropagationTests.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using System.Collections;
9+
using System.Collections.Generic;
10+
using Amazon.EventBridge.Model;
11+
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.EventBridge;
12+
using Datadog.Trace.DuckTyping;
13+
using Datadog.Trace.Vendors.Newtonsoft.Json;
14+
using FluentAssertions;
15+
using Xunit;
16+
17+
namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.AWS.EventBridge;
18+
19+
public class ContextPropagationTests
20+
{
21+
private const string DatadogKey = "_datadog";
22+
private const string StartTimeKey = "x-datadog-start-time";
23+
private const string ResourceNameKey = "x-datadog-resource-name";
24+
private const string EventBusName = "test-event-bus";
25+
26+
private readonly SpanContext _spanContext;
27+
28+
public ContextPropagationTests()
29+
{
30+
const long upper = 1234567890123456789;
31+
const ulong lower = 9876543210987654321;
32+
33+
var traceId = new TraceId(upper, lower);
34+
const ulong spanId = 6766950223540265769;
35+
_spanContext = new SpanContext(traceId, spanId, 1, "test-eventbridge", "serverless");
36+
}
37+
38+
[Fact]
39+
public void InjectTracingContext_EmptyDetail_AddsTraceContext()
40+
{
41+
var request = GeneratePutEventsRequest([
42+
new PutEventsRequestEntry { Detail = "{}", EventBusName = EventBusName }
43+
]);
44+
45+
var proxy = request.DuckCast<IPutEventsRequest>();
46+
47+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
48+
49+
var entries = (IList)proxy.Entries.Value!;
50+
entries.Count.Should().Be(1);
51+
var entry = (PutEventsRequestEntry)entries[0]!;
52+
53+
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail);
54+
detail.Should().NotBeNull();
55+
detail!.Count.Should().Be(1);
56+
57+
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject);
58+
extracted.Should().BeTrue();
59+
datadogObject.Should().NotBeNull();
60+
61+
var jsonString = JsonConvert.SerializeObject(datadogObject);
62+
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
63+
64+
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString());
65+
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString());
66+
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName);
67+
extractedTraceContext.Should().ContainKey(StartTimeKey);
68+
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty();
69+
}
70+
71+
[Fact]
72+
public void InjectTracingContext_ExistingDetail_AddsTraceContext()
73+
{
74+
var request = GeneratePutEventsRequest([
75+
new PutEventsRequestEntry { Detail = "{\"foo\":\"bar\"}", EventBusName = EventBusName }
76+
]);
77+
78+
var proxy = request.DuckCast<IPutEventsRequest>();
79+
80+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
81+
82+
var entries = (IList)proxy.Entries.Value!;
83+
entries.Count.Should().Be(1);
84+
var entry = (PutEventsRequestEntry)entries[0]!;
85+
86+
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail);
87+
detail.Should().NotBeNull();
88+
detail!.Count.Should().Be(2);
89+
detail["foo"].Should().Be("bar");
90+
91+
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject);
92+
extracted.Should().BeTrue();
93+
datadogObject.Should().NotBeNull();
94+
95+
var jsonString = JsonConvert.SerializeObject(datadogObject);
96+
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
97+
98+
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString());
99+
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString());
100+
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName);
101+
extractedTraceContext.Should().ContainKey(StartTimeKey);
102+
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty();
103+
}
104+
105+
[Fact]
106+
public void InjectTracingContext_NullDetail_AddsTraceContext()
107+
{
108+
var request = GeneratePutEventsRequest([
109+
new PutEventsRequestEntry { Detail = null, EventBusName = EventBusName }
110+
]);
111+
112+
var proxy = request.DuckCast<IPutEventsRequest>();
113+
114+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
115+
116+
var entries = (IList)proxy.Entries.Value!;
117+
entries.Count.Should().Be(1);
118+
var entry = (PutEventsRequestEntry)entries[0]!;
119+
120+
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail);
121+
detail.Should().NotBeNull();
122+
detail!.Count.Should().Be(1);
123+
124+
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject);
125+
extracted.Should().BeTrue();
126+
datadogObject.Should().NotBeNull();
127+
128+
var jsonString = JsonConvert.SerializeObject(datadogObject);
129+
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
130+
131+
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString());
132+
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString());
133+
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName);
134+
extractedTraceContext.Should().ContainKey(StartTimeKey);
135+
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty();
136+
}
137+
138+
[Fact]
139+
public void InjectTracingContext_InvalidDetail_DoesNotAddTraceContext()
140+
{
141+
var request = GeneratePutEventsRequest([
142+
new PutEventsRequestEntry { Detail = "{invalid json", EventBusName = EventBusName }
143+
]);
144+
145+
var proxy = request.DuckCast<IPutEventsRequest>();
146+
147+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
148+
149+
var entries = (IList)proxy.Entries.Value!;
150+
entries.Count.Should().Be(1);
151+
var entry = (PutEventsRequestEntry)entries[0]!;
152+
153+
entry.Detail.Should().Be("{invalid json");
154+
}
155+
156+
[Fact]
157+
public void InjectTracingContext_MultipleEntries_AddsTraceContextToAll()
158+
{
159+
var request = GeneratePutEventsRequest([
160+
new PutEventsRequestEntry { Detail = "{}", EventBusName = EventBusName },
161+
new PutEventsRequestEntry { Detail = "{\"foo\":\"bar\"}", EventBusName = EventBusName }
162+
]);
163+
164+
var proxy = request.DuckCast<IPutEventsRequest>();
165+
166+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
167+
168+
var entries = (IList)proxy.Entries.Value!;
169+
entries.Count.Should().Be(2);
170+
171+
foreach (var entry in entries)
172+
{
173+
var typedEntry = entry as PutEventsRequestEntry;
174+
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(typedEntry!.Detail);
175+
detail.Should().NotBeNull();
176+
detail!.Should().ContainKey(DatadogKey);
177+
178+
var extracted = detail!.TryGetValue(DatadogKey, out var datadogObject);
179+
extracted.Should().BeTrue();
180+
datadogObject.Should().NotBeNull();
181+
182+
var jsonString = JsonConvert.SerializeObject(datadogObject);
183+
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
184+
185+
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString());
186+
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString());
187+
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName);
188+
extractedTraceContext.Should().ContainKey(StartTimeKey);
189+
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty();
190+
}
191+
}
192+
193+
[Fact]
194+
public void InjectTracingContext_NullEventBusName_OmitsResourceName()
195+
{
196+
var request = GeneratePutEventsRequest([
197+
new PutEventsRequestEntry { Detail = "{}", EventBusName = null }
198+
]);
199+
200+
var proxy = request.DuckCast<IPutEventsRequest>();
201+
202+
ContextPropagation.InjectTracingContext(proxy, _spanContext);
203+
204+
var entries = (IList)proxy.Entries.Value!;
205+
entries.Count.Should().Be(1);
206+
var entry = (PutEventsRequestEntry)entries[0]!;
207+
208+
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail);
209+
detail.Should().NotBeNull();
210+
detail!.Count.Should().Be(1);
211+
212+
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject);
213+
extracted.Should().BeTrue();
214+
datadogObject.Should().NotBeNull();
215+
216+
var jsonString = JsonConvert.SerializeObject(datadogObject);
217+
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
218+
219+
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString());
220+
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString());
221+
extractedTraceContext.Should().NotContainKey(ResourceNameKey);
222+
extractedTraceContext.Should().ContainKey(StartTimeKey);
223+
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty();
224+
}
225+
226+
private static PutEventsRequest GeneratePutEventsRequest(List<PutEventsRequestEntry> entries)
227+
{
228+
return new PutEventsRequest { Entries = entries };
229+
}
230+
}

tracer/test/Datadog.Trace.ClrProfiler.Managed.Tests/Datadog.Trace.ClrProfiler.Managed.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.201.14" />
1919
<PackageReference Include="AWSSDK.Kinesis" Version="3.7.200.21" />
2020
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.101.88" />
21+
<PackageReference Include="AWSSDK.EventBridge" Version="3.7.303.30" />
2122
<PackageReference Include="Confluent.Kafka" Version="1.4.3" />
2223
<PackageReference Include="log4net" Version="$(Log4NetVersion)" />
2324
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />

tracer/test/Datadog.Trace.Tests/Configuration/Schema/MessagingSchemaTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,20 @@ public void CreateAwsSnsTagsReturnsCorrectImplementation(object schemaVersionObj
153153
var namingSchema = new NamingSchema(schemaVersion, peerServiceTagsEnabled, removeClientServiceNamesEnabled, DefaultServiceName, _mappings, new Dictionary<string, string>());
154154
namingSchema.Messaging.CreateAwsSnsTags("spanKind").Should().BeOfType(expectedType);
155155
}
156+
157+
[Theory]
158+
[MemberData(nameof(GetAllConfigs))]
159+
public void CreateAwsEventBridgeTagsReturnsCorrectImplementation(object schemaVersionObject, bool peerServiceTagsEnabled, bool removeClientServiceNamesEnabled)
160+
{
161+
var schemaVersion = (SchemaVersion)schemaVersionObject; // Unbox SchemaVersion, which is only defined internally
162+
var expectedType = schemaVersion switch
163+
{
164+
SchemaVersion.V0 when peerServiceTagsEnabled == false => typeof(AwsEventBridgeTags),
165+
_ => typeof(AwsEventBridgeV1Tags),
166+
};
167+
168+
var namingSchema = new NamingSchema(schemaVersion, peerServiceTagsEnabled, removeClientServiceNamesEnabled, DefaultServiceName, _mappings, new Dictionary<string, string>());
169+
namingSchema.Messaging.CreateAwsEventBridgeTags("spanKind").Should().BeOfType(expectedType);
170+
}
156171
}
157172
}

0 commit comments

Comments
 (0)