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, custom_headers, use_compression, api_key, force_v1_routes = false) ⇒ DatadogHTTPClient

Returns a new instance of DatadogHTTPClient.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/fluent/plugin/out_datadog.rb', line 321

def initialize(logger, use_ssl, no_ssl_validation, host, ssl_port, port, http_proxy, custom_headers, use_compression, api_key, force_v1_routes = false)
  @logger = logger
  protocol = use_ssl ? "https" : "http"
  port = use_ssl ? ssl_port : port
  if force_v1_routes
    @uri = URI("#{protocol}://#{host}:#{port.to_s}/v1/input/#{api_key}")
  else
    @uri = URI("#{protocol}://#{host}:#{port.to_s}/api/v2/logs")
  end
  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") + (force_v1_routes ? " using v1 routes" : " using v2 routes"))
  @client = Net::HTTP::Persistent.new name: "fluent-plugin-datadog-logcollector", proxy: proxy_uri
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE if no_ssl_validation
  custom_headers.each do |key, value|
    @client.override_headers[key] = value
  end
  unless force_v1_routes
    @client.override_headers["DD-API-KEY"] = api_key
    @client.override_headers["DD-EVP-ORIGIN"] = "fluent"
    @client.override_headers["DD-EVP-ORIGIN-VERSION"] = DatadogFluentPlugin::VERSION
  end
  @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



371
372
373
# File 'lib/fluent/plugin/out_datadog.rb', line 371

def close
  @client.shutdown
end

#send(payload) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/fluent/plugin/out_datadog.rb', line 357

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
  # on a backend error or on an http 429, retry with backoff
  if res_code >= 500 || res_code == 429
    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