Skip to content

Commit d371065

Browse files
committed
Simplify ContextManager stuff
1 parent e4094ab commit d371065

5 files changed

Lines changed: 3166 additions & 3278 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-anthropic/src/opentelemetry/instrumentation/anthropic/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _create_invocation(
114114
)
115115

116116
server_address, server_port = get_server_address_and_port(instance)
117-
invocation = handler.start_inference(
117+
invocation = handler.inference(
118118
provider=ANTHROPIC,
119119
request_model=request_model,
120120
server_address=server_address,

util/opentelemetry-util-genai/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Apply attribute for sampling on instantiation of all invocation types.
1414
([#4553](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4553))
1515
- Minor code cleanup and changes in preparation of moving google's GenAI instrumentation
16-
library to use this util library ([#4556](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4556))
16+
library to use this util library ([#4556](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4556), [#4570](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4570))
1717

1818
## Version 0.4b0 (2026-05-01)
1919

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import timeit
77
from abc import ABC, abstractmethod
8-
from contextlib import contextmanager
98
from contextvars import Token
109
from dataclasses import asdict
11-
from typing import TYPE_CHECKING, Any, Iterator, Sequence
10+
from types import TracebackType
11+
from typing import TYPE_CHECKING, Any, Sequence
1212

13-
from typing_extensions import Self, TypeAlias
13+
from typing_extensions import TypeAlias
1414

1515
from opentelemetry._logs import Logger, LogRecord
1616
from opentelemetry.context import Context, attach, detach
@@ -138,6 +138,20 @@ def _call_completion_hook(
138138
log_record=log_record,
139139
)
140140

141+
def __enter__(self):
142+
return self
143+
144+
def __exit__(
145+
self,
146+
type_: type[BaseException] | None,
147+
value: BaseException | None,
148+
traceback: TracebackType | None,
149+
) -> None:
150+
if value:
151+
self.fail(value)
152+
raise
153+
self.stop()
154+
141155
@abstractmethod
142156
def _apply_finish(self, error: Error | None = None) -> None:
143157
"""Apply finish telemetry (attributes, metrics, events)."""
@@ -165,16 +179,6 @@ def fail(self, error: Error | BaseException) -> None:
165179
error = Error(type=type(error), message=str(error))
166180
self._finish(error)
167181

168-
@contextmanager
169-
def _managed(self) -> Iterator[Self]:
170-
"""Context manager that calls stop() on success or fail() on exception."""
171-
try:
172-
yield self
173-
except Exception as exc:
174-
self.fail(exc)
175-
raise
176-
self.stop()
177-
178182

179183
def get_content_attributes(
180184
*,

0 commit comments

Comments
 (0)