Class: Karafka::Core::Monitoring::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/core/monitoring/event.rb

Overview

Single notification event wrapping payload with id

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, payload, time = nil) ⇒ Event

Returns a new instance of Event.

Parameters:

  • id (String, Symbol)

    id of the event

  • payload (Hash)

    event payload

  • time (Float, nil) (defaults to: nil)

    execution time, stored separately to avoid eager hash allocation. Merged into the payload lazily only when ‘#payload` is accessed.



14
15
16
17
18
19
# File 'lib/karafka/core/monitoring/event.rb', line 14

def initialize(id, payload, time = nil)
  @id = id
  @raw_payload = payload
  @time = time
  @payload = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/karafka/core/monitoring/event.rb', line 8

def id
  @id
end

Instance Method Details

#[](name) ⇒ Object

Hash access to the payload data (if present) Provides direct access to time without triggering payload hash construction.

Parameters:

  • name (String, Symbol)


35
36
37
38
39
# File 'lib/karafka/core/monitoring/event.rb', line 35

def [](name)
  return @time if name == :time && @time

  @raw_payload.fetch(name)
end

#payloadHash

Returns full payload including time (if set). The merged hash is built lazily on first access to avoid allocating a new Hash when listeners only use ‘#[]`.

Returns:

  • (Hash)

    full payload including time (if set). The merged hash is built lazily on first access to avoid allocating a new Hash when listeners only use ‘#[]`.



23
24
25
26
27
28
29
# File 'lib/karafka/core/monitoring/event.rb', line 23

def payload
  @payload ||= if @time
    @raw_payload.empty? ? { time: @time } : @raw_payload.merge(time: @time)
  else
    @raw_payload
  end
end