Class: Fluent::Plugin::BunyanParser

Inherits:
JSONParser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_bunyan.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



23
24
25
26
# File 'lib/fluent/plugin/parser_bunyan.rb', line 23

def configure(conf)
  conf["time_format"] = "%iso8601"
  super(conf)
end

#parse(text) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/parser_bunyan.rb', line 28

def parse(text)
  # Example of a log line from containerd we want to get stuff from:
  # 2020-03-16T22:14:05.710251038+09:00 stdout F {"UserAgent":"kube-probe/1.17","hostname":"helloworld-go-helloworld-5649dfd7bd-k8954","level":30,"msg":"request start: GET /","name":"helloworld","pid":1,"remote":"10.1.10.1:60114","reqid":"e4745ed6-82d4-4013-aeda-1d02bef42331","scope":"/","tid":17,"time":"2020-03-16T13:14:05Z","topic":"route","v":0}
  # Example of a log line from docker for mac we want to get stuff from:
  # {"log":"{\"UserAgent\":\"HTTPie/2.0.0\",\"hostname\":\"helloworld-859745b47d-5dlbn\",\"level\":30,\"msg\":\"request start: GET /\",\"name\":\"helloworld\",\"pid\":1,\"remote\":\"192.168.65.3:42018\",\"reqid\":\"ba054f49-d97e-4970-89c4-dd3b75270e6a\",\"scope\":\"/\",\"tid\":14,\"time\":\"2020-03-24T08:29:02Z\",\"topic\":\"route\",\"v\":0}\n","stream":"stdout","time":"2020-03-24T08:29:02.3995324Z"}
  if text =~ /[^{]*({.*)/
    super(text.sub(/[^{]*({.*)/, '\1')) do |time, record|
      if record then
        if record["log"] then # This is a Docker style log message
          begin
            r = JSON.load(record["log"])
            r.delete("time")
            record.merge!(r)
            record.delete("log")
          rescue JSON::ParserError
          end
        end
        record["severity"] = severity(record["level"] || 0)
        record["host"]     = record.delete("hostname")
        record["message"]  = record.delete("msg")

        record.delete("v")
      end
      yield time, record
    end
  end
end

#severity(level) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/parser_bunyan.rb', line 56

def severity(level)
  return "fatal" if level >= 60
  return "error" if level >= 50
  return "warn"  if level >= 40
  return "info"  if level >= 30
  return "debug" if level >= 20
  return "trace"
end

#versionObject



65
66
67
# File 'lib/fluent/plugin/parser_bunyan.rb', line 65

def version
  return "0.0.5"
end