class OpenTelemetry::Instrumentation::RSpec::Formatter

An RSpec Formatter that outputs Otel spans

Attributes

output[R]

Public Class Methods

current_timestamp() click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 21
def self.current_timestamp
  @clock.call
end
new(output = StringIO.new, tracer_provider = OpenTelemetry.tracer_provider) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 25
def initialize(output = StringIO.new, tracer_provider = OpenTelemetry.tracer_provider)
  @spans_and_tokens = []
  @output = output
  @tracer_provider = tracer_provider
end

Public Instance Methods

current_timestamp() click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 35
def current_timestamp
  self.class.current_timestamp
end
example_finished(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 69
def example_finished(notification)
  pop_and_finalize_span do |span|
    result = notification.example.execution_result

    span.set_attribute('rspec.example.result', result.status.to_s)

    if (exception = result.exception)
      span.record_exception(exception)
      span.set_attribute('rspec.example.failure_message', exception.message) if exception.is_a? ::RSpec::Expectations::ExpectationNotMetError
      span.status = OpenTelemetry::Trace::Status.error(exception.message)
    end
  end
end
example_group_finished(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 54
def example_group_finished(notification)
  pop_and_finalize_span
end
example_group_started(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 48
def example_group_started(notification)
  description = notification.group.description
  span = tracer.start_span(description, start_timestamp: current_timestamp)
  track_span(span)
end
example_started(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 58
def example_started(notification)
  example = notification.example
  attributes = {
    'rspec.example.location' => example.location.to_s,
    'rspec.example.full_description' => example.full_description.to_s,
    'rspec.example.described_class' => example.metadata[:described_class].to_s
  }
  span = tracer.start_span(example.description, attributes: attributes, start_timestamp: current_timestamp)
  track_span(span)
end
pop_and_finalize_span() { |span| ... } click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 90
def pop_and_finalize_span
  span, token = *@spans_and_tokens.shift
  return unless span.recording?

  yield span if block_given?

  span.finish(end_timestamp: current_timestamp)
  OpenTelemetry::Context.detach(token)
end
start(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 39
def start(notification)
  span = tracer.start_span('RSpec suite', start_timestamp: current_timestamp)
  track_span(span)
end
stop(notification) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 44
def stop(notification)
  pop_and_finalize_span
end
tracer() click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 31
def tracer
  @tracer ||= @tracer_provider.tracer('OpenTelemetry::Instrumentation::RSpec', OpenTelemetry::Instrumentation::RSpec::VERSION)
end
track_span(span) click to toggle source
# File lib/opentelemetry/instrumentation/rspec/formatter.rb, line 83
def track_span(span)
  token = OpenTelemetry::Context.attach(
    OpenTelemetry::Trace.context_with_span(span)
  )
  @spans_and_tokens.unshift([span, token])
end