|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; |
| 17 | + |
| 18 | +import static software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AwsExperimentalAttributes.AWS_AGENT_ID; |
| 19 | +import static software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AwsExperimentalAttributes.AWS_DATA_SOURCE_ID; |
| 20 | +import static software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AwsExperimentalAttributes.AWS_KNOWLEDGE_BASE_ID; |
| 21 | + |
| 22 | +import io.opentelemetry.api.common.AttributeKey; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.function.Function; |
| 28 | + |
| 29 | +enum AwsBedrockResourceType { |
| 30 | + AGENT_TYPE(AWS_AGENT_ID, RequestAccess::getAgentId), |
| 31 | + DATA_SOURCE_TYPE(AWS_DATA_SOURCE_ID, RequestAccess::getDataSourceId), |
| 32 | + KNOWLEDGE_BASE_TYPE(AWS_KNOWLEDGE_BASE_ID, RequestAccess::getKnowledgeBaseId); |
| 33 | + |
| 34 | + @SuppressWarnings("ImmutableEnumChecker") |
| 35 | + private final AttributeKey<String> keyAttribute; |
| 36 | + |
| 37 | + @SuppressWarnings("ImmutableEnumChecker") |
| 38 | + private final Function<Object, String> attributeValueAccessor; |
| 39 | + |
| 40 | + AwsBedrockResourceType( |
| 41 | + AttributeKey<String> keyAttribute, Function<Object, String> attributeValueAccessor) { |
| 42 | + this.keyAttribute = keyAttribute; |
| 43 | + this.attributeValueAccessor = attributeValueAccessor; |
| 44 | + } |
| 45 | + |
| 46 | + public AttributeKey<String> getKeyAttribute() { |
| 47 | + return keyAttribute; |
| 48 | + } |
| 49 | + |
| 50 | + public Function<Object, String> getAttributeValueAccessor() { |
| 51 | + return attributeValueAccessor; |
| 52 | + } |
| 53 | + |
| 54 | + public static AwsBedrockResourceType getRequestType(String requestClass) { |
| 55 | + return AwsBedrockResourceTypeMap.BEDROCK_REQUEST_MAP.get(requestClass); |
| 56 | + } |
| 57 | + |
| 58 | + public static AwsBedrockResourceType getResponseType(String responseClass) { |
| 59 | + return AwsBedrockResourceTypeMap.BEDROCK_RESPONSE_MAP.get(responseClass); |
| 60 | + } |
| 61 | + |
| 62 | + private static class AwsBedrockResourceTypeMap { |
| 63 | + private static final Map<String, AwsBedrockResourceType> BEDROCK_REQUEST_MAP = new HashMap<>(); |
| 64 | + private static final Map<String, AwsBedrockResourceType> BEDROCK_RESPONSE_MAP = new HashMap<>(); |
| 65 | + |
| 66 | + // Bedrock request/response mapping |
| 67 | + // We only support operations that are related to the resource and where the context contains |
| 68 | + // the AgentID/DataSourceID/KnowledgeBaseID. |
| 69 | + // AgentID |
| 70 | + private static final List<String> agentRequestClasses = |
| 71 | + Arrays.asList( |
| 72 | + "CreateAgentActionGroupRequest", |
| 73 | + "CreateAgentAliasRequest", |
| 74 | + "DeleteAgentActionGroupRequest", |
| 75 | + "DeleteAgentAliasRequest", |
| 76 | + "DeleteAgentRequest", |
| 77 | + "DeleteAgentVersionRequest", |
| 78 | + "GetAgentActionGroupRequest", |
| 79 | + "GetAgentAliasRequest", |
| 80 | + "GetAgentRequest", |
| 81 | + "GetAgentVersionRequest", |
| 82 | + "ListAgentActionGroupsRequest", |
| 83 | + "ListAgentAliasesRequest", |
| 84 | + "ListAgentKnowledgeBasesRequest", |
| 85 | + "ListAgentVersionsRequest", |
| 86 | + "PrepareAgentRequest", |
| 87 | + "UpdateAgentActionGroupRequest", |
| 88 | + "UpdateAgentAliasRequest", |
| 89 | + "UpdateAgentRequest"); |
| 90 | + private static final List<String> agentResponseClasses = |
| 91 | + Arrays.asList( |
| 92 | + "DeleteAgentAliasResult", |
| 93 | + "DeleteAgentResult", |
| 94 | + "DeleteAgentVersionResult", |
| 95 | + "PrepareAgentResult"); |
| 96 | + // DataSourceID |
| 97 | + private static final List<String> dataSourceRequestClasses = |
| 98 | + Arrays.asList("DeleteDataSourceRequest", "GetDataSourceRequest", "UpdateDataSourceRequest"); |
| 99 | + private static final List<String> dataSourceResponseClasses = |
| 100 | + Arrays.asList("DeleteDataSourceResult"); |
| 101 | + // KnowledgeBaseID |
| 102 | + private static final List<String> knowledgeBaseRequestClasses = |
| 103 | + Arrays.asList( |
| 104 | + "AssociateAgentKnowledgeBaseRequest", |
| 105 | + "CreateDataSourceRequest", |
| 106 | + "DeleteKnowledgeBaseRequest", |
| 107 | + "DisassociateAgentKnowledgeBaseRequest", |
| 108 | + "GetAgentKnowledgeBaseRequest", |
| 109 | + "GetKnowledgeBaseRequest", |
| 110 | + "ListDataSourcesRequest", |
| 111 | + "UpdateAgentKnowledgeBaseRequest"); |
| 112 | + private static final List<String> knowledgeBaseResponseClasses = |
| 113 | + Arrays.asList("DeleteKnowledgeBaseResult"); |
| 114 | + |
| 115 | + private AwsBedrockResourceTypeMap() {} |
| 116 | + |
| 117 | + static { |
| 118 | + // Populate the BEDROCK_REQUEST_MAP |
| 119 | + for (String agentRequestClass : agentRequestClasses) { |
| 120 | + BEDROCK_REQUEST_MAP.put(agentRequestClass, AwsBedrockResourceType.AGENT_TYPE); |
| 121 | + } |
| 122 | + for (String dataSourceRequestClass : dataSourceRequestClasses) { |
| 123 | + BEDROCK_REQUEST_MAP.put(dataSourceRequestClass, AwsBedrockResourceType.DATA_SOURCE_TYPE); |
| 124 | + } |
| 125 | + for (String knowledgeBaseRequestClass : knowledgeBaseRequestClasses) { |
| 126 | + BEDROCK_REQUEST_MAP.put( |
| 127 | + knowledgeBaseRequestClass, AwsBedrockResourceType.KNOWLEDGE_BASE_TYPE); |
| 128 | + } |
| 129 | + |
| 130 | + // Populate the BEDROCK_RESPONSE_MAP |
| 131 | + for (String agentResponseClass : agentResponseClasses) { |
| 132 | + BEDROCK_REQUEST_MAP.put(agentResponseClass, AwsBedrockResourceType.AGENT_TYPE); |
| 133 | + } |
| 134 | + for (String dataSourceResponseClass : dataSourceResponseClasses) { |
| 135 | + BEDROCK_REQUEST_MAP.put(dataSourceResponseClass, AwsBedrockResourceType.DATA_SOURCE_TYPE); |
| 136 | + } |
| 137 | + for (String knowledgeBaseResponseClass : knowledgeBaseResponseClasses) { |
| 138 | + BEDROCK_REQUEST_MAP.put( |
| 139 | + knowledgeBaseResponseClass, AwsBedrockResourceType.KNOWLEDGE_BASE_TYPE); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments