Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Last active February 29, 2024 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/60fbc0827ce668b1ac8b2c5f1f7f13d2 to your computer and use it in GitHub Desktop.
Save romanbsd/60fbc0827ce668b1ac8b2c5f1f7f13d2 to your computer and use it in GitHub Desktop.
module ExternalApiRuntime
extend ActiveSupport::Concern
module ClassMethods
def log_process_action(payload)
messages = super
api_runtime = payload[:api_runtime]
messages << (format('ExternalAPI: %.1fms', api_runtime.to_f)) if api_runtime
messages
end
end
attr_internal :api_runtime
def initialize(...)
super
self.api_runtime = 0
ActiveSupport::Notifications.subscribe('external_api.call') do |_name, starts, ends, _, _env|
self.api_runtime += 1000 * (ends - starts)
end
end
def append_info_to_payload(payload)
super
payload[:api_runtime] = api_runtime
self.api_runtime = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment