Skip to content

Commit bec5327

Browse files
INFRA-7585 support json_lines format
logstash-plugins#112
1 parent 7d2b3f6 commit bec5327

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/logstash/outputs/http.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ class PluginInternalQueueLeftoverError < StandardError; end
8888
# If message, then the body will be the result of formatting the event according to message
8989
#
9090
# Otherwise, the event is sent as json.
91-
config :format, :validate => ["json", "json_batch", "form", "message"], :default => "json"
91+
config :format, :validate => ["json", "json_batch", "json_lines", "form", "message"], :default => "json"
92+
93+
# Set this to true if you want format url with batch body. Formating url is done by first element in batch
94+
config :batch_url_format, :validate => :boolean, :default => false
9295

9396
# Set this to true if you want to enable gzip compression for your http requests
9497
config :http_compression, :validate => :boolean, :default => false
@@ -112,11 +115,12 @@ def register
112115
when "form" ; @content_type = "application/x-www-form-urlencoded"
113116
when "json" ; @content_type = "application/json"
114117
when "json_batch" ; @content_type = "application/json"
118+
when "json_lines"; @content_type = "application/x-ndjson"
115119
when "message" ; @content_type = "text/plain"
116120
end
117121
end
118122

119-
@is_batch = @format == "json_batch"
123+
@is_batch = @format == "json_batch" || @format == "json_lines"
120124

121125
@headers["Content-Type"] = @content_type
122126

@@ -242,7 +246,14 @@ def send_event(event, attempt)
242246
body = event_body(event)
243247

244248
# Send the request
245-
url = @is_batch ? @url : event.sprintf(@url)
249+
250+
# Format url
251+
if @is_batch
252+
url = @batch_url_format ? event[0].sprintf(@url) : @url
253+
else
254+
url = event.sprintf(@url)
255+
end
256+
246257
headers = @is_batch ? @headers : event_headers(event)
247258

248259
# Compress the body and add appropriate header
@@ -336,6 +347,8 @@ def event_body(event)
336347
event.sprintf(@message)
337348
elsif @format == "json_batch"
338349
LogStash::Json.dump(event.map {|e| map_event(e) })
350+
elsif @format == "json_lines"
351+
(event.map { |e| LogStash::Json.dump(map_event(e)) }).join("\n")
339352
else
340353
encode(map_event(event))
341354
end

logstash-output-http.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-http'
3-
s.version = '5.5.0'
3+
s.version = '5.5.0-jlines'
44
s.licenses = ['Apache License (2.0)']
55
s.summary = "Sends events to a generic HTTP or HTTPS endpoint"
66
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"

0 commit comments

Comments
 (0)