Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_lines and url format when body is batch #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/logstash/outputs/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ 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", "json_lines", "form", "message"], :default => "json"

# Set this to true if you want format url with batch body. Formating url is done by first element in batch
config :batch_url_format, :validate => :boolean, :default => false

# Set this to true if you want to enable gzip compression for your http requests
config :http_compression, :validate => :boolean, :default => false
Expand All @@ -105,11 +108,12 @@ def register
when "form" ; @content_type = "application/x-www-form-urlencoded"
when "json" ; @content_type = "application/json"
when "json_batch" ; @content_type = "application/json"
when "json_lines"; @content_type = "application/json_lines"
when "message" ; @content_type = "text/plain"
end
end

@is_batch = @format == "json_batch"
@is_batch = @format == "json_batch" || @format == "json_lines"

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

Expand Down Expand Up @@ -226,7 +230,14 @@ def send_event(event, attempt)
body = event_body(event)

# Send the request
url = @is_batch ? @url : event.sprintf(@url)

# Format url
if @is_batch
url = @batch_url_format ? event[0].sprintf(@url) : @url
else
url = event.sprintf(@url)
end

headers = @is_batch ? @headers : event_headers(event)

# Compress the body and add appropriate header
Expand Down Expand Up @@ -305,6 +316,8 @@ def event_body(event)
event.sprintf(@message)
elsif @format == "json_batch"
LogStash::Json.dump(event.map {|e| map_event(e) })
elsif @format == "json_lines"
(event.map { |e| LogStash::Json.dump(map_event(e)) }).join("\n")
else
encode(map_event(event))
end
Expand Down