Skip to content

Conversation

@fractalwrench
Copy link
Member

@fractalwrench fractalwrench commented Jan 16, 2026

Goal

Adds syntactic sugar to make it easier for library consumers to wrap operations with a Span. This works by providing a higher-order function that ends the span after its execution is ended.

This has the added bonus of:

  1. Requiring a status to be set when ending the span
  2. Automatically recording exceptions as events when they happen in the operation
  3. Automatically setting the status to Error if the operation throws an exception

Originally the approach suggested AutoCloseable but I feel this approach is preferable given that it's now in an opt-in module of syntactic enhancements.

Closes #4

Testing

Added unit tests.

@fractalwrench fractalwrench requested a review from a team as a code owner January 16, 2026 12:33
@codecov
Copy link

codecov bot commented Jan 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.61%. Comparing base (d1dfece) to head (60ff6a7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
+ Coverage   90.60%   90.61%   +0.01%     
==========================================
  Files         215      215              
  Lines        2298     2302       +4     
  Branches      233      233              
==========================================
+ Hits         2082     2086       +4     
  Misses        163      163              
  Partials       53       53              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fractalwrench fractalwrench force-pushed the fw/auto-closeable branch 2 times, most recently from a3cd282 to e2f5bd6 Compare January 19, 2026 10:39
@fractalwrench fractalwrench changed the title Support AutoCloseable on span [DNM] Support AutoCloseable on span Jan 20, 2026
@fractalwrench fractalwrench added the do not merge This PR should not be merged yet label Jan 20, 2026
bidetofevil
bidetofevil previously approved these changes Jan 21, 2026
Copy link

@bidetofevil bidetofevil left a comment

Choose a reason for hiding this comment

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

LGTM with a comment about whether we need to fail a span if we are auto-closing due to an exception/error.

Also added a comment about adding a test that verifies thrown exceptions/errors still lead to the span being closed

@fractalwrench
Copy link
Member Author

There's some discussion on #4 about how opentelemetry-java decided against AutoCloseable due to the syntax making it trickier to set the status when a span completes. I'll hold off merging until the discussion has a path forward.

@fractalwrench fractalwrench changed the title [DNM] Support AutoCloseable on span Add syntactic sugar for wrapping operations with a span Jan 26, 2026
@fractalwrench fractalwrench dismissed bidetofevil’s stale review January 26, 2026 17:09

Review is stale as I've changed approach to use an extension function instead.

@fractalwrench fractalwrench requested review from a team and bidetofevil January 26, 2026 17:10
@fractalwrench fractalwrench removed the do not merge This PR should not be merged yet label Jan 26, 2026
@fractalwrench
Copy link
Member Author

Based on discussion in #4 I've tweaked this changeset to use an extension function instead of AutoCloseable.

Copy link

@bidetofevil bidetofevil left a comment

Choose a reason for hiding this comment

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

Nice. LGTM.

@fractalwrench fractalwrench requested a review from a team January 29, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make SpanModel implement AutoCloseable

2 participants