Method: Logtail::LogDevices::HTTP#initialize

Defined in:
lib/logtail/log_devices/http.rb

#initialize(source_token, options = {}) ⇒ HTTP

Instantiates a new HTTP log device that can be passed to Logtail::Logger#initialize.

The class maintains a buffer which is flushed in batches to the Logtail API. 2 options control when the flush happens, ‘:batch_byte_size` and `:flush_interval`. If either of these are surpassed, the buffer will be flushed.

By default, the buffer will apply back pressure when the rate of log messages exceeds the maximum delivery rate. If you don’t want to sacrifice app performance in this case you can drop the log messages instead by passing a DroppingSizedQueue via the ‘:request_queue` option.

Examples:

Basic usage

Logtail::Logger.new(Logtail::LogDevices::HTTP.new("<source_token>", ingesting_host: "<ingesting_host>"))

Apply back pressure instead of dropping messages

http_log_device = Logtail::LogDevices::HTTP.new("<source_token>", ingesting_host: "<ingesting_host>", request_queue: SizedQueue.new(25))
Logtail::Logger.new(http_log_device)

Parameters:

  • source_token (String)

    The API key provided to you after you add your source to [Better Stack](telemetry.betterstack.com).

  • options (Hash) (defaults to: {})

    the options to create a HTTP log device with.

  • attributes (Hash)

    a customizable set of options



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logtail/log_devices/http.rb', line 71

def initialize(source_token, options = {})
  # Handle backward-compatibility of argument names
  options[:ingesting_host] ||= options[:logtail_host]
  options[:ingesting_port] ||= options[:logtail_port]
  options[:ingesting_scheme] ||= options[:logtail_scheme]

  @source_token = source_token || raise(ArgumentError.new("The source_token parameter cannot be blank"))
  @ingesting_host = options[:ingesting_host] || ENV['INGESTING_HOST'] || ENV['LOGTAIL_HOST'] || DEFAULT_INGESTING_HOST
  @ingesting_port = options[:ingesting_port] || ENV['INGESTING_PORT'] || ENV['LOGTAIL_PORT'] || DEFAULT_INGESTING_PORT
  @ingesting_scheme = options[:ingesting_scheme] || ENV['INGESTING_SCHEME'] || ENV['LOGTAIL_SCHEME'] || DEFAULT_INGESTING_SCHEME
  @batch_size = options[:batch_size] || 1_000
  @flush_continuously = options[:flush_continuously] != false
  @flush_interval = options[:flush_interval] || 2 # 2 seconds
  @requests_per_conn = options[:requests_per_conn] || 2_500
  @msg_queue = FlushableDroppingSizedQueue.new(@batch_size)
  @request_queue = options[:request_queue] || FlushableDroppingSizedQueue.new(25)
  @successive_error_count = 0
  @requests_in_flight = 0
end