Module: Chronos::Integrations::OpenTelemetry

Defined in:
lib/chronos/integrations/opentelemetry.rb

Overview

Optional, dependency-free bridge to an already configured OpenTelemetry SDK.

Class Method Summary collapse

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/chronos/integrations/opentelemetry.rb', line 24

def active?
  !current_context.empty?
end

.current_contextObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chronos/integrations/opentelemetry.rb', line 7

def current_context
  return {} unless defined?(::OpenTelemetry::Trace) && ::OpenTelemetry::Trace.respond_to?(:current_span)

  span = ::OpenTelemetry::Trace.current_span
  context = span.context if span && span.respond_to?(:context)
  return {} unless context && (!context.respond_to?(:valid?) || context.valid?)

  trace_id = hex_identifier(context, :hex_trace_id, :trace_id, 32)
  span_id = hex_identifier(context, :hex_span_id, :span_id, 16)
  return {} if trace_id.empty? || span_id.empty?

  {"trace_id" => trace_id, "span_id" => span_id,
   "trace_flags" => sampled?(context) ? "01" : "00", "source" => "opentelemetry"}
rescue StandardError
  {}
end

.hex_identifier(context, hex_method, numeric_method, width) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/chronos/integrations/opentelemetry.rb', line 28

def hex_identifier(context, hex_method, numeric_method, width)
  value = context.public_send(hex_method) if context.respond_to?(hex_method)
  value ||= context.public_send(numeric_method).to_i.to_s(16) if context.respond_to?(numeric_method)
  value.to_s.downcase.rjust(width, "0")[-width, width]
rescue StandardError
  ""
end

.sampled?(context) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/chronos/integrations/opentelemetry.rb', line 36

def sampled?(context)
  flags = context.trace_flags if context.respond_to?(:trace_flags)
  flags.respond_to?(:sampled?) ? flags.sampled? : flags.to_i.odd?
rescue StandardError
  false
end