Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
Meters,
SpanAttributes,
)
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer, set_span_in_context
from opentelemetry.trace.status import Status, StatusCode
from typing_extensions import Coroutine
from wrapt import wrap_function_wrapper
Expand Down Expand Up @@ -564,6 +564,7 @@ def _wrap(
_handle_input(span, event_logger, kwargs)

start_time = time.time()
ctx_token = context_api.attach(set_span_in_context(span))
try:
response = wrapped(*args, **kwargs)
except Exception as e: # pylint: disable=broad-except
Expand All @@ -582,6 +583,8 @@ def _wrap(
span.set_status(Status(StatusCode.ERROR, str(e)))
span.end()
raise
finally:
context_api.detach(ctx_token)

end_time = time.time()

Expand Down Expand Up @@ -696,6 +699,7 @@ async def _awrap(
await _ahandle_input(span, event_logger, kwargs)

start_time = time.time()
ctx_token = context_api.attach(set_span_in_context(span))
try:
response = await wrapped(*args, **kwargs)
except Exception as e: # pylint: disable=broad-except
Expand All @@ -714,6 +718,8 @@ async def _awrap(
span.set_status(Status(StatusCode.ERROR, str(e)))
span.end()
raise
finally:
context_api.detach(ctx_token)

if is_streaming_response(response):
return AnthropicAsyncStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY,
Meters,
)
from opentelemetry.trace import Span, SpanKind, get_tracer
from opentelemetry.trace import Span, SpanKind, get_tracer, set_span_in_context
from opentelemetry.trace.status import Status, StatusCode
from wrapt import wrap_function_wrapper

Expand Down Expand Up @@ -336,6 +336,7 @@ def with_instrumentation(*args, **kwargs):
kind=SpanKind.CLIENT,
attributes=span_attributes,
)
ctx_token = context_api.attach(set_span_in_context(span))
try:
response = fn(*args, **kwargs)
except Exception as e:
Expand All @@ -344,6 +345,8 @@ def with_instrumentation(*args, **kwargs):
span.set_status(Status(StatusCode.ERROR, str(e)))
span.end()
raise
finally:
context_api.detach(ctx_token)
if span.is_recording():
_handle_converse_stream(span, kwargs, response, metric_params, event_logger)

Expand Down