Class: Observability::Event
- Inherits:
-
Object
- Object
- Observability::Event
- Extended by:
- Forwardable, Loggability
- Defined in:
- lib/observability/event.rb
Constant Summary collapse
- FORMAT_VERSION =
The event format version to send with all events
1
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Delegate some read-only methods to the #fields Hash.
-
#start ⇒ Object
readonly
The monotonic clock time of when the event was created.
-
#timestamp ⇒ Object
readonly
The Time the event was created.
-
#type ⇒ Object
readonly
The type of the event, which should be a string of the form: ‘foo.bar.baz’.
Instance Method Summary collapse
-
#initialize(type, **fields) ⇒ Event
constructor
Create a new event.
-
#merge(fields) ⇒ Object
Merge the specified
fieldsinto the event’s data. -
#resolve ⇒ Object
(also: #to_h)
Finalize all of the event’s data and return it as a Hash.
-
#resolve_value(value) ⇒ Object
Resolve the given
valueinto a serializable object.
Constructor Details
#initialize(type, **fields) ⇒ Event
Create a new event
26 27 28 29 30 31 |
# File 'lib/observability/event.rb', line 26 def initialize( type, **fields ) @type = type.freeze = Time.now @start = Concurrent.monotonic_time @fields = fields end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Delegate some read-only methods to the #fields Hash.
52 53 54 |
# File 'lib/observability/event.rb', line 52 def fields @fields end |
#start ⇒ Object (readonly)
The monotonic clock time of when the event was created
48 49 50 |
# File 'lib/observability/event.rb', line 48 def start @start end |
#timestamp ⇒ Object (readonly)
The Time the event was created
44 45 46 |
# File 'lib/observability/event.rb', line 44 def end |
#type ⇒ Object (readonly)
The type of the event, which should be a string of the form: ‘foo.bar.baz’
40 41 42 |
# File 'lib/observability/event.rb', line 40 def type @type end |
Instance Method Details
#merge(fields) ⇒ Object
Merge the specified fields into the event’s data. :TODO: Handle conflicts?
62 63 64 65 66 |
# File 'lib/observability/event.rb', line 62 def merge( fields ) self.fields.merge!( fields ) rescue FrozenError => err raise "event is already resolved", cause: err end |
#resolve ⇒ Object Also known as: to_h
Finalize all of the event’s data and return it as a Hash.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/observability/event.rb', line 70 def resolve unless @fields.frozen? self.log.debug "Resolving event %#x" % [ self.object_id ] data = self.fields.merge( :@type => self.type, :@timestamp => self., :@version => FORMAT_VERSION ) data = data.transform_values( &self.method(:resolve_value) ) @fields = data.freeze end return @fields end |
#resolve_value(value) ⇒ Object
Resolve the given value into a serializable object.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/observability/event.rb', line 88 def resolve_value( value ) case when value.respond_to?( :call ) # Procs, Methods return value.call( self ) when value.respond_to?( :iso8601 ) # Time, Date, DateTime, etc. return value.iso8601( 6 ) else return value end end |