Class: OpenTelemetry::Instrumentation::ActiveJob::Handlers::Default

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

Overview

Default handler to create internal spans for events This class provides default template methods that derived classes may override to generate spans and register contexts.

Direct Known Subclasses

Enqueue, Perform

Instance Method Summary collapse

Constructor Details

#initialize(parent_span_provider, mapper, config) ⇒ Default

Returns a new instance of Default.

Parameters:

  • parent_span_provider (Object)

    provides access to the top most parent span (usually the ingress span)

  • mapper (Callable)

    converts ActiveSupport::Notifications payloads to span attributes

  • config (Hash)

    of instrumentation options



17
18
19
20
21
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 17

def initialize(parent_span_provider, mapper, config)
  @mapper = mapper
  @config = config
  @parent_span_provider = parent_span_provider
end

Instance Method Details

#finish(_name, _id, payload) ⇒ Hash

Creates a span and registers it with the current context

Parameters:

  • _name (String)

    of the Event (unused)

  • _id (String)

    of the event (unused)

  • payload (Hash)

    containing job run information

Returns:

  • (Hash)

    with the span and generated context tokens



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 55

def finish(_name, _id, payload)
  otel = payload.delete(:__otel)
  span = otel&.fetch(:span)
  token = otel&.fetch(:ctx_token)

  on_exception((payload[:error] || payload[:exception_object]), span)
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e)
ensure
  finish_span(span, token)
end

#finish_span(span, token) ⇒ Object

Finishes the provided spans and also detaches the associated contexts

Parameters:

  • span (OpenTelemetry::Trace::Span)
  • token (Numeric)

    to unregister



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 71

def finish_span(span, token)
  # closes the span after all attributes have been finalized
  begin
    if span&.recording?
      span.status = OpenTelemetry::Trace::Status.ok if span.status.code == OpenTelemetry::Trace::Status::UNSET
      span.finish
    end
  rescue StandardError => e
    OpenTelemetry.handle_error(exception: e)
  end

  begin
    OpenTelemetry::Context.detach(token)
  rescue StandardError => e
    OpenTelemetry.handle_error(exception: e)
  end
end

#on_exception(exception, span) ⇒ Object

Records exceptions on spans and sets Span statuses to Error

Handled exceptions are recorded on internal spans related to the event. E.g. discard events are recorded on the discard.active_job span Handled exceptions are not copied to the ingress span, but it does set the status to Error making it easier to know that a job has failed Unhandled exceptions bubble up to the ingress span and are recorded there.

Parameters:

  • exception (Exception)

    to report as a Span Event

  • the (OpenTelemetry::Trace::Span)

    currently active span used to record the exception and set the status



97
98
99
100
101
102
103
104
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 97

def on_exception(exception, span)
  return unless exception && span

  span.record_exception(exception)
  span.status =
    @parent_span_provider.current_span.status =
      OpenTelemetry::Trace::Status.error("Unexpected ActiveJob Error #{exception.class.name}")
end

#start(name, id, payload) ⇒ Hash

Invoked by ActiveSupport::Notifications at the start of the instrumentation block It amends the otel context of a Span and Context tokens to the payload

Parameters:

  • name (String)

    of the Event

  • id (String)

    of the event

  • payload (Hash)

    containing job run information

Returns:

  • (Hash)

    the payload passed as a method argument



30
31
32
33
34
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 30

def start(name, id, payload)
  payload.merge!(__otel: start_span(name, id, payload))
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e)
end

#start_span(name, _id, payload) ⇒ Hash

Creates a span and registers it with the current context

Parameters:

  • name (String)

    of the Event

  • id (String)

    of the event

  • payload (Hash)

    containing job run information

Returns:

  • (Hash)

    with the span and generated context tokens



42
43
44
45
46
47
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 42

def start_span(name, _id, payload)
  span = tracer.start_span(name, attributes: @mapper.call(payload))
  token = OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))

  { span: span, ctx_token: token }
end

#tracerObject



106
107
108
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 106

def tracer
  OpenTelemetry::Instrumentation::ActiveJob::Instrumentation.instance.tracer
end