-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathAwsStepFunctionsCommon.cs
More file actions
93 lines (79 loc) · 4.17 KB
/
AwsStepFunctionsCommon.cs
File metadata and controls
93 lines (79 loc) · 4.17 KB
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
// <copyright file="AwsStepFunctionsCommon.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;
using System.Diagnostics.CodeAnalysis;
using Datadog.Trace.Configuration;
using Datadog.Trace.Configuration.Schema;
using Datadog.Trace.Logging;
using Datadog.Trace.Tagging;
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.StepFunctions
{
internal static class AwsStepFunctionsCommon
{
internal const string IntegrationName = nameof(Configuration.IntegrationId.AwsStepFunctions);
internal const IntegrationId IntegrationId = Configuration.IntegrationId.AwsStepFunctions;
private const string DatadogAwsStepFunctionsServiceName = "aws-stepfunctions";
private const string StepFunctionsServiceName = "StepFunctions";
private const string StepFunctionsRequestOperationName = "stepfunctions.request";
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(AwsStepFunctionsCommon));
public static Scope? CreateScope(Tracer tracer, string operation, string spanKind, out AwsStepFunctionsTags? 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.Messaging.CreateAwsStepFunctionsTags(spanKind);
var (serviceName, serviceNameSource) = perTraceSettings.GetServiceNameMetadata(DatadogAwsStepFunctionsServiceName);
var operationName = GetOperationName(tracer, spanKind);
scope = tracer.StartActiveInternal(operationName, parent: parentContext, tags: tags, serviceName: serviceName, serviceNameSource: serviceNameSource);
var span = scope.Span;
span.Type = SpanTypes.Http;
span.ResourceName = $"{StepFunctionsServiceName}.{operation}";
tags.Service = StepFunctionsServiceName;
tags.Operation = operation;
tags.SetAnalyticsSampleRate(IntegrationId, perTraceSettings.Settings, enabledWithGlobalSetting: false);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating or populating scope.");
}
return scope;
}
/// <summary>
/// Extracts the state machine name from the state machine ARN. The ARN is expected to be in the format: `arn:aws:states:region:account:stateMachine:name`.
/// </summary>
[return: NotNullIfNotNull(nameof(stateMachineArn))]
public static string? GetStateMachineName(string? stateMachineArn)
{
if (stateMachineArn is null)
{
return stateMachineArn;
}
var lastSeparationIndex = stateMachineArn.LastIndexOf(':') + 1;
return stateMachineArn.Substring(lastSeparationIndex);
}
internal static string GetOperationName(Tracer tracer, string spanKind)
{
if (tracer.CurrentTraceSettings.Schema.Version == SchemaVersion.V0)
{
return StepFunctionsRequestOperationName;
}
return spanKind switch
{
SpanKinds.Consumer => tracer.CurrentTraceSettings.Schema.Messaging.GetInboundOperationName(MessagingSchema.OperationType.AwsStepFunctions),
SpanKinds.Producer => tracer.CurrentTraceSettings.Schema.Messaging.GetOutboundOperationName(MessagingSchema.OperationType.AwsStepFunctions),
_ => "aws.stepfunctions.request"
};
}
}
}