Skip to content

Add Sentry.with_child_span for easier span recording#1783

Merged
st0012 merged 2 commits intomasterfrom
add-sentry-with-child-span
Apr 8, 2022
Merged

Add Sentry.with_child_span for easier span recording#1783
st0012 merged 2 commits intomasterfrom
add-sentry-with-child-span

Conversation

@st0012
Copy link
Copy Markdown
Contributor

@st0012 st0012 commented Apr 4, 2022

This PR adds a new Sentry.with_child_span API. It'll allow users to record a child span with a block without the need to getting the current span from scope, checking its presence...etc. A simple block wrapping will do the trick:

Sentry.with_child_span(op: "my op") do |child_span|
  # my operation
  # operation result will be the return value
end

Note that if there's no span in the current scope, the block will still be executed. But the yield child_span will be nil instead.

With this API, it's also easier to fix the child span nesting issue reported in #1723.

@st0012 st0012 added this to the 5.3.0 milestone Apr 4, 2022
@st0012 st0012 self-assigned this Apr 4, 2022
@st0012 st0012 requested a review from sl0thentr0py April 4, 2022 20:37
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 4, 2022

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.40%. Comparing base (fa46e86) to head (4af72c0).
⚠️ Report is 561 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1783      +/-   ##
==========================================
- Coverage   98.40%   98.40%   -0.01%     
==========================================
  Files         145      145              
  Lines        8480     8536      +56     
==========================================
+ Hits         8345     8400      +55     
- Misses        135      136       +1     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@st0012
Copy link
Copy Markdown
Contributor Author

st0012 commented Apr 7, 2022

ping @sl0thentr0py


begin
current_span.with_child_span(**attributes) do |child_span|
get_current_scope.set_span(child_span)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just an observation, all the current usages of set_span set it to the transaction and we don't really use scope#get_span either so this will be the first time the span on the scope gets used right?

➜  sentry-ruby git:(master) ✗ ag --ignore 'spec' set_span
sentry-rails/lib/sentry/rails/action_cable.rb
18:              scope.set_span(transaction) if transaction

sentry-rails/lib/sentry/rails/active_job.rb
31:                scope.set_span(transaction) if transaction

sentry-resque/lib/sentry/resque.rb
28:              scope.set_span(transaction) if transaction

sentry-delayed_job/lib/sentry/delayed_job/plugin.rb
22:            scope.set_span(transaction) if transaction

sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb
21:        scope.set_span(transaction) if transaction

sentry-ruby/lib/sentry/scope.rb
132:    def set_span(span)

sentry-ruby/lib/sentry/rack/capture_exceptions.rb
23:            scope.set_span(transaction) if transaction

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, because most the usages in the SDK are to create the top-level transaction. But as described in #1784, sentry-rails's subscriber should use spans instead. And users will generally use get_span and set_span for spans more than transactions.

Copy link
Copy Markdown
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

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

sorry for the delay, busy week :/

can you maybe add an integration test too where you add a child span with this new api in an existing request/response cycle transaction?

@st0012 st0012 force-pushed the add-sentry-with-child-span branch from 0807404 to 4af72c0 Compare April 8, 2022 08:28

second_span = transaction.spans.last
expect(second_span[:op]).to eq("second level")
expect(second_span[:parent_span_id]).to eq(first_span[:span_id])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sl0thentr0py I've added the integration test here.

@st0012 st0012 requested a review from sl0thentr0py April 8, 2022 08:33
Copy link
Copy Markdown
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

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

thx!

@st0012 st0012 merged commit a1ea253 into master Apr 8, 2022
@st0012 st0012 deleted the add-sentry-with-child-span branch April 8, 2022 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants