Skip to content

Conversation

@poshinchen
Copy link
Contributor

Description

Agentcore with strands sometimes see the warning message of Tried calling _add_event on an ended spanTried calling _add_event on an ended span.

In our current code base, we control the lifecycle of the spans, which sometimes could go wrong. Therefore, this PR makes the change to let OTEL handle the lifecycle in the use_span (end_on_exit=True).

  1. It'll record the exception automatically
  2. It'll end the span automatically when exiting the function

Related Issues

Issue on Agentcore

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link

codecov bot commented Dec 5, 2025

Codecov Report

❌ Patch coverage is 91.83673% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/strands/event_loop/event_loop.py 95.45% 1 Missing and 1 partial ⚠️
src/strands/telemetry/tracer.py 60.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@poshinchen poshinchen requested a review from a team December 5, 2025 16:00
@poshinchen poshinchen self-assigned this Dec 5, 2025
@poshinchen poshinchen marked this pull request as ready for review December 5, 2025 16:01
Copy link
Member

@zastrowm zastrowm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with the intent of the changes, but have some nit callouts about the docs/code

) -> None:
"""End an event loop cycle span with results.
Note: When using with trace_api.use_span(end_on_exit=True), the span will be automatically
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're forcing callers to close this now, right? If so, document that instead of When using with trace_api.use_span(end_on_exit=True), as that implies they can do this, but we're saying You must do this or close it explicitly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTEL will end the span automatically when exiting the function (regardless of success or exception being thrown). Previously we ended the span and recorded the exception by ourselves in our tracer setup in the tracer.end_xxx method. This is not ideal as OTEL can record the exception by itself.

message: The message response from this cycle.
tool_result_message: Optional tool result message if a tool was called.
error: Optional exception if the cycle failed.
error: Optional exception if the cycle failed (not used when end_on_exit=True).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is end_on_exit not True?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove this! It should be always True.

self._add_event(span, "gen_ai.choice", event_attributes=event_attributes)
self._end_span(span, attributes, error)

# Note: self._end_span() is commented out because when using trace_api.use_span(end_on_exit=True),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callers have no control over this; we can leave an explaining comment saying "We don't use self._end_span() because callers are responsible via closing"

But don't leave this commented out, just remove it

},
)
mock_span.set_status.assert_called_once_with(StatusCode.OK)
mock_span.end.assert_called_once()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be asserting that it was closed correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not closed in our tracer anymore, but I can add validation in other places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants