Class: LogentriesOutput

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

Defined Under Namespace

Classes: ConnectionFailure

Instance Method Summary collapse

Instance Method Details

#clientObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/out_logentries.rb', line 33

def client
  @_socket ||= if @use_ssl
    context    = OpenSSL::SSL::SSLContext.new
    socket     = TCPSocket.new "api.logentries.com", 20000
    ssl_client = OpenSSL::SSL::SSLSocket.new socket, context

    ssl_client.connect
  else
    TCPSocket.new "data.logentries.com", @no_ssl_port
  end
end

#configure(conf) ⇒ Object



18
19
20
21
22
23
# File 'lib/fluent/plugin/out_logentries.rb', line 18

def configure(conf)
  super

  @tokens    = nil
  @last_edit = Time.at(0)
end

#format(tag, time, record) ⇒ Object

This method is called when an event reaches Fluentd.



46
47
48
# File 'lib/fluent/plugin/out_logentries.rb', line 46

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

#generate_tokens_listObject

Parse an YML file and generate a list of tokens. It will only re-generate the list on changes.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/out_logentries.rb', line 52

def generate_tokens_list
  begin
    edit_time = File.mtime(@config_path)

    if edit_time > @last_edit
      @tokens    = YAML::load_file(@config_path)
      @last_edit = edit_time

      log.info "Token(s) list updated."
    end
  rescue Exception => e
    log.warn "Could not load configuration. #{e.message}"
  end
end

#get_token(tag, record) ⇒ Object

Returns the correct token to use for a given tag / records



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fluent/plugin/out_logentries.rb', line 68

def get_token(tag, record)
  app_name = record["app_name"] || ""

  # Config Structure
  # -----------------------
  # app-name:
  #   app: TOKEN
  #   access: TOKEN (optional)
  #   error: TOKEN  (optional)
  @tokens.each do |key, value|
    if tag.index(key) != nil || app_name.index(key) != nil
      default = value['app']

      case tag
        when @tag_access_log
          return value['access'] || default
        when @tag_error_log
          return value['error']  || default

        else
          return default
      end
    end
  end

  return nil
end

#send_logentries(data) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin/out_logentries.rb', line 112

def send_logentries(data)
  retries = 0
  begin
    client.puts data
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
    if retries < @max_retries
      retries += 1
      @_socket = nil
      log.warn "Could not push logs to Logentries, resetting connection and trying again. #{e.message}"
      sleep 5**retries
      retry
    end
    raise ConnectionFailure, "Could not push logs to Logentries after #{retries} retries. #{e.message}"
  end
end

#shutdownObject



29
30
31
# File 'lib/fluent/plugin/out_logentries.rb', line 29

def shutdown
  super
end

#startObject



25
26
27
# File 'lib/fluent/plugin/out_logentries.rb', line 25

def start
  super
end

#write(chunk) ⇒ Object

NOTE! This method is called by internal thread, not Fluentd’s main thread. So IO wait doesn’t affect other plugins.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/out_logentries.rb', line 97

def write(chunk)
  generate_tokens_list()
  return unless @tokens.is_a? Hash

  chunk.msgpack_each do |tag, record|
    next unless record.is_a? Hash
    next unless record.has_key? "message"

    token = get_token(tag, record)
    next if token.nil?

    send_logentries(token + ' ' + record["message"])
  end
end