Skip to content

Commit 8089ea8

Browse files
added custom headers support (#69)
* added custom headers support * fixed tests
1 parent 93811f1 commit 8089ea8

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/fluent/plugin/out_datadog.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class RetryableError < StandardError;
5252
config_param :max_retries, :integer, :default => -1
5353
config_param :max_backoff, :integer, :default => 30
5454
config_param :use_http, :bool, :default => true
55+
config_param :custom_headers, :hash, :default => {}
5556
config_param :use_compression, :bool, :default => true
5657
config_param :compression_level, :integer, :default => 6
5758
config_param :no_ssl_validation, :bool, :default => false
@@ -98,7 +99,7 @@ def formatted_to_msgpack_binary?
9899

99100
def start
100101
super
101-
@client = new_client(log, @api_key, @use_http, @use_ssl, @no_ssl_validation, @host, @ssl_port, @port, @http_proxy, @use_compression, @force_v1_routes)
102+
@client = new_client(log, @api_key, @use_http, @use_ssl, @no_ssl_validation, @host, @ssl_port, @port, @http_proxy, @custom_headers, @use_compression, @force_v1_routes)
102103
end
103104

104105
def shutdown
@@ -270,9 +271,9 @@ def gzip_compress(payload, compression_level)
270271
end
271272

272273
# Build a new transport client
273-
def new_client(logger, api_key, use_http, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, use_compression, force_v1_routes)
274+
def new_client(logger, api_key, use_http, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, custom_headers, use_compression, force_v1_routes)
274275
if use_http
275-
DatadogHTTPClient.new logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, use_compression, api_key, force_v1_routes
276+
DatadogHTTPClient.new logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, custom_headers, use_compression, api_key, force_v1_routes
276277
else
277278
DatadogTCPClient.new logger, use_ssl, no_ssl_validation, host, ssl_port, port
278279
end
@@ -310,7 +311,7 @@ class DatadogHTTPClient < DatadogClient
310311
require 'net/http'
311312
require 'net/http/persistent'
312313

313-
def initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, use_compression, api_key, force_v1_routes = false)
314+
def initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, custom_headers, use_compression, api_key, force_v1_routes = false)
314315
@logger = logger
315316
protocol = use_ssl ? "https" : "http"
316317
port = use_ssl ? ssl_port : port
@@ -328,6 +329,9 @@ def initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_pr
328329
logger.info("Starting HTTP connection to #{protocol}://#{host}:#{port.to_s} with compression " + (use_compression ? "enabled" : "disabled") + (force_v1_routes ? " using v1 routes" : " using v2 routes"))
329330
@client = Net::HTTP::Persistent.new name: "fluent-plugin-datadog-logcollector", proxy: proxy_uri
330331
@client.verify_mode = OpenSSL::SSL::VERIFY_NONE if no_ssl_validation
332+
custom_headers.each do |key, value|
333+
@client.override_headers[key] = value
334+
end
331335
unless force_v1_routes
332336
@client.override_headers["DD-API-KEY"] = api_key
333337
@client.override_headers["DD-EVP-ORIGIN"] = "fluent"

test/plugin/test_out_datadog.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def create_valid_subject
229229
api_key = 'XXX'
230230
stub_dd_request_with_return_code(api_key, 500)
231231
payload = '{}'
232-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key, true
232+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key, true
233233
assert_raise(Fluent::DatadogOutput::RetryableError) do
234234
client.send(payload)
235235
end
@@ -241,7 +241,7 @@ def create_valid_subject
241241
api_key = 'XXX'
242242
stub_dd_request_with_return_code(api_key, 429)
243243
payload = '{}'
244-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key, true
244+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key, true
245245
assert_raise(Fluent::DatadogOutput::RetryableError) do
246246
client.send(payload)
247247
end
@@ -251,7 +251,7 @@ def create_valid_subject
251251
api_key = 'XXX'
252252
stub_dd_request_with_return_code(api_key, 400)
253253
payload = '{}'
254-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key, true
254+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key, true
255255
assert_nothing_raised do
256256
client.send(payload)
257257
end
@@ -264,7 +264,7 @@ def create_valid_subject
264264
api_key = 'XXX'
265265
stub_dd_request_with_return_code(api_key, 500, true)
266266
payload = '{}'
267-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key
267+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key
268268
assert_raise(Fluent::DatadogOutput::RetryableError) do
269269
client.send(payload)
270270
end
@@ -274,7 +274,7 @@ def create_valid_subject
274274
api_key = 'XXX'
275275
stub_dd_request_with_return_code(api_key, 429, true)
276276
payload = '{}'
277-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key
277+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key
278278
assert_raise(Fluent::DatadogOutput::RetryableError) do
279279
client.send(payload)
280280
end
@@ -284,7 +284,7 @@ def create_valid_subject
284284
api_key = 'XXX'
285285
stub_dd_request_with_return_code(api_key, 400, true)
286286
payload = '{}'
287-
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, false, api_key
287+
client = Fluent::DatadogOutput::DatadogHTTPClient.new Logger.new(STDOUT), false, false, "datadog.com", 443, 80, nil, {}, false, api_key
288288
assert_nothing_raised do
289289
client.send(payload)
290290
end

0 commit comments

Comments
 (0)