Class: RSpecTelemetry::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_telemetry/recorder.rb

Constant Summary collapse

EXAMPLE_ID =

FactoryBot notifications read this to attach themselves to the active example.

:rspec_telemetry_example_id

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, writer: nil, summary: nil) ⇒ Recorder

Returns a new instance of Recorder.



12
13
14
15
16
17
# File 'lib/rspec_telemetry/recorder.rb', line 12

def initialize(config, writer: nil, summary: nil)
  @config = config
  @writer = writer || Writer.new(config.output_path, flush_each: config.flush_each)
  @summary = summary || Summary.new(config)
  @started = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/rspec_telemetry/recorder.rb', line 10

def config
  @config
end

#summaryObject (readonly)

Returns the value of attribute summary.



10
11
12
# File 'lib/rspec_telemetry/recorder.rb', line 10

def summary
  @summary
end

Class Method Details

.thread_idObject



70
71
72
73
# File 'lib/rspec_telemetry/recorder.rb', line 70

def self.thread_id
  t = Thread.current
  t.respond_to?(:native_thread_id) && t.native_thread_id ? t.native_thread_id : t.object_id
end

Instance Method Details

#clear_current_exampleObject



66
67
68
# File 'lib/rspec_telemetry/recorder.rb', line 66

def clear_current_example
  Thread.current[EXAMPLE_ID] = nil
end

#common_fields(type) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec_telemetry/recorder.rb', line 51

def common_fields(type)
  {
    type: type,
    timestamp: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ"),
    monotonic_time: Process.clock_gettime(Process::CLOCK_MONOTONIC),
    pid: Process.pid,
    thread_id: self.class.thread_id,
    example_id: Thread.current[EXAMPLE_ID]
  }
end

#finishObject



43
44
45
46
47
48
49
# File 'lib/rspec_telemetry/recorder.rb', line 43

def finish
  return unless @started

  SummaryPrinter.print(@summary, @config) if @config.print_summary
  @writer.close
  @started = false
end

#flushObject



39
40
41
# File 'lib/rspec_telemetry/recorder.rb', line 39

def flush
  @writer.flush
end

#record(type, fields = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rspec_telemetry/recorder.rb', line 30

def record(type, fields = {})
  return unless @config.enabled && @started

  event = common_fields(type).merge(fields)
  @writer.write(event)
  @summary.add(event)
  event
end

#set_current_example(id) ⇒ Object



62
63
64
# File 'lib/rspec_telemetry/recorder.rb', line 62

def set_current_example(id)
  Thread.current[EXAMPLE_ID] = id
end

#startObject



19
20
21
22
23
24
# File 'lib/rspec_telemetry/recorder.rb', line 19

def start
  return if @started || !@config.enabled

  @writer.open
  @started = true
end

#started?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rspec_telemetry/recorder.rb', line 26

def started?
  @started
end