Class: Yes::Core::OpenTelemetry::OtlSpan
- Inherits:
-
Object
- Object
- Yes::Core::OpenTelemetry::OtlSpan
- Defined in:
- lib/yes/core/open_telemetry/otl_span.rb
Overview
Wraps OpenTelemetry span creation with SQL tracking support.
Defined Under Namespace
Classes: OtlData
Instance Attribute Summary collapse
-
#otl_data ⇒ OtlData
readonly
Span configuration.
-
#otl_tracer ⇒ Object
readonly
OpenTelemetry tracer instance.
Instance Method Summary collapse
-
#initialize(otl_data:, otl_tracer:) ⇒ OtlSpan
constructor
A new instance of OtlSpan.
-
#otl_span(*args, **kwargs) { ... } ⇒ Object
Creates a span and executes the given block within it.
Constructor Details
#initialize(otl_data:, otl_tracer:) ⇒ OtlSpan
Returns a new instance of OtlSpan.
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_data ⇒ OtlData (readonly)
Returns span configuration.
26 27 28 |
# File 'lib/yes/core/open_telemetry/otl_span.rb', line 26 def otl_data @otl_data end |
#otl_tracer ⇒ Object (readonly)
Returns 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.
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 |