In my app/services directory, I have a bunch of services that each have an initialize method and a public perform method that takes no arguments.
A simple example service to show the structure might look like this:
module Users
class CreateService
def initialize(name)
@name = name
end
def perform
# code to create the user
end
end
My goal is to add tracing to my application so that I can have traces for when each of my services in invoked, without needing to significantly modify the code of each of my services. In the end, I am hoping to make it so that when any of my services has its perform() method called, traces are automatically generated.
I believe this should be possible using ActiveSupport::Callbacks, similarly to how ActiveJob lets us define, say, around_perform callbacks for our jobs. I attempted to create a concern that I could include in my services which would instrument the desired behavior
module Traceable
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
included do
include ActiveSupport::Callbacks
define_callbacks :perform
set_callback :perform, :around, lambda { |r, block|
puts "start the trace"
result = block.call
puts "end the trace"
}
end
end
However, after including this concern, the callback I'd like to define (uniform for all services) still isn't getting called. How can I make this work? Thank you!
References:
https://api.rubyonrails.org/classes/ActiveSupport/Callbacks/ClassMethods.html
https://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html
https://edgeapi.rubyonrails.org/classes/ActiveJob/Callbacks/ClassMethods.html#method-i-around_perform (view the source code for the around_perform class method)