Class: Fluent::Plugin::ArchagentOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_archagent.rb

Instance Method Summary collapse

Constructor Details

#initializeArchagentOutput

Returns a new instance of ArchagentOutput.



5
6
7
8
9
# File 'lib/fluent/plugin/out_archagent.rb', line 5

def initialize
  super
  require 'net/http'
  require 'uri'
end

Instance Method Details

#configure(conf) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fluent/plugin/out_archagent.rb', line 15

def configure(conf)
  super

  endpoint_url = 'http://localhost:' + @port
  # Check if endpoint URL is valid
  unless endpoint_url =~ /^#{URI.regexp}$/
    fail Fluent::ConfigError, 'port invalid'
  end

  begin
    @uri = URI.parse(endpoint_url)
  rescue URI::InvalidURIError
    raise Fluent::ConfigError, 'port invalid'
  end
end

#connectObject



64
65
66
67
68
69
70
# File 'lib/fluent/plugin/out_archagent.rb', line 64

def connect
  @http ||= Net::HTTP.start(
    @uri.host,
    @uri.port,
    keep_alive_timeout: 60.0
  )
end

#disconnectObject



72
73
74
75
76
# File 'lib/fluent/plugin/out_archagent.rb', line 72

def disconnect
  return unless defined?(@http)
  return unless @http
  @http.finish
end

#format(tag, time, record) ⇒ Object



31
32
33
# File 'lib/fluent/plugin/out_archagent.rb', line 31

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#shutdownObject



39
40
41
42
43
# File 'lib/fluent/plugin/out_archagent.rb', line 39

def shutdown
  super

  disconnect
end

#startObject



35
36
37
# File 'lib/fluent/plugin/out_archagent.rb', line 35

def start
  super
end

#write(chunk) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fluent/plugin/out_archagent.rb', line 45

def write(chunk)
  log_data = []
  chunk.msgpack_each do |(tag, time, record)|
    record['host'] = (record.has_key? 'host') && record['host'] != nil && record['host'] != "" ? \
        record['host'].to_s : 'unknown'
    record['code'] = (record.has_key? 'code') && record['code'] != nil && record['code'] != "" ? \
        record['code'].to_i : -1
    record['size'] = (record.has_key? 'size') && record['size'] != nil && record['size'] != "" ? \
        record['size'].to_i : -1
    record['latency'] = (record.has_key? 'latency') && record['latency'] != nil && \
        record['latency'] != "" ? record['latency'].to_i : -1
    log_data << record
  end

  log_data_req = create_request(log_data)
  response = connect.request(log_data_req)
  return
end