From 078aa890b1357f008710bab44d2ff70b15caa398 Mon Sep 17 00:00:00 2001 From: Thomas Andrejak Date: Tue, 16 Jun 2020 16:55:20 +0200 Subject: [PATCH] Add support for logstash codecs --- CHANGELOG.md | 3 +++ CONTRIBUTORS | 1 + docs/index.asciidoc | 9 ++++++--- lib/logstash/outputs/http.rb | 12 ++++++++++-- logstash-output-http.gemspec | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1442dbd..95c509e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.2.5 + - Add support for logstash codecs + ## 5.2.4 - Relax dependency on http_client mixin since current major works on both diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c6ba555..9b17e9f 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -9,6 +9,7 @@ Contributors: * Kurt Hurtado (kurtado) * Pier-Hugues Pellerin (ph) * Richard Pijnenburg (electrical) +* Thomas Andrejak (ToToL) Note: If you've sent us patches, bug reports, or otherwise contributed to Logstash, and you aren't on the list above and want to be, please let us know diff --git a/docs/index.asciidoc b/docs/index.asciidoc index f9bd88d..ab12684 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -29,7 +29,7 @@ Consider this when tuning this plugin for performance. Additionally, note that when parallel execution is used strict ordering of events is not guaranteed! -Beware, this gem does not yet support codecs. Please use the 'format' option for now. +Beware, to use codecs, please use the 'format' option for now. [id="plugins-{type}s-{plugin}-options"] ==== Http Output Configuration Options @@ -47,7 +47,7 @@ This plugin supports the following configuration options plus the <> |<>|No | <> |<>|No | <> |<>|No -| <> |<>, one of `["json", "json_batch", "form", "message"]`|No +| <> |<>, one of `["json", "json_batch", "form", "message", "codec"]`|No | <> |<>|No | <> |<>|No | <> |<>, one of `["put", "post", "patch", "delete", "get", "head"]`|Yes @@ -136,6 +136,7 @@ If not specified, this defaults to the following: * if format is "json", "application/json" * if format is "json_batch", "application/json". Each Logstash batch of events will be concatenated into a single array and sent in one request. * if format is "form", "application/x-www-form-urlencoded" +* if format is "codec", "text/xml" [id="plugins-{type}s-{plugin}-cookies"] ===== `cookies` @@ -157,7 +158,7 @@ Should redirects be followed? Defaults to `true` [id="plugins-{type}s-{plugin}-format"] ===== `format` - * Value can be any of: `json`, `json_batch`, `form`, `message` + * Value can be any of: `json`, `json_batch`, `form`, `message`, `codec` * Default value is `"json"` Set the format of the http body. @@ -171,6 +172,8 @@ into a query parameter string, e.g. `foo=bar&baz=fizz...` If message, then the body will be the result of formatting the event according to message +If codec, then the body will be the result of executing the specified codec + Otherwise, the event is sent as json. [id="plugins-{type}s-{plugin}-headers"] diff --git a/lib/logstash/outputs/http.rb b/lib/logstash/outputs/http.rb index 3f37af7..b91f0b8 100644 --- a/lib/logstash/outputs/http.rb +++ b/lib/logstash/outputs/http.rb @@ -81,14 +81,20 @@ class LogStash::Outputs::Http < LogStash::Outputs::Base # If message, then the body will be the result of formatting the event according to message # # Otherwise, the event is sent as json. - config :format, :validate => ["json", "json_batch", "form", "message"], :default => "json" + config :format, :validate => ["json", "json_batch", "form", "message", "codec"], :default => "json" # Set this to true if you want to enable gzip compression for your http requests config :http_compression, :validate => :boolean, :default => false config :message, :validate => :string + default :codec, :validate => :string, :default => "json" + def register + @codec.on_event do |event, payload| + payload + end + @http_method = @http_method.to_sym # We count outstanding requests with this queue @@ -106,6 +112,7 @@ def register when "json" ; @content_type = "application/json" when "json_batch" ; @content_type = "application/json" when "message" ; @content_type = "text/plain" + when "codec" ; @content_type = "text/plain" end end @@ -298,11 +305,12 @@ def log_failure(message, opts) # Format the HTTP body def event_body(event) - # TODO: Create an HTTP post data codec, use that here if @format == "json" LogStash::Json.dump(map_event(event)) elsif @format == "message" event.sprintf(@message) + elsif @format == "codec" + @codec.encode(event) elsif @format == "json_batch" LogStash::Json.dump(event.map {|e| map_event(e) }) else diff --git a/logstash-output-http.gemspec b/logstash-output-http.gemspec index c5d2d87..6c0dfa5 100644 --- a/logstash-output-http.gemspec +++ b/logstash-output-http.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-http' - s.version = '5.2.4' + s.version = '5.2.5' s.licenses = ['Apache License (2.0)'] s.summary = "Sends events to a generic HTTP or HTTPS endpoint" 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"