Skip to content

Structured event subscribers - #55690

Merged
gmcgibbon merged 9 commits into
rails:mainfrom
Shopify:ac-structured-event-subscribers
Sep 23, 2025
Merged

gmcgibbon merged 9 commits into
rails:mainfrom
Shopify:ac-structured-event-subscribers

Conversation

@gmcgibbon

@gmcgibbon gmcgibbon commented Sep 17, 2025 •

Copy link
Copy Markdown
Member

Motivation / Background

This Pull Request has been created to add structured events for framework notifications via the use of ActiveSupport::StructuredEventSubscriber subclasses. Events may be emitted with the #emit_event or #emit_debug_event methods like this, similar to a traditional LogSubscriber:

class MyStructuredEventSubscriber < ActiveSupport::StructuredEventSubscriber
  def notification(event)
    emit_event("my.notification", data: event.payload)
  end
end

MyStructuredEventSubscriber.attach_to :namespace

Detail

This Pull Request adds structured event subscribers for each framework library that has log subscribers for notifications. The idea is to mirror structured events for each existing framework log subscriber.

Additional information

Eventually, we would like to unify notification subscribing and emit logs for these structured events (instead of maintaining separate log subscribers), and that will happen in a followup change. For now, this is focused on adding support for events and minimizing the set of changes as to not break anything.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

end

def rails_root # :doc:
@root ||= "#{Rails.try(:root)}/"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This might be a bit undesirable. The log subscriber waits until a logger is set, but the structured event subscriber doesn't need to so things get triggered early enough that Rails sometimes doesn't have root defined yet. Generally, we should not be referencing Rails at all here, and I want to work on injecting the root here in a cleaner way when I consolidate the notification subscribers. But, I can try to to do that earlier if we feel strongly about this being bad.

@gmcgibbon

Copy link
Copy Markdown
Member Author

Looks like there's some CI failures I need to look at in Action View and the base class tests. Will do that ASAP.

@gmcgibbon
gmcgibbon force-pushed the ac-structured-event-subscribers branch 11 times, most recently from 3ad8ba5 to f8b663a Compare September 20, 2025 04:43
exception_message: exception.message,
mail: event.payload[:mail],
)
elsif event.payload[:perform_deliveries]

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.

Why have separate events for what was the same notficiation?
https://edgeguides.rubyonrails.org/active_support_instrumentation.html#deliver-action-mailer

Are we going to publicly document these events, and their payloads (as stable) as their AS::Notification counterparts?

@gmcgibbon gmcgibbon Sep 20, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • We should document these somewhere, yes. I'll look into that. There's no guides page yet. Given that, I think we should add it in a followup patch.
  • We do this in the job subscriber too. The notification is too general, and I think it makes more sense to emit specific events in these cases. I'll think about consolidating them to one event, but I don't want to include an error keys in the payload unless there's an actual error, for example.

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.

I think a new guide next to the AS instrumentation one "Active Support Structured Events" could make sense.

But once we document them, making changes are more difficult, so good to get feedback on the events being added here first. Maybe they could stay experimental and undocumented for now?

The other thing, now we have essentially two places to maintain the same basic "event" from Rails. So if someone wants to add a new field to the payload to sql.active_record (for example) -- they now have to do it in multiple places, which can be a source for confusion and bugs.

How we have CI to tell us if a new config flag is missing from the configuring guide, I always thought it'd be nice to have for notifications as well, that might help.

There is some benefit to keeping the event names and payloads consistent between the two, but I also can appreciate the chance to make them better before people are depending on them. I don't have a strong opinion on this type of change, just wanted to call it out as a question.

Keep up the good work! :)

@gmcgibbon gmcgibbon Sep 23, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changing events in two places is unfortunate, but because notification events can have non-serializable data, and structured events should be serializable, there's likely going to be a translation layer needed for all of these regardless. In terms of the original discussion about the multiple IFs, I will refactor these instances to a singular event being emitted with variable payload content to mirror related notification events.

Thanks again for all your feedback!

@zzak

zzak commented Sep 20, 2025

Copy link
Copy Markdown
Member

What would be the preferred way to turn these off if you don't need them? They seem to be like other LogSubscribers so AS::Subscriber.subscibers. 🤔

Are there any concern of overhead by essentially doubling all the notification events?

Sorry just random thoughts popped into my head looking at this, I think it will take some time to review the events for each framework but it's nice they are all in one PR. Thanks for working on this!

@gmcgibbon

gmcgibbon commented Sep 20, 2025 •

Copy link
Copy Markdown
Member Author

Are there any concern of overhead by essentially doubling all the notification events?

I tried doing this already by disabling method calls for debug mode events with debug_only, but I agree there could be more done. I can try to silence all event method calls if there are no subscribers. That should help reduce overhead in all scenarios. Thanks for the feedback!!

@gmcgibbon
gmcgibbon force-pushed the ac-structured-event-subscribers branch 3 times, most recently from e629953 to 0a3763a Compare September 20, 2025 08:29
@gmcgibbon
gmcgibbon merged commit a8a0d29 into rails:main Sep 23, 2025
@gmcgibbon
gmcgibbon deleted the ac-structured-event-subscribers branch September 23, 2025 18:22
y-yagi added a commit to y-yagi/rails that referenced this pull request Nov 9, 2025
Currently, structured events for Active Record doesn't emit on v8.1.
This is because `active_record/structured_event_subscriber` isn't loaded.
I guess we just forgot to require it in rails#55690.

In `main` branch, this was already fixed by rails#55900.
I think we should fix this on 8.1 because we mentioned structured
events for Active Record on CHANGELOG.
https://github.com/rails/rails/blob/8-1-stable/activerecord/CHANGELOG.md#rails-810-october-22-2025
y-yagi added a commit to y-yagi/rails that referenced this pull request Nov 9, 2025
Currently, structured events for Active Record doesn't emit on v8.1.
This is because `active_record/structured_event_subscriber` isn't loaded.
I guess we just forgot to require it in rails#55690.

In `main` branch, this was already fixed by rails#55900.
I think we should fix this on 8.1 because we mentioned structured
events for Active Record on CHANGELOG.
https://github.com/rails/rails/blob/8-1-stable/activerecord/CHANGELOG.md#rails-810-october-22-2025
y-yagi added a commit to y-yagi/rails that referenced this pull request Nov 9, 2025
Currently, structured events for Active Record doesn't emit on v8.1.
This is because `active_record/structured_event_subscriber` isn't loaded.
I guess we just forgot to require it in rails#55690.

In `main` branch, this was already fixed by rails#55900.
I think we should fix this on 8.1 because we mentioned structured
events for Active Record on CHANGELOG.
https://github.com/rails/rails/blob/8-1-stable/activerecord/CHANGELOG.md#rails-810-october-22-2025
y-yagi added a commit to y-yagi/rails that referenced this pull request Nov 9, 2025
Currently, structured events for Active Record doesn't emit on v8.1.
This is because `active_record/structured_event_subscriber` isn't loaded.
I guess we just forgot to require it in rails#55690.

In `main` branch, this was already fixed by rails#55900.
I think we should fix this on 8.1 because we mentioned structured
events for Active Record on CHANGELOG.
https://github.com/rails/rails/blob/8-1-stable/activerecord/CHANGELOG.md#rails-810-october-22-2025
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
In Rails 8.2, SQL logging was refactored with a new pipeline:
Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber
(rails/rails#55690, rails/rails#55900).

Both StructuredEventSubscriber and LogSubscriber have their own
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper
only patched LogSubscriber's copy, but SCHEMA queries are now filtered
upstream at StructuredEventSubscriber before they ever reach LogSubscriber.

Since all oracle-enhanced schema methods (table_comment, column_comment,
foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the
query name, none of their SQL appeared in the test logger, causing 12
test failures.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
nuclearspike added a commit to nuclearspike/rails that referenced this pull request Jul 27, 2026
The structured-events rewrite (rails#55690) replaced the old render Start
subscriber with a new class that dropped the silenced? method rails#50751 had
added to its predecessor. Because the notifications Fanout treats
subscribers without silenced? as permanently active,
render_template.action_view and render_layout.action_view now run the
full instrumentation machinery on every render in every environment,
only for the emission to no-op inside ActiveSupport.event_reporter.debug
when debug mode is off (the default outside development).

Restoring silenced? (using the same two-condition shape as
ActiveSupport::StructuredEventSubscriber#silenced? and the original
rails#50751 fix) lets the notification layer skip that work at the source,
exactly as the pre-structured-events subscriber did. Behavior in debug
mode is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
nuclearspike added a commit to nuclearspike/rails that referenced this pull request Jul 29, 2026
The structured-events rewrite (rails#55690) replaced the old render Start
subscriber with a new class that dropped the silenced? method rails#50751 had
added to its predecessor. Because the notifications Fanout treats
subscribers without silenced? as permanently active,
render_template.action_view and render_layout.action_view now run the
full instrumentation machinery on every render in every environment,
only for the emission to no-op inside ActiveSupport.event_reporter.debug
when debug mode is off (the default outside development).

Restoring silenced? - silencing whenever the event reporter is not in
debug mode, as the rails#50751 fix did for the log level - lets the
notification layer skip that work at the source. Behavior in debug mode
is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
nuclearspike added a commit to nuclearspike/rails that referenced this pull request Jul 30, 2026
The structured-events rewrite (rails#55690) replaced the old render Start
subscriber with a new class that dropped the silenced? method rails#50751 had
added to its predecessor. Because the notifications Fanout treats
subscribers without silenced? as permanently active,
render_template.action_view and render_layout.action_view now run the
full instrumentation machinery on every render in every environment,
only for the emission to no-op inside ActiveSupport.event_reporter.debug
when debug mode is off (the default outside development).

Restoring silenced? - silencing whenever the event reporter is not in
debug mode, as the rails#50751 fix did for the log level - lets the
notification layer skip that work at the source. Behavior in debug mode
is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
nuclearspike added a commit to nuclearspike/rails that referenced this pull request Jul 30, 2026
The structured-events rewrite (rails#55690) replaced the old render Start
subscriber with a new class that dropped the silenced? method rails#50751 had
added to its predecessor. Because the notifications Fanout treats
subscribers without silenced? as permanently active,
render_template.action_view and render_layout.action_view now run the
full instrumentation machinery on every render in every environment,
only for the emission to no-op inside ActiveSupport.event_reporter.debug
when debug mode is off (the default outside development).

Restoring silenced? - silencing whenever the event reporter is not in
debug mode, as the rails#50751 fix did for the log level - lets the
notification layer skip that work at the source. Behavior in debug mode
is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
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