Class: OpenTelemetry::Instrumentation::ActiveSupport::SpanSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/active_support/span_subscriber.rb

Constant Summary collapse

ALWAYS_VALID_PAYLOAD_TYPES =
[TrueClass, FalseClass, String, Numeric, Symbol].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: []) ⇒ SpanSubscriber

Returns a new instance of SpanSubscriber.



54
55
56
57
58
59
# File 'lib/opentelemetry/instrumentation/active_support/span_subscriber.rb', line 54

def initialize(name:, tracer:, notification_payload_transform: nil, disallowed_notification_payload_keys: [])
  @span_name = name.split('.')[0..1].reverse.join(' ').freeze
  @tracer = tracer
  @notification_payload_transform = notification_payload_transform
  @disallowed_notification_payload_keys = disallowed_notification_payload_keys
end

Instance Method Details

#finish(name, id, payload) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opentelemetry/instrumentation/active_support/span_subscriber.rb', line 74

def finish(name, id, payload)
  span = payload.delete(:__opentelemetry_span)
  token = payload.delete(:__opentelemetry_ctx_token)
  return unless span && token

  attrs = transform_payload(payload).each_with_object({}) do |(k, v), accum|
    accum[k.to_s] = sanitized_value(v) if valid_payload_key?(k) && valid_payload_value?(v)
  end

  span.add_attributes(attrs)

  if (e = payload[:exception_object])
    span.record_exception(e)
    span.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{e.class}")
  end

  span.finish
  OpenTelemetry::Context.detach(token)
end

#start(name, id, payload) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opentelemetry/instrumentation/active_support/span_subscriber.rb', line 61

def start(name, id, payload)
  span = @tracer.start_span(@span_name, kind: :internal)
  token = OpenTelemetry::Context.attach(
    OpenTelemetry::Trace.context_with_span(span)
  )
  payload.merge!(
    __opentelemetry_span: span,
    __opentelemetry_ctx_token: token
  )

  [span, token]
end