Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

erichmachado
Copy link

@erichmachado erichmachado commented Dec 9, 2024

This PR aims to introduce support for Regexp patterns to the OpenTelemetry::Instrumentation::ActiveSupport.subscribe method.

This should be in line with a similar feature available on ActiveSupport::Notifications.subscribe.

With this update, users will be able to:

  1. Subscribe to multiple events at once, if desired.
  2. Use the Regexp pattern directly without having to resort to a custom span_name_formatter to work around crashes when processing the event name.
  3. Process the incoming event name if span_name_formatter is set, since the name parameter in the initializer includes a reference to the Regexp pattern in this case.

Copy link

linux-foundation-easycla bot commented Dec 9, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

it 'does not trace an event by default' do
ActiveSupport::Notifications.subscribe(notification_name) do
# pass
describe 'when subscribing to events directly' do
Copy link
Author

Choose a reason for hiding this comment

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

The tests in this context are the same as before, I've just added one level of indentation.

Comment on lines 209 to 211
ActiveSupport::Notifications.notifier.all_listeners_for(notification_name).each do |listener|
ActiveSupport::Notifications.unsubscribe(listener)
end
Copy link
Author

Choose a reason for hiding this comment

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

This prevents subscriptions from leaking across tests. Just using ActiveSupport::Notifications.unsubscribe with a Regexp won't work as intended.

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
Copy link
Author

Choose a reason for hiding this comment

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

This context contains the same tests as above, but exercising the pattern subscription.

@@ -137,11 +138,20 @@ def sanitized_value(value)
end
end

def span_name(name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you meant now. Duping the string will now result in a separate string allocation and an additional case statement per subscription call. The difference in use between the regexp matcher and string based subscriber is only for span names right?

I think maybe it would be best in this case then to use the span name found on the payload and then keep the name instance variable as more as a indexing value that we can use to unsubscribe registrations or inspect the object to know what it's subscribed to but not use it for span naming. Does that help?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your review @arielvalentin!

I think maybe it would be best in this case then to use the span name found on the payload and then keep the name instance variable as more as a indexing value that we can use to unsubscribe registrations or inspect the object to know what it's subscribed to but not use it for span naming. Does that help?

It does help. It's also a behavioural change, meaning this test will break:

it 'memoizes the span name' do
span, = subscriber.start('oh.hai', 'abc', {})
_(span.name).must_equal(notification_name)
end

Which is the behaviour I was trying to preserve there. In this case, would it make sense to just remove that test?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that should be ok.

That test is a bit strange because I was guarding against the very unlikely bug that AS Notifications to invoke the subscriber with an event that it was not subscribed to.

You can probably delete it.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you! I've updated that on b8c0fca. Can you please check if this is what you were looking for?

@erichmachado erichmachado force-pushed the em-feat-active-support-instrumentation-regexp-subscription branch 2 times, most recently from 6cf922f to 9791e56 Compare December 10, 2024 23:35
@erichmachado erichmachado force-pushed the em-feat-active-support-instrumentation-regexp-subscription branch from 9791e56 to 835edc3 Compare December 10, 2024 23:40
Comment on lines 398 to 434
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
Copy link
Author

Choose a reason for hiding this comment

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

I had to skip these two tests on Active Support 6.1 since there's a bug in this version that prevents the subscriber from properly finishing the spans if another subscriber raises an error.

See rails/rails#43282 for further details.

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.

2 participants