fluent-plugin-elasticsearch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +11 -0
- data/fluent-plugin-elasticsearch.gemspec +22 -0
- data/lib/fluent/plugin/out_elasticsearch.rb +62 -0
- data/test/helper.rb +0 -0
- data/test/plugin/test_out_elasticsearch.rb +140 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Fluent::Plugin::Elasticsearch
|
2
|
+
|
3
|
+
I wrote this so you can search logs routed through Fluentd.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install fluent-plugin-elasticsearch
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
In your fluentd configration, use `type elasticsearch`. Additional configuration is optional, default values would look like this:
|
12
|
+
|
13
|
+
```
|
14
|
+
host localhost
|
15
|
+
port 9200
|
16
|
+
index_name fluentd
|
17
|
+
index_type fluentd
|
18
|
+
```
|
19
|
+
|
20
|
+
|
21
|
+
**More options:**
|
22
|
+
|
23
|
+
```
|
24
|
+
logstash_format true # defaults to false
|
25
|
+
```
|
26
|
+
|
27
|
+
This is meant to make writing data into elasticsearch compatible to what logstash writes. By doing this, one could take advantade of [kibana](http://kibana.org/).
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
```
|
32
|
+
include_tag_key true # defaults to false
|
33
|
+
tag_key tag # defaults to tag
|
34
|
+
```
|
35
|
+
|
36
|
+
This will add the fluentd tag in the json record. For instance, if you have a config like this:
|
37
|
+
|
38
|
+
```
|
39
|
+
<match my.logs>
|
40
|
+
type elasticsearch
|
41
|
+
include_tag_key true
|
42
|
+
tag_key _key
|
43
|
+
</match>
|
44
|
+
```
|
45
|
+
|
46
|
+
The record inserted into elasticsearch would be
|
47
|
+
|
48
|
+
```
|
49
|
+
{"_key":"my.logs", "name":"Johnny Doeie"}
|
50
|
+
```
|
51
|
+
|
52
|
+
---
|
53
|
+
|
54
|
+
fluentd-plugin-elasticsearch is a buffered output that uses elasticseach's bulk API. So additional buffer configuration would be (with default values):
|
55
|
+
|
56
|
+
```
|
57
|
+
buffer_type memory
|
58
|
+
flush_interval 60
|
59
|
+
retry_limit 17
|
60
|
+
retry_wait 1.0
|
61
|
+
num_threads 1
|
62
|
+
```
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "fluent-plugin-elasticsearch"
|
6
|
+
s.version = '0.1.0'
|
7
|
+
s.authors = ["diogo"]
|
8
|
+
s.email = ["team@uken.com"]
|
9
|
+
s.description = %q{ElasticSearch output plugin for Fluent event collector}
|
10
|
+
s.summary = s.description
|
11
|
+
s.homepage = "https://github.com/uken/fluent-plugin-elasticsearch"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split($/)
|
14
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_runtime_dependency "fluentd"
|
19
|
+
|
20
|
+
s.add_development_dependency "rake"
|
21
|
+
s.add_development_dependency "webmock"
|
22
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'net/http'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
class Fluent::ElasticsearchOutput < Fluent::BufferedOutput
|
6
|
+
Fluent::Plugin.register_output('elasticsearch', self)
|
7
|
+
|
8
|
+
config_param :host, :string, :default => 'localhost'
|
9
|
+
config_param :port, :integer, :default => 9200
|
10
|
+
config_param :logstash_format, :bool, :default => false
|
11
|
+
config_param :type_name, :string, :default => "fluentd"
|
12
|
+
config_param :index_name, :string, :default => "fluentd"
|
13
|
+
|
14
|
+
include Fluent::SetTagKeyMixin
|
15
|
+
config_set_default :include_tag_key, false
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def configure(conf)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def start
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def format(tag, time, record)
|
30
|
+
[tag, time, record].to_msgpack
|
31
|
+
end
|
32
|
+
|
33
|
+
def shutdown
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
def write(chunk)
|
38
|
+
bulk_message = []
|
39
|
+
|
40
|
+
chunk.msgpack_each do |tag, time, record|
|
41
|
+
if @logstash_format
|
42
|
+
record.merge!({"@timestamp" => Time.at(time).to_datetime.to_s})
|
43
|
+
target_index = "logstash-#{Time.now.strftime("%Y.%m.%d")}"
|
44
|
+
else
|
45
|
+
target_index = @index_name
|
46
|
+
end
|
47
|
+
|
48
|
+
if @include_tag_key
|
49
|
+
record.merge!(@tag_key => tag)
|
50
|
+
end
|
51
|
+
|
52
|
+
bulk_message << { "index" => {"_index" => target_index, "_type" => type_name} }.to_json
|
53
|
+
bulk_message << record.to_json
|
54
|
+
end
|
55
|
+
bulk_message << ""
|
56
|
+
|
57
|
+
http = Net::HTTP.new(@host, @port.to_i)
|
58
|
+
request = Net::HTTP::Post.new("/_bulk")
|
59
|
+
request.body = bulk_message.join("\n")
|
60
|
+
http.request(request)
|
61
|
+
end
|
62
|
+
end
|
data/test/helper.rb
ADDED
File without changes
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'fluent/test'
|
4
|
+
require 'fluent/plugin/out_elasticsearch'
|
5
|
+
|
6
|
+
require 'webmock/test_unit'
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
$:.push File.expand_path("../lib", __FILE__)
|
10
|
+
$:.push File.dirname(__FILE__)
|
11
|
+
|
12
|
+
WebMock.disable_net_connect!
|
13
|
+
|
14
|
+
class ElasticsearchOutput < Test::Unit::TestCase
|
15
|
+
attr_accessor :index_cmds
|
16
|
+
|
17
|
+
def setup
|
18
|
+
Fluent::Test.setup
|
19
|
+
@driver = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def driver(tag='test', conf='')
|
23
|
+
@driver ||= Fluent::Test::BufferedOutputTestDriver.new(Fluent::ElasticsearchOutput, tag).configure(conf)
|
24
|
+
end
|
25
|
+
|
26
|
+
def sample_record
|
27
|
+
{'age' => 26}
|
28
|
+
end
|
29
|
+
|
30
|
+
def stub_elastic(url="http://localhost:9200/_bulk")
|
31
|
+
stub_request(:post, url).with do |req|
|
32
|
+
@index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_writes_to_default_index
|
37
|
+
stub_elastic
|
38
|
+
driver.emit(sample_record)
|
39
|
+
driver.run
|
40
|
+
assert_equal('fluentd', index_cmds.first['index']['_index'])
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_writes_to_default_type
|
44
|
+
stub_elastic
|
45
|
+
driver.emit(sample_record)
|
46
|
+
driver.run
|
47
|
+
assert_equal('fluentd', index_cmds.first['index']['_type'])
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_writes_to_speficied_index
|
51
|
+
driver.configure("index_name myindex\n")
|
52
|
+
stub_elastic
|
53
|
+
driver.emit(sample_record)
|
54
|
+
driver.run
|
55
|
+
assert_equal('myindex', index_cmds.first['index']['_index'])
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_writes_to_speficied_type
|
59
|
+
driver.configure("type_name mytype\n")
|
60
|
+
stub_elastic
|
61
|
+
driver.emit(sample_record)
|
62
|
+
driver.run
|
63
|
+
assert_equal('mytype', index_cmds.first['index']['_type'])
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_writes_to_speficied_host
|
67
|
+
driver.configure("host 192.168.33.50\n")
|
68
|
+
elastic_request = stub_elastic("http://192.168.33.50:9200/_bulk")
|
69
|
+
driver.emit(sample_record)
|
70
|
+
driver.run
|
71
|
+
assert_requested(elastic_request)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_writes_to_speficied_port
|
75
|
+
driver.configure("port 9201\n")
|
76
|
+
elastic_request = stub_elastic("http://localhost:9201/_bulk")
|
77
|
+
driver.emit(sample_record)
|
78
|
+
driver.run
|
79
|
+
assert_requested(elastic_request)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_makes_bulk_request
|
83
|
+
stub_elastic
|
84
|
+
driver.emit(sample_record)
|
85
|
+
driver.emit(sample_record.merge('age' => 27))
|
86
|
+
driver.run
|
87
|
+
assert_equal(4, index_cmds.count)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_all_records_are_preserved_in_bulk
|
91
|
+
stub_elastic
|
92
|
+
driver.emit(sample_record)
|
93
|
+
driver.emit(sample_record.merge('age' => 27))
|
94
|
+
driver.run
|
95
|
+
assert_equal(26, index_cmds[1]['age'])
|
96
|
+
assert_equal(27, index_cmds[3]['age'])
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_writes_to_logstash_index
|
100
|
+
driver.configure("logstash_format true\n")
|
101
|
+
logstash_index = "logstash-#{Time.now.strftime("%Y.%m.%d")}"
|
102
|
+
stub_elastic
|
103
|
+
driver.emit(sample_record)
|
104
|
+
driver.run
|
105
|
+
assert_equal(logstash_index, index_cmds.first['index']['_index'])
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_doesnt_add_logstash_timestamp_by_default
|
109
|
+
stub_elastic
|
110
|
+
driver.emit(sample_record)
|
111
|
+
driver.run
|
112
|
+
assert_nil(index_cmds[1]['@timestamp'])
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_adds_logstash_timestamp_when_configured
|
116
|
+
driver.configure("logstash_format true\n")
|
117
|
+
stub_elastic
|
118
|
+
ts = DateTime.now.to_s
|
119
|
+
driver.emit(sample_record)
|
120
|
+
driver.run
|
121
|
+
assert(index_cmds[1].has_key? '@timestamp')
|
122
|
+
assert_equal(index_cmds[1]['@timestamp'], ts)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_doesnt_add_tag_key_by_default
|
126
|
+
stub_elastic
|
127
|
+
driver.emit(sample_record)
|
128
|
+
driver.run
|
129
|
+
assert_nil(index_cmds[1]['tag'])
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_adds_tag_key_when_configured
|
133
|
+
driver('mytag').configure("include_tag_key true\n")
|
134
|
+
stub_elastic
|
135
|
+
driver.emit(sample_record)
|
136
|
+
driver.run
|
137
|
+
assert(index_cmds[1].has_key?('tag'))
|
138
|
+
assert_equal(index_cmds[1]['tag'], 'mytag')
|
139
|
+
end
|
140
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-elasticsearch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- diogo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: fluentd
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: webmock
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: ElasticSearch output plugin for Fluent event collector
|
63
|
+
email:
|
64
|
+
- team@uken.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- fluent-plugin-elasticsearch.gemspec
|
75
|
+
- lib/fluent/plugin/out_elasticsearch.rb
|
76
|
+
- test/helper.rb
|
77
|
+
- test/plugin/test_out_elasticsearch.rb
|
78
|
+
homepage: https://github.com/uken/fluent-plugin-elasticsearch
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: -3051582702752188354
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -3051582702752188354
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.23
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: ElasticSearch output plugin for Fluent event collector
|
108
|
+
test_files:
|
109
|
+
- test/helper.rb
|
110
|
+
- test/plugin/test_out_elasticsearch.rb
|