Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Datadog.Trace.Configuration;
using Datadog.Trace.Logging;
using Datadog.Trace.Tagging;
using Datadog.Trace.Util;

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.DynamoDb
{
Expand All @@ -21,7 +22,7 @@ internal static class AwsDynamoDbCommon
internal const string IntegrationName = nameof(Configuration.IntegrationId.AwsDynamoDb);
internal const IntegrationId IntegrationId = Configuration.IntegrationId.AwsDynamoDb;

public static Scope CreateScope(Tracer tracer, string operation, out AwsDynamoDbTags tags, ISpanContext parentContext = null)
public static Scope CreateScopeBatch(Tracer tracer, string operation, IBatchRequest request, out AwsDynamoDbTags tags, ISpanContext parentContext = null)
{
tags = null;

Expand All @@ -48,6 +49,126 @@ public static Scope CreateScope(Tracer tracer, string operation, out AwsDynamoDb

tags.Service = DynamoDbServiceName;
tags.Operation = operation;
TagBatchRequest(request, tags, scope);
bool isServerless = EnvironmentHelpers.IsAwsLambda();
if (isServerless && tags.AwsRegion != null)
{
tags.PeerService = "dynamodb." + tags.AwsRegion + ".amazonaws.com";
tags.PeerServiceSource = "peer.service";
}
else if (!isServerless)
{
tags.PeerService = tags.TableName;
tags.PeerServiceSource = Trace.Tags.TableName;
}

perTraceSettings.Schema.RemapPeerService(tags);
tags.SetAnalyticsSampleRate(IntegrationId, perTraceSettings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating or populating scope.");
}

// always returns the scope, even if it's null because we couldn't create it,
// or we couldn't populate it completely (some tags is better than no tags)
return scope;
}

public static Scope CreateScopeKeys(Tracer tracer, string operation, IAmazonDynamoDbRequestWithKnownKeys request, out AwsDynamoDbTags tags, ISpanContext parentContext = null)
{
tags = null;

var perTraceSettings = tracer.CurrentTraceSettings;
if (!perTraceSettings.Settings.IsIntegrationEnabled(IntegrationId) || !perTraceSettings.Settings.IsIntegrationEnabled(AwsConstants.IntegrationId))
{
// integration disabled, don't create a scope, skip this trace
return null;
}

Scope scope = null;

try
{
tags = perTraceSettings.Schema.Database.CreateAwsDynamoDbTags();
var serviceName = perTraceSettings.GetServiceName(DatadogAwsDynamoDbServiceName);
scope = tracer.StartActiveInternal(DynamoDbOperationName, parent: parentContext, tags: tags, serviceName: serviceName);
var span = scope.Span;

// This is needed to showcase the DynamoDB action in the
// span details. This will also cause to repeat an HTTP span.
span.Type = SpanTypes.DynamoDb;
span.ResourceName = $"{DynamoDbServiceName}.{operation}";

tags.Service = DynamoDbServiceName;
tags.Operation = operation;
bool isServerless = EnvironmentHelpers.IsAwsLambda();
if (isServerless && tags.AwsRegion != null)
{
tags.PeerService = "dynamodb." + tags.AwsRegion + ".amazonaws.com";
tags.PeerServiceSource = "peer.service";
}
else if (!isServerless)
{
tags.PeerService = request.TableName;
tags.PeerServiceSource = Trace.Tags.TableName;
}

perTraceSettings.Schema.RemapPeerService(tags);
tags.SetAnalyticsSampleRate(IntegrationId, perTraceSettings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating or populating scope.");
}

// always returns the scope, even if it's null because we couldn't create it,
// or we couldn't populate it completely (some tags is better than no tags)
return scope;
}

public static Scope CreateScopeTable(Tracer tracer, string operation, IAmazonDynamoDbRequestWithTableName request, out AwsDynamoDbTags tags, ISpanContext parentContext = null)
{
tags = null;

var perTraceSettings = tracer.CurrentTraceSettings;
if (!perTraceSettings.Settings.IsIntegrationEnabled(IntegrationId) || !perTraceSettings.Settings.IsIntegrationEnabled(AwsConstants.IntegrationId))
{
// integration disabled, don't create a scope, skip this trace
return null;
}

Scope scope = null;

try
{
tags = perTraceSettings.Schema.Database.CreateAwsDynamoDbTags();
var serviceName = perTraceSettings.GetServiceName(DatadogAwsDynamoDbServiceName);
scope = tracer.StartActiveInternal(DynamoDbOperationName, parent: parentContext, tags: tags, serviceName: serviceName);
var span = scope.Span;

// This is needed to showcase the DynamoDB action in the
// span details. This will also cause to repeat an HTTP span.
span.Type = SpanTypes.DynamoDb;
span.ResourceName = $"{DynamoDbServiceName}.{operation}";

tags.Service = DynamoDbServiceName;
tags.Operation = operation;
bool isServerless = EnvironmentHelpers.IsAwsLambda();
if (isServerless && tags.AwsRegion != null)
{
tags.PeerService = "dynamodb." + tags.AwsRegion + ".amazonaws.com";
tags.PeerServiceSource = "peer.service";
}
else if (!isServerless)
{
tags.PeerService = request.TableName;
tags.PeerServiceSource = Trace.Tags.TableName;
}

perTraceSettings.Schema.RemapPeerService(tags);
tags.SetAnalyticsSampleRate(IntegrationId, perTraceSettings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TBatchGetItemRequest>(TTa
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagBatchRequest(request, tags, scope);
var scope = AwsDynamoDbCommon.CreateScopeBatch(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);

return new CallTargetState(scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TBatchGetItemRequest>(TTa
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagBatchRequest(request, tags, scope);
var scope = AwsDynamoDbCommon.CreateScopeBatch(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);

return new CallTargetState(scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TBatchWriteItemRequest>(T
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagBatchRequest(request, tags, scope);
var scope = AwsDynamoDbCommon.CreateScopeBatch(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);

return new CallTargetState(scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TBatchWriteItemRequest>(T
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagBatchRequest(request, tags, scope);
var scope = AwsDynamoDbCommon.CreateScopeBatch(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);

return new CallTargetState(scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TDeleteItemRequest>(TTarg
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeKeys(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

if (!Tracer.Instance.Settings.SpanPointersEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TDeleteItemRequest>(TTarg
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeKeys(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

if (!Tracer.Instance.Settings.SpanPointersEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TGetItemRequest>(TTarget
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TGetItemRequest>(TTarget
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TPutItemRequest>(TTarget
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TPutItemRequest>(TTarget
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TScanRequest>(TTarget ins
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TScanRequest>(TTarget ins
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeTable(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

return new CallTargetState(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TUpdateItemRequest>(TTarg
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeKeys(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

if (!Tracer.Instance.Settings.SpanPointersEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TUpdateItemRequest>(TTarg
return CallTargetState.GetDefault();
}

var scope = AwsDynamoDbCommon.CreateScope(Tracer.Instance, Operation, out AwsDynamoDbTags tags);
var scope = AwsDynamoDbCommon.CreateScopeKeys(Tracer.Instance, Operation, request, out AwsDynamoDbTags tags);
AwsDynamoDbCommon.TagTableNameAndResourceName(request.TableName, tags, scope);

if (!Tracer.Instance.Settings.SpanPointersEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Datadog.Trace.DuckTyping;
using Datadog.Trace.Logging;
using Datadog.Trace.Tagging;
using Datadog.Trace.Util;

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.EventBridge
{
Expand All @@ -25,7 +26,7 @@ internal static class AwsEventBridgeCommon
internal const string IntegrationName = nameof(IntegrationId.AwsEventBridge);
private const IntegrationId IntegrationId = Configuration.IntegrationId.AwsEventBridge;

public static Scope? CreateScope(Tracer tracer, string operation, string spanKind, out AwsEventBridgeTags? tags, ISpanContext? parentContext = null)
public static Scope? CreateScope(Tracer tracer, string operation, string spanKind, IPutEventsRequest request, out AwsEventBridgeTags? tags, ISpanContext? parentContext = null)
{
tags = null;

Expand All @@ -51,6 +52,28 @@ internal static class AwsEventBridgeCommon

tags.Service = EventBridgeServiceName;
tags.Operation = operation;
var busName = GetBusName(request.Entries.Value);
if (busName is not null)
{
// We use RuleName to stay consistent with other runtimes
// TODO rename rulename tag to busname across all runtimes
tags.RuleName = busName;
}

bool isOutbound = (spanKind == SpanKinds.Client) || (spanKind == SpanKinds.Producer);
bool isServerless = EnvironmentHelpers.IsAwsLambda();
if (isServerless && isOutbound && tags.AwsRegion != null)
{
tags.PeerService = "events." + tags.AwsRegion + ".amazonaws.com";
tags.PeerServiceSource = "peer.service";
}
else if (!isServerless && isOutbound)
{
tags.PeerService = tags.RuleName;
tags.PeerServiceSource = Trace.Tags.RuleName;
}

perTraceSettings.Schema.RemapPeerService(tags);
tags.SetAnalyticsSampleRate(IntegrationId, perTraceSettings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TPutEventsRequest>(TTarge
}

var tracer = Tracer.Instance;
var scope = AwsEventBridgeCommon.CreateScope(tracer, Operation, SpanKind, out var tags);
if (tags is not null)
{
var busName = AwsEventBridgeCommon.GetBusName(request.Entries.Value);
if (busName is not null)
{
// We use RuleName to stay consistent with other runtimes
// TODO rename rulename tag to busname across all runtimes
tags.RuleName = busName;
}
}
var scope = AwsEventBridgeCommon.CreateScope(tracer, Operation, SpanKind, request, out var tags);

var context = new PropagationContext(scope?.Span.Context, Baggage.Current);
ContextPropagation.InjectContext(tracer, request, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,7 @@ internal static CallTargetState OnMethodBegin<TTarget, TPutEventsRequest>(TTarge
}

var tracer = Tracer.Instance;
var scope = AwsEventBridgeCommon.CreateScope(tracer, Operation, SpanKind, out var tags);
if (tags is not null)
{
var busName = AwsEventBridgeCommon.GetBusName(request.Entries.Value);
if (busName is not null)
{
// We use RuleName to stay consistent with other runtimes
// TODO rename rulename tag to busname across all runtimes
tags.RuleName = busName;
}
}
var scope = AwsEventBridgeCommon.CreateScope(tracer, Operation, SpanKind, request, out var tags);

var context = new PropagationContext(scope?.Span.Context, Baggage.Current);
ContextPropagation.InjectContext(tracer, request, context);
Expand Down
Loading
Loading