Class: Fluent::DatadogOutput::DatadogHTTPClient

Inherits:
DatadogClient show all
Defined in:
lib/fluent/plugin/out_datadog.rb

Overview

HTTP datadog client

Instance Method Summary collapse

Methods inherited from DatadogClient

#send_retries

Constructor Details

#initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, use_compression, api_key) ⇒ DatadogHTTPClient

Returns a new instance of DatadogHTTPClient.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/fluent/plugin/out_datadog.rb', line 304

def initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, use_compression, api_key)
  @logger = logger
  protocol = use_ssl ? "https" : "http"
  port = use_ssl ? ssl_port : port
  @uri = URI("#{protocol}://#{host}:#{port.to_s}/v1/input/#{api_key}")
  proxy_uri = :ENV
  if http_proxy
    proxy_uri = URI.parse(http_proxy)
  elsif ENV['HTTP_PROXY'] || ENV['http_proxy']
    logger.info("Using HTTP proxy defined in `HTTP_PROXY`/`http_proxy` env vars")
  end
  logger.info("Starting HTTP connection to #{protocol}://#{host}:#{port.to_s} with compression " + (use_compression ? "enabled" : "disabled"))
  @client = Net::HTTP::Persistent.new name: "fluent-plugin-datadog-logcollector", proxy: proxy_uri
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE if no_ssl_validation
  @client.override_headers["Content-Type"] = "application/json"
  if use_compression
    @client.override_headers["Content-Encoding"] = "gzip"
  end
  if !@client.proxy_uri.nil?
    # Log the proxy settings as resolved by the HTTP client
    logger.info("Using HTTP proxy #{@client.proxy_uri.scheme}://#{@client.proxy_uri.host}:#{@client.proxy_uri.port} username: #{@client.proxy_uri.user ? "set" : "unset"}, password: #{@client.proxy_uri.password ? "set" : "unset"}")
  end
end

Instance Method Details

#closeObject



341
342
343
# File 'lib/fluent/plugin/out_datadog.rb', line 341

def close
  @client.shutdown
end

#send(payload) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/fluent/plugin/out_datadog.rb', line 328

def send(payload)
  request = Net::HTTP::Post.new @uri.request_uri
  request.body = payload
  response = @client.request @uri, request
  res_code = response.code.to_i
  if res_code >= 500
    raise RetryableError.new "Unable to send payload: #{res_code} #{response.message}"
  end
  if res_code >= 400
    @logger.error("Unable to send payload due to client error: #{res_code} #{response.message}")
  end
end