Skip to content

Commit 2731985

Browse files
Add Structured Event Reporter
Ref: rails#50452 This adds a Structured Event Reporter to Rails, accessible via `Rails.event`. It allows you to report events to a subscriber, and provides mechanisms for adding tags and context to events. Events encompass "structured logs", but also "business events", as well as telemetry events such as metrics and logs. The Event Reporter is designed to be a single interface for producing any kind of event in a Rails application. We separate the emission of events from how these events reach end consumers; applications are expected to define their own subscribers, and the Event Reporter is responsible for emitting events to these subscribers.
1 parent 2c3ea36 commit 2731985

File tree

12 files changed

+1671
-0
lines changed

12 files changed

+1671
-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)