Class: Loggability::LogDevice::Datadog

Inherits:
Http show all
Defined in:
lib/loggability/log_device/datadog.rb

Overview

A log device that sends logs to Datadog’s HTTP endpoint for receiving logs

Constant Summary collapse

DEFAULT_ENDPOINT =

Datadog’s HTTP endpoint URL for sending logs to

URI( "https://http-intake.logs.datadoghq.com/v1/input" )
MAX_BATCH_SIZE =

The max number of messages that can be sent to datadog in a single payload

480
MAX_MESSAGE_BYTESIZE =

The max size in bytes for a single message. Limiting the message size to 200kB to leave room for other info such as tags, metadata, etc. DataDog’s max size for a single log entry is 256kB

204_800
MAX_BATCH_BYTESIZE =

The max size in bytes of all messages in the batch. Limiting the total messages size to 4MB to leave room for other info such as tags, metadata, etc. Datadog’s max size for the entire payload is 5MB

4_194_304
DEFAULT_OPTIONS =

Override the default HTTP device options for sending logs to DD

{
  max_batch_size: MAX_BATCH_SIZE,
  max_message_bytesize: MAX_MESSAGE_BYTESIZE,
  max_batch_bytesize: MAX_BATCH_BYTESIZE,
}

Constants inherited from Http

Http::DEFAULT_BATCH_INTERVAL, Http::DEFAULT_EXECUTOR_CLASS, Http::DEFAULT_MAX_BATCH_SIZE, Http::DEFAULT_MAX_MESSAGE_BYTESIZE, Http::DEFAULT_MAX_QUEUE_BYTESIZE, Http::DEFAULT_WRITE_TIMEOUT

Constants inherited from Loggability::LogDevice

DEVICE_TARGET_REGEX

Instance Attribute Summary collapse

Attributes inherited from Http

#batch_interval, #endpoint, #executor, #executor_class, #last_send_time, #logs_queue, #logs_queue_bytesize, #max_batch_bytesize, #max_batch_size, #max_message_bytesize, #max_queue_bytesize, #timer_task, #write_timeout

Instance Method Summary collapse

Methods inherited from Http

#batch_ready?, #close, #get_next_log_payload, #http_client, #running?, #send_logs, #start, #start_executor, #start_timer_task, #stop, #write

Methods inherited from Loggability::LogDevice

#close, create, load_device_type, parse_device_spec, #write

Constructor Details

#initialize(api_key, endpoint = DEFAULT_ENDPOINT, options = {}) ⇒ Datadog

Create a new Datadog



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/loggability/log_device/datadog.rb', line 46

def initialize( api_key, endpoint=DEFAULT_ENDPOINT, options={} )
  if endpoint.is_a?( Hash )
    options = endpoint
    endpoint = DEFAULT_ENDPOINT
  end

  super( endpoint, options )

  @api_key = api_key
  @hostname = Socket.gethostname
end

Instance Attribute Details

#api_keyObject (readonly)

The configured Datadog API key



69
70
71
# File 'lib/loggability/log_device/datadog.rb', line 69

def api_key
  @api_key
end

#hostnameObject (readonly)

The name of the current host



65
66
67
# File 'lib/loggability/log_device/datadog.rb', line 65

def hostname
  @hostname
end

Instance Method Details

#format_log_message(message) ⇒ Object

Format an individual log message for Datadog.



73
74
75
76
77
78
# File 'lib/loggability/log_device/datadog.rb', line 73

def format_log_message( message )
  return {
    hostname: self.hostname,
    message: message
  }.to_json
end

#make_batch_requestObject

Overridden to add the configured API key to the headers of each request.



82
83
84
85
86
87
88
# File 'lib/loggability/log_device/datadog.rb', line 82

def make_batch_request
  request = super

  request[ 'DD-API-KEY' ] = self.api_key

  return request
end