Class: Sentry::JobExtensions::Extension

Inherits:
Object
  • Object
show all
Includes:
LoggingHelper
Defined in:
lib/sentry/job_extensions/extension.rb

Overview

Base class for extensions that let a background-job backend enrich Sentry's job integrations. A backend gem subclasses it and overrides the hooks it can supply; jobs are opaque to Sentry and only interpreted by the extension.

Constant Summary collapse

PUBLISH_OP =
"queue.publish"
PROCESS_OP =
"queue.process"
MESSAGING_DATA_ATTRIBUTES =
{
  system: Span::DataConventions::MESSAGING_SYSTEM,
  message_id: Span::DataConventions::MESSAGING_MESSAGE_ID,
  destination: Span::DataConventions::MESSAGING_DESTINATION_NAME,
  receive_latency: Span::DataConventions::MESSAGING_MESSAGE_RECEIVE_LATENCY,
  retry_count: Span::DataConventions::MESSAGING_MESSAGE_RETRY_COUNT,
  body_size: Span::DataConventions::MESSAGING_MESSAGE_BODY_SIZE
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, integration:, origin:) ⇒ Extension

Creates an extension. Raises ArgumentError if the name is not a Symbol.

Parameters:

  • name (Symbol)
  • integration (Module) —

    the integration that captures the job's exceptions

  • origin (String) —

    the origin of the job's spans

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
# File 'lib/sentry/job_extensions/extension.rb', line 38

def initialize(name:, integration:, origin:)
  raise ArgumentError, "job extension name must be a Symbol, got #{name.inspect}" unless name.is_a?(Symbol)

  @name = name
  @integration = integration
  @origin = origin
end

Instance Attribute Details

#name ⇒ Symbol (readonly)

The extension's name, used in SDK log messages.

Returns:

  • (Symbol)


31
32
33
# File 'lib/sentry/job_extensions/extension.rb', line 31

def name
  @name
end

Instance Method Details

#capture_exception(job:, exception:, contexts: {}, tags: {}, extra: {}, **context) ⇒ Event?

Captures an exception raised by the job, enriched with the extension's error data.

Parameters:

  • job (Object)
  • exception (Exception)

Returns:



105
106
107
108
109
110
111
112
# File 'lib/sentry/job_extensions/extension.rb', line 105

def capture_exception(job:, exception:, contexts: {}, tags: {}, extra: {}, **context)
  return unless Sentry.initialized?

  data = safely("collect the error data", fallback: EMPTY_ERROR_DATA) { normalized_error_data(error_data(job: job, exception: exception, **context)) }
  capturing_integration = safely("pick the capturing integration", fallback: @integration) { integration(job: job, **context) }

  capturing_integration.capture_exception(exception, **merged_error_data(data, contexts: contexts, tags: tags, extra: extra))
end

#error_data(job:, exception:) ⇒ Hash

Returns data to add to the event captured for a failed job. Data already on the scope or passed explicitly by the job integration takes precedence.

Parameters:

  • job (Object)
  • exception (Exception)

Returns:

  • (Hash) —

    with optional :contexts, :tags and :extra



152
153
154
# File 'lib/sentry/job_extensions/extension.rb', line 152

def error_data(job:, exception:, **)
  {}
end

#extract_trace_headers(job:) ⇒ Hash{String => String}

Returns the trace propagation headers the job carries, so that performing it continues the trace it was enqueued in.

Parameters:

  • job (Object)

Returns:

  • (Hash{String => String})


199
200
201
# File 'lib/sentry/job_extensions/extension.rb', line 199

def extract_trace_headers(job:, **)
  {}
end

#finish_job(transaction:, status:) ⇒ void

This method returns an undefined value.

Finishes the job's transaction with the given status.

Parameters:



93
94
95
96
97
98
# File 'lib/sentry/job_extensions/extension.rb', line 93

def finish_job(transaction:, status:)
  return if !transaction || transaction.timestamp

  transaction.set_http_status(status)
  transaction.finish
end

#inject_trace_headers(job:, payload:, headers:) ⇒ Object

Returns the backend's job payload carrying the trace propagation headers.

Parameters:

  • job (Object)
  • payload (Object)
  • headers (Hash{String => String})

Returns:

  • (Object) —

    a payload of the same class as the one given



190
191
192
# File 'lib/sentry/job_extensions/extension.rb', line 190

def inject_trace_headers(job:, payload:, headers:, **)
  payload
end

#instrument_enqueue(job:, **context, &enqueue) ⇒ Object

Wraps enqueueing a job in a queue.publish span.

Parameters:

  • job (Object)

Yield Returns:

  • (Object) —

    the result of enqueueing the job

Returns:

  • (Object) —

    the block's result



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sentry/job_extensions/extension.rb', line 51

def instrument_enqueue(job:, **context, &enqueue)
  return enqueue.call unless safely("decide whether to record the producer span", fallback: true) { record_publish_span?(job: job, **context) }

  Sentry.with_child_span(op: PUBLISH_OP) do |span|
    if span
      span.set_description(safely("describe the producer span") { publish_description(job: job, **context) })
      span.set_origin(safely("set the producer span origin", fallback: @origin) { publish_span_origin(job: job, **context) })
      set_messaging_data(span, hook_hash("collect the producer messaging data") { publish_messaging_data(job: job, **context) })
    end

    enqueue.call
  end
end

#integration(job:) ⇒ Module

Returns the integration that captures the job's exceptions. Defaults to the extension's integration.

Parameters:

  • job (Object)

Returns:

  • (Module)


258
259
260
# File 'lib/sentry/job_extensions/extension.rb', line 258

def integration(job:, **)
  @integration
end

#messaging_data(job:) ⇒ Hash

Returns messaging attributes for the job's spans.

Parameters:

  • job (Object)

Returns:

  • (Hash) —

    with optional :system, :message_id, :destination, :receive_latency, :retry_count and :body_size



208
209
210
# File 'lib/sentry/job_extensions/extension.rb', line 208

def messaging_data(job:, **)
  {}
end

#publish_description(job:) ⇒ String?

Returns the description of the queue.publish span. Defaults to the job's class name.

Parameters:

  • job (Object)

Returns:

  • (String, nil)


233
234
235
# File 'lib/sentry/job_extensions/extension.rb', line 233

def publish_description(job:, **)
  job.class.name
end

#publish_messaging_data(job:, **context) ⇒ Hash

Returns messaging attributes for the queue.publish span. Defaults to #messaging_data.

Parameters:

  • job (Object)

Returns:

  • (Hash)


217
218
219
# File 'lib/sentry/job_extensions/extension.rb', line 217

def publish_messaging_data(job:, **context)
  messaging_data(job: job, **context)
end

#publish_span_origin(job:, **context) ⇒ String

Returns the origin of the queue.publish span. Defaults to #span_origin.

Parameters:

  • job (Object)

Returns:

  • (String)


249
250
251
# File 'lib/sentry/job_extensions/extension.rb', line 249

def publish_span_origin(job:, **context)
  span_origin(job: job, **context)
end

#record_publish_span?(job:) ⇒ Boolean

Returns whether to record a queue.publish span when the job is enqueued.

Parameters:

  • job (Object)

Returns:

  • (Boolean)


134
135
136
# File 'lib/sentry/job_extensions/extension.rb', line 134

def record_publish_span?(job:, **)
  true
end

#report_exception?(job:, exception:, **context) ⇒ Boolean

Returns whether to capture the job's exception. By default only when the backend won't retry the job.

Parameters:

  • job (Object)
  • exception (Exception)

Returns:

  • (Boolean)


170
171
172
# File 'lib/sentry/job_extensions/extension.rb', line 170

def report_exception?(job:, exception:, **context)
  !will_retry?(job: job, exception: exception, **context)
end

#report_retried_exception?(job:, exception:) ⇒ Boolean

Returns whether to capture an exception that the job integration's own retry mechanism retries, so the backend never sees it.

Parameters:

  • job (Object)
  • exception (Exception)

Returns:

  • (Boolean)


180
181
182
# File 'lib/sentry/job_extensions/extension.rb', line 180

def report_retried_exception?(job:, exception:, **)
  false
end

#safely(context, fallback: nil) ⇒ Object

Runs a hook, logging its failure and returning fallback instead of raising.

Parameters:

  • context (String) —

    what the hook does, used in the log message

Returns:

  • (Object)


118
119
120
121
122
123
# File 'lib/sentry/job_extensions/extension.rb', line 118

def safely(context, fallback: nil)
  yield
rescue StandardError => e
  log_failure(context, e)
  fallback
end

#scope_data(job:) ⇒ Hash

Returns data to apply to the scope the job is performed in.

Parameters:

  • job (Object)

Returns:

  • (Hash) —

    with optional :contexts, :tags, :extra and :user



142
143
144
# File 'lib/sentry/job_extensions/extension.rb', line 142

def scope_data(job:, **)
  {}
end

#span_origin(job:) ⇒ String

Returns the origin of the job's transaction. Defaults to the extension's origin.

Parameters:

  • job (Object)

Returns:

  • (String)


241
242
243
# File 'lib/sentry/job_extensions/extension.rb', line 241

def span_origin(job:, **)
  @origin
end

#start_job(scope:, job:, **context) ⇒ Transaction?

Prepares the scope for performing the job and starts its queue.process transaction, continuing the trace the job was enqueued in.

Parameters:

  • scope (Scope)
  • job (Object)

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sentry/job_extensions/extension.rb', line 71

def start_job(scope:, job:, **context)
  data = hook_hash("collect the scope data") { scope_data(job: job, **context) }
  safely("apply the scope data") { apply_scope_data(scope, data) }

  job_name = safely("name the transaction") { transaction_name(job: job, **context) }
  scope.set_transaction_name(job_name, source: :task) if job_name

  trace_headers = hook_hash("extract trace headers") { extract_trace_headers(job: job, **context) }
  origin = safely("set the transaction origin", fallback: @origin) { span_origin(job: job, **context) }
  transaction = start_transaction(scope, trace_headers, origin)
  return unless transaction

  scope.set_span(transaction)
  set_messaging_data(transaction, hook_hash("collect the messaging data") { messaging_data(job: job, **context) })
  transaction
end

#transaction_name(job:) ⇒ String?

Returns the name of the job's transaction. Defaults to the job's class name.

Parameters:

  • job (Object)

Returns:

  • (String, nil)


225
226
227
# File 'lib/sentry/job_extensions/extension.rb', line 225

def transaction_name(job:, **)
  job.class.name
end

#will_retry?(job:) ⇒ Boolean

Returns whether the backend will retry the failed job.

Parameters:

  • job (Object)

Returns:

  • (Boolean)


160
161
162
# File 'lib/sentry/job_extensions/extension.rb', line 160

def will_retry?(job:, **)
  false
end