Class: Sentry::OpenTelemetry::SpanProcessor

Inherits:
OpenTelemetry::SDK::Trace::SpanProcessor
  • Object
show all
Includes:
Singleton
Defined in:
lib/sentry/opentelemetry/span_processor.rb

Constant Summary collapse

SEMANTIC_CONVENTIONS =
::OpenTelemetry::SemanticConventions::Trace
INTERNAL_SPAN_KINDS =
i[client internal]
SPAN_ORIGIN =
"auto.otel"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpanProcessor

Returns a new instance of SpanProcessor.



20
21
22
23
# File 'lib/sentry/opentelemetry/span_processor.rb', line 20

def initialize
  @span_map = {}
  setup_event_processor
end

Instance Attribute Details

#span_mapHash (readonly)

The mapping from otel span ids to sentry spans

Returns:

  • (Hash)


18
19
20
# File 'lib/sentry/opentelemetry/span_processor.rb', line 18

def span_map
  @span_map
end

Instance Method Details

#clearObject



85
86
87
# File 'lib/sentry/opentelemetry/span_processor.rb', line 85

def clear
  @span_map = {}
end

#on_finish(otel_span) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sentry/opentelemetry/span_processor.rb', line 69

def on_finish(otel_span)
  return unless Sentry.initialized? && Sentry.configuration.instrumenter == :otel
  return unless otel_span.context.valid?

  sentry_span = @span_map.delete(otel_span.context.hex_span_id)
  return unless sentry_span

  if sentry_span.is_a?(Sentry::Transaction)
    update_transaction_with_otel_data(sentry_span, otel_span)
  else
    update_span_with_otel_data(sentry_span, otel_span)
  end

  sentry_span.finish(end_timestamp: otel_span.end_timestamp / 1e9)
end

#on_start(otel_span, parent_context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
# File 'lib/sentry/opentelemetry/span_processor.rb', line 25

def on_start(otel_span, parent_context)
  return unless Sentry.initialized? && Sentry.configuration.instrumenter == :otel
  return unless otel_span.context.valid?
  return if from_sentry_sdk?(otel_span)

  trace_data = get_trace_data(otel_span, parent_context)

  sentry_parent_span = @span_map[trace_data.parent_span_id] if trace_data.parent_span_id

  sentry_span = if sentry_parent_span
    sentry_parent_span.start_child(
      span_id: trace_data.span_id,
      description: otel_span.name,
      start_timestamp: otel_span.start_timestamp / 1e9,
      origin: SPAN_ORIGIN
    )
  else
    options = {
      instrumenter: :otel,
      name: otel_span.name,
      span_id: trace_data.span_id,
      trace_id: trace_data.trace_id,
      parent_span_id: trace_data.parent_span_id,
      parent_sampled: trace_data.parent_sampled,
      baggage: trace_data.baggage,
      start_timestamp: otel_span.start_timestamp / 1e9,
      origin: SPAN_ORIGIN,
      custom_sampling_context: {
        otel: otel_context_hash(otel_span).merge(
          kind: otel_span.kind,
          instrumentation_scope: {
            name: otel_span.instrumentation_scope.name,
            version: otel_span.instrumentation_scope.version
          }
        )
      }
    }

    Sentry.start_transaction(**options)
  end

  @span_map[trace_data.span_id] = sentry_span
end