|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import abc |
| 4 | +import inspect |
| 5 | +from typing import Dict, Optional |
| 6 | + |
| 7 | +from amazon.opentelemetry.distro._aws_attribute_keys import ( |
| 8 | + AWS_BEDROCK_AGENT_ID, |
| 9 | + AWS_BEDROCK_DATA_SOURCE_ID, |
| 10 | + AWS_BEDROCK_GUARDRAIL_ID, |
| 11 | + AWS_BEDROCK_KNOWLEDGE_BASE_ID, |
| 12 | +) |
| 13 | +from amazon.opentelemetry.distro._aws_span_processing_util import GEN_AI_REQUEST_MODEL, GEN_AI_SYSTEM |
| 14 | +from opentelemetry.instrumentation.botocore.extensions.types import ( |
| 15 | + _AttributeMapT, |
| 16 | + _AwsSdkCallContext, |
| 17 | + _AwsSdkExtension, |
| 18 | + _BotoResultT, |
| 19 | +) |
| 20 | +from opentelemetry.trace.span import Span |
| 21 | + |
| 22 | +_AGENT_ID: str = "agentId" |
| 23 | +_KNOWLEDGE_BASE_ID: str = "knowledgeBaseId" |
| 24 | +_DATA_SOURCE_ID: str = "dataSourceId" |
| 25 | +_GUARDRAIL_ID: str = "guardrailId" |
| 26 | +_MODEL_ID: str = "modelId" |
| 27 | +_AWS_BEDROCK_SYSTEM: str = "aws_bedrock" |
| 28 | + |
| 29 | + |
| 30 | +class _BedrockAgentOperation(abc.ABC): |
| 31 | + """ |
| 32 | + We use subclasses and operation names to handle specific Bedrock Agent operations. |
| 33 | + - Only operations involving Agent, DataSource, or KnowledgeBase resources are supported. |
| 34 | + - Operations without these specified resources are not covered. |
| 35 | + - When an operation involves multiple resources (e.g., AssociateAgentKnowledgeBase), |
| 36 | + we map it to one resource based on some judgement classification of rules. |
| 37 | +
|
| 38 | + For detailed API documentation on Bedrock Agent operations, visit: |
| 39 | + https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html |
| 40 | + """ |
| 41 | + |
| 42 | + request_attributes: Optional[Dict[str, str]] = None |
| 43 | + response_attributes: Optional[Dict[str, str]] = None |
| 44 | + |
| 45 | + @classmethod |
| 46 | + @abc.abstractmethod |
| 47 | + def operation_names(cls): |
| 48 | + pass |
| 49 | + |
| 50 | + |
| 51 | +class _AgentOperation(_BedrockAgentOperation): |
| 52 | + """ |
| 53 | + This class covers BedrockAgent API related to <a |
| 54 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_Agent.html">Agents</a>, |
| 55 | + and extracts agent-related attributes. |
| 56 | + """ |
| 57 | + |
| 58 | + request_attributes = { |
| 59 | + AWS_BEDROCK_AGENT_ID: _AGENT_ID, |
| 60 | + } |
| 61 | + response_attributes = { |
| 62 | + AWS_BEDROCK_AGENT_ID: _AGENT_ID, |
| 63 | + } |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def operation_names(cls): |
| 67 | + return [ |
| 68 | + "CreateAgentActionGroup", |
| 69 | + "CreateAgentAlias", |
| 70 | + "DeleteAgentActionGroup", |
| 71 | + "DeleteAgentAlias", |
| 72 | + "DeleteAgent", |
| 73 | + "DeleteAgentVersion", |
| 74 | + "GetAgentActionGroup", |
| 75 | + "GetAgentAlias", |
| 76 | + "GetAgent", |
| 77 | + "GetAgentVersion", |
| 78 | + "ListAgentActionGroups", |
| 79 | + "ListAgentAliases", |
| 80 | + "ListAgentKnowledgeBases", |
| 81 | + "ListAgentVersions", |
| 82 | + "PrepareAgent", |
| 83 | + "UpdateAgentActionGroup", |
| 84 | + "UpdateAgentAlias", |
| 85 | + "UpdateAgent", |
| 86 | + ] |
| 87 | + |
| 88 | + |
| 89 | +class _KnowledgeBaseOperation(_BedrockAgentOperation): |
| 90 | + """ |
| 91 | + This class covers BedrockAgent API related to <a |
| 92 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_KnowledgeBase.html">KnowledgeBases</a>, |
| 93 | + and extracts knowledge base-related attributes. |
| 94 | +
|
| 95 | + Note: The 'CreateDataSource' operation does not have a 'dataSourceId' in the context, |
| 96 | + but it always comes with a 'knowledgeBaseId'. Therefore, we categorize it under 'knowledgeBaseId' operations. |
| 97 | + """ |
| 98 | + |
| 99 | + request_attributes = { |
| 100 | + AWS_BEDROCK_KNOWLEDGE_BASE_ID: _KNOWLEDGE_BASE_ID, |
| 101 | + } |
| 102 | + response_attributes = { |
| 103 | + AWS_BEDROCK_KNOWLEDGE_BASE_ID: _KNOWLEDGE_BASE_ID, |
| 104 | + } |
| 105 | + |
| 106 | + @classmethod |
| 107 | + def operation_names(cls): |
| 108 | + return [ |
| 109 | + "AssociateAgentKnowledgeBase", |
| 110 | + "CreateDataSource", |
| 111 | + "DeleteKnowledgeBase", |
| 112 | + "DisassociateAgentKnowledgeBase", |
| 113 | + "GetAgentKnowledgeBase", |
| 114 | + "GetKnowledgeBase", |
| 115 | + "ListDataSources", |
| 116 | + "UpdateAgentKnowledgeBase", |
| 117 | + ] |
| 118 | + |
| 119 | + |
| 120 | +class _DataSourceOperation(_BedrockAgentOperation): |
| 121 | + """ |
| 122 | + This class covers BedrockAgent API related to <a |
| 123 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DataSource.html">DataSources</a>, |
| 124 | + and extracts data source-related attributes. |
| 125 | + """ |
| 126 | + |
| 127 | + request_attributes = { |
| 128 | + AWS_BEDROCK_DATA_SOURCE_ID: _DATA_SOURCE_ID, |
| 129 | + } |
| 130 | + response_attributes = { |
| 131 | + AWS_BEDROCK_DATA_SOURCE_ID: _DATA_SOURCE_ID, |
| 132 | + } |
| 133 | + |
| 134 | + @classmethod |
| 135 | + def operation_names(cls): |
| 136 | + return ["DeleteDataSource", "GetDataSource", "UpdateDataSource"] |
| 137 | + |
| 138 | + |
| 139 | +# _OPERATION_NAME_TO_CLASS_MAPPING maps operation names to their corresponding classes |
| 140 | +# by iterating over all subclasses of _BedrockAgentOperation and extract operations |
| 141 | +# by calling operation_names() function. |
| 142 | +_OPERATION_NAME_TO_CLASS_MAPPING = { |
| 143 | + op_name: op_class |
| 144 | + for op_class in [_KnowledgeBaseOperation, _DataSourceOperation, _AgentOperation] |
| 145 | + for op_name in op_class.operation_names() |
| 146 | + if inspect.isclass(op_class) and issubclass(op_class, _BedrockAgentOperation) and not inspect.isabstract(op_class) |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +class _BedrockAgentExtension(_AwsSdkExtension): |
| 151 | + """ |
| 152 | + This class is an extension for <a |
| 153 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html"> |
| 154 | + Agents for Amazon Bedrock</a>. |
| 155 | +
|
| 156 | + This class primarily identify three types of resource based operations: _AgentOperation, _KnowledgeBaseOperation, |
| 157 | + and _DataSourceOperation. We only support operations that are related to the resource |
| 158 | + and where the context contains the resource ID. |
| 159 | + """ |
| 160 | + |
| 161 | + def __init__(self, call_context: _AwsSdkCallContext): |
| 162 | + super().__init__(call_context) |
| 163 | + self._operation_class = _OPERATION_NAME_TO_CLASS_MAPPING.get(call_context.operation) |
| 164 | + |
| 165 | + def extract_attributes(self, attributes: _AttributeMapT): |
| 166 | + if self._operation_class is None: |
| 167 | + return |
| 168 | + for attribute_key, request_param_key in self._operation_class.request_attributes.items(): |
| 169 | + request_param_value = self._call_context.params.get(request_param_key) |
| 170 | + if request_param_value: |
| 171 | + attributes[attribute_key] = request_param_value |
| 172 | + |
| 173 | + def on_success(self, span: Span, result: _BotoResultT): |
| 174 | + if self._operation_class is None: |
| 175 | + return |
| 176 | + |
| 177 | + for attribute_key, response_param_key in self._operation_class.response_attributes.items(): |
| 178 | + response_param_value = result.get(response_param_key) |
| 179 | + if response_param_value: |
| 180 | + span.set_attribute( |
| 181 | + attribute_key, |
| 182 | + response_param_value, |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +class _BedrockAgentRuntimeExtension(_AwsSdkExtension): |
| 187 | + """ |
| 188 | + This class is an extension for <a |
| 189 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html"> |
| 190 | + Agents for Amazon Bedrock Runtime</a>. |
| 191 | + """ |
| 192 | + |
| 193 | + def extract_attributes(self, attributes: _AttributeMapT): |
| 194 | + agent_id = self._call_context.params.get(_AGENT_ID) |
| 195 | + if agent_id: |
| 196 | + attributes[AWS_BEDROCK_AGENT_ID] = agent_id |
| 197 | + |
| 198 | + knowledge_base_id = self._call_context.params.get(_KNOWLEDGE_BASE_ID) |
| 199 | + if knowledge_base_id: |
| 200 | + attributes[AWS_BEDROCK_KNOWLEDGE_BASE_ID] = knowledge_base_id |
| 201 | + |
| 202 | + |
| 203 | +class _BedrockExtension(_AwsSdkExtension): |
| 204 | + """ |
| 205 | + This class is an extension for <a |
| 206 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock.html">Bedrock</a>. |
| 207 | + """ |
| 208 | + |
| 209 | + # pylint: disable=no-self-use |
| 210 | + def on_success(self, span: Span, result: _BotoResultT): |
| 211 | + # _GUARDRAIL_ID can only be retrieved from the response, not from the request |
| 212 | + guardrail_id = result.get(_GUARDRAIL_ID) |
| 213 | + if guardrail_id: |
| 214 | + span.set_attribute( |
| 215 | + AWS_BEDROCK_GUARDRAIL_ID, |
| 216 | + guardrail_id, |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +class _BedrockRuntimeExtension(_AwsSdkExtension): |
| 221 | + """ |
| 222 | + This class is an extension for <a |
| 223 | + href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock_Runtime.html"> |
| 224 | + Amazon Bedrock Runtime</a>. |
| 225 | + """ |
| 226 | + |
| 227 | + def extract_attributes(self, attributes: _AttributeMapT): |
| 228 | + attributes[GEN_AI_SYSTEM] = _AWS_BEDROCK_SYSTEM |
| 229 | + |
| 230 | + model_id = self._call_context.params.get(_MODEL_ID) |
| 231 | + if model_id: |
| 232 | + attributes[GEN_AI_REQUEST_MODEL] = model_id |
0 commit comments