Class: OpenTelemetry::Instrumentation::ActiveJob::Handlers::Default
- Inherits:
-
Object
- Object
- OpenTelemetry::Instrumentation::ActiveJob::Handlers::Default
- 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.
Instance Method Summary collapse
-
#finish(_name, _id, payload) ⇒ Hash
Creates a span and registers it with the current context.
-
#finish_span(span, token) ⇒ Object
Finishes the provided spans and also detaches the associated contexts.
-
#initialize(parent_span_provider, mapper, config) ⇒ Default
constructor
A new instance of Default.
-
#on_exception(exception, span) ⇒ Object
Records exceptions on spans and sets Span statuses to
Error
. -
#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.
-
#start_span(name, _id, payload) ⇒ Hash
Creates a span and registers it with the current context.
- #tracer ⇒ Object
Constructor Details
#initialize(parent_span_provider, mapper, config) ⇒ Default
Returns a new instance of Default.
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
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 58 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 74 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.
100 101 102 103 104 105 106 107 |
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 100 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
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
42 43 44 45 46 47 48 49 50 |
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 42 def start_span(name, _id, payload) job = payload.fetch(:job) event_name = name.delete_suffix(".#{EVENT_NAMESPACE}") span_name = span_name(job, event_name) span = tracer.start_span(span_name, attributes: @mapper.call(payload)) token = OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span)) { span: span, ctx_token: token } end |
#tracer ⇒ Object
109 110 111 |
# File 'lib/opentelemetry/instrumentation/active_job/handlers/default.rb', line 109 def tracer OpenTelemetry::Instrumentation::ActiveJob::Instrumentation.instance.tracer end |