Class: Gritz::Middleware::Logging

Inherits:
Object
  • Object
show all
Defined in:
/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/middleware/logging.rb

Overview

Logs one structured completion row per RPC, including streaming failures.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Logging

Returns a new instance of Logging.



10
11
12
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/middleware/logging.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(context) ⇒ Object



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
# File '/home/runner/work/gritz/gritz/sources/gritz-core/lib/gritz/middleware/logging.rb', line 14

def call(context)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  code = :ok
  @app.call(context)
rescue Gritz::Error => e
  code = e.code
  raise
rescue StandardError
  code = :internal
  raise
ensure
  context.logger.info do
    duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000
    fields = { request_id: context.request_id, service: context.method.service,
               method: context.method.name, code: code, duration_ms: duration.round(3),
               peer: context.peer, worker: context.worker, pid: Process.pid, bytes_in: context.bytes_in, bytes_out: context.bytes_out }
    fields.merge!(context.store.fetch(:gritz_error, {}))
    fields = prepare(fields, context.log_redact)
    if context.log_format == :logfmt
      fields.map do |key, value|
        value = value.to_s if value.is_a?(Symbol)
        value = JSON.generate(value) if value.is_a?(Hash) || value.is_a?(Array)
        "#{key}=#{JSON.generate(value)}"
      end.join(" ")
    else
      JSON.generate(fields)
    end
  end
end