Class: Yes::Core::OpenTelemetry::OtlSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/open_telemetry/otl_span.rb

Overview

Wraps OpenTelemetry span creation with SQL tracking support.

Examples:

span = OtlSpan.new(otl_data: OtlData.new(span_name: 'MySpan'), otl_tracer: tracer)
span.otl_span(arg1, arg2) { do_work }

Defined Under Namespace

Classes: OtlData

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(otl_data:, otl_tracer:) ⇒ OtlSpan

Returns a new instance of OtlSpan.

Parameters:

  • otl_data (OtlData)

    span configuration

  • otl_tracer (Object)

    OpenTelemetry tracer instance



33
34
35
36
# File 'lib/yes/core/open_telemetry/otl_span.rb', line 33

def initialize(otl_data:, otl_tracer:)
  @otl_data = otl_data
  @otl_tracer = otl_tracer
end

Instance Attribute Details

#otl_dataOtlData (readonly)

Returns span configuration.

Returns:



26
27
28
# File 'lib/yes/core/open_telemetry/otl_span.rb', line 26

def otl_data
  @otl_data
end

#otl_tracerObject (readonly)

Returns OpenTelemetry tracer instance.

Returns:

  • (Object)

    OpenTelemetry tracer instance



29
30
31
# File 'lib/yes/core/open_telemetry/otl_span.rb', line 29

def otl_tracer
  @otl_tracer
end

Instance Method Details

#otl_span(*args, **kwargs) { ... } ⇒ Object

Creates a span and executes the given block within it.

Parameters:

  • args (Array)

    positional arguments passed to the links extractor

  • kwargs (Hash)

    keyword arguments passed to the links extractor

Yields:

  • the block to execute within the span

Returns:

  • (Object)

    the return value of the block



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yes/core/open_telemetry/otl_span.rb', line 44

def otl_span(*args, **kwargs, &block)
  links = otl_links(args, kwargs)

  parent_span = ::OpenTelemetry::Trace.current_span.context.valid? ? ::OpenTelemetry::Trace.current_span : nil
  root_track_sql = parent_span&.try(:attributes)&.[]('root_track_sql') ||
                   parent_span&.try(:attributes)&.[]('track_sql')

  otl_tracer.in_span(
    otl_data.span_name || 'UnknownName',
    links:,
    kind: otl_data.span_kind,
    attributes: {
      'track_sql' => otl_data.track_sql,
      'root_track_sql' => root_track_sql || false
    }.merge(otl_data.span_attributes)
  ) do
    next unless block_given?
    next yield if !root_track_sql && !otl_data.track_sql
    next yield if parent_span.present? && root_track_sql

    callback = lambda do |sql_event|
      next if %w[SCHEMA TRANSACTION].include?(sql_event.payload[:name])

      otl_tracer.in_span("SQL #{sql_event.payload[:name]}") do |span|
        span.set_attribute('db.system', 'postgresql')
        span.set_attribute('db.statement', sql_event.payload[:sql])
        span.set_attribute('db.binds', sql_event.payload[:binds].map do |attr|
          next { name: attr.name, value: attr.value } if attr.respond_to?(:name) && attr.respond_to?(:value)

          { name: attr.class.to_s, value: attr }
        end.to_json)
        span.set_attribute('db.event_name', sql_event.payload[:name])
      end
    end
    ActiveSupport::Notifications.subscribed(callback, 'sql.active_record', &block)
  end
end