Class: OpenTelemetry::Instrumentation::ActiveJob::Handlers::Perform

Inherits:
Default
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/active_job/handlers/perform.rb

Overview

Handles perform.active_job to generate ingress spans

Constant Summary collapse

EVENT_NAME =
'process'

Instance Method Summary collapse

Methods inherited from Default

#finish, #finish_span, #initialize, #on_exception, #start, #tracer

Constructor Details

This class inherits a constructor from OpenTelemetry::Instrumentation::ActiveJob::Handlers::Default

Instance Method Details

#attach_consumer_context(span, parent_context) ⇒ Numeric

This method attaches a span to multiple contexts:

  1. Registers the ingress span as the top level ActiveJob span. This is used later to enrich the ingress span in children, e.g. setting span status to error when a child event like discard terminates due to an error
  2. Registers the ingress span as the "active" span, which is the default behavior of the SDK.


47
48
49
50
51
52
# File 'lib/opentelemetry/instrumentation/active_job/handlers/perform.rb', line 47

def attach_consumer_context(span, parent_context)
  consumer_context = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context)
  internal_context = OpenTelemetry::Instrumentation::ActiveJob.context_with_span(span, parent_context: consumer_context)

  OpenTelemetry::Context.attach(internal_context)
end

#start_span(name, _id, payload) ⇒ Hash

Overrides the Default#start_span method to create an ingress span and registers it with the current context



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/opentelemetry/instrumentation/active_job/handlers/perform.rb', line 22

def start_span(name, _id, payload)
  job = payload.fetch(:job)
  span_name = span_name(job, EVENT_NAME)
  parent_context = OpenTelemetry.propagation.extract(job.__otel_headers)

  # TODO: Refactor into a propagation strategy
  propagation_style = @config[:propagation_style]
  if propagation_style == :child
    span = tracer.start_span(span_name, with_parent: parent_context, kind: :consumer, attributes: @mapper.call(payload))
  else
    span_context = OpenTelemetry::Trace.current_span(parent_context).context
    links = [OpenTelemetry::Trace::Link.new(span_context)] if span_context.valid? && propagation_style == :link
    span = tracer.start_root_span(span_name, kind: :consumer, attributes: @mapper.call(payload), links: links)
  end

  { span: span, ctx_token: attach_consumer_context(span, parent_context) }
end