Module: Nais::Log::Parser

Defined in:
lib/nais/log/parser.rb,
lib/nais/log/parser/version.rb

Constant Summary collapse

VERSION =
"0.6.1"

Class Method Summary collapse

Class Method Details

.flatten_hash(hash, path = "") ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/nais/log/parser.rb', line 9

def Parser.flatten_hash(hash, path = "")
  hash.each_with_object({}) do |(k, v), ret|
    key = (path + k.to_s).tr('.','_')
    if v.is_a? Hash
      ret.merge! Parser.flatten_hash(v, key.to_s + "_")
    else
      ret[key] = v
    end
  end
end

.get_exceptions(str) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/nais/log/parser.rb', line 20

def Parser.get_exceptions(str)
  exps = str.scan(/\b[A-Z]\w+Exception\b/)
  if exps.any?
    exps.uniq!
    exps.size == 1 ? exps.first : exps
  end
end

.parse_accesslog(str) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nais/log/parser.rb', line 74

def Parser.parse_accesslog(str)
  if m = str.match(/^(\S+) +(?:(\S+) )?(\S+) \[([^\]]+)\] \"([^\"]*)\" (\S+) (\S+)(.*)/)
    r = {}
    r['remote_ip'] = m[1]
    r['ident'] = m[2] unless (m[2].nil? || m[2] == '-')
    r['user'] = m[3] unless m[3] == '-'
    r['timestamp'] = Time.strptime(m[4], "%d/%b/%Y:%H:%M:%S %Z").iso8601
    r['request'] = m[5]
    r['response_code'] = m[6] unless m[6] == '-'
    r['content_length'] = m[7] unless m[7] == '-'
    ext = m[8] unless m[8] == ''
    return r, ext
  else
    return nil
  end
end

.parse_accesslog_with_processing_time(str) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/nais/log/parser.rb', line 91

def Parser.parse_accesslog_with_processing_time(str)
  p = Parser.parse_accesslog(str)
  if !p.nil? && m = p[1].match(/^\s+([0-9.]+)(?:[�m]?s)?$/)
    p[0]['processing_time'] = m[1]
    return p[0]
  else
    return nil
  end
end

.parse_glog(str) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nais/log/parser.rb', line 101

def Parser.parse_glog(str)
  if m = str.match(/^([IWEF])(\d{4} \d\d:\d\d:\d\d.\d{6})\s+(\S+)\s([^:]+):(\d+)\]\s+(.*)/)
    r = {}
    r['level'] = case m[1]
                 when 'I'
                   'Info'
                 when 'W'
                   'Warning'
                 when 'E'
                   'Error'
                 when 'F'
                   'Fatal'
                 end
    r['timestamp'] = Time.strptime(m[2], "%m%d %H:%M:%S.%N").iso8601(9)
    r['thread'] = m[3]
    r['file'] = m[4]
    r['line'] = m[5]
    r['message'] = m[6]
    return r
  else
    return nil
  end
end

.prefix_nonstandard_fields(record) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nais/log/parser.rb', line 62

def Parser.prefix_nonstandard_fields(record)
  r = {}
  record.each{|k,v|
    if k =~ /^(?:@timestamp|@version|type|received_at|message|container|host|namespace|application|pod|thread|component|category|level|stack_trace|exception|cluster|envclass|content_length|remote_ip|response_code|user|ident|processing_time)$/
      r[k] = record[k]
    else
      r["x_"+k] = record[k]
    end
  }
  r
end

.remap_elasticsearch_fields(time, record) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/nais/log/parser.rb', line 44

def Parser.remap_elasticsearch_fields(time, record)
  record["received_at"] = Time.new.iso8601(9)
  if !record.has_key?("@timestamp")
    record["@timestamp"] = record.delete("timestamp") if record.has_key?("timestamp")
    record["@timestamp"] = Time.at(time).iso8601(9) unless record.has_key?("@timestamp")
  end
  record["message"] = record.delete("log") unless record.has_key?("message")
  record
end

.remap_java_fields(record) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/nais/log/parser.rb', line 54

def Parser.remap_java_fields(record)
  record["thread"] = record.delete("thread_name") if record.has_key?("thread_name")
  record["component"] = record.delete("logger_name") if record.has_key?("logger_name")
  record["level"].capitalize! if record.has_key?("level")
  record.delete("level_value")
  record
end

.remap_kubernetes_fields(record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nais/log/parser.rb', line 28

def Parser.remap_kubernetes_fields(record)
  record["category"] = record.delete("stream") if record.has_key?("stream")
  if record["docker"].is_a?(Hash)
    record["container"] = record["docker"]["container_id"]
    record.delete("docker")
  end
  if record["kubernetes"].is_a?(Hash)
    record["host"] = record["kubernetes"]["host"]
    record["namespace"] = record["kubernetes"]["namespace_name"]
    record["application"] = record["kubernetes"]["container_name"]
    record["pod"] = record["kubernetes"]["pod_name"]
    record.delete("kubernetes")
  end
  record
end