chore: refactor use_span to be close automatically
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).
- It'll record the exception automatically
- 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
- [x] I ran
hatch run prepare
Checklist
- [x] I have read the CONTRIBUTING document
- [x] 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
- [x] My changes generate no new warnings
- [x] 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 Report
:x: 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 :warning: |
| src/strands/telemetry/tracer.py | 60.00% | 1 Missing and 1 partial :warning: |
:loudspeaker: Thoughts on this report? Let us know!