|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Sentry |
| 4 | + module JobExtensions |
| 5 | + # @api private |
| 6 | + class Consumer < Instrumentation |
| 7 | + OP_NAME = "queue.process" |
| 8 | + |
| 9 | + attr_reader :integration, :origin |
| 10 | + |
| 11 | + def initialize(integration:, origin:, registry: Sentry.job_extensions) |
| 12 | + super(registry: registry) |
| 13 | + @integration = integration |
| 14 | + @origin = origin |
| 15 | + end |
| 16 | + |
| 17 | + def apply_scope_data(scope, job, default_scope_data: {}) |
| 18 | + apply_scope_values(scope, default_scope_data) |
| 19 | + data = registry.call(registry.for(job), :scope_data, job: job) |
| 20 | + apply_scope_values(scope, data) |
| 21 | + end |
| 22 | + |
| 23 | + def transaction_name(job) |
| 24 | + registry.call(registry.for(job), :transaction_name, job: job) |
| 25 | + end |
| 26 | + |
| 27 | + def start_transaction(scope, job, transaction: nil, trace_headers: nil, payload: nil, always_continue_trace: false, messaging_data: {}, default_retry_count: nil) |
| 28 | + extension = registry.for(job) |
| 29 | + options = { |
| 30 | + name: scope.transaction_name, |
| 31 | + source: scope.transaction_source, |
| 32 | + op: OP_NAME, |
| 33 | + origin: registry.call(extension, :span_origin, job: job) || origin |
| 34 | + } |
| 35 | + |
| 36 | + if (trace_headers.nil? || trace_headers.empty?) && payload |
| 37 | + extracted_headers = registry.call(extension, :extract_trace_headers, job: job, payload: payload) |
| 38 | + |
| 39 | + unless extracted_headers.nil? |
| 40 | + trace_headers = extracted_headers |
| 41 | + always_continue_trace = true |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + if transaction |
| 46 | + transaction.set_name(options[:name], source: options[:source]) |
| 47 | + transaction.set_origin(options[:origin]) |
| 48 | + else |
| 49 | + transaction = if always_continue_trace || (trace_headers && !trace_headers.empty?) |
| 50 | + continued = Sentry.continue_trace(trace_headers, **options) |
| 51 | + Sentry.start_transaction(transaction: continued, **options) |
| 52 | + else |
| 53 | + Sentry.start_transaction(**options) |
| 54 | + end |
| 55 | + end |
| 56 | + return unless transaction |
| 57 | + |
| 58 | + set_messaging_data(transaction, resolve(messaging_data).reject { |_key, value| value.nil? }) |
| 59 | + set_messaging_data(transaction, registry.call(extension, :messaging_data, job: job)) |
| 60 | + set_retry_count(transaction, extension, job, default: resolve(default_retry_count)) |
| 61 | + |
| 62 | + scope.set_span(transaction) |
| 63 | + transaction |
| 64 | + end |
| 65 | + |
| 66 | + def finish_transaction(transaction, status) |
| 67 | + return if !transaction || transaction.timestamp |
| 68 | + |
| 69 | + transaction.set_http_status(status) |
| 70 | + transaction.finish |
| 71 | + end |
| 72 | + |
| 73 | + def will_retry?(job) |
| 74 | + info = registry.call(registry.for(job), :retry_info, job: job) |
| 75 | + info ? info[:will_retry] : false |
| 76 | + end |
| 77 | + |
| 78 | + def report_exception?(job, exception) |
| 79 | + decision = registry.call(registry.for(job), :report_exception?, job: job, exception: exception) |
| 80 | + decision.nil? ? !will_retry?(job) : decision |
| 81 | + end |
| 82 | + |
| 83 | + def report_retried_exception?(job, exception, default:) |
| 84 | + decision = registry.call(registry.for(job), :report_retried_exception?, job: job, exception: exception) |
| 85 | + decision.nil? ? default : decision |
| 86 | + end |
| 87 | + |
| 88 | + def capture_exception(job, exception, contexts: {}, tags: {}, extra: {}) |
| 89 | + extension = registry.for(job) |
| 90 | + error_data = additive_error_data( |
| 91 | + registry.call(extension, :error_data, job: job, exception: exception) |
| 92 | + ) |
| 93 | + |
| 94 | + options = { |
| 95 | + extra: error_data[:extra].merge(matching_keys(extra, error_data[:extra])), |
| 96 | + tags: error_data[:tags].merge(matching_keys(tags, error_data[:tags])), |
| 97 | + hint: { background: false } |
| 98 | + } |
| 99 | + contexts = error_data[:contexts].merge(matching_keys(contexts, error_data[:contexts])) |
| 100 | + options[:contexts] = contexts unless contexts.empty? |
| 101 | + |
| 102 | + capturing_integration = registry.call(extension, :integration, job: job) || integration |
| 103 | + capturing_integration.capture_exception(exception, **options) |
| 104 | + end |
| 105 | + |
| 106 | + private |
| 107 | + |
| 108 | + def apply_scope_values(scope, data) |
| 109 | + scope.set_contexts(data[:contexts]) if data[:contexts] |
| 110 | + scope.set_tags(data[:tags]) if data[:tags] |
| 111 | + scope.set_extras(data[:extra]) if data[:extra] |
| 112 | + scope.set_user(data[:user]) if data[:user] |
| 113 | + end |
| 114 | + |
| 115 | + def additive_error_data(data) |
| 116 | + scope = Sentry.get_current_scope |
| 117 | + |
| 118 | + { |
| 119 | + contexts: matching_keys(data.fetch(:contexts, {}), scope.contexts).to_h do |name, context| |
| 120 | + scope_context = scope.contexts.fetch(name, {}) |
| 121 | + context = matching_keys(context, scope_context) |
| 122 | + [name, context.merge(scope_context)] |
| 123 | + end, |
| 124 | + tags: matching_keys(data.fetch(:tags, {}), scope.tags).reject { |key, _value| scope.tags.key?(key) }, |
| 125 | + extra: matching_keys(data.fetch(:extra, {}), scope.extra).reject { |key, _value| scope.extra.key?(key) } |
| 126 | + } |
| 127 | + end |
| 128 | + |
| 129 | + def matching_keys(data, reference) |
| 130 | + keys = reference.keys.to_h { |key| [key.to_s, key] } |
| 131 | + data.transform_keys { |key| keys.fetch(key.to_s, key) } |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments