Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
73a764c
cherry pick changes from previous PR
keith-decker Sep 17, 2025
0d749a9
move span utils to new file
keith-decker Sep 17, 2025
77d9c3c
remove span state, use otel context for parent/child
keith-decker Sep 17, 2025
054ebe9
flatten LLMInvocation to use attributes instead of dict keys
keith-decker Sep 17, 2025
9d3926f
helper function and docstrings
keith-decker Sep 17, 2025
635b7f8
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 18, 2025
9837cf4
refactor: store span and context token in LLMInvocation instead of Sp…
keith-decker Sep 18, 2025
1a172d1
refactor: rename prompts/chat_generations to input_messages/output_me…
keith-decker Sep 19, 2025
465ca78
refactor: simplify TelemetryHandler API by moving invocation data man…
keith-decker Sep 19, 2025
cd5aaa6
refactor: update relative imports to absolute imports
keith-decker Sep 19, 2025
8347d17
Update handler to use a context manager instead of start_llm and stop…
keith-decker Sep 22, 2025
742a36f
resolve tox -e doc failure
keith-decker Sep 22, 2025
00e08a8
safeguard against empty request-model
keith-decker Sep 22, 2025
109625c
Merge branch 'main' into util-genai-inference-clean
keith-decker Sep 22, 2025
80c94bf
fix tox typecheck errors for utils
keith-decker Sep 22, 2025
f07c61f
refactor: move tracer to generator, clean up dead code
keith-decker Sep 22, 2025
d6f722f
remove unused linting hint
keith-decker Sep 22, 2025
fdc7f50
back off stricter request-model requirements
keith-decker Sep 22, 2025
da20448
reintroduce manual start/stop for langchain callback flow
keith-decker Sep 22, 2025
18c3365
Fix typecheck in langchain instrumentation (#3773)
wrisa Sep 23, 2025
b232b9a
botocore: Add support for AWS Secrets Manager semantic convention att…
lukeina2z Sep 23, 2025
c1ff846
Merge branch 'main' into util-genai-inference-clean
aabmass Sep 23, 2025
4a08d7f
clean up context handler, clarify unit tests
keith-decker Sep 23, 2025
243bf8d
remove generator concept
keith-decker Sep 25, 2025
70903cc
Merge branch 'genai-utils-e2e-dev' into e2e-inference-merge
keith-decker Sep 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

### Added
- `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute
([#3765](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3765))

## Version 1.37.0/0.58b0 (2025-09-11)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@dataclass
class _SpanState:
span: Span
children: List[UUID] = field(default_factory=list)
children: List[UUID] = field(default_factory=lambda: list())


class _SpanManager:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def loader():
"bedrock-runtime": _lazy_load(".bedrock", "_BedrockRuntimeExtension"),
"dynamodb": _lazy_load(".dynamodb", "_DynamoDbExtension"),
"lambda": _lazy_load(".lmbd", "_LambdaExtension"),
"secretsmanager": _lazy_load(
".secretsmanager", "_SecretsManagerExtension"
),
"stepfunctions": _lazy_load(".sfns", "_StepFunctionsExtension"),
"sns": _lazy_load(".sns", "_SnsExtension"),
"sqs": _lazy_load(".sqs", "_SqsExtension"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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
#
# http://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.
from opentelemetry.instrumentation.botocore.extensions.types import (
_AttributeMapT,
_AwsSdkExtension,
_BotocoreInstrumentorContext,
_BotoResultT,
)
from opentelemetry.semconv._incubating.attributes.aws_attributes import (
AWS_SECRETSMANAGER_SECRET_ARN,
)
from opentelemetry.trace.span import Span


class _SecretsManagerExtension(_AwsSdkExtension):
def extract_attributes(self, attributes: _AttributeMapT):
"""
SecretId is extracted if a secret ARN, the function extracts the attribute
only if the SecretId parameter is provided as an arn which starts with
`arn:aws:secretsmanager:`
"""
secret_id = self._call_context.params.get("SecretId")
if secret_id and secret_id.startswith("arn:aws:secretsmanager:"):
attributes[AWS_SECRETSMANAGER_SECRET_ARN] = secret_id

def on_success(
self,
span: Span,
result: _BotoResultT,
instrumentor_context: _BotocoreInstrumentorContext,
):
secret_arn = result.get("ARN")
if secret_arn:
span.set_attribute(AWS_SECRETSMANAGER_SECRET_ARN, secret_arn)
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 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
#
# http://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 botocore.session
from moto import mock_aws

from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.semconv._incubating.attributes.aws_attributes import (
AWS_SECRETSMANAGER_SECRET_ARN,
)
from opentelemetry.test.test_base import TestBase


class TestSecretsManagerExtension(TestBase):
def setUp(self):
super().setUp()
BotocoreInstrumentor().instrument()
session = botocore.session.get_session()
session.set_credentials(
access_key="access-key", secret_key="secret-key"
)
self.region = "us-west-2"
self.client = session.create_client(
"secretsmanager", region_name=self.region
)

def tearDown(self):
super().tearDown()
BotocoreInstrumentor().uninstrument()

def create_secret_and_get_arn(self, name: str = "test-secret") -> str:
"""
Create a secret in mocked Secrets Manager and return its ARN.
"""
# Clear spans before creating secret for helper method
self.memory_exporter.clear()
response = self.client.create_secret(
Name=name, SecretString="test-secret-value"
)
return response["ARN"]

@mock_aws
def test_tag_resource_with_arn(self):
secret_arn = self.create_secret_and_get_arn()

self.client.tag_resource(
SecretId=secret_arn, Tags=[{"Key": "Environment", "Value": "Test"}]
)

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 2)
span = spans[1] # tag_resource span
self.assertEqual(
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN],
secret_arn,
)

@mock_aws
def test_create_secret(self):
secret_name = "test-secret"
response = self.client.create_secret(
Name=secret_name, SecretString="test-secret-value"
)
secret_arn = response["ARN"]

spans = self.memory_exporter.get_finished_spans()
assert spans
self.assertEqual(len(spans), 1)
span = spans[0] # create_secret span
# Should capture ARN from response
self.assertEqual(
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN],
secret_arn,
)
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,24 @@
from contextlib import contextmanager
from typing import Any, Iterator, Optional

from opentelemetry.util.genai.generators import SpanGenerator
from opentelemetry import context as otel_context
from opentelemetry import trace
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAI,
)
from opentelemetry.semconv.schemas import Schemas
from opentelemetry.trace import (
SpanKind,
Tracer,
get_tracer,
set_span_in_context,
)
from opentelemetry.util.genai.span_utils import (
_apply_error_attributes,
_apply_finish_attributes,
)
from opentelemetry.util.genai.types import Error, LLMInvocation
from opentelemetry.util.genai.version import __version__


class TelemetryHandler:
Expand All @@ -73,32 +89,63 @@ class TelemetryHandler:
"""

def __init__(self, **kwargs: Any):
self._generator = SpanGenerator(**kwargs)
tracer_provider = kwargs.get("tracer_provider")
tracer = get_tracer(
__name__,
__version__,
tracer_provider,
schema_url=Schemas.V1_36_0.value,
)
self._tracer: Tracer = tracer or trace.get_tracer(__name__)

def start_llm(
self,
invocation: LLMInvocation,
) -> LLMInvocation:
"""Start an LLM invocation and create a pending span entry."""
self._generator.start(invocation)
# Create a span and attach it as current; keep the token to detach later
span = self._tracer.start_span(
name=f"{GenAI.GenAiOperationNameValues.CHAT.value} {invocation.request_model}",
kind=SpanKind.CLIENT,
)
invocation.span = span
invocation.context_token = otel_context.attach(
set_span_in_context(span)
)
return invocation

def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation:
def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pylint: disable=no-self-use
"""Finalize an LLM invocation successfully and end its span."""
invocation.end_time = time.time()
self._generator.finish(invocation)
if invocation.context_token is None or invocation.span is None:
# TODO: Provide feedback that this invocation was not started
return invocation

_apply_finish_attributes(invocation.span, invocation)
# Detach context and end span
otel_context.detach(invocation.context_token)
invocation.span.end()
return invocation

def fail_llm(
def fail_llm( # pylint: disable=no-self-use
self, invocation: LLMInvocation, error: Error
) -> LLMInvocation:
"""Fail an LLM invocation and end its span with error status."""
invocation.end_time = time.time()
self._generator.error(error, invocation)
if invocation.context_token is None or invocation.span is None:
# TODO: Provide feedback that this invocation was not started
return invocation

_apply_error_attributes(invocation.span, error)
# Detach context and end span
otel_context.detach(invocation.context_token)
invocation.span.end()
return invocation

@contextmanager
def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
def llm(
self, invocation: Optional[LLMInvocation] = None
) -> Iterator[LLMInvocation]:
"""Context manager for LLM invocations.

Only set data attributes on the invocation object, do not modify the span or context.
Expand All @@ -107,6 +154,10 @@ def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
If an exception occurs inside the context, marks the span as error, ends it, and
re-raises the original exception.
"""
if invocation is None:
invocation = LLMInvocation(
request_model="",
)
self.start_llm(invocation)
try:
yield invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def _apply_common_span_attributes(
"""
request_model = invocation.request_model
provider = invocation.provider

span.update_name(
f"{GenAI.GenAiOperationNameValues.CHAT.value} {request_model}"
)
span.set_attribute(
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
)
Expand Down
37 changes: 15 additions & 22 deletions util/opentelemetry-util-genai/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ def test_llm_start_and_stop_creates_span(self): # pylint: disable=no-self-use
)

# Start and stop LLM invocation using context manager
invocation = LLMInvocation(
request_model="test-model",
input_messages=[message],
provider="test-provider",
attributes={"custom_attr": "value"},
)

with self.telemetry_handler.llm(invocation):
with self.telemetry_handler.llm() as invocation:
invocation.request_model = "test-model"
invocation.input_messages = [message]
invocation.provider = "test-provider"
invocation.attributes = {"custom_attr": "value"}
assert invocation.span is not None
invocation.output_messages = [chat_generation]
invocation.attributes.update({"extra": "info"})
Expand Down Expand Up @@ -234,20 +231,16 @@ def test_parent_child_span_relationship(self):
role="AI", parts=[Text(content="ok")], finish_reason="stop"
)

# Start parent and child using nested contexts (child becomes child span of parent)
parent_invocation = LLMInvocation(
request_model="parent-model",
input_messages=[message],
provider="test-provider",
)
child_invocation = LLMInvocation(
request_model="child-model",
input_messages=[message],
provider="test-provider",
)

with self.telemetry_handler.llm(parent_invocation):
with self.telemetry_handler.llm(child_invocation):
with self.telemetry_handler.llm() as parent_invocation:
parent_invocation.request_model = "parent-model"
parent_invocation.input_messages = [message]
parent_invocation.provider = "test-provider"
# Perform things here, calling a tool, processing, etc.
with self.telemetry_handler.llm() as child_invocation:
child_invocation.request_model = "child-model"
child_invocation.input_messages = [message]
child_invocation.provider = "test-provider"
# Perform things here, calling a tool, processing, etc.
# Stop child first by exiting inner context
child_invocation.output_messages = [chat_generation]
# Then stop parent by exiting outer context
Expand Down
Loading