Skip to content

Commit 1037508

Browse files
authored
Merge pull request rails#55334 from Shopify/ac-structured-events
Structured Event Reporting in Rails
2 parents 049e832 + 071670b commit 1037508

File tree

13 files changed

+1702
-0
lines changed

13 files changed

+1702
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
* Add Structured Event Reporter, accessible via `Rails.event`.
2+
3+
The Event Reporter provides a unified interface for producing structured events in Rails
4+
applications:
5+
6+
```ruby
7+
Rails.event.notify("user.signup", user_id: 123, email: "[email protected]")
8+
```
9+
10+
It supports adding tags to events:
11+
12+
```ruby
13+
Rails.event.tagged("graphql") do
14+
# Event includes tags: { graphql: true }
15+
Rails.event.notify("user.signup", user_id: 123, email: "[email protected]")
16+
end
17+
```
18+
19+
As well as context:
20+
```ruby
21+
# All events will contain context: {request_id: "abc123", shop_id: 456}
22+
Rails.event.set_context(request_id: "abc123", shop_id: 456)
23+
```
24+
25+
Events are emitted to subscribers. Applications register subscribers to
26+
control how events are serialized and emitted. Rails provides several default
27+
encoders that can be used to serialize events to common formats:
28+
29+
```ruby
30+
class MySubscriber
31+
def emit(event)
32+
encoded_event = ActiveSupport::EventReporter.encoder(:json).encode(event)
33+
StructuredLogExporter.export(encoded_event)
34+
end
35+
end
36+
37+
Rails.event.subscribe(MySubscriber.new)
38+
```
39+
40+
*Adrianna Chang*
41+
142
* Make `ActiveSupport::Logger` `#freeze`-friendly.
243

344
*Joshua Young*

activesupport/lib/active_support.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module ActiveSupport
4848
autoload :ExecutionWrapper
4949
autoload :Executor
5050
autoload :ErrorReporter
51+
autoload :EventReporter
5152
autoload :FileUpdateChecker
5253
autoload :EventedFileUpdateChecker
5354
autoload :ForkTracker
@@ -110,6 +111,9 @@ def self.eager_load!
110111
@error_reporter = ActiveSupport::ErrorReporter.new
111112
singleton_class.attr_accessor :error_reporter # :nodoc:
112113

114+
@event_reporter = ActiveSupport::EventReporter.new
115+
singleton_class.attr_accessor :event_reporter # :nodoc:
116+
113117
def self.cache_format_version
114118
Cache.format_version
115119
end

0 commit comments

Comments
 (0)