Class: Onlylogs::HttpLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/onlylogs/http_logger.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
100
DEFAULT_FLUSH_INTERVAL =
0.5
DEFAULT_MAX_QUEUE_SIZE =
10_000
DEFAULT_OPEN_TIMEOUT =

Keep timeouts short: a single slow/dead drain must never stall the app for long.

0.5
DEFAULT_READ_TIMEOUT =
0.5
CIRCUIT_FAILURE_THRESHOLD =

Open the circuit after this many consecutive failed sends

3
CIRCUIT_COOLDOWN =

…and keep it open for this long once it is open.

30

Instance Method Summary collapse

Constructor Details

#initialize(local_fallback: $stdout, drain_url: ENV["ONLYLOGS_DRAIN_URL"], batch_size: ENV.fetch("ONLYLOGS_BATCH_SIZE", DEFAULT_BATCH_SIZE).to_i, flush_interval: ENV.fetch("ONLYLOGS_FLUSH_INTERVAL", DEFAULT_FLUSH_INTERVAL).to_f, max_queue_size: ENV.fetch("ONLYLOGS_MAX_QUEUE_SIZE", DEFAULT_MAX_QUEUE_SIZE).to_i, open_timeout: ENV.fetch("ONLYLOGS_OPEN_TIMEOUT", DEFAULT_OPEN_TIMEOUT).to_f, read_timeout: ENV.fetch("ONLYLOGS_READ_TIMEOUT", DEFAULT_READ_TIMEOUT).to_f, circuit_cooldown: ENV.fetch("ONLYLOGS_CIRCUIT_COOLDOWN", CIRCUIT_COOLDOWN).to_f) ⇒ HttpLogger

Returns a new instance of HttpLogger.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/onlylogs/http_logger.rb', line 31

def initialize(
  local_fallback: $stdout,
  drain_url: ENV["ONLYLOGS_DRAIN_URL"],
  batch_size: ENV.fetch("ONLYLOGS_BATCH_SIZE", DEFAULT_BATCH_SIZE).to_i,
  flush_interval: ENV.fetch("ONLYLOGS_FLUSH_INTERVAL", DEFAULT_FLUSH_INTERVAL).to_f,
  max_queue_size: ENV.fetch("ONLYLOGS_MAX_QUEUE_SIZE", DEFAULT_MAX_QUEUE_SIZE).to_i,
  open_timeout: ENV.fetch("ONLYLOGS_OPEN_TIMEOUT", DEFAULT_OPEN_TIMEOUT).to_f,
  read_timeout: ENV.fetch("ONLYLOGS_READ_TIMEOUT", DEFAULT_READ_TIMEOUT).to_f,
  circuit_cooldown: ENV.fetch("ONLYLOGS_CIRCUIT_COOLDOWN", CIRCUIT_COOLDOWN).to_f
)
  super(local_fallback)
  @drain_url = drain_url
  @batch_size = batch_size
  @flush_interval = flush_interval
  @max_queue_size = max_queue_size
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @circuit_cooldown = circuit_cooldown
  @queue = Queue.new
  @mutex = Mutex.new

  @consecutive_failures = 0
  @circuit_open_until = nil
  @dropped = 0

  if @drain_url
    start_sender
  else
    $stderr.puts "Onlylogs::HttpLogger error: ONLYLOGS_DRAIN_URL is not set; logger is disabled." # rubocop:disable Style/StderrPuts
  end
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/onlylogs/http_logger.rb', line 63

def add(severity, message = nil, progname = nil, &block)
  return true unless @drain_url

  if message.nil?
    if block_given?
      message = block.call
    else
      message = progname
      progname = nil
    end
  end

  formatted = format_message(format_severity(severity), Time.now, progname, message.to_s)
  enqueue(formatted.chomp) if formatted
  super
end

#closeObject



80
81
82
83
84
# File 'lib/onlylogs/http_logger.rb', line 80

def close
  flush
  @running = false
  @sender_thread&.join(2)
end

#flushObject



86
87
88
89
# File 'lib/onlylogs/http_logger.rb', line 86

def flush
  send_batch(drain_queue)
  super
end