Class: Observability::Event

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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
  @timestamp = Time.now
  @start     = Concurrent.monotonic_time
  @fields    = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Delegate some read-only methods to the #fields Hash.



52
53
54
# File 'lib/observability/event.rb', line 52

def fields
  @fields
end

#startObject (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

#timestampObject (readonly)

The Time the event was created



44
45
46
# File 'lib/observability/event.rb', line 44

def timestamp
  @timestamp
end

#typeObject (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

#resolveObject 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.timestamp,
      :@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