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.



14
15
16
17
18
19
# File 'lib/rspec_telemetry/recorder.rb', line 14

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.



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

def config
  @config
end

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

Class Method Details

.thread_idObject



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

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



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

def clear_current_example
  Thread.current[EXAMPLE_ID] = nil
end

#common_fields(type) ⇒ Object



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

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



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

def finish
  return unless @started

  @writer.close
  @started = false
end

#flushObject



41
42
43
# File 'lib/rspec_telemetry/recorder.rb', line 41

def flush
  @writer.flush
end

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



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

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



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

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

#startObject



21
22
23
24
25
26
# File 'lib/rspec_telemetry/recorder.rb', line 21

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

  @writer.open
  @started = true
end

#started?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rspec_telemetry/recorder.rb', line 28

def started?
  @started
end