Skip to content

Commit 93ecfc1

Browse files
keith-deckerwrisaxrmxlukeina2ztammy-baylis-swi
authored
E2e inference merge (#15)
* cherry pick changes from previous PR * move span utils to new file * remove span state, use otel context for parent/child * flatten LLMInvocation to use attributes instead of dict keys * helper function and docstrings * refactor: store span and context token in LLMInvocation instead of SpanGenerator * refactor: rename prompts/chat_generations to input_messages/output_messages for clarity * refactor: simplify TelemetryHandler API by moving invocation data management to LLMInvocation class * refactor: update relative imports to absolute imports * Update handler to use a context manager instead of start_llm and stop_llm * resolve tox -e doc failure * safeguard against empty request-model * fix tox typecheck errors for utils * refactor: move tracer to generator, clean up dead code * remove unused linting hint * back off stricter request-model requirements * reintroduce manual start/stop for langchain callback flow * Fix typecheck in langchain instrumentation (open-telemetry#3773) * fix typecheck * fix ruff and added changelog * added lambda list * Update instrumentation-genai/opentelemetry-instrumentation-langchain/CHANGELOG.md --------- Co-authored-by: Riccardo Magliocchetti <[email protected]> * botocore: Add support for AWS Secrets Manager semantic convention attribute (open-telemetry#3765) * botocore: Add support for AWS Secrets Manager semantic convention attribute AWS Secrets Manager defines semantic convention attribute: AWS_SECRETSMANAGER_SECRET_ARN: Final = "aws.secretsmanager.secret.arn" https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-secrets-manager-attributes Currently, this attribute is not set in the botocore instrumentation library. This PR adds support for them by extracting values from both Request and Response objects. Tests Added new unit tests (passing). Verified with: tox -e py312-test-instrumentation-botocore tox -e spellcheck tox -e lint-instrumentation-botocore tox -e ruff Backward Compatibility This change is backward compatible. It only adds instrumentation for additional AWS resources and does not modify existing behavior in the auto-instrumentation library. * add ChangeLog. * Update instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/secretsmanager.py Co-authored-by: Tammy Baylis <[email protected]> * Update instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_secretsmanager.py --------- Co-authored-by: Tammy Baylis <[email protected]> Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]> * clean up context handler, clarify unit tests * remove generator concept --------- Co-authored-by: wrisa <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]> Co-authored-by: Luke (GuangHui) Zhang <[email protected]> Co-authored-by: Tammy Baylis <[email protected]> Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Aaron Abbott <[email protected]>
1 parent 1c57ab7 commit 93ecfc1

File tree

8 files changed

+218
-32
lines changed

8 files changed

+218
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
### Fixed
15+
16+
### Added
17+
- `opentelemetry-instrumentation`: botocore: Add support for AWS Secrets Manager semantic convention attribute
18+
([#3765](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3765))
19+
1420
## Version 1.37.0/0.58b0 (2025-09-11)
1521

1622
### Fixed

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/span_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
@dataclass
3232
class _SpanState:
3333
span: Span
34-
children: List[UUID] = field(default_factory=list)
34+
children: List[UUID] = field(default_factory=lambda: list())
3535

3636

3737
class _SpanManager:

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def loader():
3535
"bedrock-runtime": _lazy_load(".bedrock", "_BedrockRuntimeExtension"),
3636
"dynamodb": _lazy_load(".dynamodb", "_DynamoDbExtension"),
3737
"lambda": _lazy_load(".lmbd", "_LambdaExtension"),
38+
"secretsmanager": _lazy_load(
39+
".secretsmanager", "_SecretsManagerExtension"
40+
),
3841
"stepfunctions": _lazy_load(".sfns", "_StepFunctionsExtension"),
3942
"sns": _lazy_load(".sns", "_SnsExtension"),
4043
"sqs": _lazy_load(".sqs", "_SqsExtension"),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from opentelemetry.instrumentation.botocore.extensions.types import (
15+
_AttributeMapT,
16+
_AwsSdkExtension,
17+
_BotocoreInstrumentorContext,
18+
_BotoResultT,
19+
)
20+
from opentelemetry.semconv._incubating.attributes.aws_attributes import (
21+
AWS_SECRETSMANAGER_SECRET_ARN,
22+
)
23+
from opentelemetry.trace.span import Span
24+
25+
26+
class _SecretsManagerExtension(_AwsSdkExtension):
27+
def extract_attributes(self, attributes: _AttributeMapT):
28+
"""
29+
SecretId is extracted if a secret ARN, the function extracts the attribute
30+
only if the SecretId parameter is provided as an arn which starts with
31+
`arn:aws:secretsmanager:`
32+
"""
33+
secret_id = self._call_context.params.get("SecretId")
34+
if secret_id and secret_id.startswith("arn:aws:secretsmanager:"):
35+
attributes[AWS_SECRETSMANAGER_SECRET_ARN] = secret_id
36+
37+
def on_success(
38+
self,
39+
span: Span,
40+
result: _BotoResultT,
41+
instrumentor_context: _BotocoreInstrumentorContext,
42+
):
43+
secret_arn = result.get("ARN")
44+
if secret_arn:
45+
span.set_attribute(AWS_SECRETSMANAGER_SECRET_ARN, secret_arn)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import botocore.session
16+
from moto import mock_aws
17+
18+
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
19+
from opentelemetry.semconv._incubating.attributes.aws_attributes import (
20+
AWS_SECRETSMANAGER_SECRET_ARN,
21+
)
22+
from opentelemetry.test.test_base import TestBase
23+
24+
25+
class TestSecretsManagerExtension(TestBase):
26+
def setUp(self):
27+
super().setUp()
28+
BotocoreInstrumentor().instrument()
29+
session = botocore.session.get_session()
30+
session.set_credentials(
31+
access_key="access-key", secret_key="secret-key"
32+
)
33+
self.region = "us-west-2"
34+
self.client = session.create_client(
35+
"secretsmanager", region_name=self.region
36+
)
37+
38+
def tearDown(self):
39+
super().tearDown()
40+
BotocoreInstrumentor().uninstrument()
41+
42+
def create_secret_and_get_arn(self, name: str = "test-secret") -> str:
43+
"""
44+
Create a secret in mocked Secrets Manager and return its ARN.
45+
"""
46+
# Clear spans before creating secret for helper method
47+
self.memory_exporter.clear()
48+
response = self.client.create_secret(
49+
Name=name, SecretString="test-secret-value"
50+
)
51+
return response["ARN"]
52+
53+
@mock_aws
54+
def test_tag_resource_with_arn(self):
55+
secret_arn = self.create_secret_and_get_arn()
56+
57+
self.client.tag_resource(
58+
SecretId=secret_arn, Tags=[{"Key": "Environment", "Value": "Test"}]
59+
)
60+
61+
spans = self.memory_exporter.get_finished_spans()
62+
assert spans
63+
self.assertEqual(len(spans), 2)
64+
span = spans[1] # tag_resource span
65+
self.assertEqual(
66+
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN],
67+
secret_arn,
68+
)
69+
70+
@mock_aws
71+
def test_create_secret(self):
72+
secret_name = "test-secret"
73+
response = self.client.create_secret(
74+
Name=secret_name, SecretString="test-secret-value"
75+
)
76+
secret_arn = response["ARN"]
77+
78+
spans = self.memory_exporter.get_finished_spans()
79+
assert spans
80+
self.assertEqual(len(spans), 1)
81+
span = spans[0] # create_secret span
82+
# Should capture ARN from response
83+
self.assertEqual(
84+
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN],
85+
secret_arn,
86+
)

util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,24 @@
6262
from contextlib import contextmanager
6363
from typing import Any, Iterator, Optional
6464

65-
from opentelemetry.util.genai.generators import SpanGenerator
65+
from opentelemetry import context as otel_context
66+
from opentelemetry import trace
67+
from opentelemetry.semconv._incubating.attributes import (
68+
gen_ai_attributes as GenAI,
69+
)
70+
from opentelemetry.semconv.schemas import Schemas
71+
from opentelemetry.trace import (
72+
SpanKind,
73+
Tracer,
74+
get_tracer,
75+
set_span_in_context,
76+
)
77+
from opentelemetry.util.genai.span_utils import (
78+
_apply_error_attributes,
79+
_apply_finish_attributes,
80+
)
6681
from opentelemetry.util.genai.types import Error, LLMInvocation
82+
from opentelemetry.util.genai.version import __version__
6783

6884

6985
class TelemetryHandler:
@@ -73,32 +89,63 @@ class TelemetryHandler:
7389
"""
7490

7591
def __init__(self, **kwargs: Any):
76-
self._generator = SpanGenerator(**kwargs)
92+
tracer_provider = kwargs.get("tracer_provider")
93+
tracer = get_tracer(
94+
__name__,
95+
__version__,
96+
tracer_provider,
97+
schema_url=Schemas.V1_36_0.value,
98+
)
99+
self._tracer: Tracer = tracer or trace.get_tracer(__name__)
77100

78101
def start_llm(
79102
self,
80103
invocation: LLMInvocation,
81104
) -> LLMInvocation:
82105
"""Start an LLM invocation and create a pending span entry."""
83-
self._generator.start(invocation)
106+
# Create a span and attach it as current; keep the token to detach later
107+
span = self._tracer.start_span(
108+
name=f"{GenAI.GenAiOperationNameValues.CHAT.value} {invocation.request_model}",
109+
kind=SpanKind.CLIENT,
110+
)
111+
invocation.span = span
112+
invocation.context_token = otel_context.attach(
113+
set_span_in_context(span)
114+
)
84115
return invocation
85116

86-
def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation:
117+
def stop_llm(self, invocation: LLMInvocation) -> LLMInvocation: # pylint: disable=no-self-use
87118
"""Finalize an LLM invocation successfully and end its span."""
88119
invocation.end_time = time.time()
89-
self._generator.finish(invocation)
120+
if invocation.context_token is None or invocation.span is None:
121+
# TODO: Provide feedback that this invocation was not started
122+
return invocation
123+
124+
_apply_finish_attributes(invocation.span, invocation)
125+
# Detach context and end span
126+
otel_context.detach(invocation.context_token)
127+
invocation.span.end()
90128
return invocation
91129

92-
def fail_llm(
130+
def fail_llm( # pylint: disable=no-self-use
93131
self, invocation: LLMInvocation, error: Error
94132
) -> LLMInvocation:
95133
"""Fail an LLM invocation and end its span with error status."""
96134
invocation.end_time = time.time()
97-
self._generator.error(error, invocation)
135+
if invocation.context_token is None or invocation.span is None:
136+
# TODO: Provide feedback that this invocation was not started
137+
return invocation
138+
139+
_apply_error_attributes(invocation.span, error)
140+
# Detach context and end span
141+
otel_context.detach(invocation.context_token)
142+
invocation.span.end()
98143
return invocation
99144

100145
@contextmanager
101-
def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
146+
def llm(
147+
self, invocation: Optional[LLMInvocation] = None
148+
) -> Iterator[LLMInvocation]:
102149
"""Context manager for LLM invocations.
103150
104151
Only set data attributes on the invocation object, do not modify the span or context.
@@ -107,6 +154,10 @@ def llm(self, invocation: LLMInvocation) -> Iterator[LLMInvocation]:
107154
If an exception occurs inside the context, marks the span as error, ends it, and
108155
re-raises the original exception.
109156
"""
157+
if invocation is None:
158+
invocation = LLMInvocation(
159+
request_model="",
160+
)
110161
self.start_llm(invocation)
111162
try:
112163
yield invocation

util/opentelemetry-util-genai/src/opentelemetry/util/genai/span_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def _apply_common_span_attributes(
4848
"""
4949
request_model = invocation.request_model
5050
provider = invocation.provider
51-
51+
span.update_name(
52+
f"{GenAI.GenAiOperationNameValues.CHAT.value} {request_model}"
53+
)
5254
span.set_attribute(
5355
GenAI.GEN_AI_OPERATION_NAME, GenAI.GenAiOperationNameValues.CHAT.value
5456
)

util/opentelemetry-util-genai/tests/test_utils.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,11 @@ def test_llm_start_and_stop_creates_span(self): # pylint: disable=no-self-use
135135
)
136136

137137
# Start and stop LLM invocation using context manager
138-
invocation = LLMInvocation(
139-
request_model="test-model",
140-
input_messages=[message],
141-
provider="test-provider",
142-
attributes={"custom_attr": "value"},
143-
)
144-
145-
with self.telemetry_handler.llm(invocation):
138+
with self.telemetry_handler.llm() as invocation:
139+
invocation.request_model = "test-model"
140+
invocation.input_messages = [message]
141+
invocation.provider = "test-provider"
142+
invocation.attributes = {"custom_attr": "value"}
146143
assert invocation.span is not None
147144
invocation.output_messages = [chat_generation]
148145
invocation.attributes.update({"extra": "info"})
@@ -234,20 +231,16 @@ def test_parent_child_span_relationship(self):
234231
role="AI", parts=[Text(content="ok")], finish_reason="stop"
235232
)
236233

237-
# Start parent and child using nested contexts (child becomes child span of parent)
238-
parent_invocation = LLMInvocation(
239-
request_model="parent-model",
240-
input_messages=[message],
241-
provider="test-provider",
242-
)
243-
child_invocation = LLMInvocation(
244-
request_model="child-model",
245-
input_messages=[message],
246-
provider="test-provider",
247-
)
248-
249-
with self.telemetry_handler.llm(parent_invocation):
250-
with self.telemetry_handler.llm(child_invocation):
234+
with self.telemetry_handler.llm() as parent_invocation:
235+
parent_invocation.request_model = "parent-model"
236+
parent_invocation.input_messages = [message]
237+
parent_invocation.provider = "test-provider"
238+
# Perform things here, calling a tool, processing, etc.
239+
with self.telemetry_handler.llm() as child_invocation:
240+
child_invocation.request_model = "child-model"
241+
child_invocation.input_messages = [message]
242+
child_invocation.provider = "test-provider"
243+
# Perform things here, calling a tool, processing, etc.
251244
# Stop child first by exiting inner context
252245
child_invocation.output_messages = [chat_generation]
253246
# Then stop parent by exiting outer context

0 commit comments

Comments
 (0)