semantic_logger 5.1.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -12
- data/Rakefile +15 -4
- data/docs/api.md +196 -19
- data/docs/appenders.md +579 -68
- data/docs/config.md +647 -38
- data/docs/index.md +31 -5
- data/docs/log.md +56 -17
- data/docs/metrics.md +144 -30
- data/docs/operations.md +339 -24
- data/docs/rails.md +396 -8
- data/docs/security.md +222 -27
- data/docs/testing.md +28 -3
- data/docs/upgrading.md +938 -7
- data/lib/semantic_logger/appender/async.rb +42 -11
- data/lib/semantic_logger/appender/bugsnag.rb +7 -2
- data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
- data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
- data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
- data/lib/semantic_logger/appender/file.rb +160 -37
- data/lib/semantic_logger/appender/graylog.rb +101 -11
- data/lib/semantic_logger/appender/honeybadger.rb +7 -1
- data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
- data/lib/semantic_logger/appender/http.rb +308 -46
- data/lib/semantic_logger/appender/io.rb +11 -6
- data/lib/semantic_logger/appender/kafka.rb +204 -44
- data/lib/semantic_logger/appender/loki.rb +55 -9
- data/lib/semantic_logger/appender/mongodb.rb +109 -5
- data/lib/semantic_logger/appender/new_relic.rb +3 -4
- data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
- data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
- data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
- data/lib/semantic_logger/appender/sentry.rb +3 -4
- data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
- data/lib/semantic_logger/appender/splunk.rb +11 -2
- data/lib/semantic_logger/appender/splunk_http.rb +92 -1
- data/lib/semantic_logger/appender/syslog.rb +149 -32
- data/lib/semantic_logger/appender/tcp.rb +73 -98
- data/lib/semantic_logger/appender/text_output.rb +65 -0
- data/lib/semantic_logger/appender/udp.rb +140 -18
- data/lib/semantic_logger/appender/wrapper.rb +10 -7
- data/lib/semantic_logger/appender.rb +115 -10
- data/lib/semantic_logger/appenders.rb +145 -5
- data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
- data/lib/semantic_logger/base.rb +115 -81
- data/lib/semantic_logger/call_site.rb +235 -0
- data/lib/semantic_logger/context.rb +222 -0
- data/lib/semantic_logger/core_ext/process.rb +20 -5
- data/lib/semantic_logger/core_ext/warning.rb +70 -0
- data/lib/semantic_logger/deprecation.rb +139 -0
- data/lib/semantic_logger/exception_details.rb +51 -0
- data/lib/semantic_logger/formatters/base.rb +186 -20
- data/lib/semantic_logger/formatters/color.rb +18 -3
- data/lib/semantic_logger/formatters/default.rb +53 -8
- data/lib/semantic_logger/formatters/ecs.rb +9 -4
- data/lib/semantic_logger/formatters/json.rb +9 -0
- data/lib/semantic_logger/formatters/logfmt.rb +155 -21
- data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
- data/lib/semantic_logger/formatters/loki.rb +430 -38
- data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
- data/lib/semantic_logger/formatters/one_line.rb +101 -3
- data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
- data/lib/semantic_logger/formatters/pattern.rb +68 -12
- data/lib/semantic_logger/formatters/raw.rb +40 -11
- data/lib/semantic_logger/formatters/signalfx.rb +55 -15
- data/lib/semantic_logger/formatters/syslog.rb +114 -20
- data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
- data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
- data/lib/semantic_logger/formatters.rb +1 -0
- data/lib/semantic_logger/levels.rb +10 -1
- data/lib/semantic_logger/log.rb +384 -82
- data/lib/semantic_logger/loggable.rb +56 -73
- data/lib/semantic_logger/logger.rb +31 -5
- data/lib/semantic_logger/metric/new_relic.rb +33 -18
- data/lib/semantic_logger/metric/signalfx.rb +29 -18
- data/lib/semantic_logger/metric/statsd.rb +41 -15
- data/lib/semantic_logger/processor.rb +194 -5
- data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
- data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
- data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
- data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
- data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
- data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
- data/lib/semantic_logger/queue_processor/request.rb +93 -0
- data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
- data/lib/semantic_logger/queue_processor.rb +519 -170
- data/lib/semantic_logger/ractor_copy.rb +120 -0
- data/lib/semantic_logger/ractor_processor.rb +82 -0
- data/lib/semantic_logger/ractor_receiver.rb +282 -0
- data/lib/semantic_logger/ractor_support.rb +28 -0
- data/lib/semantic_logger/redactor.rb +310 -0
- data/lib/semantic_logger/semantic_logger.rb +328 -81
- data/lib/semantic_logger/subscriber.rb +250 -10
- data/lib/semantic_logger/sync_processor.rb +237 -26
- data/lib/semantic_logger/test/capture_log_events.rb +26 -5
- data/lib/semantic_logger/test/minitest.rb +2 -1
- data/lib/semantic_logger/test/rspec.rb +1 -1
- data/lib/semantic_logger/trap_safe.rb +113 -0
- data/lib/semantic_logger/utils.rb +482 -67
- data/lib/semantic_logger/version.rb +1 -1
- data/lib/semantic_logger.rb +36 -3
- metadata +41 -5
- data/lib/semantic_logger/core_ext/thread.rb +0 -30
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
module SemanticLogger
|
|
2
|
+
class QueueProcessor
|
|
3
|
+
# Internal: reports what happens on the worker thread of a queue processor, its progress and its
|
|
4
|
+
# problems, to the internal logger of its appender.
|
|
5
|
+
#
|
|
6
|
+
# It knows that logger, which is the appender's to choose, and makes sure that a report neither
|
|
7
|
+
# stops the worker thread nor raises into a calling thread, whatever the logger. It counts the log
|
|
8
|
+
# messages dropped because the queue was full, and those discarded because they could not be
|
|
9
|
+
# written, and reports each count at most once per interval.
|
|
10
|
+
class Reporter
|
|
11
|
+
# The kinds of positional parameter that Method#parameters reports, other than *rest.
|
|
12
|
+
POSITIONAL = %i[req opt].freeze
|
|
13
|
+
private_constant :POSITIONAL
|
|
14
|
+
|
|
15
|
+
def initialize(appender)
|
|
16
|
+
@appender = appender
|
|
17
|
+
@dropped = ThrottledCounter.new
|
|
18
|
+
@failed = ThrottledCounter.new(reported_at: nil)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Returns the internal logger of the appender, which it can replace at any time.
|
|
22
|
+
def logger = @appender.logger
|
|
23
|
+
|
|
24
|
+
# Returns [Integer] the number of log messages dropped because the queue was full.
|
|
25
|
+
def dropped_count = @dropped.total
|
|
26
|
+
|
|
27
|
+
# Returns [Integer] the number of log messages discarded because they could not be written,
|
|
28
|
+
# whether their content or the destination was at fault (see Subscriber#message_error?).
|
|
29
|
+
def failed_count = @failed.total
|
|
30
|
+
|
|
31
|
+
# Log to the internal logger, ignoring any StandardError. These calls may run after Ruby has
|
|
32
|
+
# released file handles during shutdown, and the internal logger may not take them, such as a
|
|
33
|
+
# Ruby Logger, which has no #trace, returned by a custom appender's own #logger.
|
|
34
|
+
#
|
|
35
|
+
# Such a logger also takes only the message. So the exception is passed only when there is one,
|
|
36
|
+
# and is added to the message instead for a logger that does not take it (see #takes_exception?).
|
|
37
|
+
def log(level, message, exception = nil)
|
|
38
|
+
internal = logger
|
|
39
|
+
return internal.public_send(level, message) unless exception
|
|
40
|
+
return internal.public_send(level, message, exception) if takes_exception?(internal, level)
|
|
41
|
+
|
|
42
|
+
internal.public_send(
|
|
43
|
+
level,
|
|
44
|
+
Utils.join_texts(message, " -- ", Utils.exception_name(exception), ": ", Utils.exception_message(exception))
|
|
45
|
+
)
|
|
46
|
+
rescue StandardError
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Record a log message dropped because the queue was full, reporting the running count at most
|
|
51
|
+
# once every `interval` seconds. The report gives the reason for the latest drop: a queue too
|
|
52
|
+
# small for the volume of messages, or, when `stopped`, a worker thread that has stopped for
|
|
53
|
+
# good, so that nothing empties the queue, however large.
|
|
54
|
+
def dropped(max_queue_size, stopped:, interval:)
|
|
55
|
+
@dropped.record(interval: interval) do |count, seconds|
|
|
56
|
+
advice =
|
|
57
|
+
if stopped
|
|
58
|
+
"The worker thread has stopped after failing to write (see async_max_retries), so nothing " \
|
|
59
|
+
"empties the queue until SemanticLogger.reopen(force: true) starts it again"
|
|
60
|
+
else
|
|
61
|
+
"The queue is full (max_queue_size: #{max_queue_size}). " \
|
|
62
|
+
"Consider reducing the log level, increasing max_queue_size, or changing the appenders"
|
|
63
|
+
end
|
|
64
|
+
log(:warn, "Async: Dropped #{count} log messages in the last #{seconds.round} seconds. #{advice}")
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Report that the worker thread has fallen behind, when the log message it took off the queue
|
|
69
|
+
# was logged more than `threshold` seconds ago, with the number of messages still `queued`.
|
|
70
|
+
def lag(entry, threshold, queued)
|
|
71
|
+
diff = Time.now - entry.time
|
|
72
|
+
return unless diff > threshold
|
|
73
|
+
|
|
74
|
+
log(:warn, "Async: Appender thread has fallen behind by #{diff} seconds with #{queued} messages " \
|
|
75
|
+
"queued up. Consider reducing the log level or changing the appenders")
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Report the number of log messages queued ahead of a command, such as a flush, that is being
|
|
79
|
+
# submitted: at :warn for more than 1,000, at :info for more than 100, otherwise at :trace, and
|
|
80
|
+
# not at all for none.
|
|
81
|
+
def queued(count, command)
|
|
82
|
+
level = (count > 1_000 && :warn) || (count > 100 && :info) || (count.positive? && :trace)
|
|
83
|
+
log(level, "Async: Queued log messages: #{count}, running command: #{command}") if level
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Record log messages discarded because they could not be written, whether their content or the
|
|
87
|
+
# destination was at fault, if any. The first is reported straight away, with the error. After
|
|
88
|
+
# that the count is reported, with the latest error, at most once every `interval` seconds.
|
|
89
|
+
def failed(logs, exception, interval:)
|
|
90
|
+
return if logs.empty?
|
|
91
|
+
|
|
92
|
+
@failed.record(logs.size, interval: interval) do |count, seconds|
|
|
93
|
+
period = seconds ? " in the last #{seconds.round} seconds" : ""
|
|
94
|
+
log(:error, "Async: Discarded #{count} log message(s)#{period} that could not be written, " \
|
|
95
|
+
"most recently from #{logs.first.name}", exception)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
# Whether the logger's method for the level takes the exception after the message, as a Semantic
|
|
102
|
+
# Logger logger does. A Ruby or Rails Logger takes only the message: at most one positional
|
|
103
|
+
# parameter, and no *rest. Decided before the call, rather than by rescuing ArgumentError, which a
|
|
104
|
+
# logger that does take it can raise too, such as a broadcast logger one of whose loggers raises,
|
|
105
|
+
# after the others have written the report.
|
|
106
|
+
def takes_exception?(internal, level)
|
|
107
|
+
parameters = internal.method(level).parameters
|
|
108
|
+
parameters.any? { |type, _name| type == :rest } ||
|
|
109
|
+
parameters.count { |type, _name| POSITIONAL.include?(type) } > 1
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
module SemanticLogger
|
|
2
|
+
class QueueProcessor
|
|
3
|
+
# Internal: a command, such as :flush or :close, submitted on the queue for the worker thread
|
|
4
|
+
# to run, while the submitting thread waits for the reply. Or a task, a Proc, handed over for the
|
|
5
|
+
# worker thread to run without waiting for it (see QueueProcessor#hand_over), which nobody waits on.
|
|
6
|
+
#
|
|
7
|
+
# It knows its command, whether it has run, and how its reply gets back to the submitting
|
|
8
|
+
# thread, and makes sure that the submitting thread is never left waiting on a reply that cannot
|
|
9
|
+
# come. It also tells whoever counts the requests waiting, once, when it is no longer waiting.
|
|
10
|
+
class Request
|
|
11
|
+
attr_reader :command
|
|
12
|
+
|
|
13
|
+
# The block, if any, is called once the request is no longer waiting (see #done).
|
|
14
|
+
def initialize(command, &on_done)
|
|
15
|
+
@command = command
|
|
16
|
+
@replies = Queue.new
|
|
17
|
+
@on_done = on_done
|
|
18
|
+
@ran = false
|
|
19
|
+
# A single token, which the first call to #done takes.
|
|
20
|
+
@waiting = Queue.new
|
|
21
|
+
@waiting << true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Whether the worker thread has run this request (see #reply).
|
|
25
|
+
def ran? = @ran
|
|
26
|
+
|
|
27
|
+
# Called once the request has run, or will not: one that could not be added to the queue, or
|
|
28
|
+
# that the worker thread took off the queue but skipped, such as one behind a close. Calls the
|
|
29
|
+
# block given to #initialize the first time only, whichever thread calls it, so that the request
|
|
30
|
+
# is counted out once. Also called in a signal trap, where Ruby allows a Queue to be used, but
|
|
31
|
+
# not a Mutex to be locked.
|
|
32
|
+
def done
|
|
33
|
+
@on_done&.call if @waiting.pop(timeout: 0)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Called by the worker thread: run the block for this request, then reply with true, or
|
|
37
|
+
# with false when the block raised. Replies either way.
|
|
38
|
+
def reply
|
|
39
|
+
succeeded = false
|
|
40
|
+
yield
|
|
41
|
+
succeeded = true
|
|
42
|
+
ensure
|
|
43
|
+
@ran = true
|
|
44
|
+
done
|
|
45
|
+
@replies.push(succeeded)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Called by the worker thread: run this command on the appender, or this task, then reply.
|
|
49
|
+
# Returns false once message processing must stop (:close). A command it does not know is
|
|
50
|
+
# yielded to the block, for the caller to report, and otherwise ignored.
|
|
51
|
+
def run(appender)
|
|
52
|
+
case command
|
|
53
|
+
when Proc
|
|
54
|
+
reply { command.call }
|
|
55
|
+
when :flush
|
|
56
|
+
reply { appender.flush }
|
|
57
|
+
when :close
|
|
58
|
+
reply { appender.close }
|
|
59
|
+
return false
|
|
60
|
+
else
|
|
61
|
+
reply { yield(command) if block_given? }
|
|
62
|
+
end
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Called once no worker thread will run this request, such as one passed on to the next worker
|
|
67
|
+
# thread when none is running (see QueueProcessor#pass_on). A task runs on a new thread, since
|
|
68
|
+
# nothing then writes to the appender, and returns that thread. A command is not run:
|
|
69
|
+
# it replies false, so that the submitting thread does not wait for it.
|
|
70
|
+
def abandon
|
|
71
|
+
# A handed-over task runs on a thread of its own, so that one that raises, even SystemStackError,
|
|
72
|
+
# never keeps the requests released with it from being released.
|
|
73
|
+
return Thread.new(&command) if command.is_a?(Proc)
|
|
74
|
+
|
|
75
|
+
done
|
|
76
|
+
@replies.push(false)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Called by the submitting thread: wait for the reply from the worker thread.
|
|
80
|
+
#
|
|
81
|
+
# The block returns whether a worker thread that can still reply is running. Returns the
|
|
82
|
+
# reply, or false once none is, for example once the worker gave up after
|
|
83
|
+
# async_max_retries, or was replaced after a fork.
|
|
84
|
+
def wait
|
|
85
|
+
loop do
|
|
86
|
+
reply = @replies.pop(timeout: 1)
|
|
87
|
+
return reply unless reply.nil?
|
|
88
|
+
return @replies.pop(timeout: 0) || false unless yield
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module SemanticLogger
|
|
2
|
+
class QueueProcessor
|
|
3
|
+
# Internal: counts occurrences of a problem, such as a dropped log message, and has it
|
|
4
|
+
# reported at most once per interval instead of once per occurrence.
|
|
5
|
+
#
|
|
6
|
+
# It knows the total number of occurrences, how many there have been since the previous
|
|
7
|
+
# report, and when that report was made. What to report, and where, is up to the caller.
|
|
8
|
+
#
|
|
9
|
+
# Thread-safe: occurrences may be recorded from any thread, and from a signal trap, such as one
|
|
10
|
+
# that logs while the queue is full. Ruby does not allow a Mutex to be locked in a trap, so the
|
|
11
|
+
# occurrences recorded there wait on a Queue, which it does allow, until a thread started for
|
|
12
|
+
# them, or the next read or record where the Mutex can be locked, adds them in.
|
|
13
|
+
class ThrottledCounter
|
|
14
|
+
# Returns [Float] the seconds on the monotonic clock, which the intervals between reports are
|
|
15
|
+
# measured with, so that a change of the system clock, such as NTP stepping it back, neither
|
|
16
|
+
# holds back a report nor makes one early.
|
|
17
|
+
def self.now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
18
|
+
|
|
19
|
+
# Parameters:
|
|
20
|
+
# reported_at: [Float|nil]
|
|
21
|
+
# When counting starts, on the monotonic clock (see .now), so that the first report is due
|
|
22
|
+
# one interval later.
|
|
23
|
+
# nil: Report the first occurrence straight away.
|
|
24
|
+
# Default: ThrottledCounter.now
|
|
25
|
+
def initialize(reported_at: ThrottledCounter.now)
|
|
26
|
+
@reported_at = reported_at
|
|
27
|
+
@total = 0
|
|
28
|
+
@unreported = 0
|
|
29
|
+
@mutex = Mutex.new
|
|
30
|
+
@waiting = Queue.new
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Returns [Integer] the total number of occurrences recorded. Read in a signal trap, it leaves
|
|
34
|
+
# out those recorded in a trap that have not been added in yet.
|
|
35
|
+
def total
|
|
36
|
+
TrapSafe.try_synchronize(@mutex) { add(0) }
|
|
37
|
+
@total
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Record one or more occurrences.
|
|
41
|
+
#
|
|
42
|
+
# When the previous report was at least `interval` seconds ago, yields the number of
|
|
43
|
+
# occurrences since then (including these) and the seconds elapsed since that report (nil
|
|
44
|
+
# when there has been no report yet), then starts counting afresh for the next report.
|
|
45
|
+
#
|
|
46
|
+
# In a signal trap the occurrences are only counted, and are added in and reported, when a
|
|
47
|
+
# report is due, by a thread started for them, just after the trap (see #wait).
|
|
48
|
+
def record(count = 1, interval:, &report_block)
|
|
49
|
+
report = nil
|
|
50
|
+
counted = TrapSafe.try_synchronize(@mutex) do
|
|
51
|
+
add(count)
|
|
52
|
+
now = ThrottledCounter.now
|
|
53
|
+
elapsed = @reported_at && (now - @reported_at)
|
|
54
|
+
unless elapsed && elapsed < interval
|
|
55
|
+
report = [@unreported, elapsed]
|
|
56
|
+
@unreported = 0
|
|
57
|
+
@reported_at = now
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
return wait(count, interval, &report_block) unless counted
|
|
61
|
+
|
|
62
|
+
# Reported without holding the lock, so that the report can record occurrences too.
|
|
63
|
+
yield(*report) if report
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
# Leaves occurrences recorded where the lock cannot be taken, such as in a signal trap, on the
|
|
69
|
+
# waiting Queue. The first to wait starts a thread, which can take the lock, to record none:
|
|
70
|
+
# that adds in every occurrence waiting by then, and reports them when a report is due. Any
|
|
71
|
+
# left waiting, such as those that come while it is not due, are added in by the next #record
|
|
72
|
+
# or #total that can lock.
|
|
73
|
+
def wait(count, interval, &report_block)
|
|
74
|
+
@waiting << count
|
|
75
|
+
Thread.new { record(0, interval: interval, &report_block) } if @waiting.size == 1
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Adds the occurrences, and those waiting on the queue, to the counts. Holding the lock.
|
|
80
|
+
def add(count)
|
|
81
|
+
while (waiting = @waiting.pop(timeout: 0))
|
|
82
|
+
count += waiting
|
|
83
|
+
end
|
|
84
|
+
@total += count
|
|
85
|
+
@unreported += count
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|