Class: CommandTower::Messaging::Contract::Observability::OperationLogger

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/contract/observability/operation_logger.rb

Class Method Summary collapse

Class Method Details

.around(operation:, request:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/command_tower/messaging/contract/observability/operation_logger.rb', line 9

def around(operation:, request:)
  correlation_id = Correlation.resolve
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  base = {
    correlation_id:,
    messaging_operation: operation,
  }.merge(request_fields(request))

  Publisher.info(
    base.merge(event: "messaging.#{operation}.started"),
  )

  result = yield

  Publisher.info(
    base.merge(
      event: "messaging.#{operation}.succeeded",
      duration_ms: elapsed_ms(started_at),
    ).merge(result_fields(result)),
  )

  result
rescue Contract::ValidationError => e
  log_failure(base, started_at, operation, e, :warn, "validation_failed")
  raise
rescue Contract::NotFoundError => e
  log_failure(base, started_at, operation, e, :warn, "not_found")
  raise
rescue Contract::InvariantError => e
  log_failure(base, started_at, operation, e, :error, "invariant_violation")
  raise
rescue StandardError => e
  log_failure(base, started_at, operation, e, :error, "unexpected")
  raise
end