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.
-
#id ⇒ Object
readonly
The ID of the event, used to pass context through application boundaries.
-
#parent_id ⇒ Object
readonly
The ID of the containing context event, if there is one.
-
#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’.
Class Method Summary collapse
-
.generate_id ⇒ Object
Generate a new Event ID.
-
.id_generator ⇒ Object
Return a generator that can return a unique ID string for identifying Events across application boundaries.
Instance Method Summary collapse
-
#initialize(type, parent = nil, **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, parent = nil, **fields) ⇒ Event
Create a new event
40 41 42 43 44 45 46 47 |
# File 'lib/observability/event.rb', line 40 def initialize( type, parent=nil, **fields ) @id = self.class.generate_id @parent_id = parent&.id @type = type.freeze @timestamp = Time.now @start = Concurrent.monotonic_time @fields = fields end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Delegate some read-only methods to the #fields Hash.
76 77 78 |
# File 'lib/observability/event.rb', line 76 def fields @fields end |
#id ⇒ Object (readonly)
The ID of the event, used to pass context through application boundaries
56 57 58 |
# File 'lib/observability/event.rb', line 56 def id @id end |
#parent_id ⇒ Object (readonly)
The ID of the containing context event, if there is one
60 61 62 |
# File 'lib/observability/event.rb', line 60 def parent_id @parent_id end |
#start ⇒ Object (readonly)
The monotonic clock time of when the event was created
72 73 74 |
# File 'lib/observability/event.rb', line 72 def start @start end |
#timestamp ⇒ Object (readonly)
The Time the event was created
68 69 70 |
# File 'lib/observability/event.rb', line 68 def @timestamp end |
#type ⇒ Object (readonly)
The type of the event, which should be a string of the form: ‘foo.bar.baz’
64 65 66 |
# File 'lib/observability/event.rb', line 64 def type @type end |
Class Method Details
.generate_id ⇒ Object
Generate a new Event ID.
34 35 36 |
# File 'lib/observability/event.rb', line 34 def self::generate_id return self.id_generator.generate end |
.id_generator ⇒ Object
Return a generator that can return a unique ID string for identifying Events across application boundaries.
28 29 30 |
# File 'lib/observability/event.rb', line 28 def self::id_generator return @id_generator ||= UUID.new end |
Instance Method Details
#merge(fields) ⇒ Object
Merge the specified fields into the event’s data. :TODO: Handle conflicts?
86 87 88 89 90 |
# File 'lib/observability/event.rb', line 86 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/observability/event.rb', line 94 def resolve unless @fields.frozen? self.log.debug "Resolving event %#x" % [ self.object_id ] data = self.fields.merge( :@id => self.id, :@parent_id => self.parent_id, :@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.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/observability/event.rb', line 114 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 |