Class: Fluent::ElasticsearchOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::ElasticsearchOutput
show all
- Includes:
- SetTagKeyMixin
- Defined in:
- lib/fluent/plugin/out_elasticsearch.rb
Defined Under Namespace
Classes: ConnectionFailure
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ElasticsearchOutput.
32
33
34
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 32
def initialize
super
end
|
Instance Method Details
#client ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 44
def client
@_es ||= begin
adapter_conf = lambda {|f| f.adapter :patron }
transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(get_connection_options.merge(
options: {
reload_connections: true,
retry_on_failure: 5,
transport_options: {
request: { timeout: @request_timeout }
}
}), &adapter_conf)
es = Elasticsearch::Client.new transport: transport
begin
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})!" unless es.ping
rescue Faraday::ConnectionFailed => e
raise ConnectionFailure, "Can not reach Elasticsearch cluster (#{connection_options_description})! #{e.message}"
end
log.info "Connection opened to Elasticsearch cluster => #{connection_options_description}"
es
end
end
|
36
37
38
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 36
def configure(conf)
super
end
|
#connection_options_description ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 101
def connection_options_description
get_connection_options[:hosts].map do |host_info|
attributes = host_info.dup
attributes[:password] = 'obfuscated' if attributes.has_key?(:password)
attributes.inspect
end.join(', ')
end
|
109
110
111
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 109
def format(tag, time, record)
[tag, time, record].to_msgpack
end
|
#get_connection_options ⇒ Object
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
95
96
97
98
99
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 68
def get_connection_options
raise "`password` must be present if `user` is present" if @user && !@password
hosts = if @hosts
@hosts.split(',').map do |host_str|
if host_str.match(%r{^[^:]+\:\d+$})
{
host: host_str.split(':')[0],
port: (host_str.split(':')[1] || @port).to_i,
scheme: @scheme
}
else
uri = URI(host_str)
%w(user password path).inject(host: uri.host, port: uri.port, scheme: uri.scheme) do |hash, key|
hash[key.to_sym] = uri.public_send(key) unless uri.public_send(key).nil? || uri.public_send(key) == ''
hash
end
end
end.compact
else
[{host: @host, port: @port, scheme: @scheme}]
end.each do |host|
host.merge!(user: @user, password: @password) if !host[:user] && @user
host.merge!(path: @path) if !host[:path] && @path
end
{
hosts: hosts
}
end
|
#send(data) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 158
def send(data)
retries = 0
begin
client.bulk body: data
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
if retries < 2
retries += 1
@_es = nil
log.warn "Could not push logs to Elasticsearch, resetting connection and trying again. #{e.message}"
sleep 2**retries
retry
end
raise ConnectionFailure, "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
end
end
|
#shutdown ⇒ Object
113
114
115
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 113
def shutdown
super
end
|
#start ⇒ Object
40
41
42
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 40
def start
super
end
|
#write(chunk) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/fluent/plugin/out_elasticsearch.rb', line 117
def write(chunk)
bulk_message = []
chunk.msgpack_each do |tag, time, record|
next unless record.is_a? Hash
if @logstash_format
if record.has_key?("@timestamp")
time = Time.parse record["@timestamp"]
else
record.merge!({"@timestamp" => Time.at(time).to_datetime.to_s})
end
if @utc_index
target_index = "#{@logstash_prefix}-#{Time.at(time).getutc.strftime("#{@logstash_dateformat}")}"
else
target_index = "#{@logstash_prefix}-#{Time.at(time).strftime("#{@logstash_dateformat}")}"
end
else
target_index = @index_name
end
if @include_tag_key
record.merge!(@tag_key => tag)
end
meta = { "index" => {"_index" => target_index, "_type" => type_name} }
if @id_key && record[@id_key]
meta['index']['_id'] = record[@id_key]
end
if @parent_key && record[@parent_key]
meta['index']['_parent'] = record[@parent_key]
end
bulk_message << meta
bulk_message << record
end
send(bulk_message) unless bulk_message.empty?
bulk_message.clear
end
|