Module: HAProxyLogParser

Defined in:
lib/haproxy_log_parser.rb,
lib/haproxy_log_parser/entry.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =
IO.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp.freeze

Class Method Summary collapse

Class Method Details

Converts a captured cookie string to a Hash.

Parameters:

  • string (String)

Returns:

  • (Hash{String => String})


83
84
85
86
87
88
89
90
# File 'lib/haproxy_log_parser.rb', line 83

def decode_captured_cookie(string)
  if string == '-'
    {}
  else
    key, value = string.split('=', 2)
    {unescape(key) => unescape(value)}
  end
end

.decode_captured_headers(string) ⇒ Array<String>

Converts a captured headers string to an Array.

Parameters:

  • string (String)

Returns:

  • (Array<String>)


96
97
98
# File 'lib/haproxy_log_parser.rb', line 96

def decode_captured_headers(string)
  string.split('|', -1).map! { |header| unescape(header) }
end

.parse(line) ⇒ Entry?

Returns an Entry object resulting from the given HAProxy HTTP-format log line, or nil if the line appears to be invalid.

Parameters:

  • line (String)

    a line from an HAProxy log

Returns:



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/haproxy_log_parser.rb', line 18

def parse(line)
  result = @parser.parse(line)
  return nil unless result

  entry = Entry.new
  [
    :client_ip, :frontend_name, :transport_mode,
    :backend_name, :server_name, :termination_state
  ].each do |field|
    entry.send("#{field}=", result.send(field).text_value)
  end
  [
    :client_port, :tq, :tw, :tc, :tr, :tt, :status_code, :bytes_read,
    :actconn, :feconn, :beconn, :srv_conn, :retries, :srv_queue,
    :backend_queue
  ].each do |field|
    entry.send("#{field}=", result.send(field).text_value.to_i)
  end

  entry.accept_date = parse_accept_date(result.accept_date.text_value)
  [:captured_request_cookie, :captured_response_cookie].each do |field|
    cookie = decode_captured_cookie(result.send(field).text_value)
    entry.send("#{field}=", cookie)
  end

  [:captured_request_headers, :captured_response_headers].each do |field|
    if result.send(field).respond_to?(:headers)
      headers = decode_captured_headers(
        result.send(field).headers.text_value
      )
    else
      headers = []
    end
    entry.send("#{field}=", headers)
  end

  entry.http_request = unescape(result.http_request.text_value)

  entry
end

.parse_accept_date(string) ⇒ Time

Converts the value of an accept_date field to a Time object.

Parameters:

  • string (String)

Returns:

  • (Time)


74
75
76
77
# File 'lib/haproxy_log_parser.rb', line 74

def parse_accept_date(string)
  parts = string.split(/[\/:.]/)
  Time.local(*parts.values_at(2, 1, 0, 3..6))
end

.unescape(string) ⇒ String

Returns the given string un-escaped. See the “Logging > Non-printable characters” section in HAProxy documentation.

Parameters:

  • string (String)

Returns:

  • (String)


64
65
66
67
68
# File 'lib/haproxy_log_parser.rb', line 64

def unescape(string)
  string.gsub(/#[[:xdigit:]]{2}/) do |match|
    match[1..-1].to_i(16).chr
  end
end