Class: Heavylog::Formatters::ECS

Inherits:
Object
  • Object
show all
Defined in:
lib/heavylog/formatters/ecs.rb

Constant Summary collapse

ECS_MAP =

mapping from heavylog standard keys to ECS www.elastic.co/guide/en/ecs/current/ecs-reference.html

{
  "request_start"      => "@timestamp",
  "messages"           => "message",
  "request_id"         => "http.request.id",
  "method"             => "http.request.method",
  "referrer"           => "http.request.referrer",
  "format"             => "http.response.format",
  "status"             => "http.response.status_code",
  "location"           => "http.response.location",
  "ip"                 => "source.address",
  "host"               => "url.domain",
  "path"               => "url.original",
  "user_agent"         => "user_agent.original",
  "controller"         => "heavylog.controller",
  "action"             => "heavylog.action",
  "unpermitted_params" => "heavylog.unpermitted_params",
  "args"               => "heavylog.args",
  "duration"           => "heavylog.duration",
  "view_runtime"       => "heavylog.view_runtime",
  "db_runtime"         => "heavylog.db_runtime",
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/heavylog/formatters/ecs.rb', line 31

def call(data)
  ECS_MAP.each do |original, correct|
    dig_set(data, correct.split("."), data.delete(original)) if data.key?(original)
  end

  dig_set(data, %w[event module], "heavylog")
  dig_set(data, %w[event category], "web")

  unless data.dig("event", "dataset")
    value = data.dig("heavylog", "controller") == "SidekiqLogger" ? "heavylog.sidekiq" : "heavylog.rails"
    dig_set(data, %w[event dataset], value)
  end

  if (code = data.dig("http", "response", "status_code"))
    dig_set(data, %w[event outcome], (200..399).cover?(code) ? "success" : "failure")
  end

  dig_set(data, %w[source ip], data.dig("source", "address")) unless data.dig("source", "ip")
  dig_set(data, %w[url path], data.dig("url", "original")) unless data.dig("url", "path")

  ::JSON.dump(data)
end