Skip to content

Instantly share code, notes, and snippets.

@coorasse
Last active November 22, 2023 14:24
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 coorasse/06cf42b4daaa77a873702273c641e13c to your computer and use it in GitHub Desktop.
Save coorasse/06cf42b4daaa77a873702273c641e13c to your computer and use it in GitHub Desktop.
Sentry - skip fast transactions
# /config/initializers/sentry.rb
config.before_send_transaction = lambda do |event, _hint|
relevant_span = event.spans.find { |s| s[:op] == 'view.process_action.action_controller' }
if relevant_span
sampling_config = { 50 => 1, 200 => 10, 500 => 20, 1000 => 50 }
duration_in_ms = (relevant_span[:timestamp] - relevant_span[:start_timestamp]) * 1000
sampling_percentage = sampling_config.
sort.
find { |threshold, _percentage| duration_in_ms <= threshold }.
try(:[], 1) || 100
# Perform the sampling check
Random.rand(100) <= sampling_percentage ? event : nil
else
event
end
end
@coorasse
Copy link
Author

coorasse commented Nov 22, 2023

This can be simplified if don't have a sampling_config:

config.before_send_transaction = lambda do |event, _hint|
  relevant_span = event.spans.find { |s| s[:op] == 'view.process_action.action_controller' }
  if relevant_span
    duration_in_ms = (relevant_span[:timestamp] - relevant_span[:start_timestamp]) * 1000

    sampling_percentage = duration_in_ms <= 100 ? 1 : 100

    # Perform the sampling check
    Random.rand(100) <= sampling_percentage ? event : nil
  else
    event
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment