Class: OpenTelemetry::Instrumentation::Rage::Handlers::Deferred

Inherits:
Rage::Telemetry::Handler
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/rage/handlers/deferred.rb

Overview

The class propagates OpenTelemetry context to deferred tasks and wraps the enqueuing and processing of deferred tasks in spans.

Class Method Summary collapse

Class Method Details

.create_enqueue_span(task_class:, task_context:) ⇒ Object

Parameters:

  • task_class (Class)

    the class of the deferred task

  • task_context (Hash)

    the context for the deferred task



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/opentelemetry/instrumentation/rage/handlers/deferred.rb', line 22

def self.create_enqueue_span(task_class:, task_context:)
  Rage::Instrumentation.instance.tracer.in_span("#{task_class} enqueue", kind: :producer) do |span|
    OpenTelemetry.propagation.inject(task_context)

    result = yield

    if result.error?
      span.record_exception(result.exception)
      span.status = OpenTelemetry::Trace::Status.error
    end
  end
end

.create_perform_span(task_class:, task:, task_context:) ⇒ Object

Parameters:

  • task_class (Class)

    the class of the deferred task

  • task (Rage::Deferred::Task)

    the deferred task instance

  • task_context (Hash)

    the context for the deferred task



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/opentelemetry/instrumentation/rage/handlers/deferred.rb', line 38

def self.create_perform_span(task_class:, task:, task_context:)
  otel_context = OpenTelemetry.propagation.extract(task_context)

  OpenTelemetry::Context.with_current(otel_context) do
    parent_span_context = OpenTelemetry::Trace.current_span(otel_context).context
    links = [OpenTelemetry::Trace::Link.new(parent_span_context)] if parent_span_context.valid?

    span = Rage::Instrumentation.instance.tracer.start_root_span(
      "#{task_class} perform",
      links:,
      kind: :consumer
    )

    OpenTelemetry::Trace.with_span(span) do
      result = yield

      if result.error?
        span.record_exception(result.exception)
        span.status = OpenTelemetry::Trace::Status.error
      end
    ensure
      span.finish
    end
  end
end