logstash-output-treasure_data 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94b4e2ddb3fa7e20ff41d85834692ca01db13bee
4
+ data.tar.gz: d0a3d5437e18f34ba7db5fc38d5edc03f9efea97
5
+ SHA512:
6
+ metadata.gz: 52c0f763330105b4e351512575c8256c4e40565d25b83c8b6ec116bf0c8bfd1d11a9a2d80949dbc6f83ac5206b355d4c01747ec2a08944d9a25c24ce28197110
7
+ data.tar.gz: a7bccbfe3a94d5bd0878da55280de3af317947ff961a2288efb5e7e08c7a5115b499e12321070f21f48c883867d4a6b16d9809581644e231db9c1ff34f2f1d3e
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+ - Initial version
3
+
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Satoshi Tagomori (tagomoris)
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-output-treasure_data
2
+ Treasure Data output plugin.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2015- Treasure Data, Inc. <https://www.treasuredata.com/>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Treasure Data, Inc.
2
+ Copyright 2015- Treasure Data, Inc.
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Logstash Plugin for Tresure Data service
2
+
3
+ [![Build
4
+ Status](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Outputs/job/logstash-plugin-output-example-unit/badge/icon)](http://build-eu-00.elastic.co/view/LS%20Plugins/view/LS%20Outputs/job/logstash-plugin-output-example-unit/)
5
+
6
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
7
+
8
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
9
+
10
+ ## Documentation
11
+
12
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
13
+
14
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
15
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
16
+
17
+ ## Need Help?
18
+
19
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
20
+
21
+ ## Developing
22
+
23
+ ### 1. Plugin Developement and Testing
24
+
25
+ #### Code
26
+ - To get started, you'll need JRuby with the Bundler gem installed.
27
+
28
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
29
+
30
+ - Install dependencies
31
+ ```sh
32
+ bundle install
33
+ ```
34
+
35
+ #### Test
36
+
37
+ - Update your dependencies
38
+
39
+ ```sh
40
+ bundle install
41
+ ```
42
+
43
+ - Run tests
44
+
45
+ ```sh
46
+ bundle exec rspec
47
+ ```
48
+
49
+ ### 2. Running your unpublished Plugin in Logstash
50
+
51
+ #### 2.1 Run in a local Logstash clone
52
+
53
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
54
+ ```ruby
55
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
56
+ ```
57
+ - Install plugin
58
+ ```sh
59
+ bin/plugin install --no-verify
60
+ ```
61
+ - Run Logstash with your plugin
62
+ ```sh
63
+ bin/logstash -e 'filter {awesome {}}'
64
+ ```
65
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
66
+
67
+ #### 2.2 Run in an installed Logstash
68
+
69
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
70
+
71
+ - Build your plugin gem
72
+ ```sh
73
+ gem build logstash-filter-awesome.gemspec
74
+ ```
75
+ - Install the plugin from the Logstash home
76
+ ```sh
77
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
78
+ ```
79
+ - Start Logstash and proceed to test the plugin
80
+
81
+ ## Contributing
82
+
83
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
84
+
85
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
86
+
87
+ It is more important to the community that you are able to contribute.
88
+
89
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,166 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require "logstash/json"
5
+ require "stud/buffer"
6
+
7
+ require "td-client"
8
+ require "msgpack"
9
+ require "uuid"
10
+
11
+ require "stringio"
12
+ require "zlib"
13
+
14
+ require_relative "./treasure_data/version"
15
+
16
+ # Logstash output plugin to send data to Treasure Data service.
17
+
18
+ class LogStash::Event
19
+ def to_msgpack(packer=nil)
20
+ # LogStash objects (ex: LogStash::Timestamp) are impossible to serialize by msgpack
21
+ begin
22
+ @data.reject{|a,b| a == TIMESTAMP }.to_msgpack
23
+ rescue ArgumentError => e
24
+ LogStash::Json.load(@data.to_json).to_msgpack
25
+ end
26
+ end
27
+ end
28
+
29
+ class LogStash::Outputs::TreasureData < LogStash::Outputs::Base
30
+ include Stud::Buffer
31
+
32
+ config_name "treasure_data"
33
+
34
+ IMPORT_SIZE_LIMIT = 32 * 1024 * 1024
35
+ RECORD_KEYS_LIMIT = 512
36
+ RECORD_SIZE_LIMIT = 32 * 1024 * 1024
37
+
38
+ UUID_FORMAT = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/
39
+
40
+ config :apikey, validate: :password, required: true
41
+ config :auto_create_table, validate: :boolean, default: true
42
+ config :endpoint, validate: :string, default: TreasureData::API::NEW_DEFAULT_ENDPOINT
43
+ config :use_ssl, validate: :boolean, default: true
44
+
45
+ config :database, validate: :string, required: true
46
+ config :table, validate: :string, required: true
47
+
48
+ config :http_proxy, validate: :string, default: nil
49
+ config :connect_timeout, validate: :number, default: nil
50
+ config :read_timeout, validate: :number, default: nil
51
+ config :send_timeout, validate: :number, default: nil
52
+
53
+ config :flush_size, validate: :number, default: 10000 # 10,000 records for flush
54
+ config :flush_interval, validate: :number, default: 60 * 5 # 15min for flush
55
+
56
+ public
57
+ def register
58
+ @empty_gz_data = TreasureData::API.create_empty_gz_data
59
+ @user_agent = "logstash-output-treasure_data: #{VERSION}".freeze
60
+ @uuid = UUID.new
61
+
62
+ TreasureData::API.validate_database_name(@database)
63
+ TreasureData::API.validate_table_name(@table)
64
+
65
+ client_opts = {
66
+ ssl: @use_ssl,
67
+ http_proxy: @http_proxy,
68
+ user_agent: @user_agent,
69
+ endpoint: @endpoint,
70
+ connect_timeout: @connect_timeout,
71
+ read_timeout: @read_timeout,
72
+ send_timeout: @send_timeout
73
+ }
74
+ @client = TreasureData::Client.new(@apikey.value, client_opts)
75
+
76
+ buffer_initialize(
77
+ max_items: @flush_size,
78
+ max_interval: @flush_interval,
79
+ logger: @logger
80
+ )
81
+ end # def register
82
+
83
+ public
84
+ def receive(event)
85
+ record = event.clone
86
+ @logger.debug "receive a event"
87
+
88
+ record['time'] ||= (record.timestamp.to_i || Time.now.to_i)
89
+ if record.to_hash.size > RECORD_KEYS_LIMIT
90
+ raise "Too many number of keys (#{record.keys.size} keys)"
91
+ end
92
+
93
+ row = record.to_msgpack
94
+ if row.bytesize > RECORD_SIZE_LIMIT
95
+ raise "Too large record (#{row.bytesize} bytes with keys:#{record.keys.join(',')})"
96
+ end
97
+
98
+ buffer_receive(row)
99
+ @logger.debug "buffered a event"
100
+ end
101
+
102
+ public
103
+ def flush(events, teardown = false)
104
+ @logger.debug "flushing #{events} events (may include chunk uuid)"
105
+ return if events.size < 1
106
+ if UUID_FORMAT !~ events.first
107
+ new_uuid = @uuid.generate
108
+ @logger.debug "adding chunk uuid #{new_uuid}"
109
+ events.unshift new_uuid
110
+ end
111
+
112
+ uuid = events.first
113
+ io = StringIO.new
114
+ @logger.debug "gzipping chunk #{uuid}"
115
+ Zlib::GzipWriter.wrap(io){|f|
116
+ events.each do |row|
117
+ unless row == uuid
118
+ f.write row
119
+ end
120
+ end
121
+ f.finish
122
+ }
123
+ data = io.string
124
+ @logger.debug "sending gzipped chunk #{uuid}, #{data.bytesize} bytes"
125
+ begin
126
+ @client.import(@database, @table, "msgpack.gz", data, data.bytesize, uuid)
127
+
128
+ rescue TreasureData::NotFoundError => e
129
+ raise unless @auto_create_table
130
+
131
+ @logger.info "creating missing table #{@table} on database #{@database} for chunk #{uuid}"
132
+ ensure_table_exists(@client, @database, @table)
133
+ @logger.info "retrying upload chunk #{uuid}"
134
+ retry
135
+ end
136
+
137
+ @logger.debug "done #{uuid}"
138
+ end # def flush
139
+
140
+ public
141
+ def ensure_table_exists(client, database, table)
142
+ begin
143
+ client.create_log_table(database, table)
144
+ rescue TreasureData::NotFoundError => e
145
+ client.create_database(database)
146
+ client.create_log_table(database, table)
147
+ rescue TreasureData::AlreadyExistsError => e
148
+ # ignore
149
+ end
150
+ end
151
+
152
+ public
153
+ def on_flush_error(e)
154
+ @logger.warn "flush error #{e.class}: #{e.message}"
155
+ end
156
+
157
+ public
158
+ def on_full_buffer_error(opts={})
159
+ @logger.warn "buffer exceeds limits: pending:#{opts[:pending_count]}, outgoing:#{opts[:outgoing_count]}"
160
+ end
161
+
162
+ public
163
+ def close
164
+ buffer_flush(final: true)
165
+ end
166
+ end # class LogStash::Outputs::Example
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+
4
+ class LogStash::Outputs::TreasureData < LogStash::Outputs::Base
5
+ VERSION = "0.0.1"
6
+ end
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-treasure_data'
3
+ s.version = "0.1.0"
4
+ s.licenses = ["Apache License (2.0)"]
5
+ s.summary = "Logstash plugin to store data into Treasure Data service."
6
+ s.description = "This gem is a logstash plugin to store data from logstash to Treasure Data service. This gem is not a stand-alone program."
7
+ s.authors = ["Satoshi Tagomori"]
8
+ s.email = "tagomoris@gmail.com"
9
+ s.homepage = "https://github.com/tagomoris/logstash-output-treasure_data"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", ">= 2.0.0", "< 3.0.0"
22
+ s.add_runtime_dependency "logstash-codec-plain"
23
+ s.add_runtime_dependency "msgpack", "= 0.7.2dev1"
24
+ s.add_runtime_dependency "td-client", "= 0.9.0dev2"
25
+ s.add_runtime_dependency "uuid"
26
+ s.add_development_dependency "logstash-devutils"
27
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/codecs/plain"
4
+ require "logstash/event"
5
+
6
+ require "logstash/outputs/treasure_data"
7
+
8
+ describe LogStash::Outputs::TreasureData do
9
+ let(:sample_event) { LogStash::Event.new }
10
+ let(:output) { LogStash::Outputs::TreasureData.new(config) }
11
+ let(:config) do
12
+ {
13
+ "apikey" => "0/xxxxxxxxxxxxxxxxx",
14
+ "database" => "logstash",
15
+ "table" => "spec",
16
+ }
17
+ end
18
+
19
+ before do
20
+ output.register
21
+ end
22
+
23
+ describe "receive message" do
24
+ subject { output.receive(sample_event) }
25
+
26
+ it "returns a nil" do
27
+ expect(subject).to be_nil
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-treasure_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Satoshi Tagomori
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 2.0.0
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-codec-plain
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '='
51
+ - !ruby/object:Gem::Version
52
+ version: 0.7.2dev1
53
+ name: msgpack
54
+ prerelease: false
55
+ type: :runtime
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '='
59
+ - !ruby/object:Gem::Version
60
+ version: 0.7.2dev1
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '='
65
+ - !ruby/object:Gem::Version
66
+ version: 0.9.0dev2
67
+ name: td-client
68
+ prerelease: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '='
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.0dev2
75
+ - !ruby/object:Gem::Dependency
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ name: uuid
82
+ prerelease: false
83
+ type: :runtime
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ name: logstash-devutils
96
+ prerelease: false
97
+ type: :development
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: This gem is a logstash plugin to store data from logstash to Treasure Data service. This gem is not a stand-alone program.
104
+ email: tagomoris@gmail.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - CHANGELOG.md
110
+ - CONTRIBUTORS
111
+ - DEVELOPER.md
112
+ - Gemfile
113
+ - LICENSE
114
+ - NOTICE.TXT
115
+ - README.md
116
+ - lib/logstash/outputs/treasure_data.rb
117
+ - lib/logstash/outputs/treasure_data/version.rb
118
+ - logstash-output-treasure_data.gemspec
119
+ - spec/outputs/treasure_data_spec.rb
120
+ homepage: https://github.com/tagomoris/logstash-output-treasure_data
121
+ licenses:
122
+ - Apache License (2.0)
123
+ metadata:
124
+ logstash_plugin: 'true'
125
+ logstash_group: output
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.8
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Logstash plugin to store data into Treasure Data service.
146
+ test_files:
147
+ - spec/outputs/treasure_data_spec.rb
148
+ has_rdoc: