-
Notifications
You must be signed in to change notification settings - Fork 97
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
Allow ActiveJob before_perform callbacks to call ignore! and similar Scout APIs #336
Comments
Same here. This bug was misleading, as @cschneid thanks for reporting this, as it is good to know that this is a gem bug that smb else has, and not code bug. |
Thanks for this piece of information. Here's our workaround: # app/concerns/apm_sampling
module ApmSampling
extend ActiveSupport::Concern
private def apm_sample
ScoutApm::Transaction.ignore! unless apm_sample?
end
private def apm_sample?
true
end
def perform_with_apm_sampling(*args)
apm_sample
perform_without_apm_sampling(*args)
end
included do
case
when self < ActiveJob::Base || self < Sidekiq::Worker
def self.method_added(method_name)
case method_name
when :perform
# Avoid reentrance
unless method_defined?(:perform_without_apm_sampling)
alias_method :perform_without_apm_sampling, :perform
alias_method :perform, :perform_with_apm_sampling # This invokes method_added(:perform)
end
end
end
when respond_to?(:before_action)
before_action :apm_sample
else
# There is no before_action callback in ActionController::Metal.
# You need to manually call apm_sampling from within each action method.
end
end
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include ApmSampling
private def apm_sample?
rand < 0.05
end
end
# app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
# Make sure to include this first, especially before adding authentication callbacks.
# Otherwise all authentication errors are submitted without sampling.
include ApmSampling
private def apm_sample?
rand < 0.01
end
end We didn't use Module prepend because it didn't work nicely with rspec-mocks. |
I've run into the same problem and the workaround proposed by @knu was a great help. The only issue i'm still trying to get around is that there are still certain jobs that are harder to implement sampling on, such as the |
To introduce sampling to jobs that inherit from # config/initializers/scout_apm_job_sampling.rb
module ScoutApmJobSampling
extend ActiveSupport::Concern
included do
before_perform :sample_requests_for_scout
end
private
def sample_requests_for_scout
sample_rate = 0.01
if rand > sample_rate
ScoutApm::Transaction.ignore!
end
end
end
ActiveSupport.on_load(:active_job) do
include ScoutApmJobSampling
end |
ensure we can set a request to be ignored in a background processing system via a before hook and the request manager should return the ignored request instead of creating a new one related issues scoutapp#336
A customer running Sidekiq via ActiveJob reports that calling ignore from a before_perform callback is not working. Calling the ignore from inside the job's perform method appears to be fine however.
The sequence a task gets executed appears to be:
Scout injects itself as a server_middleware, so we should have the state necessary to ignore or rename or whatever API call you need by then.
The text was updated successfully, but these errors were encountered: