Class: NulogyMessageBusConsumer::Steps::LogMessages

Inherits:
Object
  • Object
show all
Defined in:
lib/nulogy_message_bus_consumer/steps/log_messages.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger, clock: Clock.new) ⇒ LogMessages

Returns a new instance of LogMessages.



4
5
6
7
# File 'lib/nulogy_message_bus_consumer/steps/log_messages.rb', line 4

def initialize(logger, clock: Clock.new)
  @logger = logger
  @clock = clock
end

Instance Method Details

#call(message:, **_) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nulogy_message_bus_consumer/steps/log_messages.rb', line 9

def call(message:, **_)
  @logger.info(JSON.dump({
    event: "message_received",
    kafka_message_id: message.id,
    message: "Received #{message.id}"
  }))

  result = yield

  millis = diff_millis(message.created_at, @clock.ms)
  @logger.info(JSON.dump({
    event: "message_processed",
    kafka_message_id: message.id,
    message: "Processed #{message.id} (#{message.topic}##{message.partition}@#{message.offset})",
    result: result,
    time_to_processed: millis
  }))

  result
end

#diff_millis(oldest_nanos, newest_millis) ⇒ Object



32
33
34
35
36
# File 'lib/nulogy_message_bus_consumer/steps/log_messages.rb', line 32

def diff_millis(oldest_nanos, newest_millis)
  old_millis = oldest_nanos / 1000

  newest_millis - old_millis
end