forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds observability to the new event bus
This commit adds observability abilities to the new event bus adapter: - It adds creation time and caller location to the `Spree::Event::Event` instance bound to the subscribers. - It wraps the execution of listeners within a new `Spree::Event::Execution` class, which wraps the execution time, a benchmark of the operation, and the result returned by the subscription. Both the `Spree::Event::Event` and the list of `Spree::Event::Execution` are wrapped within a new `Spree::Event::Firing` object, which is returned on `Spree::Event.fire`. Therefore, a typical flow could be getting the event's caller from within the subscriber block and then inspecting the firing object from there if needed. Having all the metadata collected in a single place when we do `Spree::Event.fire` will simplify things if we implement some event store in the future.
- Loading branch information
1 parent
f928f1a
commit d4d3c0c
Showing
9 changed files
with
254 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Event | ||
# Execution of a {Spree::Event::Listener} | ||
# | ||
# When an event is fired, it executes all subscribed listeners. Every single | ||
# execution is represented as an instance of this class. It contains the | ||
# result value of the listener, along with helpful metadata as the time of | ||
# the execution or a benchmark for it. | ||
# | ||
# You'll most likely interact with this class for debugging or logging | ||
# purposes through the returned value in {Spree::Event.fire}. | ||
class Execution | ||
# The listener to which the execution belongs | ||
# | ||
# @return [Spree::Event::Listener] | ||
attr_reader :listener | ||
|
||
# The value returned by the {#listener}'s block | ||
# | ||
# @return [Any] | ||
attr_reader :result | ||
|
||
# Benchmark for the {#listener}'s block | ||
# | ||
# @return [Benchmark::Tms] | ||
attr_reader :benchmark | ||
|
||
# Time of execution | ||
# | ||
# @return [Time] | ||
attr_reader :execution_time | ||
|
||
# @private | ||
def initialize(listener:, result:, benchmark:, execution_time: Time.now.utc) | ||
@listener = listener | ||
@result = result | ||
@benchmark = benchmark | ||
@execution_time = execution_time | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Event | ||
# The result of firing an event | ||
# | ||
# It encapsulates a fired {Spree::Event::Event} as well as the | ||
# {Spree::Event::Execution}s it originated. | ||
# | ||
# This class is useful mainly for debugging and logging purposes. An | ||
# instance of it is returned on {Spree::Event.fire}. | ||
class Firing | ||
# Fired event | ||
# | ||
# @return [Spree::Event::Event] | ||
attr_reader :event | ||
|
||
# Listener executions that the firing originated | ||
# | ||
# @return [Array<Spree::Event::Execution>] | ||
attr_reader :executions | ||
|
||
# @api private | ||
def initialize(event:, executions:) | ||
@event = event | ||
@executions = executions | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.