-
Notifications
You must be signed in to change notification settings - Fork 582
feat(instrumentation-aws-sdk): add bedrock extension to apply gen ai conventions #2700
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 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6cb00fa
feat(instrumentation-aws-sdk): add bedrock extension
anuraaga 765c280
Use minimum bedrock that supports Converse
anuraaga 94d7fe4
Cleanups
anuraaga 36ca8fd
Update deps
anuraaga fc8fd69
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 5bf7306
git add
anuraaga e2366aa
Cleanup
anuraaga 018cb5c
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga b8e3c16
Extract converse handler
anuraaga c33aaaa
Add pre-implementation test case of InvokeModel
anuraaga f768a7d
git add
anuraaga 114211d
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 52900ed
TAV
anuraaga ab76ddb
Cleanup
anuraaga ae73969
Setup fixed credentials for dryrun mode
anuraaga fc9a99f
Cleanup
anuraaga 8aaf08c
regenerate src/semconv.ts with .../scripts/gen-semconv-ts.js (sorted …
trentm bde108c
briefly mention the custom bedrock-runtime instr in the README
trentm fa12583
drop the skip-test-if support added in #2723 because it is no longer …
trentm c1fc1f6
let's *not* remove the skip-test-if.js script even though it isn't cu…
trentm c220322
Merge branch 'main' into aws-bedrock-extension
trentm 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
134 changes: 134 additions & 0 deletions
134
plugins/node/opentelemetry-instrumentation-aws-sdk/src/semconv.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,134 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* This file contains a copy of unstable semantic convention definitions | ||
* used by this package. | ||
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv | ||
*/ | ||
|
||
/** | ||
* The Generative AI product as identified by the client or server instrumentation. | ||
* | ||
* @example "openai" | ||
* | ||
* @note The `gen_ai.system` describes a family of GenAI models with specific model identified | ||
* by `gen_ai.request.model` and `gen_ai.response.model` attributes. | ||
* | ||
* The actual GenAI product may differ from the one identified by the client. | ||
* For example, when using OpenAI client libraries to communicate with Mistral, the `gen_ai.system` | ||
* is set to `openai` based on the instrumentation's best knowledge. | ||
* | ||
* For custom model, a custom friendly name **SHOULD** be used. | ||
* If none of these options apply, the `gen_ai.system` **SHOULD** be set to `_OTHER`. | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_SYSTEM = 'gen_ai.system'; | ||
|
||
/** | ||
* The name of the operation being performed. | ||
* | ||
* @note If one of the predefined values applies, but specific system uses a different name it's **RECOMMENDED** to document it in the semantic conventions for specific GenAI system and use system-specific name in the instrumentation. If a different name is not documented, instrumentation libraries **SHOULD** use applicable predefined value. | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_OPERATION_NAME = 'gen_ai.operation.name'; | ||
|
||
/** | ||
* The name of the GenAI model a request is being made to. | ||
* | ||
* @example "gpt-4" | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_REQUEST_MODEL = 'gen_ai.request.model'; | ||
|
||
/** | ||
* The maximum number of tokens the model generates for a request. | ||
* | ||
* @example 100 | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_REQUEST_MAX_TOKENS = 'gen_ai.request.max_tokens'; | ||
|
||
/** | ||
* The temperature setting for the GenAI request. | ||
* | ||
* @example 0.0 | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_REQUEST_TEMPERATURE = 'gen_ai.request.temperature'; | ||
|
||
/** | ||
* The top_p sampling setting for the GenAI request. | ||
* | ||
* @example 1.0 | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_REQUEST_TOP_P = 'gen_ai.request.top_p'; | ||
|
||
/** | ||
* List of sequences that the model will use to stop generating further tokens. | ||
* | ||
* @example ["forest", "lived"] | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_REQUEST_STOP_SEQUENCES = | ||
'gen_ai.request.stop_sequences'; | ||
|
||
/** | ||
* The number of tokens used in the GenAI input (prompt). | ||
* | ||
* @example 100 | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_USAGE_INPUT_TOKENS = 'gen_ai.usage.input_tokens'; | ||
|
||
/** | ||
* The number of tokens used in the GenAI response (completion). | ||
* | ||
* @example 180 | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_USAGE_OUTPUT_TOKENS = 'gen_ai.usage.output_tokens'; | ||
|
||
/** | ||
* Array of reasons the model stopped generating tokens, corresponding to each generation received. | ||
* | ||
* @example ["stop"] | ||
* @example ["stop", "length"] | ||
* | ||
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`. | ||
*/ | ||
export const ATTR_GEN_AI_RESPONSE_FINISH_REASONS = | ||
'gen_ai.response.finish_reasons'; | ||
|
||
/** | ||
* Enum value "aws.bedrock" for attribute {@link ATTR_GEN_AI_SYSTEM}. | ||
*/ | ||
export const GEN_AI_SYSTEM_VALUE_AWS_BEDROCK = 'aws.bedrock'; | ||
|
||
/** | ||
* Enum value "chat" for attribute {@link ATTR_GEN_AI_OPERATION_NAME}. | ||
*/ | ||
export const GEN_AI_OPERATION_NAME_VALUE_CHAT = 'chat'; |
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
134 changes: 134 additions & 0 deletions
134
plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/bedrock-runtime.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,134 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Attributes, DiagLogger, Span, Tracer } from '@opentelemetry/api'; | ||
import { RequestMetadata, ServiceExtension } from './ServiceExtension'; | ||
import { | ||
ATTR_GEN_AI_SYSTEM, | ||
ATTR_GEN_AI_OPERATION_NAME, | ||
ATTR_GEN_AI_REQUEST_MODEL, | ||
ATTR_GEN_AI_REQUEST_MAX_TOKENS, | ||
ATTR_GEN_AI_REQUEST_TEMPERATURE, | ||
ATTR_GEN_AI_REQUEST_TOP_P, | ||
ATTR_GEN_AI_REQUEST_STOP_SEQUENCES, | ||
ATTR_GEN_AI_USAGE_INPUT_TOKENS, | ||
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, | ||
ATTR_GEN_AI_RESPONSE_FINISH_REASONS, | ||
GEN_AI_OPERATION_NAME_VALUE_CHAT, | ||
GEN_AI_SYSTEM_VALUE_AWS_BEDROCK, | ||
} from '../semconv'; | ||
import { | ||
AwsSdkInstrumentationConfig, | ||
NormalizedRequest, | ||
NormalizedResponse, | ||
} from '../types'; | ||
|
||
export class BedrockRuntimeServiceExtension implements ServiceExtension { | ||
requestPreSpanHook( | ||
request: NormalizedRequest, | ||
config: AwsSdkInstrumentationConfig, | ||
diag: DiagLogger | ||
): RequestMetadata { | ||
switch (request.commandName) { | ||
case 'Converse': | ||
return this.requestPreSpanHookConverse(request, config, diag); | ||
} | ||
|
||
return { | ||
isIncoming: false, | ||
}; | ||
} | ||
|
||
private requestPreSpanHookConverse( | ||
request: NormalizedRequest, | ||
config: AwsSdkInstrumentationConfig, | ||
diag: DiagLogger | ||
): RequestMetadata { | ||
let spanName = GEN_AI_OPERATION_NAME_VALUE_CHAT; | ||
const spanAttributes: Attributes = { | ||
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK, | ||
[ATTR_GEN_AI_OPERATION_NAME]: GEN_AI_OPERATION_NAME_VALUE_CHAT, | ||
}; | ||
|
||
const modelId = request.commandInput.modelId; | ||
if (modelId) { | ||
spanAttributes[ATTR_GEN_AI_REQUEST_MODEL] = modelId; | ||
if (spanName) { | ||
spanName += ` ${modelId}`; | ||
} | ||
} | ||
|
||
const inferenceConfig = request.commandInput.inferenceConfig; | ||
if (inferenceConfig) { | ||
const { maxTokens, temperature, topP, stopSequences } = inferenceConfig; | ||
if (maxTokens !== undefined) { | ||
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = maxTokens; | ||
} | ||
if (temperature !== undefined) { | ||
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = temperature; | ||
} | ||
if (topP !== undefined) { | ||
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = topP; | ||
} | ||
if (stopSequences !== undefined) { | ||
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = stopSequences; | ||
} | ||
} | ||
|
||
return { | ||
spanName, | ||
isIncoming: false, | ||
spanAttributes, | ||
}; | ||
} | ||
|
||
responseHook( | ||
response: NormalizedResponse, | ||
span: Span, | ||
tracer: Tracer, | ||
config: AwsSdkInstrumentationConfig | ||
) { | ||
if (!span.isRecording()) { | ||
return; | ||
Check warning on line 104 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/bedrock-runtime.ts
|
||
} | ||
|
||
switch (response.request.commandName) { | ||
case 'Converse': | ||
return this.responseHookConverse(response, span, tracer, config); | ||
} | ||
} | ||
|
||
private responseHookConverse( | ||
response: NormalizedResponse, | ||
span: Span, | ||
tracer: Tracer, | ||
config: AwsSdkInstrumentationConfig | ||
) { | ||
const { stopReason, usage } = response.data; | ||
if (usage) { | ||
const { inputTokens, outputTokens } = usage; | ||
if (inputTokens !== undefined) { | ||
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, inputTokens); | ||
} | ||
if (outputTokens !== undefined) { | ||
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens); | ||
} | ||
} | ||
|
||
if (stopReason !== undefined) { | ||
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [stopReason]); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove this comment since the bedrock-runtime tests are only for
^3.587.0