Class: LogStash::Outputs::DatadogLogs::DatadogHTTPClient

Inherits:
DatadogClient
  • Object
show all
Defined in:
lib/logstash/outputs/datadog_logs.rb

Constant Summary collapse

RETRYABLE_EXCEPTIONS =
[
    ::Manticore::Timeout,
    ::Manticore::SocketException,
    ::Manticore::ClientProtocolException,
    ::Manticore::ResolutionFailure
]

Instance Method Summary collapse

Methods inherited from DatadogClient

#send_retries

Constructor Details

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

Returns a new instance of DatadogHTTPClient.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/logstash/outputs/datadog_logs.rb', line 199

def initialize(logger, use_ssl, no_ssl_validation, host, port, use_compression, api_key, force_v1_routes, http_proxy)
  @logger = logger
  protocol = use_ssl ? "https" : "http"

  @headers = {"Content-Type" => "application/json"}
  if use_compression
    @headers["Content-Encoding"] = "gzip"
  end

  if force_v1_routes
    @url = "#{protocol}://#{host}:#{port.to_s}/v1/input/#{api_key}"
  else
    @url = "#{protocol}://#{host}:#{port.to_s}/api/v2/logs"
    @headers["DD-API-KEY"] = api_key
    @headers["DD-EVP-ORIGIN"] = "logstash"
    @headers["DD-EVP-ORIGIN-VERSION"] = DatadogLogStashPlugin::VERSION
  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"))

  config = {}
  config[:ssl][:verify] = :disable if no_ssl_validation
  if http_proxy != ""
    config[:proxy] = http_proxy
  end
  @client = Manticore::Client.new(config)
end

Instance Method Details

#closeObject



252
253
254
# File 'lib/logstash/outputs/datadog_logs.rb', line 252

def close
  @client.close
end

#retryable_exception?(exception) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/logstash/outputs/datadog_logs.rb', line 248

def retryable_exception?(exception)
  RETRYABLE_EXCEPTIONS.any? { |e| exception.is_a?(e) }
end

#send(payload) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/logstash/outputs/datadog_logs.rb', line 227

def send(payload)
  begin
    response = @client.post(@url, :body => payload, :headers => @headers).call
    # in case of error or 429, we will retry sending this payload
    if response.code >= 500 || response.code == 429
      raise RetryableError.new "Unable to send payload: #{response.code} #{response.body}"
    end
    if response.code >= 400
      @logger.error("Unable to send payload due to client error: #{response.code} #{response.body}")
    end
  rescue => client_exception
    should_retry = retryable_exception?(client_exception)
    if should_retry
      raise RetryableError.new "Unable to send payload #{client_exception.message}"
    else
      raise client_exception
    end
  end

end