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 1 commit
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
207 changes: 207 additions & 0 deletions
207
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,207 @@ | ||
| // 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']; | ||
|
|
||
| const operationToBedrockAgentServiceMapping: { [key: string]: { [key: string]: string } } = {}; | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (const operation of AGENT_OPERATIONS) { | ||
| operationToBedrockAgentServiceMapping[operation] = { [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_AGENT_ID]: AGENT_ID }; | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| for (const operation of KNOWLEDGE_BASE_OPERATIONS) { | ||
| operationToBedrockAgentServiceMapping[operation] = { | ||
| [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_KNOWLEDGE_BASE_ID]: KNOWLEDGE_BASE_ID, | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| for (const operation of DATA_SOURCE_OPERATIONS) { | ||
| operationToBedrockAgentServiceMapping[operation] = { | ||
| [AWS_ATTRIBUTE_KEYS.AWS_BEDROCK_DATA_SOURCE_ID]: DATA_SOURCE_ID, | ||
| }; | ||
| } | ||
|
|
||
| export class BedrockAgentServiceExtension implements ServiceExtension { | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bedrockAgentServiceInfo: { [key: string]: string } = {}; | ||
| 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 && Object.keys(operationToBedrockAgentServiceMapping).includes(operation)) { | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.bedrockAgentServiceInfo = operationToBedrockAgentServiceMapping[operation]; | ||
| // This should only trigger once; it is the easy way to access the 0th and only entry | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (const serviceInfo of Object.entries(this.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 { | ||
| // This should only trigger once; it is the easy way to access the 0th and only entry | ||
| for (const serviceInfo of Object.entries(this.bedrockAgentServiceInfo)) { | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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.