-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Enable support for Regexp
patterns when subscribing to Active Support's instrumentation Events
#1296
base: main
Are you sure you want to change the base?
feat: Enable support for Regexp
patterns when subscribing to Active Support's instrumentation Events
#1296
Changes from 5 commits
b1d707f
b8c0fca
835edc3
f541dfb
95223a8
ae89d18
ed978b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,6 @@ def finish(name, id, payload) | |
instrumentation.install({}) | ||
end | ||
|
||
it 'memoizes the span name' do | ||
span, = subscriber.start('oh.hai', 'abc', {}) | ||
_(span.name).must_equal(notification_name) | ||
end | ||
|
||
it 'uses the provided tracer' do | ||
subscriber = OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber.new( | ||
name: 'oh.hai', | ||
|
@@ -205,123 +200,238 @@ def finish(name, id, payload) | |
end | ||
|
||
describe 'instrument' do | ||
before do | ||
ActiveSupport::Notifications.unsubscribe(notification_name) | ||
after do | ||
ActiveSupport::Notifications.notifier.listeners_for(notification_name).each do |listener| | ||
ActiveSupport::Notifications.unsubscribe(listener) | ||
end | ||
end | ||
|
||
it 'does not trace an event by default' do | ||
ActiveSupport::Notifications.subscribe(notification_name) do | ||
# pass | ||
describe 'when subscribing to events directly' do | ||
arielvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it 'does not trace an event by default' do | ||
ActiveSupport::Notifications.subscribe(notification_name) do | ||
# pass | ||
end | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
_(last_span).must_be_nil | ||
end | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
_(last_span).must_be_nil | ||
end | ||
|
||
it 'traces an event when a span subscriber is used' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
it 'traces an event when a span subscriber is used' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(last_span.kind).must_equal(:internal) | ||
end | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(last_span.kind).must_equal(:internal) | ||
end | ||
|
||
describe 'when using a custom span name formatter' do | ||
describe 'when using the LEGACY_NAME_FORMATTER' do | ||
let(:span_name_formatter) { OpenTelemetry::Instrumentation::ActiveSupport::LEGACY_NAME_FORMATTER } | ||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
describe 'when using a custom span name formatter' do | ||
describe 'when using the LEGACY_NAME_FORMATTER' do | ||
let(:span_name_formatter) { OpenTelemetry::Instrumentation::ActiveSupport::LEGACY_NAME_FORMATTER } | ||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('foo bar') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
describe 'when using a custom formatter' do | ||
let(:span_name_formatter) { ->(name) { "custom.#{name}" } } | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('foo bar') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('custom.bar.foo') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
end | ||
|
||
describe 'when using a custom formatter' do | ||
let(:span_name_formatter) { ->(name) { "custom.#{name}" } } | ||
describe 'when using a invalid formatter' do | ||
it 'defaults to the notification name' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: ->(_) {}) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('custom.bar.foo') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
describe 'when using a unstable formatter' do | ||
it 'defaults to the notification name' do | ||
allow(OpenTelemetry).to receive(:handle_error).with(exception: RuntimeError, message: String) | ||
|
||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: ->(_) { raise 'boom' }) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
end | ||
|
||
describe 'when using a invalid formatter' do | ||
it 'defaults to the notification name' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: ->(_) {}) | ||
it 'finishes spans even when block subscribers blow up' do | ||
ActiveSupport::Notifications.subscribe(notification_name) { raise 'boom' } | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
|
||
expect do | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
|
||
describe 'when using a unstable formatter' do | ||
it 'defaults to the notification name' do | ||
allow(OpenTelemetry).to receive(:handle_error).with(exception: RuntimeError, message: String) | ||
it 'finishes spans even when complex subscribers blow up' do | ||
ActiveSupport::Notifications.subscribe(notification_name, CrashingEndSubscriber.new) | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
|
||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name, nil, nil, span_name_formatter: ->(_) { raise 'boom' }) | ||
expect do | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
it 'finishes spans even when block subscribers blow up' do | ||
ActiveSupport::Notifications.subscribe(notification_name) { raise 'boom' } | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
it 'supports unsubscribe' do | ||
obj = OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
ActiveSupport::Notifications.unsubscribe(obj) | ||
|
||
expect do | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(obj.class).must_equal(ActiveSupport::Notifications::Fanout::Subscribers::Evented) | ||
_(last_span).must_be_nil | ||
end | ||
|
||
it 'supports setting the span kind' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, 'bar.foo', nil, [], kind: :client) | ||
ActiveSupport::Notifications.instrument('bar.foo', extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('bar.foo') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(last_span.kind).must_equal(:client) | ||
end | ||
end | ||
|
||
it 'finishes spans even when complex subscribers blow up' do | ||
ActiveSupport::Notifications.subscribe(notification_name, CrashingEndSubscriber.new) | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
describe 'when subscribing to events matching a regular expression' do | ||
arielvalentin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let(:notification_pattern) { /.*\.foo/ } | ||
|
||
expect do | ||
it 'does not trace an event by default' do | ||
ActiveSupport::Notifications.subscribe(notification_pattern) do | ||
# pass | ||
end | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
_(last_span).must_be_nil | ||
end | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
it 'traces an event when a span subscriber is used' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
it 'supports unsubscribe' do | ||
obj = OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_name) | ||
ActiveSupport::Notifications.unsubscribe(obj) | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(last_span.kind).must_equal(:internal) | ||
end | ||
|
||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
describe 'when using a custom span name formatter' do | ||
describe 'when using the LEGACY_NAME_FORMATTER' do | ||
let(:span_name_formatter) { OpenTelemetry::Instrumentation::ActiveSupport::LEGACY_NAME_FORMATTER } | ||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('foo bar') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
_(obj.class).must_equal(ActiveSupport::Notifications::Fanout::Subscribers::Evented) | ||
_(last_span).must_be_nil | ||
end | ||
describe 'when using a custom formatter' do | ||
let(:span_name_formatter) { ->(name) { "custom.#{name}" } } | ||
|
||
it 'supports setting the span kind' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, 'bar.foo', nil, [], kind: :client) | ||
ActiveSupport::Notifications.instrument('bar.foo', extra: 'context') | ||
it 'uses the user supplied formatter' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern, nil, nil, span_name_formatter: span_name_formatter) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
Comment on lines
+363
to
+365
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please confirm that the indentation changes are using spaces and not tabs. There are a few other sections where it looks like indentation changed and I want to make sure that you are using spaces there as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've double checked it here and it's all spaces. 👍 |
||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('bar.foo') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
_(last_span.kind).must_equal(:client) | ||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal('custom.bar.foo') | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
describe 'when using a invalid formatter' do | ||
it 'defaults to the notification name' do | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern, nil, nil, span_name_formatter: ->(_) {}) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
|
||
describe 'when using a unstable formatter' do | ||
it 'defaults to the notification name' do | ||
allow(OpenTelemetry).to receive(:handle_error).with(exception: RuntimeError, message: String) | ||
|
||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern, nil, nil, span_name_formatter: ->(_) { raise 'boom' }) | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
end | ||
|
||
it 'finishes spans even when block subscribers blow up' do | ||
# This scenario cannot be exercised reliably on Active Support < 7.0 since the #finish method | ||
# will never be called by the notifier if another subscriber raises an error. | ||
# | ||
# See this PR for additional details: https://github.com/rails/rails/pull/43282 | ||
skip 'Notifications will be broken in this scenario on Active Support < 7.0' if ActiveSupport.version < Gem::Version.new('7.0') | ||
|
||
ActiveSupport::Notifications.subscribe(notification_pattern) { raise 'boom' } | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern) | ||
|
||
expect do | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
|
||
it 'finishes spans even when complex subscribers blow up' do | ||
# This scenario cannot be exercised reliably on Active Support < 7.0 since the #finish method | ||
# will never be called by the notifier if another subscriber raises an error. | ||
# | ||
# See this PR for additional details: https://github.com/rails/rails/pull/43282 | ||
skip 'Notifications will be broken in this scenario on Active Support < 7.0' if ActiveSupport.version < Gem::Version.new('7.0') | ||
|
||
ActiveSupport::Notifications.subscribe(notification_pattern, CrashingEndSubscriber.new) | ||
OpenTelemetry::Instrumentation::ActiveSupport.subscribe(tracer, notification_pattern) | ||
|
||
expect do | ||
ActiveSupport::Notifications.instrument(notification_name, extra: 'context') | ||
end.must_raise RuntimeError | ||
|
||
_(last_span).wont_be_nil | ||
_(last_span.name).must_equal(notification_name) | ||
_(last_span.attributes['extra']).must_equal('context') | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename this attribute to
pattern
now that it will match the argument passed into AS notification subscribe. This will help us disambiguate the shadowedname
parameter passed into thestart
andfinish
methods as well.https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html#method-c-subscribe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the attribute name to
@pattern
on ae89d18 @arielvalentin, as requested.I didn't touch the
name
parameter to avoid disrupting the interface. Let me know if you want this to be renamed as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please. This Subscriber is part of the internal API of this gem and i don't think it is used externally at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for confirming. It should be fixed on ed978b2.