logstash-output-timber 1.0.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
+ SHA256:
3
+ metadata.gz: 8bf83aa2d2c4239a019db421f39fe16b87f0ede8613a6126b9d8ce7aa17742f9
4
+ data.tar.gz: 29ab8df17494515feec2301af889b2ec5d277564ae2594ce03f965dc4413d8ba
5
+ SHA512:
6
+ metadata.gz: 02b47b2a374880b263f0fbbb733b363785ade764cee499e226eda5ccc4b66b163b9bea50f5ff741d439962ea30ba6e1b3700a5ff64113d9006a231f974ac6f68
7
+ data.tar.gz: 74d986e655bddaf3d3008221fb01abb7c1c82ea272c3289ea303d651e739e13298c740a5cd747ed2949bd95058ade00bd9938f9900d0c30f02a4b2a022825bcf
data/CHANGELOG.md ADDED
@@ -0,0 +1,66 @@
1
+ ## 4.3.3
2
+ - Docs: Add plugin description.
3
+
4
+ ## 4.3.2
5
+ - Docs: Update plugin title.
6
+
7
+ ## 4.3.1
8
+ - Fix deadlock that could occur in certain situations. All users should upgrade to the latest version.
9
+ This deadlock was caused by certain async HTTP APIs being called out of order thus creating a race.
10
+
11
+ ## 4.3.0
12
+ - Add `user` and `password` options to support HTTP basic auth
13
+
14
+ ## 4.2.0
15
+ - Allow nested array in field definition, See https://github.com/logstash-plugins/logstash-output-http/pull/53
16
+
17
+ ## 4.1.0
18
+ - Allow nested field definitions in `mappings`
19
+
20
+ ## 4.0.0
21
+ - Major overhaul of internals, adds new retry options
22
+ - Allow users to specify non-standard response codes as ignorable
23
+ - Set concurrency level to shared allowing for greater efficiency across threads
24
+
25
+ ## 3.1.1
26
+ - Relax constraint on logstash-core-plugin-api to >= 1.60 <= 2.99
27
+
28
+ ## 3.1.0
29
+ - breaking,config: Remove deprecated config 'verify_ssl'. Please use 'ssl_certificate_validation'.
30
+
31
+ ## 3.0.1
32
+ - Republish all the gems under jruby.
33
+
34
+ ## 3.0.0
35
+ - Update the plugin to the version 2.0 of the plugin api, this change is required for Logstash 5.0 compatibility. See https://github.com/elastic/logstash/issues/5141
36
+
37
+ ## 2.1.3
38
+ - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash
39
+
40
+ ## 2.1.2
41
+ - New dependency requirements for logstash-core for the 5.0 release
42
+
43
+ ## 2.1.1
44
+ - Require http_client mixin with better keepalive handling
45
+
46
+
47
+ ## 2.1.0
48
+ - Properly close the client on #close
49
+ - Optimized execution for Logstash 2.2 ng pipeline
50
+
51
+ ## 2.0.5
52
+ - fixed memory leak
53
+
54
+ ## 2.0.3
55
+ - fixed potential race condition on async callbacks
56
+ - silenced specs equest logs and other spec robustness fixes
57
+
58
+ ## 2.0.0
59
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
60
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
61
+ - Dependency on logstash-core update to 2.0
62
+
63
+ ## 1.1.0
64
+ - Concurrent execution
65
+ - Add many HTTP options via the http_client mixin
66
+ - Switch to manticore as HTTP Client
data/CONTRIBUTORS ADDED
@@ -0,0 +1,17 @@
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
+ * Christian S. (squiddle)
6
+ * Colin Surprenant (colinsurprenant)
7
+ * John E. Vincent (lusis)
8
+ * Jordan Sissel (jordansissel)
9
+ * Kurt Hurtado (kurtado)
10
+ * Pier-Hugues Pellerin (ph)
11
+ * Richard Pijnenburg (electrical)
12
+ * Ben Johnson (binarylogic)
13
+
14
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
15
+ Logstash, and you aren't on the list above and want to be, please let us know
16
+ and we'll make sure you're here. Contributions from folks like you are what make
17
+ open source awesome.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2016 Elasticsearch <http://www.elastic.co>
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
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Logstash Plugin
2
+
3
+ [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-output-http.svg)](https://travis-ci.org/logstash-plugins/logstash-output-http)
4
+
5
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
+
7
+ 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.
8
+
9
+ ## Documentation
10
+
11
+ 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/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ # Logstash 2.3 and higher
59
+ bin/logstash-plugin install --no-verify
60
+
61
+ # Prior to Logstash 2.3
62
+ bin/plugin install --no-verify
63
+
64
+ ```
65
+ - Run Logstash with your plugin
66
+ ```sh
67
+ bin/logstash -e 'filter {awesome {}}'
68
+ ```
69
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
70
+
71
+ #### 2.2 Run in an installed Logstash
72
+
73
+ 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:
74
+
75
+ - Build your plugin gem
76
+ ```sh
77
+ gem build logstash-output-http.gemspec
78
+ ```
79
+ - Install the plugin from the Logstash home
80
+ ```sh
81
+ # Logstash 2.3 and higher
82
+ bin/logstash-plugin install --no-verify
83
+
84
+ # Prior to Logstash 2.3
85
+ bin/plugin install --no-verify
86
+
87
+ ```
88
+ - Start Logstash and proceed to test the plugin
89
+
90
+ ## Contributing
91
+
92
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
93
+
94
+ 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.
95
+
96
+ It is more important to the community that you are able to contribute.
97
+
98
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,212 @@
1
+ :plugin: timber
2
+ :type: output
3
+
4
+ ///////////////////////////////////////////
5
+ START - GENERATED VARIABLES, DO NOT EDIT!
6
+ ///////////////////////////////////////////
7
+ :version: %VERSION%
8
+ :release_date: %RELEASE_DATE%
9
+ :changelog_url: %CHANGELOG_URL%
10
+ :include_path: ../../../../logstash/docs/include
11
+ ///////////////////////////////////////////
12
+ END - GENERATED VARIABLES, DO NOT EDIT!
13
+ ///////////////////////////////////////////
14
+
15
+ [id="plugins-{type}s-{plugin}"]
16
+
17
+ === Timber output plugin
18
+
19
+ include::{include_path}/plugin_header.asciidoc[]
20
+
21
+ ==== Description
22
+
23
+ This output sends structured events to the Timber.io logging service.
24
+
25
+ It's a highly efficient HTTP transport that uses batching and retries for
26
+ fast and reliable delivery.
27
+
28
+ This output will execute up to 'pool_max' requests in parallel for performance.
29
+ Consider this when tuning this plugin for performance. The default of 50 should
30
+ be sufficient for most setups.
31
+
32
+ Additionally, note that when parallel execution is used strict ordering of events is not
33
+ guaranteed!
34
+
35
+ [id="plugins-{type}s-{plugin}-options"]
36
+ ==== Timber Output Configuration Options
37
+
38
+ This plugin supports the following configuration options plus the <<plugins-{type}s-{plugin}-common-options>> described later.
39
+
40
+ [cols="<,<,<",options="header",]
41
+ |=======================================================================
42
+ |Setting |Input type|Required
43
+ | <<plugins-{type}s-{plugin}-api_key>> |Your Timber.io API key|No
44
+ | <<plugins-{type}s-{plugin}-cacert>> |a valid filesystem path|No
45
+ | <<plugins-{type}s-{plugin}-client_cert>> |a valid filesystem path|No
46
+ | <<plugins-{type}s-{plugin}-client_key>> |a valid filesystem path|No
47
+ | <<plugins-{type}s-{plugin}-connect_timeout>> |<<number,number>>|No
48
+ | <<plugins-{type}s-{plugin}-keystore>> |a valid filesystem path|No
49
+ | <<plugins-{type}s-{plugin}-keystore_password>> |<<password,password>>|No
50
+ | <<plugins-{type}s-{plugin}-keystore_type>> |<<string,string>>|No
51
+ | <<plugins-{type}s-{plugin}-pool_max>> |<<number,number>>|No
52
+ | <<plugins-{type}s-{plugin}-proxy>> |<<,>>|No
53
+ | <<plugins-{type}s-{plugin}-request_timeout>> |<<number,number>>|No
54
+ | <<plugins-{type}s-{plugin}-socket_timeout>> |<<number,number>>|No
55
+ | <<plugins-{type}s-{plugin}-truststore>> |a valid filesystem path|No
56
+ | <<plugins-{type}s-{plugin}-truststore_password>> |<<password,password>>|No
57
+ | <<plugins-{type}s-{plugin}-truststore_type>> |<<string,string>>|No
58
+ |=======================================================================
59
+
60
+ Also see <<plugins-{type}s-{plugin}-common-options>> for a list of options supported by all
61
+ output plugins.
62
+
63
+ &nbsp;
64
+
65
+ [id="plugins-{type}s-{plugin}-api_key"]
66
+ ===== `cacert`
67
+
68
+ * Value type is <<string,string>>
69
+ * There is no default value for this setting.
70
+
71
+ Your Timber.io API key. You can obtain your API by creating an app in the
72
+ [Timber console](https://app.timber.io).
73
+
74
+
75
+ [id="plugins-{type}s-{plugin}-client_cert"]
76
+ ===== `client_cert`
77
+
78
+ * Value type is <<path,path>>
79
+ * There is no default value for this setting.
80
+
81
+ If you'd like to use a client certificate (note, most people don't want this) set the path to the x509 cert here
82
+
83
+
84
+ [id="plugins-{type}s-{plugin}-client_key"]
85
+ ===== `client_key`
86
+
87
+ * Value type is <<path,path>>
88
+ * There is no default value for this setting.
89
+
90
+ If you're using a client certificate specify the path to the encryption key here
91
+
92
+
93
+ [id="plugins-{type}s-{plugin}-connect_timeout"]
94
+ ===== `connect_timeout`
95
+
96
+ * Value type is <<number,number>>
97
+ * Default value is `10`
98
+
99
+ Timeout (in seconds) to wait for a connection to be established. Default is `10s`
100
+
101
+
102
+ [id="plugins-{type}s-{plugin}-keystore"]
103
+ ===== `keystore`
104
+
105
+ * Value type is <<path,path>>
106
+ * There is no default value for this setting.
107
+
108
+ If you need to use a custom keystore (`.jks`) specify that here. This does not work with .pem keys!
109
+
110
+
111
+ [id="plugins-{type}s-{plugin}-keystore_password"]
112
+ ===== `keystore_password`
113
+
114
+ * Value type is <<password,password>>
115
+ * There is no default value for this setting.
116
+
117
+ Specify the keystore password here.
118
+ Note, most .jks files created with keytool require a password!
119
+
120
+
121
+ [id="plugins-{type}s-{plugin}-keystore_type"]
122
+ ===== `keystore_type`
123
+
124
+ * Value type is <<string,string>>
125
+ * Default value is `"JKS"`
126
+
127
+ Specify the keystore type here. One of `JKS` or `PKCS12`. Default is `JKS`
128
+
129
+
130
+ [id="plugins-{type}s-{plugin}-pool_max"]
131
+ ===== `pool_max`
132
+
133
+ * Value type is <<number,number>>
134
+ * Default value is `50`
135
+
136
+ Max number of concurrent connections. Defaults to `50`
137
+
138
+
139
+ [id="plugins-{type}s-{plugin}-proxy"]
140
+ ===== `proxy`
141
+
142
+ * Value type is <<string,string>>
143
+ * There is no default value for this setting.
144
+
145
+ If you'd like to use an HTTP proxy . This supports multiple configuration syntaxes:
146
+
147
+ 1. Proxy host in form: `http://proxy.org:1234`
148
+ 2. Proxy host in form: `{host => "proxy.org", port => 80, scheme => 'http', user => 'username@host', password => 'password'}`
149
+ 3. Proxy host in form: `{url => 'http://proxy.org:1234', user => 'username@host', password => 'password'}`
150
+
151
+
152
+ [id="plugins-{type}s-{plugin}-request_timeout"]
153
+ ===== `request_timeout`
154
+
155
+ * Value type is <<number,number>>
156
+ * Default value is `60`
157
+
158
+ This module makes it easy to add a very fully configured HTTP client to logstash
159
+ based on [Manticore](https://github.com/cheald/manticore).
160
+ For an example of its usage see https://github.com/logstash-plugins/logstash-input-http_poller
161
+ Timeout (in seconds) for the entire request
162
+
163
+
164
+ [id="plugins-{type}s-{plugin}-socket_timeout"]
165
+ ===== `socket_timeout`
166
+
167
+ * Value type is <<number,number>>
168
+ * Default value is `10`
169
+
170
+ Timeout (in seconds) to wait for data on the socket. Default is `10s`
171
+
172
+
173
+ [id="plugins-{type}s-{plugin}-ssl_certificate_validation"]
174
+ ===== `ssl_certificate_validation`
175
+
176
+ * Value type is <<boolean,boolean>>
177
+ * Default value is `true`
178
+
179
+ Set this to false to disable SSL/TLS certificate validation
180
+ Note: setting this to false is generally considered insecure!
181
+
182
+
183
+ [id="plugins-{type}s-{plugin}-truststore"]
184
+ ===== `truststore`
185
+
186
+ * Value type is <<path,path>>
187
+ * There is no default value for this setting.
188
+
189
+ If you need to use a custom truststore (`.jks`) specify that here. This does not work with .pem certs!
190
+
191
+
192
+ [id="plugins-{type}s-{plugin}-truststore_password"]
193
+ ===== `truststore_password`
194
+
195
+ * Value type is <<password,password>>
196
+ * There is no default value for this setting.
197
+
198
+ Specify the truststore password here.
199
+ Note, most .jks files created with keytool require a password!
200
+
201
+
202
+ [id="plugins-{type}s-{plugin}-truststore_type"]
203
+ ===== `truststore_type`
204
+
205
+ * Value type is <<string,string>>
206
+ * Default value is `"JKS"`
207
+
208
+ Specify the truststore type here. One of `JKS` or `PKCS12`. Default is `JKS`
209
+
210
+
211
+ [id="plugins-{type}s-{plugin}-common-options"]
212
+ include::{include_path}/{type}.asciidoc[]
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require "logstash/config/mixin"
3
+ require "manticore"
4
+
5
+ # This contains the HTTP client code within it's own namespace. It is based off
6
+ # of the logstash-mixin-http_client plugin.
7
+ class LogStash::Outputs::Timber < LogStash::Outputs::Base
8
+ module HttpClient
9
+ class InvalidHTTPConfigError < StandardError; end
10
+
11
+ def self.included(base)
12
+ base.extend(self)
13
+ base.setup_http_client_config
14
+ end
15
+
16
+ def setup_http_client_config
17
+ # Timeout (in seconds) for the entire request
18
+ config :request_timeout, :validate => :number, :default => 60
19
+
20
+ # Timeout (in seconds) to wait for data on the socket. Default is `10s`
21
+ config :socket_timeout, :validate => :number, :default => 10
22
+
23
+ # Timeout (in seconds) to wait for a connection to be established. Default is `10s`
24
+ config :connect_timeout, :validate => :number, :default => 10
25
+
26
+ # Max number of concurrent connections. Defaults to `50`
27
+ config :pool_max, :validate => :number, :default => 50
28
+
29
+ # If you need to use a custom X.509 CA (.pem certs) specify the path to that here
30
+ config :cacert, :validate => :path
31
+
32
+ # If you'd like to use a client certificate (note, most people don't want this) set the path to the x509 cert here
33
+ config :client_cert, :validate => :path
34
+
35
+ # If you're using a client certificate specify the path to the encryption key here
36
+ config :client_key, :validate => :path
37
+
38
+ # If you need to use a custom keystore (`.jks`) specify that here. This does not work with .pem keys!
39
+ config :keystore, :validate => :path
40
+
41
+ # Specify the keystore password here.
42
+ # Note, most .jks files created with keytool require a password!
43
+ config :keystore_password, :validate => :password
44
+
45
+ # Specify the keystore type here. One of `JKS` or `PKCS12`. Default is `JKS`
46
+ config :keystore_type, :validate => :string, :default => "JKS"
47
+
48
+ # If you need to use a custom truststore (`.jks`) specify that here. This does not work with .pem certs!
49
+ config :truststore, :validate => :path
50
+
51
+ # Specify the truststore password here.
52
+ # Note, most .jks files created with keytool require a password!
53
+ config :truststore_password, :validate => :password
54
+
55
+ # Specify the truststore type here. One of `JKS` or `PKCS12`. Default is `JKS`
56
+ config :truststore_type, :validate => :string, :default => "JKS"
57
+
58
+ # If you'd like to use an HTTP proxy . This supports multiple configuration syntaxes:
59
+ #
60
+ # 1. Proxy host in form: `http://proxy.org:1234`
61
+ # 2. Proxy host in form: `{host => "proxy.org", port => 80, scheme => 'http', user => 'username@host', password => 'password'}`
62
+ # 3. Proxy host in form: `{url => 'http://proxy.org:1234', user => 'username@host', password => 'password'}`
63
+ config :proxy
64
+ end
65
+
66
+ def build_http_client_config
67
+ c = {
68
+ connect_timeout: @connect_timeout,
69
+ socket_timeout: @socket_timeout,
70
+ request_timeout: @request_timeout,
71
+ follow_redirects: true,
72
+ automatic_retries: 1,
73
+ retry_non_idempotent: true,
74
+ check_connection_timeout: 200,
75
+ pool_max: @pool_max,
76
+ pool_max_per_route: @pool_max,
77
+ cookies: false,
78
+ keepalive: true
79
+ }
80
+
81
+ if @proxy
82
+ # Symbolize keys if necessary
83
+ c[:proxy] = @proxy.is_a?(Hash) ?
84
+ @proxy.reduce({}) {|memo,(k,v)| memo[k.to_sym] = v; memo} :
85
+ @proxy
86
+ end
87
+
88
+ c[:ssl] = {}
89
+ if @cacert
90
+ c[:ssl][:ca_file] = @cacert
91
+ end
92
+
93
+ if @truststore
94
+ c[:ssl].merge!(
95
+ :truststore => @truststore,
96
+ :truststore_type => @truststore_type,
97
+ :truststore_password => @truststore_password.value
98
+ )
99
+
100
+ if c[:ssl][:truststore_password].nil?
101
+ raise LogStash::ConfigurationError, "Truststore declared without a password! This is not valid, please set the 'truststore_password' option"
102
+ end
103
+ end
104
+
105
+ if @keystore
106
+ c[:ssl].merge!(
107
+ :keystore => @keystore,
108
+ :keystore_type => @keystore_type,
109
+ :keystore_password => @keystore_password.value
110
+ )
111
+
112
+ if c[:ssl][:keystore_password].nil?
113
+ raise LogStash::ConfigurationError, "Keystore declared without a password! This is not valid, please set the 'keystore_password' option"
114
+ end
115
+ end
116
+
117
+ if @client_cert && @client_key
118
+ c[:ssl][:client_cert] = @client_cert
119
+ c[:ssl][:client_key] = @client_key
120
+ elsif !!@client_cert ^ !!@client_key
121
+ raise InvalidHTTPConfigError, "You must specify both client_cert and client_key for an HTTP client, or neither!"
122
+ end
123
+
124
+ c
125
+ end
126
+
127
+ def http_client
128
+ @http_client ||= Manticore::Client.new(build_http_client_config)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+
3
+ # Core classes
4
+ require "base64"
5
+ require "uri"
6
+
7
+ # Logstash classes
8
+ require "logstash/namespace"
9
+ require "logstash/json"
10
+ require "logstash/outputs/base"
11
+ require "logstash/outputs/timber/http_client"
12
+
13
+ class LogStash::Outputs::Timber < LogStash::Outputs::Base
14
+ include HttpClient
15
+
16
+ VERSION = "1.0.0".freeze
17
+ CONTENT_TYPE = "application/json".freeze
18
+ MAX_ATTEMPTS = 3
19
+ METHOD = :post.freeze
20
+ RETRYABLE_MANTICORE_EXCEPTIONS = [
21
+ ::Manticore::Timeout,
22
+ ::Manticore::SocketException,
23
+ ::Manticore::ClientProtocolException,
24
+ ::Manticore::ResolutionFailure,
25
+ ::Manticore::SocketTimeout
26
+ ].freeze
27
+ RETRYABLE_CODES = [429, 500, 502, 503, 504].freeze
28
+ URL = "https://logs.timber.io/frames".freeze
29
+ USER_AGENT = "Timber Logstash/#{VERSION}".freeze
30
+
31
+ # Attribute for testing purposes only
32
+ attr_writer :url
33
+
34
+ concurrency :shared
35
+
36
+ # This output lets you send events to the Timber.io logging service.
37
+ #
38
+ # This output will execute up to 'pool_max' requests in parallel for performance.
39
+ # Consider this when tuning this plugin for performance.
40
+ #
41
+ # Additionally, note that when parallel execution is used strict ordering of events is not
42
+ # guaranteed!
43
+ #
44
+ # Beware, this gem does not yet support codecs. Please use the 'format' option for now.
45
+ config_name "timber"
46
+
47
+ # Your Timber API key. This can be obtained by creating an app at https://app.timber.io.
48
+ # Already have an app? You can find your API key in your app's settings.
49
+ config :api_key, :validate => :string, :required => :true
50
+
51
+ def register
52
+ encoded_api_key = Base64.urlsafe_encode64(@api_key).chomp
53
+ authorization_value = "Basic #{encoded_api_key}"
54
+ @headers = {
55
+ "Authorization" => authorization_value,
56
+ "Content-Type" => CONTENT_TYPE,
57
+ "User-Agent" => USER_AGENT
58
+ }
59
+ @url = URL
60
+ end
61
+
62
+ def multi_receive(events)
63
+ send_events(events, 1)
64
+ end
65
+
66
+ def close
67
+ http_client.close
68
+ end
69
+
70
+ private
71
+ def send_events(events, attempt)
72
+ if attempt > MAX_ATTEMPTS
73
+ @logger.warn(
74
+ "Max attempts exceeded, dropping events",
75
+ :attempt => attempt
76
+ )
77
+ return false
78
+ end
79
+
80
+ response =
81
+ begin
82
+ hash_events = events.collect(&:to_hash)
83
+ body = LogStash::Json.dump(hash_events)
84
+ http_client.post(@url, :body => body, :headers => @headers)
85
+ rescue Exception => e
86
+ if retryable_exception?(e)
87
+ @logger.warn(
88
+ "Attempt #{attempt}, retryable exception when making request",
89
+ :attempt => attempt,
90
+ :class => e.class.name,
91
+ :message => e.message,
92
+ :backtrace => e.backtrace
93
+ )
94
+ return send_events(events, attempt + 1)
95
+ else
96
+ @logger.error(
97
+ "Attempt #{attempt}, fatal exception when making request",
98
+ :attempt => attempt,
99
+ :class => e.class.name,
100
+ :message => e.message,
101
+ :backtrace => e.backtrace
102
+ )
103
+ return false
104
+ end
105
+ end
106
+
107
+ code = response.code
108
+
109
+ if code >= 200 && code <= 299
110
+ true
111
+ elsif RETRYABLE_CODES.include?(code)
112
+ @logger.warn(
113
+ "Bad retryable response from the Timber API",
114
+ :attempt => attempt,
115
+ :code => code
116
+ )
117
+ sleep_time = sleep_for_attempt(attempt)
118
+ sleep(sleep_time)
119
+ send_events(events, attempt + 1)
120
+ else
121
+ @logger.error(
122
+ "Bad fatal response from the Timber API",
123
+ :attempt => attempt,
124
+ :code => code
125
+ )
126
+ false
127
+ end
128
+ end
129
+
130
+ def retryable_exception?(e)
131
+ RETRYABLE_MANTICORE_EXCEPTIONS.any? do |exception_class|
132
+ e.is_a?(exception_class)
133
+ end
134
+ end
135
+
136
+ def sleep_for_attempt(attempt)
137
+ sleep_for = attempt ** 2
138
+ sleep_for = sleep_for <= 60 ? sleep_for : 60
139
+ (sleep_for / 2) + (rand(0..sleep_for) / 2)
140
+ end
141
+ end
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-timber'
3
+ s.version = "1.0.0"
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This output send events to the Timber.io logging service"
6
+ s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Elastic"]
8
+ s.email = 'info@elastic.co'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"]
14
+
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
20
+
21
+ # Gem dependencies
22
+ s.add_runtime_dependency 'logstash-core-plugin-api', '>= 1.60', '<= 2.99'
23
+ s.add_runtime_dependency 'manticore', '>= 0.5.2', '< 1.0.0'
24
+
25
+ s.add_development_dependency 'logstash-devutils'
26
+ s.add_development_dependency 'logstash-codec-plain'
27
+ s.add_development_dependency 'sinatra'
28
+ s.add_development_dependency 'webrick'
29
+ end
@@ -0,0 +1,145 @@
1
+ require "logstash/devutils/rspec/spec_helper"
2
+ require "logstash/outputs/timber"
3
+ require "logstash/codecs/plain"
4
+ require "thread"
5
+ require "sinatra"
6
+
7
+ PORT = rand(65535-1024) + 1025
8
+
9
+ class LogStash::Outputs::Timber
10
+ attr_writer :agent
11
+ attr_reader :request_tokens
12
+ end
13
+
14
+ # note that Sinatra startup and shutdown messages are directly logged to stderr so
15
+ # it is not really possible to disable them without reopening stderr which is not advisable.
16
+ #
17
+ # == Sinatra (v1.4.6) has taken the stage on 51572 for development with backup from WEBrick
18
+ # == Sinatra has ended his set (crowd applauds)
19
+
20
+ class TestApp < Sinatra::Base
21
+ # disable WEBrick logging
22
+ def self.server_settings
23
+ { :AccessLog => [], :Logger => WEBrick::BasicLog::new(nil, WEBrick::BasicLog::FATAL) }
24
+ end
25
+
26
+ def self.add_request(request)
27
+ self.requests << request
28
+ end
29
+
30
+ def self.requests
31
+ @requests ||= []
32
+ end
33
+
34
+ def self.reset_requests
35
+ @requests = []
36
+ end
37
+
38
+ post "/good" do
39
+ self.class.add_request(request)
40
+ [200, "Good"]
41
+ end
42
+
43
+ post "/auth_error" do
44
+ self.class.add_request(request)
45
+ [403, "Bad"]
46
+ end
47
+
48
+ post "/server_error" do
49
+ self.class.add_request(request)
50
+ [500, "Bad"]
51
+ end
52
+ end
53
+
54
+ RSpec.configure do |config|
55
+ #http://stackoverflow.com/questions/6557079/start-and-call-ruby-http-server-in-the-same-script
56
+ def sinatra_run_wait(app, opts)
57
+ queue = Queue.new
58
+
59
+ t = java.lang.Thread.new(
60
+ proc do
61
+ begin
62
+ app.run!(opts) do |server|
63
+ queue.push("started")
64
+ end
65
+ rescue => e
66
+ puts "Error in webserver thread #{e}"
67
+ # ignore
68
+ end
69
+ end
70
+ )
71
+ t.daemon = true
72
+ t.start
73
+ queue.pop # blocks until the run! callback runs
74
+ end
75
+
76
+ config.before(:suite) do
77
+ sinatra_run_wait(TestApp, :port => PORT, :server => 'webrick')
78
+ puts "Test webserver on port #{PORT}"
79
+ end
80
+ end
81
+
82
+ describe LogStash::Outputs::Timber do
83
+ let(:port) { PORT }
84
+ let(:output) { described_class.new({"api_key" => "123:abcd1234", "pool_max" => 1}) }
85
+ let(:event) { LogStash::Event.new({"message" => "hi"}) }
86
+ let(:requests) { TestApp.requests }
87
+
88
+ before(:each) do
89
+ output.register
90
+ TestApp.reset_requests
91
+ end
92
+
93
+ describe "#send_events" do
94
+ it "returns false when the max attempts are exceeded" do
95
+ result = output.send(:send_events, [event], 6)
96
+ expect(result).to eq(false)
97
+ end
98
+
99
+ it "returns false when the status is 403" do
100
+ output.url = "http://localhost:#{port}/auth_error"
101
+ result = output.send(:send_events, [event], 1)
102
+ expect(result).to eq(false)
103
+ expect(requests.length).to eq(1)
104
+ end
105
+
106
+ it "returns false when the status is 500" do
107
+ allow(output).to receive(:sleep_for_attempt).and_return(0)
108
+ output.url = "http://localhost:#{port}/server_error"
109
+ result = output.send(:send_events, [event], 1)
110
+ expect(result).to eq(false)
111
+ expect(requests.length).to eq(3)
112
+ end
113
+
114
+ it "returns true when the status is 200" do
115
+ output.url = "http://localhost:#{port}/good"
116
+ result = output.send(:send_events, [event], 1)
117
+ expect(result).to eq(true)
118
+ expect(requests.length).to eq(1)
119
+
120
+ request = requests.first
121
+ expect(request.env["CONTENT_TYPE"]).to eq("application/json")
122
+ expect(request.env["HTTP_AUTHORIZATION"]).to eq("Basic MTIzOmFiY2QxMjM0")
123
+ expect(request.env["HTTP_USER_AGENT"]).to eq("Timber Logstash/1.0.0")
124
+
125
+ timestamp_iso8601 = event.get("@timestamp").to_iso8601
126
+ expect(request.body.read).to eq("[{\"@timestamp\":\"#{timestamp_iso8601}\",\"@version\":\"1\",\"message\":\"hi\"}]")
127
+ end
128
+
129
+ it "handles fatal request errors" do
130
+ allow(output.send(:http_client)).to receive(:post).and_raise("boom")
131
+
132
+ output.url = "http://localhost:#{port}/good"
133
+ result = output.send(:send_events, [event], 1)
134
+ expect(result).to eq(false)
135
+ end
136
+
137
+ it "handles retryable request errors" do
138
+ expect(output.send(:http_client)).to receive(:post).exactly(3).times.and_raise(::Manticore::Timeout.new)
139
+
140
+ output.url = "http://localhost:#{port}/good"
141
+ result = output.send(:send_events, [event], 1)
142
+ expect(result).to eq(false)
143
+ end
144
+ end
145
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-timber
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-16 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: '1.60'
19
+ - - "<="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.99'
22
+ name: logstash-core-plugin-api
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 0.5.2
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: 1.0.0
42
+ name: manticore
43
+ prerelease: false
44
+ type: :runtime
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.5.2
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 1.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ name: logstash-devutils
60
+ prerelease: false
61
+ type: :development
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ name: logstash-codec-plain
74
+ prerelease: false
75
+ type: :development
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ name: sinatra
88
+ prerelease: false
89
+ type: :development
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ name: webrick
102
+ prerelease: false
103
+ type: :development
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ description: This gem is a Logstash plugin required to be installed on top of the
110
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
111
+ gem is not a stand-alone program
112
+ email: info@elastic.co
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - CHANGELOG.md
118
+ - CONTRIBUTORS
119
+ - Gemfile
120
+ - LICENSE
121
+ - NOTICE.TXT
122
+ - README.md
123
+ - docs/index.asciidoc
124
+ - lib/logstash/outputs/timber.rb
125
+ - lib/logstash/outputs/timber/http_client.rb
126
+ - logstash-output-timber.gemspec
127
+ - spec/outputs/timber_spec.rb
128
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
129
+ licenses:
130
+ - Apache License (2.0)
131
+ metadata:
132
+ logstash_plugin: 'true'
133
+ logstash_group: output
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.6.11
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: This output send events to the Timber.io logging service
154
+ test_files:
155
+ - spec/outputs/timber_spec.rb