clockwork
clockwork copied to clipboard
Instrumentation hooks for block execution
There is a nice hook for handling exceptions raised by blocks registered with every
, but no such hook for instrumenting the execution of those blocks.
What happened for us was that a block was failing to execute and we were not aware since we did not register an error_handler
. This worked for reporting errors, but did not include enough context to tell us that the error came from clockwork, which is required to implement a different alert policy on failure (create an incident after a very small number of failures, as opposed to relying on the error rate of our already high-throughput web application to create an incident).
What we ended up doing was patching the every
method to instrument execution of the registered block with the New Relic Ruby gem, as such:
module Clockwork
extend ::NewRelic::Agent::MethodTracer
class << self
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
alias every_original every
def every(period, job, options = {}, &_block)
if block_given?
every_original(period, job, options) do |*arguments|
perform_action_with_newrelic_trace(class_name: 'Clockwork', category: :task, name: 'every', params: { 'Clockwork.job': job }) do
yield(*arguments)
end
end
else
every_original(period, job, options)
end
end
end
end
This seems like behavior that could be added to the Manager
in a clean way. Thoughts?