Class: AllStak::Modules::Logs

Inherits:
Object
  • Object
show all
Defined in:
lib/allstak/modules/logs.rb

Overview

Buffered structured-log ingestion. Each log is sent as its own POST.

Constant Summary collapse

PATH =
"/ingest/v1/logs".freeze
VALID_LEVELS =
%w[debug info warn error fatal].freeze

Instance Method Summary collapse

Constructor Details

#initialize(transport, config, logger, breadcrumb_sink: nil) ⇒ Logs

Returns a new instance of Logs.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/allstak/modules/logs.rb', line 8

def initialize(transport, config, logger, breadcrumb_sink: nil)
  @transport = transport
  @config = config
  @logger = logger
  # Optional callable invoked for each accepted log so AllStak.log.*
  # entries also surface as breadcrumbs on the next captured exception.
  # Injected by the client; nil keeps Logs standalone (and recursion-free).
  @breadcrumb_sink = breadcrumb_sink
  @captured_count = 0
  @buffer = Transport::FlushBuffer.new(
    name: "logs",
    max_size: config.buffer_size,
    interval_ms: config.flush_interval_ms,
    flush_proc: method(:flush_batch),
    logger: logger
  )
end

Instance Method Details

#buffer_countObject



64
65
66
# File 'lib/allstak/modules/logs.rb', line 64

def buffer_count
  @buffer.count
end

#captured_countObject



72
73
74
# File 'lib/allstak/modules/logs.rb', line 72

def captured_count
  @captured_count
end

#debug(msg, **kw) ⇒ Object



50
# File 'lib/allstak/modules/logs.rb', line 50

def debug(msg, **kw); log("debug", msg, **kw); end

#dropped_countObject



68
69
70
# File 'lib/allstak/modules/logs.rb', line 68

def dropped_count
  @buffer.dropped_count
end

#error(msg, **kw) ⇒ Object



53
# File 'lib/allstak/modules/logs.rb', line 53

def error(msg, **kw); log("error", msg, **kw); end

#fatal(msg, **kw) ⇒ Object



54
# File 'lib/allstak/modules/logs.rb', line 54

def fatal(msg, **kw); log("fatal", msg, **kw); end

#flushObject



56
57
58
# File 'lib/allstak/modules/logs.rb', line 56

def flush
  @buffer.flush
end

#info(msg, **kw) ⇒ Object



51
# File 'lib/allstak/modules/logs.rb', line 51

def info(msg, **kw);  log("info",  msg, **kw); end

#log(level, message, service: nil, trace_id: nil, span_id: nil, request_id: nil, user_id: nil, error_id: nil, metadata: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/allstak/modules/logs.rb', line 26

def log(level, message, service: nil, trace_id: nil, span_id: nil,
        request_id: nil, user_id: nil, error_id: nil, metadata: nil)
  return if @transport.disabled?
  level = normalize_level(level)

  payload = {
    level: level,
    message: message.to_s,
    service: service || @config.service_name,
    environment: @config.environment,
    release: @config.respond_to?(:release) ? @config.release : nil,
    traceId: trace_id,
    spanId: span_id,
    requestId: request_id,
    userId: user_id,
    errorId: error_id,
    metadata: @config.release_tags.merge( || {})
  }.compact
  @captured_count += 1
  @buffer.push(payload)
  emit_breadcrumb(level, message, trace_id: trace_id, span_id: span_id)
  nil
end

#shutdownObject



60
61
62
# File 'lib/allstak/modules/logs.rb', line 60

def shutdown
  @buffer.shutdown
end

#warn(msg, **kw) ⇒ Object



52
# File 'lib/allstak/modules/logs.rb', line 52

def warn(msg, **kw);  log("warn",  msg, **kw); end