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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -12
  3. data/Rakefile +15 -4
  4. data/docs/api.md +196 -19
  5. data/docs/appenders.md +579 -68
  6. data/docs/config.md +647 -38
  7. data/docs/index.md +31 -5
  8. data/docs/log.md +56 -17
  9. data/docs/metrics.md +144 -30
  10. data/docs/operations.md +339 -24
  11. data/docs/rails.md +396 -8
  12. data/docs/security.md +222 -27
  13. data/docs/testing.md +28 -3
  14. data/docs/upgrading.md +938 -7
  15. data/lib/semantic_logger/appender/async.rb +42 -11
  16. data/lib/semantic_logger/appender/bugsnag.rb +7 -2
  17. data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
  18. data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
  19. data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
  20. data/lib/semantic_logger/appender/file.rb +160 -37
  21. data/lib/semantic_logger/appender/graylog.rb +101 -11
  22. data/lib/semantic_logger/appender/honeybadger.rb +7 -1
  23. data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
  24. data/lib/semantic_logger/appender/http.rb +308 -46
  25. data/lib/semantic_logger/appender/io.rb +11 -6
  26. data/lib/semantic_logger/appender/kafka.rb +204 -44
  27. data/lib/semantic_logger/appender/loki.rb +55 -9
  28. data/lib/semantic_logger/appender/mongodb.rb +109 -5
  29. data/lib/semantic_logger/appender/new_relic.rb +3 -4
  30. data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
  31. data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
  32. data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
  33. data/lib/semantic_logger/appender/sentry.rb +3 -4
  34. data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
  35. data/lib/semantic_logger/appender/splunk.rb +11 -2
  36. data/lib/semantic_logger/appender/splunk_http.rb +92 -1
  37. data/lib/semantic_logger/appender/syslog.rb +149 -32
  38. data/lib/semantic_logger/appender/tcp.rb +73 -98
  39. data/lib/semantic_logger/appender/text_output.rb +65 -0
  40. data/lib/semantic_logger/appender/udp.rb +140 -18
  41. data/lib/semantic_logger/appender/wrapper.rb +10 -7
  42. data/lib/semantic_logger/appender.rb +115 -10
  43. data/lib/semantic_logger/appenders.rb +145 -5
  44. data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
  45. data/lib/semantic_logger/base.rb +115 -81
  46. data/lib/semantic_logger/call_site.rb +235 -0
  47. data/lib/semantic_logger/context.rb +222 -0
  48. data/lib/semantic_logger/core_ext/process.rb +20 -5
  49. data/lib/semantic_logger/core_ext/warning.rb +70 -0
  50. data/lib/semantic_logger/deprecation.rb +139 -0
  51. data/lib/semantic_logger/exception_details.rb +51 -0
  52. data/lib/semantic_logger/formatters/base.rb +186 -20
  53. data/lib/semantic_logger/formatters/color.rb +18 -3
  54. data/lib/semantic_logger/formatters/default.rb +53 -8
  55. data/lib/semantic_logger/formatters/ecs.rb +9 -4
  56. data/lib/semantic_logger/formatters/json.rb +9 -0
  57. data/lib/semantic_logger/formatters/logfmt.rb +155 -21
  58. data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
  59. data/lib/semantic_logger/formatters/loki.rb +430 -38
  60. data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
  61. data/lib/semantic_logger/formatters/one_line.rb +101 -3
  62. data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
  63. data/lib/semantic_logger/formatters/pattern.rb +68 -12
  64. data/lib/semantic_logger/formatters/raw.rb +40 -11
  65. data/lib/semantic_logger/formatters/signalfx.rb +55 -15
  66. data/lib/semantic_logger/formatters/syslog.rb +114 -20
  67. data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
  68. data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
  69. data/lib/semantic_logger/formatters.rb +1 -0
  70. data/lib/semantic_logger/levels.rb +10 -1
  71. data/lib/semantic_logger/log.rb +384 -82
  72. data/lib/semantic_logger/loggable.rb +56 -73
  73. data/lib/semantic_logger/logger.rb +31 -5
  74. data/lib/semantic_logger/metric/new_relic.rb +33 -18
  75. data/lib/semantic_logger/metric/signalfx.rb +29 -18
  76. data/lib/semantic_logger/metric/statsd.rb +41 -15
  77. data/lib/semantic_logger/processor.rb +194 -5
  78. data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
  79. data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
  80. data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
  81. data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
  82. data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
  83. data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
  84. data/lib/semantic_logger/queue_processor/request.rb +93 -0
  85. data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
  86. data/lib/semantic_logger/queue_processor.rb +519 -170
  87. data/lib/semantic_logger/ractor_copy.rb +120 -0
  88. data/lib/semantic_logger/ractor_processor.rb +82 -0
  89. data/lib/semantic_logger/ractor_receiver.rb +282 -0
  90. data/lib/semantic_logger/ractor_support.rb +28 -0
  91. data/lib/semantic_logger/redactor.rb +310 -0
  92. data/lib/semantic_logger/semantic_logger.rb +328 -81
  93. data/lib/semantic_logger/subscriber.rb +250 -10
  94. data/lib/semantic_logger/sync_processor.rb +237 -26
  95. data/lib/semantic_logger/test/capture_log_events.rb +26 -5
  96. data/lib/semantic_logger/test/minitest.rb +2 -1
  97. data/lib/semantic_logger/test/rspec.rb +1 -1
  98. data/lib/semantic_logger/trap_safe.rb +113 -0
  99. data/lib/semantic_logger/utils.rb +482 -67
  100. data/lib/semantic_logger/version.rb +1 -1
  101. data/lib/semantic_logger.rb +36 -3
  102. metadata +41 -5
  103. data/lib/semantic_logger/core_ext/thread.rb +0 -30
@@ -31,7 +31,7 @@ module SemanticLogger
31
31
  EVENT_ATTRIBUTES = %i[
32
32
  level name message thread_name tags named_tags context
33
33
  metric metric_amount dimensions level_index duration time
34
- exception backtrace payload
34
+ exception backtrace call_site payload
35
35
  ].freeze
36
36
 
37
37
  # Matches a single SemanticLogger::Log against a set of expectations.
@@ -0,0 +1,113 @@
1
+ module SemanticLogger
2
+ # Internal: locks a Mutex only where Ruby allows it, for code that can run in a signal trap, such as
3
+ # a trap that logs, or one that reopens the log files after a rotation.
4
+ #
5
+ # Ruby raises ThreadError for Mutex#lock in a trap. Queue and Thread methods are allowed there, so
6
+ # a caller that cannot lock leaves its work on a Queue, or to a new Thread, instead.
7
+ module TrapSafe
8
+ # Seconds that .compute_if_absent waits in a signal trap for a new thread to add the value.
9
+ ADD_WAIT_SECONDS = 0.1
10
+
11
+ # Runs the block holding the mutex, and returns true. Returns false, without running the block,
12
+ # where the mutex cannot be locked: in a signal trap, or on a thread that already holds it, such
13
+ # as one that a signal trap interrupted while it did. A ThreadError raised by the block itself is
14
+ # raised again.
15
+ def self.try_synchronize(mutex)
16
+ return false if mutex.owned?
17
+
18
+ held = false
19
+ mutex.synchronize do
20
+ held = true
21
+ yield
22
+ end
23
+ true
24
+ rescue ThreadError
25
+ raise if held
26
+
27
+ false
28
+ end
29
+
30
+ # Returns [true|false] whether the calling code runs in a signal trap, for code that writes to a
31
+ # structure that locks a Mutex of its own, such as a Concurrent::Map on CRuby, which it then must
32
+ # not write to. A new Mutex is never held, so only a trap keeps it from being locked.
33
+ def self.in_trap?
34
+ ::Mutex.new.synchronize { false }
35
+ rescue ThreadError
36
+ true
37
+ end
38
+
39
+ # Returns the value that the Concurrent::Map holds for the key, adding the one that the block
40
+ # returns when it holds none, as Concurrent::Map#compute_if_absent does.
41
+ #
42
+ # Adding to it locks a Mutex on CRuby, so in a signal trap a new thread adds the block's value,
43
+ # which the trap waits for, at most ADD_WAIT_SECONDS (see .hand_off), then returns the value that
44
+ # the map holds. The new thread cannot add it while the thread that the trap interrupted is adding
45
+ # to the map, holding its Mutex: the trap then returns the block's value, and the new thread adds
46
+ # it just after the trap, unless another thread has added one for the key by then.
47
+ def self.compute_if_absent(map, key, &)
48
+ value = map[key]
49
+ return value unless value.nil?
50
+ return map.compute_if_absent(key, &) unless in_trap?
51
+
52
+ value = yield
53
+ hand_off(ADD_WAIT_SECONDS) { map.compute_if_absent(key) { value } } || value
54
+ end
55
+
56
+ # Runs the block on a new thread, for code in a signal trap whose work locks a Mutex, and waits for
57
+ # it at most the given seconds, since that thread may need a lock that the thread the trap
58
+ # interrupted holds, which it cannot release before the trap returns.
59
+ #
60
+ # Returns the block's value, or raises what it raised, when it finished in time. Otherwise returns
61
+ # nil, and the block finishes after that, when Ruby reports what it raises, if anything, as for any
62
+ # thread (see Thread#report_on_exception). Either way an exception is raised or reported, never
63
+ # both: the caller and the thread each take the one token of a queue, without waiting, as the
64
+ # caller stops waiting and as the block finishes, and whichever takes it first decides.
65
+ def self.hand_off(seconds)
66
+ started = Thread::Queue.new
67
+ token = Thread::Queue.new
68
+ done = Thread::Queue.new
69
+ token << true
70
+ decided = false
71
+ thread = nil
72
+ begin
73
+ thread = Thread.new do
74
+ started.pop
75
+ yield
76
+ ensure
77
+ # Ruby reports what the block raised only when the caller took the token, as it stopped waiting.
78
+ Thread.current.report_on_exception = token.pop(timeout: 0).nil?
79
+ done << true
80
+ end
81
+ started << true
82
+ # Waits on a queue rather than Thread#join, which would raise what the block raised before the
83
+ # outcome is decided. The thread took the token when it has finished, or is about to, so the
84
+ # caller gets the outcome. Otherwise the caller takes it as it stops waiting, so that Ruby
85
+ # reports what the block raises.
86
+ caller_gets = done.pop(timeout: seconds) || !token.pop(timeout: 0)
87
+ decided = true
88
+ caller_gets ? thread.value : nil
89
+ ensure
90
+ report_outcome(thread, token, started) if thread && !decided
91
+ end
92
+ end
93
+
94
+ # Called when the caller of .hand_off is interrupted, such as by another signal, before it decided who
95
+ # gets the block's outcome: it lets the thread start, then takes the token if the thread has not, so
96
+ # that Ruby reports what the block raises, and otherwise, once the thread has finished, reports it
97
+ # here, as Ruby would have, to standard error, so that it is never lost. An interrupt while it waits
98
+ # for the thread is raised, not reported as the block's.
99
+ def self.report_outcome(thread, token, started)
100
+ started << true
101
+ return if token.pop(timeout: 0)
102
+
103
+ begin
104
+ thread.join
105
+ rescue Exception => e
106
+ raise if thread.alive?
107
+
108
+ $stderr.write("#{thread.inspect} terminated with exception (report_on_exception is true):\n#{e.full_message}")
109
+ end
110
+ end
111
+ private_class_method :report_outcome
112
+ end
113
+ end