Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AWS SDK Changes to support Bedrock spans and Application Signals #2458

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics.Tracing;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Instrumentation.AWS;

[EventSource(Name = "OpenTelemetry-Instrumentation-AWS")]
internal sealed class AWSInstrumentationEventSource : EventSource
{
public static AWSInstrumentationEventSource Log = new();

private const int EventIdFailedToParseJson = 1;

[NonEvent]
public void JsonParserException(string format, Exception ex)
{
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.FailedToPraseJson(format, ex.ToInvariantString());
}
}

[Event(EventIdFailedToParseJson, Message = "Failed to parse Json in {0}. Error Message: '{1}'", Level = EventLevel.Warning)]
public void FailedToPraseJson(string format, string exception)
{
this.WriteEvent(EventIdFailedToParseJson, format, exception);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,90 +20,43 @@ public AWSServiceHelper(AWSSemanticConventions semanticConventions)
.AddAttributeAWSBedrockDataSourceId("DataSourceId")
.AddAttributeAWSBedrockGuardrailId("GuardrailId")
.AddAttributeAWSBedrockKnowledgeBaseId("KnowledgeBaseId")
.AddAttributeAWSSQSQueueName("QueueName")
.AddAttributeAWSS3BucketName("BucketName")
.AddAttributeAWSKinesisStreamName("StreamName")
.AddAttributeAWSSNSTopicArn("TopicArn")
.AddAttributeAWSSecretsManagerSecretArn("ARN")
.AddAttributeAWSSecretsManagerSecretArn("SecretId")
.AddAttributeAWSStepFunctionsActivityArn("ActivityArn")
.AddAttributeAWSStepFunctionsStateMachineArn("StateMachineArn")
.AddAttributeAWSLambdaResourceMappingId("UUID")
.Build();
}

internal static IReadOnlyDictionary<string, List<string>> ServiceRequestParameterMap { get; } = new Dictionary<string, List<string>>()
{
{ AWSServiceType.DynamoDbService, ["TableName"] },
{ AWSServiceType.SQSService, ["QueueUrl"] },
{ AWSServiceType.SQSService, ["QueueUrl", "QueueName"] },
{ AWSServiceType.BedrockAgentService, ["AgentId", "KnowledgeBaseId", "DataSourceId"] },
{ AWSServiceType.BedrockAgentRuntimeService, ["AgentId", "KnowledgeBaseId"] },
{ AWSServiceType.BedrockRuntimeService, ["ModelId"] },
{ AWSServiceType.S3Service, ["BucketName"] },
{ AWSServiceType.KinesisService, ["StreamName"] },
{ AWSServiceType.LambdaService, ["UUID"] },
{ AWSServiceType.SecretsManagerService, ["SecretId"] },
{ AWSServiceType.SNSService, ["TopicArn"] },
{ AWSServiceType.StepFunctionsService, ["ActivityArn", "StateMachineArn"] },
};

internal static IReadOnlyDictionary<string, List<string>> ServiceResponseParameterMap { get; } = new Dictionary<string, List<string>>()
{
{ AWSServiceType.BedrockService, ["GuardrailId"] },
{ AWSServiceType.BedrockAgentService, ["AgentId", "DataSourceId"] },
{ AWSServiceType.SecretsManagerService, ["ARN"] },
{ AWSServiceType.SQSService, ["QueueUrl"] },
};

// for Bedrock Agent operations, we map each supported operation to one resource: Agent, DataSource, or KnowledgeBase
internal static List<string> BedrockAgentAgentOps { get; } =
[
"CreateAgentActionGroup",
"CreateAgentAlias",
"DeleteAgentActionGroup",
"DeleteAgentAlias",
"DeleteAgent",
"DeleteAgentVersion",
"GetAgentActionGroup",
"GetAgentAlias",
"GetAgent",
"GetAgentVersion",
"ListAgentActionGroups",
"ListAgentAliases",
"ListAgentKnowledgeBases",
"ListAgentVersions",
"PrepareAgent",
"UpdateAgentActionGroup",
"UpdateAgentAlias",
"UpdateAgent"
];

internal static List<string> BedrockAgentKnowledgeBaseOps { get; } =
[
"AssociateAgentKnowledgeBase",
"CreateDataSource",
"DeleteKnowledgeBase",
"DisassociateAgentKnowledgeBase",
"GetAgentKnowledgeBase",
"GetKnowledgeBase",
"ListDataSources",
"UpdateAgentKnowledgeBase"
];

internal static List<string> BedrockAgentDataSourceOps { get; } =
[
"DeleteDataSource",
"GetDataSource",
"UpdateDataSource"
];

internal IDictionary<string, string> ParameterAttributeMap { get; }

internal static IReadOnlyDictionary<string, string> OperationNameToResourceMap()
{
var operationClassMap = new Dictionary<string, string>();

foreach (var op in BedrockAgentKnowledgeBaseOps)
{
operationClassMap[op] = "KnowledgeBaseId";
}

foreach (var op in BedrockAgentDataSourceOps)
{
operationClassMap[op] = "DataSourceId";
}

foreach (var op in BedrockAgentAgentOps)
{
operationClassMap[op] = "AgentId";
}

return operationClassMap;
}

internal static string GetAWSServiceName(IRequestContext requestContext)
=> Utils.RemoveAmazonPrefixFromServiceName(requestContext.ServiceMetaData.ServiceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ internal class AWSServiceType
internal const string BedrockAgentService = "Bedrock Agent";
internal const string BedrockAgentRuntimeService = "Bedrock Agent Runtime";
internal const string BedrockRuntimeService = "Bedrock Runtime";
internal const string S3Service = "S3";
internal const string KinesisService = "Kinesis";
internal const string LambdaService = "Lambda";
internal const string SecretsManagerService = "Secrets Manager";
internal const string StepFunctionsService = "SFN";

internal static bool IsDynamoDbService(string service)
=> DynamoDbService.Equals(service, StringComparison.OrdinalIgnoreCase);
Expand All @@ -33,4 +38,19 @@ internal static bool IsBedrockAgentRuntimeService(string service)

internal static bool IsBedrockRuntimeService(string service)
=> BedrockRuntimeService.Equals(service, StringComparison.OrdinalIgnoreCase);

internal static bool IsS3Service(string service)
=> S3Service.Equals(service, StringComparison.OrdinalIgnoreCase);

internal static bool IsKinesisService(string service)
=> KinesisService.Equals(service, StringComparison.OrdinalIgnoreCase);

internal static bool IsLambdaService(string service)
=> LambdaService.Equals(service, StringComparison.OrdinalIgnoreCase);

internal static bool IsSecretsManagerService(string service)
=> SecretsManagerService.Equals(service, StringComparison.OrdinalIgnoreCase);

internal static bool IsStepFunctionsService(string service)
=> StepFunctionsService.Equals(service, StringComparison.OrdinalIgnoreCase);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using Amazon.Runtime.Telemetry;
using Amazon.Util;
using OpenTelemetry.AWS;
using OpenTelemetry.Context.Propagation;

Expand Down Expand Up @@ -46,6 +47,36 @@ public override async Task<T> InvokeAsync<T>(IExecutionContext executionContext)
return ret;
}

private static string FetchRequestId(IRequestContext requestContext, IResponseContext responseContext)
{
var request_id = string.Empty;
var response = responseContext.Response;
if (response != null)
{
request_id = response.ResponseMetadata.RequestId;
}
else
{
var request_headers = requestContext.Request.Headers;
if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amzn-RequestId", out var req_id))
{
request_id = req_id;
}

if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amz-request-id", out req_id))
{
request_id = req_id;
}

if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amz-id-2", out req_id))
{
request_id = req_id;
}
}

return request_id;
}

private static void AddPropagationDataToRequest(Activity activity, IRequestContext requestContext)
{
var service = requestContext.ServiceMetaData.ServiceId;
Expand Down Expand Up @@ -81,16 +112,6 @@ private void AddResponseSpecificInformation(Activity activity, IExecutionContext
{
try
{
// for bedrock agent, extract attribute from object in response.
if (AWSServiceType.IsBedrockAgentService(service))
{
var operationName = Utils.RemoveSuffix(response.GetType().Name, "Response");
if (AWSServiceHelper.OperationNameToResourceMap()[operationName] == parameter)
{
this.AddBedrockAgentResponseAttribute(activity, response, parameter);
}
}

var property = response.GetType().GetProperty(parameter);
if (property != null)
{
Expand All @@ -107,32 +128,29 @@ private void AddResponseSpecificInformation(Activity activity, IExecutionContext
}
}
}
}

#if NET
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(
"Trimming",
"IL2075",
Justification = "The reflected properties were already used by the AWS SDK's marshallers so the properties could not have been trimmed.")]
#endif
private void AddBedrockAgentResponseAttribute(Activity activity, AmazonWebServiceResponse response, string parameter)
{
var responseObject = response.GetType().GetProperty(Utils.RemoveSuffix(parameter, "Id"));
if (responseObject != null)
// for bedrock runtime, LLM specific attributes are extracted based on the model ID.
if (AWSServiceType.IsBedrockRuntimeService(service))
{
var attributeObject = responseObject.GetValue(response);
if (attributeObject != null)
var model = this.awsSemanticConventions.TagExtractor.GetTagAttributeGenAiModelId(activity);
if (model != null)
{
var property = attributeObject.GetType().GetProperty(parameter);
if (property != null)
var modelString = model.ToString();
if (modelString != null)
{
if (this.awsServiceHelper.ParameterAttributeMap.TryGetValue(parameter, out var attribute))
{
activity.SetTag(attribute, property.GetValue(attributeObject));
}
AWSLlmModelProcessor.ProcessGenAiAttributes(activity, responseContext.Response, modelString, false, this.awsSemanticConventions);
}
}
}

var httpResponse = responseContext.HttpResponse;
if (httpResponse != null)
{
var statusCode = (int)httpResponse.StatusCode;

this.AddStatusCodeToActivity(activity, statusCode);
this.awsSemanticConventions.TagBuilder.SetTagAttributeHttpResponseHeaderContentLength(activity, httpResponse.ContentLength);
}
}

#if NET
Expand All @@ -143,7 +161,7 @@ private void AddBedrockAgentResponseAttribute(Activity activity, AmazonWebServic
#endif
private void AddRequestSpecificInformation(Activity activity, IRequestContext requestContext)
{
var service = requestContext.ServiceMetaData.ServiceId;
var service = AWSServiceHelper.GetAWSServiceName(requestContext);

if (AWSServiceHelper.ServiceRequestParameterMap.TryGetValue(service, out var parameters))
{
Expand All @@ -153,18 +171,37 @@ private void AddRequestSpecificInformation(Activity activity, IRequestContext re
{
try
{
// for bedrock agent, we only extract one attribute based on the operation.
if (AWSServiceType.IsBedrockAgentService(service))
var property = request.GetType().GetProperty(parameter);
if (property != null)
{
if (AWSServiceHelper.OperationNameToResourceMap()[AWSServiceHelper.GetAWSOperationName(requestContext)] != parameter)
// for bedrock runtime, LLM specific attributes are extracted based on the model ID.
if (AWSServiceType.IsBedrockRuntimeService(service) && parameter == "ModelId")
{
continue;
var model = property.GetValue(request);
if (model != null)
{
var modelString = model.ToString();
if (modelString != null)
{
AWSLlmModelProcessor.ProcessGenAiAttributes(activity, request, modelString, true, this.awsSemanticConventions);
}
}
}

// for secrets manager, only extract SecretId from request if it is a secret ARN.
if (AWSServiceType.IsSecretsManagerService(service) && parameter == "SecretId")
{
var secretId = property.GetValue(request);
if (secretId != null)
{
var secretIdString = secretId.ToString();
if (secretIdString != null && !secretIdString.StartsWith("arn:aws:secretsmanager:", StringComparison.Ordinal))
{
continue;
}
}
}
}

var property = request.GetType().GetProperty(parameter);
if (property != null)
{
if (this.awsServiceHelper.ParameterAttributeMap.TryGetValue(parameter, out var attribute))
{
activity.SetTag(attribute, property.GetValue(request));
Expand All @@ -183,10 +220,27 @@ private void AddRequestSpecificInformation(Activity activity, IRequestContext re
{
this.awsSemanticConventions.TagBuilder.SetTagAttributeDbSystemToDynamoDb(activity);
}
else if (AWSServiceType.IsSqsService(service))
{
SqsRequestContextHelper.AddAttributes(
requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current)));
}
else if (AWSServiceType.IsSnsService(service))
{
SnsRequestContextHelper.AddAttributes(
requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current)));
}
else if (AWSServiceType.IsBedrockRuntimeService(service))
{
this.awsSemanticConventions.TagBuilder.SetTagAttributeGenAiSystemToBedrock(activity);
}

var client = requestContext.ClientConfig;
if (client != null)
{
var region = client.RegionEndpoint?.SystemName;
this.awsSemanticConventions.TagBuilder.SetTagAttributeCloudRegion(activity, region ?? AWSSDKUtils.DetermineRegion(client.ServiceURL));
}
}

private void ProcessEndRequest(Activity? activity, IExecutionContext executionContext)
Expand All @@ -196,6 +250,13 @@ private void ProcessEndRequest(Activity? activity, IExecutionContext executionCo
return;
}

var responseContext = executionContext.ResponseContext;
var requestContext = executionContext.RequestContext;
if (this.awsSemanticConventions.TagExtractor.GetTagAttributeAWSRequestId == null)
{
this.awsSemanticConventions.TagBuilder.SetTagAttributeAWSRequestId(activity, FetchRequestId(requestContext, responseContext));
}

this.AddResponseSpecificInformation(activity, executionContext);
}

Expand Down Expand Up @@ -225,4 +286,9 @@ private void ProcessEndRequest(Activity? activity, IExecutionContext executionCo

return currentActivity;
}

private void AddStatusCodeToActivity(Activity activity, int status_code)
{
this.awsSemanticConventions.TagBuilder.SetTagAttributeHttpResponseStatusCode(activity, status_code);
}
}
Loading
Loading