generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 15
Add Bedrock, Bedrock Agent, Bedrock Runtime, and Bedrock Agent Runtime support #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
aws-distro-opentelemetry-node-autoinstrumentation/src/patches/aws/services/bedrock.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Attributes, DiagLogger, Span, SpanKind, Tracer } from '@opentelemetry/api'; | ||
| import { | ||
| AwsSdkInstrumentationConfig, | ||
| NormalizedRequest, | ||
| NormalizedResponse, | ||
| } from '@opentelemetry/instrumentation-aws-sdk'; | ||
| import { AWS_ATTRIBUTE_KEYS } from '../../../aws-attribute-keys'; | ||
| import { RequestMetadata, ServiceExtension } from '../../../third-party/otel/aws/services/ServiceExtension'; | ||
| import { AwsSpanProcessingUtil } from '../../../aws-span-processing-util'; | ||
|
|
||
| const AGENT_ID: string = 'agentId'; | ||
| const KNOWLEDGE_BASE_ID: string = 'knowledgeBaseId'; | ||
| const DATA_SOURCE_ID: string = 'dataSourceId'; | ||
| const GUARDRAIL_ID: string = 'guardrailId'; | ||
| const MODEL_ID: string = 'modelId'; | ||
| const AWS_BEDROCK_SYSTEM: string = 'aws_bedrock'; | ||
|
|
||
| const AGENT_OPERATIONS = [ | ||
| 'CreateAgentActionGroup', | ||
| 'CreateAgentAlias', | ||
| 'DeleteAgentActionGroup', | ||
| 'DeleteAgentAlias', | ||
| 'DeleteAgent', | ||
| 'DeleteAgentVersion', | ||
| 'GetAgentActionGroup', | ||
| 'GetAgentAlias', | ||
| 'GetAgent', | ||
| 'GetAgentVersion', | ||
| 'ListAgentActionGroups', | ||
| 'ListAgentAliases', | ||
| 'ListAgentKnowledgeBases', | ||
| 'ListAgentVersions', | ||
| 'PrepareAgent', | ||
| 'UpdateAgentActionGroup', | ||
| 'UpdateAgentAlias', | ||
| 'UpdateAgent', | ||
| ]; | ||
|
|
||
| const KNOWLEDGE_BASE_OPERATIONS = [ | ||
| 'AssociateAgentKnowledgeBase', | ||
| 'CreateDataSource', | ||
| 'DeleteKnowledgeBase', | ||
| 'DisassociateAgentKnowledgeBase', | ||
| 'GetAgentKnowledgeBase', | ||
| 'GetKnowledgeBase', | ||
| 'ListDataSources', | ||
| 'UpdateAgentKnowledgeBase', | ||
| ]; | ||
|
|
||
| const DATA_SOURCE_OPERATIONS = ['DeleteDataSource', 'GetDataSource', 'UpdateDataSource']; | ||
|
|
||
| // The following constants map the way we present the data in telemetry to how they appear in request/responses | ||
| // e.g. we put `aws.bedrock.knowledge_base.id` into trace data by finding `knowledgeBaseId` | ||
| const agentOperationAttributeKeyMapping = { [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_AGENT_ID]: AGENT_ID }; | ||
| const knowledgeBaseOperationAttributeKeyMapping = { | ||
| [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_KNOWLEDGE_BASE_ID]: KNOWLEDGE_BASE_ID, | ||
| }; | ||
| const dataSourceOperationAttributeKeyMapping = { | ||
| [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_DATA_SOURCE_ID]: DATA_SOURCE_ID, | ||
| }; | ||
|
|
||
| // This map allows us to get all relevant attribute key mappings for a given operation | ||
| const operationToBedrockAgentAttributesMap: { [key: string]: { [key: string]: string } } = {}; | ||
| for (const operation of AGENT_OPERATIONS) { | ||
| operationToBedrockAgentAttributesMap[operation] = agentOperationAttributeKeyMapping; | ||
| } | ||
| for (const operation of KNOWLEDGE_BASE_OPERATIONS) { | ||
| operationToBedrockAgentAttributesMap[operation] = knowledgeBaseOperationAttributeKeyMapping; | ||
| } | ||
| for (const operation of DATA_SOURCE_OPERATIONS) { | ||
| operationToBedrockAgentAttributesMap[operation] = dataSourceOperationAttributeKeyMapping; | ||
| } | ||
|
|
||
| // This class is an extension for <a | ||
| // href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html"> | ||
| // Agents for Amazon Bedrock</a>. | ||
| // This class primarily identify three types of resource based operations: AGENT_OPERATIONS, | ||
| // KNOWLEDGE_BASE_OPERATIONS, and DATA_SOURCE_OPERATIONS. We only support operations that are related to | ||
| // the resource and where the context contains the resource ID. | ||
| export class BedrockAgentServiceExtension implements ServiceExtension { | ||
| requestPreSpanHook( | ||
| request: NormalizedRequest, | ||
| config: AwsSdkInstrumentationConfig, | ||
| diag: DiagLogger | ||
| ): RequestMetadata { | ||
| const spanAttributes: Attributes = {}; | ||
| const isIncoming = false; | ||
| const spanKind: SpanKind = SpanKind.CLIENT; | ||
| let spanName: string | undefined; | ||
|
|
||
| const operation: string = request.commandName; | ||
| if (operation && operationToBedrockAgentAttributesMap[operation]) { | ||
| const bedrockAgentServiceInfo = operationToBedrockAgentAttributesMap[operation]; | ||
| for (const serviceInfo of Object.entries(bedrockAgentServiceInfo)) { | ||
| const [attributeKey, requestParamKey] = serviceInfo; | ||
| const requestParamValue = request.commandInput?.[requestParamKey]; | ||
|
|
||
| if (requestParamValue) { | ||
| spanAttributes[attributeKey] = requestParamValue; | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| isIncoming, | ||
| spanAttributes, | ||
| spanKind, | ||
| spanName, | ||
| }; | ||
| } | ||
|
|
||
| responseHook(response: NormalizedResponse, span: Span, tracer: Tracer, config: AwsSdkInstrumentationConfig): void { | ||
| const operation: string = response.request.commandName; | ||
| if (operation && operationToBedrockAgentAttributesMap[operation]) { | ||
| const bedrockAgentServiceInfo = operationToBedrockAgentAttributesMap[operation]; | ||
| for (const serviceInfo of Object.entries(bedrockAgentServiceInfo)) { | ||
| const [attributeKey, responseParamKey] = serviceInfo; | ||
| const responseParamValue = response.data[responseParamKey]; | ||
|
|
||
| if (responseParamValue) { | ||
| span.setAttribute(attributeKey, responseParamValue); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // This class is an extension for <a | ||
| // href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html"> | ||
| // Agents for Amazon Bedrock Runtime</a>. | ||
| export class BedrockAgentRuntimeServiceExtension implements ServiceExtension { | ||
| requestPreSpanHook( | ||
| request: NormalizedRequest, | ||
| config: AwsSdkInstrumentationConfig, | ||
| diag: DiagLogger | ||
| ): RequestMetadata { | ||
| const spanAttributes: Attributes = {}; | ||
| const isIncoming = false; | ||
| const spanKind: SpanKind = SpanKind.CLIENT; | ||
| let spanName: string | undefined; | ||
|
|
||
| const agentId = request.commandInput?.[AGENT_ID]; | ||
| const knowledgeBaseId = request.commandInput?.[KNOWLEDGE_BASE_ID]; | ||
|
|
||
| if (agentId) { | ||
| spanAttributes[AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_AGENT_ID] = agentId; | ||
| } | ||
| if (knowledgeBaseId) { | ||
| spanAttributes[AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_KNOWLEDGE_BASE_ID] = knowledgeBaseId; | ||
| } | ||
|
|
||
| return { | ||
| isIncoming, | ||
| spanAttributes, | ||
| spanKind, | ||
| spanName, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // This class is an extension for <a | ||
| // href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock.html">Bedrock</a>. | ||
| export class BedrockServiceExtension implements ServiceExtension { | ||
| // Must be implemented, returning empty metadata | ||
| requestPreSpanHook( | ||
| request: NormalizedRequest, | ||
| config: AwsSdkInstrumentationConfig, | ||
| diag: DiagLogger | ||
| ): RequestMetadata { | ||
| const spanAttributes: Attributes = {}; | ||
| const isIncoming = false; | ||
| const spanKind: SpanKind = SpanKind.CLIENT; | ||
| let spanName: string | undefined; | ||
| return { | ||
| isIncoming, | ||
| spanAttributes, | ||
| spanKind, | ||
| spanName, | ||
| }; | ||
| } | ||
| responseHook(response: NormalizedResponse, span: Span, tracer: Tracer, config: AwsSdkInstrumentationConfig): void { | ||
| const guardrail_id = response.data[GUARDRAIL_ID]; | ||
|
|
||
| if (guardrail_id) { | ||
| span.setAttribute(AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_GUARDRAIL_ID, guardrail_id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // This class is an extension for <a | ||
| // href="https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock_Runtime.html"> | ||
| // Amazon Bedrock Runtime</a>. | ||
| export class BedrockRuntimeServiceExtension implements ServiceExtension { | ||
| requestPreSpanHook( | ||
| request: NormalizedRequest, | ||
| config: AwsSdkInstrumentationConfig, | ||
| diag: DiagLogger | ||
| ): RequestMetadata { | ||
| const spanAttributes: Attributes = {}; | ||
| const isIncoming = false; | ||
| const spanKind: SpanKind = SpanKind.CLIENT; | ||
| let spanName: string | undefined; | ||
|
|
||
| const modelId = request.commandInput?.[MODEL_ID]; | ||
|
|
||
| spanAttributes[AwsSpanProcessingUtil.GEN_AI_SYSTEM] = AWS_BEDROCK_SYSTEM; | ||
| if (modelId) { | ||
| spanAttributes[AwsSpanProcessingUtil.GEN_AI_REQUEST_MODEL] = modelId; | ||
| } | ||
|
|
||
| return { | ||
| isIncoming, | ||
| spanAttributes, | ||
| spanKind, | ||
| spanName, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.