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

Instrument statesman for easy hook into from monitoring tools #489

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/statesman/callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ def initialize(options = { from: nil, to: nil, callback: nil })
end

def call(*args)
callback.call(*args)
ActiveSupport::Notifications.instrument "callback.statesman", {
subject: args.first.class.name,
subject_id: args.first.id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be sure these will always respond to id?

to_state: to,
from_state: from,
callback: callback.to_s,
resource: self.class.name,
} do
callback.call(*args)
end
end

def applies_to?(options = { from: nil, to: nil })
Expand Down
11 changes: 10 additions & 1 deletion lib/statesman/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
module Statesman
class Guard < Callback
def call(*args)
raise GuardFailedError.new(from, to) unless super(*args)
ActiveSupport::Notifications.instrument "guard.statesman", {
subject: args.first.class.name,
subject_id: args.first.id,
to_state: to,
from_state: from,
callback: callback.to_s,
resource: self.class.name,
} do
raise GuardFailedError.new(from, to) unless super(*args)
end
end
end
end
42 changes: 30 additions & 12 deletions lib/statesman/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,20 @@ def history
end

def transition_to!(new_state, metadata = {})
initial_state = current_state
new_state = new_state.to_s

validate_transition(from: initial_state,
to: new_state,
metadata: metadata)

@storage_adapter.create(initial_state, new_state, metadata)
ActiveSupport::Notifications.instrument "transition.statesman", {
to_state: new_state,
from_state: current_state,
resource: self.class.name,
} do
initial_state = current_state
new_state = new_state.to_s

validate_transition(from: initial_state,
to: new_state,
metadata: metadata)

@storage_adapter.create(initial_state, new_state, metadata)
end

true
rescue TransitionFailedError => e
Expand All @@ -298,13 +304,25 @@ def transition_to!(new_state, metadata = {})
end

def execute_on_failure(phase, initial_state, new_state, exception)
callbacks = callbacks_for(phase, from: initial_state, to: new_state)
callbacks.each { |cb| cb.call(@object, exception) }
ActiveSupport::Notifications.instrument "execute_on_failure.statesman", {
to_state: new_state,
from_state: initial_state,
resource: self.class.name,
} do
callbacks = callbacks_for(phase, from: initial_state, to: new_state)
callbacks.each { |cb| cb.call(@object, exception) }
end
end

def execute(phase, initial_state, new_state, transition)
callbacks = callbacks_for(phase, from: initial_state, to: new_state)
callbacks.each { |cb| cb.call(@object, transition) }
ActiveSupport::Notifications.instrument "execute.statesman", {
to_state: new_state,
from_state: initial_state,
resource: self.class.name,
} do
callbacks = callbacks_for(phase, from: initial_state, to: new_state)
callbacks.each { |cb| cb.call(@object, transition) }
end
end

def transition_to(new_state, metadata = {})
Expand Down