Module: Fluent::Mixin::Deis

Included in:
Plugin::DeisOutput
Defined in:
lib/fluent/mixin/deis.rb

Instance Method Summary collapse

Instance Method Details

#build_series(message) ⇒ Object



21
22
23
24
25
26
27
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
# File 'lib/fluent/mixin/deis.rb', line 21

def build_series(message)
  metric = parse_router_log(message['log'], message['kubernetes']['host'])
  if metric
    metric_timestamp = (
      metric['timestamp'].to_f * 1_000_000_000 + Time.now.nsec
    ).to_i # Using current nanosecond resolution to avoid replacing points

    tags = { app: metric['app'], status_code: metric['status_code'], host: metric['host'] }
    data = [
      {
        series: 'deis_router_request_time_ms',
        values: { value: metric['request_time'] },
        tags: tags,
        timestamp: metric_timestamp
      },
      {
        series: 'deis_router_response_time_ms',
        values: { value: metric['response_time'] },
        tags: tags,
        timestamp: metric_timestamp
      },
      {
        series: 'deis_router_bytes_sent',
        values: { value: metric['bytes_sent'] },
        tags: tags,
        timestamp: metric_timestamp
      }
    ]
    return data
  end
  nil
end

#from_container?(message, regex) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/fluent/mixin/deis.rb', line 14

def from_container?(message, regex)
  if kubernetes? message
    return true unless Regexp.new(regex).match(message['kubernetes']['container_name']).nil?
  end
  false
end

#from_router?(message) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/fluent/mixin/deis.rb', line 10

def from_router?(message)
  from_container?(message, 'deis-router')
end

#kubernetes?(message) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/fluent/mixin/deis.rb', line 6

def kubernetes?(message)
  !message['kubernetes'].nil?
end

#parse_router_log(message, host) ⇒ Object

href="2016-05-31T16:56:12+00:00">log”=>“ - foo - 10.164.1.1 - - - 200 - "GET / HTTP/1.0" - 211 - "-" - "ApacheBench/2.3" - "~^foo\x5C.(?<domain>.+)$" - 10.167.243.4:80 - foo.devart.io - 0.002 - 0.046n” request_time - request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client response_time - keeps time spent on receiving the response from the upstream server; the time is kept in seconds with millisecond resolution.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/mixin/deis.rb', line 57

def parse_router_log(message, host)
  split_message = message.split(' - ')
  return nil if split_message.length < 14
  metric = {}
  metric['timestamp'] = Time.parse(split_message[0])
  metric['app'] = split_message[1].strip
  metric['status_code'] = split_message[4].strip
  metric['bytes_sent'] = split_message[6].strip.to_f
  metric['response_time'] = split_message[12].strip.to_f
  metric['request_time'] = split_message[13].strip.to_f
  metric['host'] = host
  return metric
rescue Exception => e # rubocop:disable RescueException
  puts "Error:#{e.message}"
  return nil
end