Skip to content

Commit c4fc439

Browse files
authored
[chore] Fix detach failure of context (#112)
1 parent 1c196f0 commit c4fc439

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/src/opentelemetry/instrumentation/dashscope/patch/image_synthesis.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ def wrap_image_synthesis_call(wrapped, instance, args, kwargs, handler=None):
5959
# Create invocation object
6060
invocation = _create_invocation_from_image_synthesis(kwargs, model)
6161

62-
# Start LLM invocation (creates span)
63-
handler.start_llm(invocation)
64-
6562
# In sync call scenario, set a flag in context to skip span creation in async_call and wait
63+
# NOTE: This must be attached BEFORE start_llm to ensure correct LIFO detach order
6664
ctx = context.set_value(_SKIP_INSTRUMENTATION_KEY, True)
6765
token = context.attach(ctx)
6866

67+
# Start LLM invocation (creates span)
68+
handler.start_llm(invocation)
69+
6970
try:
7071
# Execute the wrapped call (internal will call async_call + wait)
7172
result = wrapped(*args, **kwargs)
@@ -83,7 +84,7 @@ def wrap_image_synthesis_call(wrapped, instance, args, kwargs, handler=None):
8384
handler.fail_llm(invocation, error)
8485
raise
8586
finally:
86-
# Restore context
87+
# Restore _SKIP_INSTRUMENTATION_KEY context
8788
if token is not None:
8889
context.detach(token)
8990

instrumentation-loongsuite/loongsuite-instrumentation-dashscope/src/opentelemetry/instrumentation/dashscope/patch/video_synthesis.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ def wrap_video_synthesis_call(wrapped, instance, args, kwargs, handler=None):
5959
# Create invocation object
6060
invocation = _create_invocation_from_video_synthesis(kwargs, model)
6161

62-
# Start LLM invocation (creates span)
63-
handler.start_llm(invocation)
64-
6562
# Set context flag to skip span creation in async_call and wait
63+
# Note: This must be attached BEFORE start_llm to ensure correct LIFO detach order
6664
ctx = context.set_value(_SKIP_INSTRUMENTATION_KEY, True)
6765
token = context.attach(ctx)
6866

67+
# Start LLM invocation (creates span)
68+
handler.start_llm(invocation)
69+
6970
try:
7071
# Execute the wrapped call (internal will call async_call + wait)
7172
result = wrapped(*args, **kwargs)
@@ -83,7 +84,7 @@ def wrap_video_synthesis_call(wrapped, instance, args, kwargs, handler=None):
8384
handler.fail_llm(invocation, error)
8485
raise
8586
finally:
86-
# Restore context
87+
# Restore _SKIP_INSTRUMENTATION_KEY context
8788
if token is not None:
8889
context.detach(token)
8990

0 commit comments

Comments
 (0)