Class: RaceGuard::Event
- Inherits:
-
Object
- Object
- RaceGuard::Event
- Defined in:
- lib/race_guard/event.rb
Overview
Structured event payload for reporting and JSON serialization.
Constant Summary collapse
- SCHEMA =
contextmay include optional well-known string keys, for example:-
suggested_fix— short remediation for human-readable logs (LogReporter) -
protect/protect_stack— set when reporting insideRaceGuard.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
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#detector ⇒ Object
readonly
Returns the value of attribute detector.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#severity ⇒ Object
readonly
Returns the value of attribute severity.
-
#thread_id ⇒ Object
readonly
Returns the value of attribute thread_id.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(detector:, message:, severity:, location: nil, thread_id: nil, context: {}, timestamp: Time.now) ⇒ Event
constructor
A new instance of Event.
- #to_h ⇒ Object
- #with_merged_context(extra) ⇒ Object
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 = .to_s @severity = validate_severity(severity) @location = location&.to_s @thread_id = thread_id&.to_s @context = context.is_a?(Hash) ? context : {} @timestamp = end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def context @context end |
#detector ⇒ Object (readonly)
Returns the value of attribute detector.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def detector @detector end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def location @location end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def @message end |
#severity ⇒ Object (readonly)
Returns the value of attribute severity.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def severity @severity end |
#thread_id ⇒ Object (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 |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
21 22 23 |
# File 'lib/race_guard/event.rb', line 21 def @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 = "report payload must be a RaceGuard::Event or a Hash (got #{payload.class})" raise ArgumentError, end end |
Instance Method Details
#to_h ⇒ Object
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' => , 'severity' => severity.to_s, '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: , severity: severity, location: location, thread_id: thread_id, context: merged, timestamp: ) end |