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 =
{ 'context' => 'Hash (JSON-serializable values)', '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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/race_guard/event.rb', line 20 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.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def context @context end |
#detector ⇒ Object (readonly)
Returns the value of attribute detector.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def detector @detector end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def location @location end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def @message end |
#severity ⇒ Object (readonly)
Returns the value of attribute severity.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def severity @severity end |
#thread_id ⇒ Object (readonly)
Returns the value of attribute thread_id.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def thread_id @thread_id end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
18 19 20 |
# File 'lib/race_guard/event.rb', line 18 def @timestamp end |
Class Method Details
.from_payload(payload) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/race_guard/event.rb', line 38 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
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/race_guard/event.rb', line 65 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
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/race_guard/event.rb', line 76 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 |