Class: RaceGuard::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/race_guard/event.rb

Overview

Structured event payload for reporting and JSON serialization.

Constant Summary collapse

SCHEMA =

context may include optional well-known string keys, for example:

  • suggested_fix — short remediation for human-readable logs (LogReporter)

  • protect / protect_stack — set when reporting inside RaceGuard.protect

{
  'context' => 'Hash (JSON-serializable values; see class comment for optional keys)',
  'detector' => 'String',
  'location' => 'String, optional',
  'message' => 'String',
  'severity' => 'String (one of info, warn, error, raise)',
  'thread_id' => 'String, optional',
  'timestamp' => 'String (ISO 8601)'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(detector:, message:, severity:, location: nil, thread_id: nil, context: {}, timestamp: Time.now) ⇒ Event

Returns a new instance of Event.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/race_guard/event.rb', line 23

def initialize(
  detector:,
  message:,
  severity:,
  location: nil,
  thread_id: nil,
  context: {},
  timestamp: Time.now
)
  @detector = detector.to_s
  @message = message.to_s
  @severity = validate_severity(severity)
  @location = location&.to_s
  @thread_id = thread_id&.to_s
  @context = context.is_a?(Hash) ? context : {}
  @timestamp = timestamp
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def context
  @context
end

#detectorObject (readonly)

Returns the value of attribute detector.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def detector
  @detector
end

#locationObject (readonly)

Returns the value of attribute location.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def message
  @message
end

#severityObject (readonly)

Returns the value of attribute severity.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def severity
  @severity
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def thread_id
  @thread_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



21
22
23
# File 'lib/race_guard/event.rb', line 21

def timestamp
  @timestamp
end

Class Method Details

.from_payload(payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/race_guard/event.rb', line 41

def self.from_payload(payload)
  case payload
  when Event
    payload
  when Hash
    kwargs = symbolize_keys_for_new(payload)
    new(**kwargs)
  else
    message = "report payload must be a RaceGuard::Event or a Hash (got #{payload.class})"
    raise ArgumentError, message
  end
end

Instance Method Details

#to_hObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/race_guard/event.rb', line 68

def to_h
  base = {
    'context' => stringify_hash_keys(context),
    'detector' => detector,
    'message' => message,
    'severity' => severity.to_s,
    'timestamp' => timestamp.utc.iso8601(3)
  }
  base.merge(optional_to_h)
end

#with_merged_context(extra) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/race_guard/event.rb', line 79

def with_merged_context(extra)
  merged = stringify_hash_keys(context).merge(stringify_hash_keys(extra))
  self.class.new(
    detector: detector,
    message: message,
    severity: severity,
    location: location,
    thread_id: thread_id,
    context: merged,
    timestamp: timestamp
  )
end