-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...lrProfiler.Managed.Tests/AutoInstrumentation/AWS/EventBridge/AwsEventBridgeCommonTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// <copyright file="AwsEventBridgeCommonTests.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using Amazon.EventBridge.Model; | ||
using Datadog.Trace.Agent; | ||
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.EventBridge; | ||
using Datadog.Trace.Configuration; | ||
using Datadog.Trace.Sampling; | ||
using FluentAssertions; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.AWS.EventBridge; | ||
|
||
public class AwsEventBridgeCommonTests | ||
{ | ||
[Fact] | ||
public void GetCorrectBusName() | ||
{ | ||
var entries = new List<PutEventsRequestEntry> | ||
{ | ||
new() { EventBusName = "test-bus-1" }, | ||
new() { EventBusName = "test-bus-2" }, | ||
new() { EventBusName = string.Empty }, | ||
new() { EventBusName = null } | ||
}; | ||
|
||
var result = AwsEventBridgeCommon.GetBusName(entries); | ||
result.Should().Be("test-bus-1"); | ||
|
||
AwsEventBridgeCommon.GetBusName(null).Should().BeNull(); | ||
AwsEventBridgeCommon.GetBusName(new List<PutEventsRequestEntry>()).Should().BeNull(); | ||
|
||
var emptyEntries = new List<PutEventsRequestEntry> | ||
{ | ||
new() { EventBusName = string.Empty }, | ||
new() { EventBusName = null } | ||
}; | ||
AwsEventBridgeCommon.GetBusName(emptyEntries).Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void GetCorrectOperationName() | ||
{ | ||
var tracerV0 = GetTracer("v0"); | ||
AwsEventBridgeCommon.GetOperationName(tracerV0).Should().Be("eventbridge.request"); | ||
|
||
var tracerV1 = GetTracer("v1"); | ||
AwsEventBridgeCommon.GetOperationName(tracerV1).Should().Be("aws.eventbridge.send"); | ||
} | ||
|
||
private static Tracer GetTracer(string schemaVersion) | ||
{ | ||
var collection = new NameValueCollection { { ConfigurationKeys.MetadataSchemaVersion, schemaVersion } }; | ||
IConfigurationSource source = new NameValueConfigurationSource(collection); | ||
var settings = new TracerSettings(source); | ||
var writerMock = new Mock<IAgentWriter>(); | ||
var samplerMock = new Mock<ITraceSampler>(); | ||
|
||
return new Tracer(settings, writerMock.Object, samplerMock.Object, scopeManager: null, statsd: null); | ||
} | ||
} |
230 changes: 230 additions & 0 deletions
230
....ClrProfiler.Managed.Tests/AutoInstrumentation/AWS/EventBridge/ContextPropagationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// <copyright file="ContextPropagationTests.cs" company="Datadog"> | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
// </copyright> | ||
|
||
#nullable enable | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Amazon.EventBridge.Model; | ||
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.EventBridge; | ||
using Datadog.Trace.DuckTyping; | ||
using Datadog.Trace.Vendors.Newtonsoft.Json; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.AWS.EventBridge; | ||
|
||
public class ContextPropagationTests | ||
{ | ||
private const string DatadogKey = "_datadog"; | ||
private const string StartTimeKey = "x-datadog-start-time"; | ||
private const string ResourceNameKey = "x-datadog-resource-name"; | ||
private const string EventBusName = "test-event-bus"; | ||
|
||
private readonly SpanContext _spanContext; | ||
|
||
public ContextPropagationTests() | ||
{ | ||
const long upper = 1234567890123456789; | ||
const ulong lower = 9876543210987654321; | ||
|
||
var traceId = new TraceId(upper, lower); | ||
const ulong spanId = 6766950223540265769; | ||
_spanContext = new SpanContext(traceId, spanId, 1, "test-eventbridge", "serverless"); | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_EmptyDetail_AddsTraceContext() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = "{}", EventBusName = EventBusName } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(1); | ||
var entry = (PutEventsRequestEntry)entries[0]!; | ||
|
||
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail); | ||
detail.Should().NotBeNull(); | ||
detail!.Count.Should().Be(1); | ||
|
||
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject); | ||
extracted.Should().BeTrue(); | ||
datadogObject.Should().NotBeNull(); | ||
|
||
var jsonString = JsonConvert.SerializeObject(datadogObject); | ||
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString); | ||
|
||
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString()); | ||
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString()); | ||
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName); | ||
extractedTraceContext.Should().ContainKey(StartTimeKey); | ||
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_ExistingDetail_AddsTraceContext() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = "{\"foo\":\"bar\"}", EventBusName = EventBusName } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(1); | ||
var entry = (PutEventsRequestEntry)entries[0]!; | ||
|
||
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail); | ||
detail.Should().NotBeNull(); | ||
detail!.Count.Should().Be(2); | ||
detail["foo"].Should().Be("bar"); | ||
|
||
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject); | ||
extracted.Should().BeTrue(); | ||
datadogObject.Should().NotBeNull(); | ||
|
||
var jsonString = JsonConvert.SerializeObject(datadogObject); | ||
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString); | ||
|
||
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString()); | ||
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString()); | ||
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName); | ||
extractedTraceContext.Should().ContainKey(StartTimeKey); | ||
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_NullDetail_AddsTraceContext() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = null, EventBusName = EventBusName } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(1); | ||
var entry = (PutEventsRequestEntry)entries[0]!; | ||
|
||
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail); | ||
detail.Should().NotBeNull(); | ||
detail!.Count.Should().Be(1); | ||
|
||
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject); | ||
extracted.Should().BeTrue(); | ||
datadogObject.Should().NotBeNull(); | ||
|
||
var jsonString = JsonConvert.SerializeObject(datadogObject); | ||
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString); | ||
|
||
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString()); | ||
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString()); | ||
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName); | ||
extractedTraceContext.Should().ContainKey(StartTimeKey); | ||
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_InvalidDetail_DoesNotAddTraceContext() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = "{invalid json", EventBusName = EventBusName } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(1); | ||
var entry = (PutEventsRequestEntry)entries[0]!; | ||
|
||
entry.Detail.Should().Be("{invalid json"); | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_MultipleEntries_AddsTraceContextToAll() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = "{}", EventBusName = EventBusName }, | ||
new PutEventsRequestEntry { Detail = "{\"foo\":\"bar\"}", EventBusName = EventBusName } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(2); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
var typedEntry = entry as PutEventsRequestEntry; | ||
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(typedEntry!.Detail); | ||
detail.Should().NotBeNull(); | ||
detail!.Should().ContainKey(DatadogKey); | ||
|
||
var extracted = detail!.TryGetValue(DatadogKey, out var datadogObject); | ||
extracted.Should().BeTrue(); | ||
datadogObject.Should().NotBeNull(); | ||
|
||
var jsonString = JsonConvert.SerializeObject(datadogObject); | ||
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString); | ||
|
||
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString()); | ||
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString()); | ||
extractedTraceContext[ResourceNameKey].Should().Be(EventBusName); | ||
extractedTraceContext.Should().ContainKey(StartTimeKey); | ||
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void InjectTracingContext_NullEventBusName_OmitsResourceName() | ||
{ | ||
var request = GeneratePutEventsRequest([ | ||
new PutEventsRequestEntry { Detail = "{}", EventBusName = null } | ||
]); | ||
|
||
var proxy = request.DuckCast<IPutEventsRequest>(); | ||
|
||
ContextPropagation.InjectTracingContext(proxy, _spanContext); | ||
|
||
var entries = (IList)proxy.Entries.Value!; | ||
entries.Count.Should().Be(1); | ||
var entry = (PutEventsRequestEntry)entries[0]!; | ||
|
||
var detail = JsonConvert.DeserializeObject<Dictionary<string, object>>(entry.Detail); | ||
detail.Should().NotBeNull(); | ||
detail!.Count.Should().Be(1); | ||
|
||
var extracted = detail.TryGetValue(DatadogKey, out var datadogObject); | ||
extracted.Should().BeTrue(); | ||
datadogObject.Should().NotBeNull(); | ||
|
||
var jsonString = JsonConvert.SerializeObject(datadogObject); | ||
var extractedTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString); | ||
|
||
extractedTraceContext!["x-datadog-parent-id"].Should().Be(_spanContext.SpanId.ToString()); | ||
extractedTraceContext["x-datadog-trace-id"].Should().Be(_spanContext.TraceId.ToString()); | ||
extractedTraceContext.Should().NotContainKey(ResourceNameKey); | ||
extractedTraceContext.Should().ContainKey(StartTimeKey); | ||
extractedTraceContext[StartTimeKey].Should().NotBeNullOrEmpty(); | ||
} | ||
|
||
private static PutEventsRequest GeneratePutEventsRequest(List<PutEventsRequestEntry> entries) | ||
{ | ||
return new PutEventsRequest { Entries = entries }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters