Skip to content

Commit debb9be

Browse files
authored
support bedrock and bedrock runtime resources Java SDK V2(#5)
This PR add AWS Bedrock and BedrockRuntime support for AWS Java SDK V2 with the following change: A. **Bedrock**: Extract `guardrailId` from API response, and add into `"aws.bedrock.guardrail.id"` span attribute. B. **Bedrock Agent**: Extract `agentId` from both API request and response, and add into `"aws.bedrock.agent.id"` span attribute. Extract `knowledgeBaseId` from API request, and add into `"aws.bedrock.knowledgebase.id"` span attribute. Extract `dataSourceId` from both API request and response,, and add into `"aws.bedrock.datasource.id"` span attribute. The instrumentation is on API operation level, we make sure only one attribute is extracted per API call, there will be no overlap/conflict to identify the resouce. C. **Bedrock Agent Runtime**: Extract `agentId` from API request, and add into `"aws.bedrock.agent.id"` span attribute. Extract `knowledgeBaseId` from API request, and add into `"aws.bedrock.knowledgebase.id"` span attribute. D. **Bedrock Runtime**: Extract the following attributes and add into span according to [ Gen AI semantic-conventions](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/gen-ai.md): ``` gen_ai.request.model gen_ai.system ```
1 parent 359d60f commit debb9be

File tree

5 files changed

+120
-4
lines changed

5 files changed

+120
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.awssdk.v2_2;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
10+
import io.opentelemetry.api.common.AttributeKey;
11+
12+
final class AwsExperimentalAttributes {
13+
static final AttributeKey<String> AWS_BUCKET_NAME = stringKey("aws.bucket.name");
14+
static final AttributeKey<String> AWS_QUEUE_URL = stringKey("aws.queue.url");
15+
static final AttributeKey<String> AWS_QUEUE_NAME = stringKey("aws.queue.name");
16+
static final AttributeKey<String> AWS_STREAM_NAME = stringKey("aws.stream.name");
17+
static final AttributeKey<String> AWS_TABLE_NAME = stringKey("aws.table.name");
18+
static final AttributeKey<String> AWS_BEDROCK_GUARDRAIL_ID =
19+
stringKey("aws.bedrock.guardrail.id");
20+
static final AttributeKey<String> AWS_BEDROCK_AGENT_ID = stringKey("aws.bedrock.agent.id");
21+
static final AttributeKey<String> AWS_BEDROCK_DATASOURCE_ID =
22+
stringKey("aws.bedrock.data_source.id");
23+
static final AttributeKey<String> AWS_BEDROCK_KNOWLEDGEBASE_ID =
24+
stringKey("aws.bedrock.knowledge_base.id");
25+
26+
// TODO: Merge in gen_ai attributes in opentelemetry-semconv-incubating once upgrade to v1.26.0
27+
static final AttributeKey<String> GEN_AI_MODEL = stringKey("gen_ai.request.model");
28+
static final AttributeKey<String> GEN_AI_SYSTEM = stringKey("gen_ai.system");
29+
30+
private AwsExperimentalAttributes() {}
31+
}

instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkRequest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
package io.opentelemetry.instrumentation.awssdk.v2_2;
77

8+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCK;
9+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCKAGENTOPERATION;
10+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCKDATASOURCEOPERATION;
11+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCKKNOWLEDGEBASEOPERATION;
12+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCKRUNTIME;
813
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.DYNAMODB;
914
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.KINESIS;
1015
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.S3;
@@ -32,6 +37,45 @@ enum AwsSdkRequest {
3237
S3Request(S3, "S3Request"),
3338
SqsRequest(SQS, "SqsRequest"),
3439
KinesisRequest(KINESIS, "KinesisRequest"),
40+
BedrockRequest(BEDROCK, "BedrockRequest"),
41+
BedrockAgentRuntimeRequest(BEDROCKAGENTOPERATION, "BedrockAgentRuntimeRequest"),
42+
BedrockRuntimeRequest(BEDROCKRUNTIME, "BedrockRuntimeRequest"),
43+
// BedrockAgent API based requests. We only support operations that are related to
44+
// Agent/DataSources/KnowledgeBases
45+
// resources and the request/response context contains the resource ID.
46+
BedrockCreateAgentActionGroupRequest(BEDROCKAGENTOPERATION, "CreateAgentActionGroupRequest"),
47+
BedrockCreateAgentAliasRequest(BEDROCKAGENTOPERATION, "CreateAgentAliasRequest"),
48+
BedrockDeleteAgentActionGroupRequest(BEDROCKAGENTOPERATION, "DeleteAgentActionGroupRequest"),
49+
BedrockDeleteAgentAliasRequest(BEDROCKAGENTOPERATION, "DeleteAgentAliasRequest"),
50+
BedrockDeleteAgentVersionRequest(BEDROCKAGENTOPERATION, "DeleteAgentVersionRequest"),
51+
BedrockGetAgentActionGroupRequest(BEDROCKAGENTOPERATION, "GetAgentActionGroupRequest"),
52+
BedrockGetAgentAliasRequest(BEDROCKAGENTOPERATION, "GetAgentAliasRequest"),
53+
BedrockGetAgentRequest(BEDROCKAGENTOPERATION, "GetAgentRequest"),
54+
BedrockGetAgentVersionRequest(BEDROCKAGENTOPERATION, "GetAgentVersionRequest"),
55+
BedrockListAgentActionGroupsRequest(BEDROCKAGENTOPERATION, "ListAgentActionGroupsRequest"),
56+
BedrockListAgentAliasesRequest(BEDROCKAGENTOPERATION, "ListAgentAliasesRequest"),
57+
BedrockListAgentKnowledgeBasesRequest(BEDROCKAGENTOPERATION, "ListAgentKnowledgeBasesRequest"),
58+
BedrocListAgentVersionsRequest(BEDROCKAGENTOPERATION, "ListAgentVersionsRequest"),
59+
BedrockPrepareAgentRequest(BEDROCKAGENTOPERATION, "PrepareAgentRequest"),
60+
BedrockUpdateAgentActionGroupRequest(BEDROCKAGENTOPERATION, "UpdateAgentActionGroupRequest"),
61+
BedrockUpdateAgentAliasRequest(BEDROCKAGENTOPERATION, "UpdateAgentAliasRequest"),
62+
BedrockUpdateAgentRequest(BEDROCKAGENTOPERATION, "UpdateAgentRequest"),
63+
BedrockBedrockAgentRequest(BEDROCKAGENTOPERATION, "BedrockAgentRequest"),
64+
BedrockDeleteDataSourceRequest(BEDROCKDATASOURCEOPERATION, "DeleteDataSourceRequest"),
65+
BedrockGetDataSourceRequest(BEDROCKDATASOURCEOPERATION, "GetDataSourceRequest"),
66+
BedrockUpdateDataSourceRequest(BEDROCKDATASOURCEOPERATION, "UpdateDataSourceRequest"),
67+
BedrocAssociateAgentKnowledgeBaseRequest(
68+
BEDROCKKNOWLEDGEBASEOPERATION, "AssociateAgentKnowledgeBaseRequest"),
69+
BedrockCreateDataSourceRequest(BEDROCKKNOWLEDGEBASEOPERATION, "CreateDataSourceRequest"),
70+
BedrockDeleteKnowledgeBaseRequest(BEDROCKKNOWLEDGEBASEOPERATION, "DeleteKnowledgeBaseRequest"),
71+
BedrockDisassociateAgentKnowledgeBaseRequest(
72+
BEDROCKKNOWLEDGEBASEOPERATION, "DisassociateAgentKnowledgeBaseRequest"),
73+
BedrockGetAgentKnowledgeBaseRequest(
74+
BEDROCKKNOWLEDGEBASEOPERATION, "GetAgentKnowledgeBaseRequest"),
75+
BedrockGetKnowledgeBaseRequest(BEDROCKKNOWLEDGEBASEOPERATION, "GetKnowledgeBaseRequest"),
76+
BedrockListDataSourcesRequest(BEDROCKKNOWLEDGEBASEOPERATION, "ListDataSourcesRequest"),
77+
BedrockUpdateAgentKnowledgeBaseRequest(
78+
BEDROCKKNOWLEDGEBASEOPERATION, "UpdateAgentKnowledgeBaseRequest"),
3579
// specific requests
3680
BatchGetItem(
3781
DYNAMODB,

instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkRequestType.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,39 @@
55

66
package io.opentelemetry.instrumentation.awssdk.v2_2;
77

8+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_BEDROCK_AGENT_ID;
9+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_BEDROCK_DATASOURCE_ID;
10+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_BEDROCK_GUARDRAIL_ID;
11+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_BEDROCK_KNOWLEDGEBASE_ID;
12+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_BUCKET_NAME;
13+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_QUEUE_NAME;
14+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_QUEUE_URL;
15+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_STREAM_NAME;
16+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.AWS_TABLE_NAME;
17+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.GEN_AI_MODEL;
818
import static io.opentelemetry.instrumentation.awssdk.v2_2.FieldMapping.request;
19+
import static io.opentelemetry.instrumentation.awssdk.v2_2.FieldMapping.response;
920

1021
import java.util.Collections;
1122
import java.util.List;
1223
import java.util.Map;
1324

1425
enum AwsSdkRequestType {
15-
S3(request("aws.bucket.name", "Bucket")),
16-
SQS(request("aws.queue.url", "QueueUrl"), request("aws.queue.name", "QueueName")),
17-
KINESIS(request("aws.stream.name", "StreamName")),
18-
DYNAMODB(request("aws.table.name", "TableName"));
26+
S3(request(AWS_BUCKET_NAME.getKey(), "Bucket")),
27+
SQS(request(AWS_QUEUE_URL.getKey(), "QueueUrl"), request(AWS_QUEUE_NAME.getKey(), "QueueName")),
28+
KINESIS(request(AWS_STREAM_NAME.getKey(), "StreamName")),
29+
DYNAMODB(request(AWS_TABLE_NAME.getKey(), "TableName")),
30+
BEDROCK(request(AWS_BEDROCK_GUARDRAIL_ID.getKey(), "guardrailIdentifier")),
31+
BEDROCKAGENTOPERATION(
32+
request(AWS_BEDROCK_AGENT_ID.getKey(), "agentId"),
33+
response(AWS_BEDROCK_AGENT_ID.getKey(), "agentId")),
34+
BEDROCKDATASOURCEOPERATION(
35+
request(AWS_BEDROCK_DATASOURCE_ID.getKey(), "dataSourceId"),
36+
response(AWS_BEDROCK_DATASOURCE_ID.getKey(), "dataSourceId")),
37+
BEDROCKKNOWLEDGEBASEOPERATION(
38+
request(AWS_BEDROCK_KNOWLEDGEBASE_ID.getKey(), "knowledgeBaseId"),
39+
response(AWS_BEDROCK_KNOWLEDGEBASE_ID.getKey(), "knowledgeBaseId")),
40+
BEDROCKRUNTIME(request(GEN_AI_MODEL.getKey(), "modelId"));
1941

2042
// Wrapping in unmodifiableMap
2143
@SuppressWarnings("ImmutableEnumChecker")

instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.instrumentation.awssdk.v2_2;
77

8+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsExperimentalAttributes.GEN_AI_SYSTEM;
9+
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.BEDROCKRUNTIME;
810
import static io.opentelemetry.instrumentation.awssdk.v2_2.AwsSdkRequestType.DYNAMODB;
911

1012
import io.opentelemetry.api.common.AttributeKey;
@@ -42,6 +44,7 @@
4244

4345
/** AWS request execution interceptor. */
4446
final class TracingExecutionInterceptor implements ExecutionInterceptor {
47+
private static final String GEN_AI_SYSTEM_BEDROCK = "aws_bedrock";
4548

4649
// the class name is part of the attribute name, so that it will be shaded when used in javaagent
4750
// instrumentation, and won't conflict with usage outside javaagent instrumentation
@@ -317,6 +320,10 @@ private void populateRequestAttributes(
317320
span.setAttribute(SemanticAttributes.DB_OPERATION, operation);
318321
}
319322
}
323+
324+
if (awsSdkRequest.type() == BEDROCKRUNTIME) {
325+
span.setAttribute(GEN_AI_SYSTEM, GEN_AI_SYSTEM_BEDROCK);
326+
}
320327
}
321328

322329
@Override

instrumentation/aws-sdk/aws-sdk-2.2/testing/src/main/groovy/io/opentelemetry/instrumentation/awssdk/v2_2/AbstractAws2ClientTest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,19 @@ abstract class AbstractAws2ClientTest extends AbstractAws2ClientCoreTest {
120120
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
121121
} else if (service == "Kinesis") {
122122
"aws.stream.name" "somestream"
123+
} else if (service == "Bedrock" && operation == "GetGuardrail") {
124+
"aws.bedrock.guardrail.id" "guardrailId"
125+
} else if (service == "BedrockAgent" && operation == "GetAgent") {
126+
"aws.bedrock.agent.id" "agentId"
127+
} else if (service == "BedrockAgent" && operation == "GetKnowledgeBase") {
128+
"aws.bedrock.knowledge_base.id" "knowledgeBaseId"
129+
} else if (service == "BedrockAgent" && operation == "GetDataSource") {
130+
"aws.bedrock.data_source.id" "datasourceId"
131+
} else if (service == "BedrockRuntime" && operation == "InvokeModel") {
132+
"gen_ai.request.model" "meta.llama2-13b-chat-v1"
133+
"gen_ai.system" "aws_bedrock"
123134
}
135+
124136
}
125137
}
126138
}

0 commit comments

Comments
 (0)