Class: Sbmt::Outbox::Middleware::OpenTelemetry::TracingItemProcessMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb

Instance Method Summary collapse

Instance Method Details

#call(item) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb', line 8

def call(item)
  return yield unless defined?(::OpenTelemetry)

  item_class = item.class
  item_options = item.options || {}
  item_options_headers = item_options[:headers] || item_options["headers"]

  return yield unless item_class && item_options_headers

  span_attributes = {
    "messaging.system" => "outbox",
    "messaging.outbox.item_id" => item.id.to_s,
    "messaging.outbox.box_type" => item_class.box_type.to_s,
    "messaging.outbox.box_name" => item_class.box_name,
    "messaging.outbox.owner" => item_class.owner,
    "messaging.destination" => item_class.name,
    "messaging.destination_kind" => "database",
    "messaging.operation" => "process"
  }

  extracted_context = ::OpenTelemetry.propagation.extract(item_options_headers)
  ::OpenTelemetry::Context.with_current(extracted_context) do
    tracer.in_span(span_name(item_class), attributes: span_attributes.compact, kind: :consumer) do
      Sbmt::Outbox.logger.with_tags(trace_id: trace_id) do
        yield
      end
    end
  end
end