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
@@ -63,7 +63,8 @@ module SemanticLogger
63
63
  #
64
64
  # level: [:trace | :debug | :info | :warn | :error | :fatal]
65
65
  # Override the log level for this appender.
66
- # Default: SemanticLogger.default_level
66
+ # Default: none (:trace), so it writes every entry that a logger emits; each logger
67
+ # filters by its own level.
67
68
  #
68
69
  # formatter: [Object|Proc]
69
70
  # An instance of a class that implements #call, or a Proc to be used to format
@@ -99,8 +100,23 @@ module SemanticLogger
99
100
  end
100
101
 
101
102
  # Re-open after process fork
103
+ #
104
+ # Builds a new notifier, which connects to the server over TCP, and then closes the connections of
105
+ # the notifier it replaces, such as after a log rotation. When connecting fails, it raises, and
106
+ # keeps the notifier it has.
107
+ #
108
+ # A child process first forgets the notifier it inherited, without closing it: over TCP, its
109
+ # connection belongs to the parent. So when connecting fails in the child, such as while the server
110
+ # cannot be reached, the appender connects again on its next write, rather than writing through the
111
+ # parent's connection. Over TLS, both processes would then carry on the same TLS session, so that
112
+ # the server could not decrypt what the parent writes next. Ruby closes the child's copy of the
113
+ # socket when it collects the notifier. GELF keeps its sockets to itself, so until then the child
114
+ # holds that copy open, and over plain TCP the server does not see the connection end when the
115
+ # parent closes it.
102
116
  def reopen
103
- uri = URI.parse(@url)
117
+ forget_inherited
118
+
119
+ uri = Utils.parse_url(@url)
104
120
  @server = uri.host
105
121
  @port = uri.port
106
122
  @protocol = uri.scheme.to_sym
@@ -109,21 +125,33 @@ module SemanticLogger
109
125
  raise(ArgumentError, "Invalid protocol value: #{@protocol}. Must be :udp or :tcp")
110
126
  end
111
127
 
112
- gelf_options[:protocol] ||= (@protocol == :tcp ? GELF::Protocol::TCP : GELF::Protocol::UDP)
113
- gelf_options[:facility] ||= application
128
+ replaced = @notifier
129
+ build_notifier
130
+ close_notifier(replaced, replaced: true)
131
+ end
114
132
 
115
- @notifier = GELF::Notifier.new(server, port, max_size, gelf_options)
116
- @notifier.collect_file_and_line = false
133
+ # Closes the notifier's TCP connections, such as when the appender is removed, and forgets it, so
134
+ # that the next write builds a new one. An error closing them is reported to the internal logger,
135
+ # rather than raised.
136
+ #
137
+ # In a forked child that has not reopened, such as one that removes the appender before its reopen
138
+ # has run, it forgets the notifier it inherited instead, without closing it: over TCP, its
139
+ # connection belongs to the parent (see #reopen).
140
+ def close
141
+ forget_inherited
142
+ close_notifier(@notifier)
143
+ ensure
144
+ @notifier = nil
117
145
  end
118
146
 
119
147
  # Returns [Hash] of parameters to send
120
148
  def call(log, logger)
121
149
  h = default_formatter.call(log, logger)
122
150
 
123
- h[:short_message] = h.delete(:message)
124
- if h[:short_message].nil?
125
- h[:short_message] = log.exception.nil? ? "<no-exception-message>" : log.exception.message
126
- end
151
+ # GELF rejects an empty short_message, such as a message of only ANSI escape codes.
152
+ h[:short_message] = h.delete(:message).to_s
153
+ h[:short_message] = short_message_of(log.exception) if h[:short_message].empty? && log.exception
154
+ h[:short_message] = "<no-exception-message>" if h[:short_message].empty?
127
155
  h[:level] = logger.level_map[log.level]
128
156
  h[:level_str] = log.level.to_s
129
157
  h[:duration_str] = h.delete(:duration)
@@ -131,16 +159,78 @@ module SemanticLogger
131
159
  end
132
160
 
133
161
  # Forward log messages
162
+ #
163
+ # GELF serializes the Hash with #to_json, which raises for Infinity, NaN, invalid UTF-8, or a
164
+ # structure nested too deeply, and writes a Struct through its #inspect, so such a Hash is
165
+ # handed over as a copy that JSON can represent (see Utils.json_ready).
166
+ #
167
+ # Builds a notifier first when the appender has none: after #close, or when building one failed as
168
+ # it was reopened after a fork.
134
169
  def log(log)
135
- notifier.notify!(formatter.call(log, self))
170
+ (notifier || build_notifier).notify!(format_log(log) { |hash| Utils.json_ready(hash) })
136
171
  true
137
172
  end
138
173
 
174
+ # GELF raises ArgumentError for a log message it cannot send, such as one too large to fit in
175
+ # the maximum number of chunks, which retrying cannot send either. Unavailable servers raise
176
+ # network errors instead.
177
+ def message_error?(exception)
178
+ exception.is_a?(ArgumentError) || super
179
+ end
180
+
181
+ # The url without any credentials. See Subscriber#inspect.
182
+ def inspect_details
183
+ "url=#{Utils.redacted_url(url).inspect}"
184
+ end
185
+
139
186
  private
140
187
 
188
+ # Returns [GELF::Notifier] a new notifier, which connects to the server over TCP, after making it
189
+ # the appender's. Records the process that built it, which owns its connection (see #reopen).
190
+ def build_notifier
191
+ @notifier = GELF::Notifier.new(server, port, max_size, notifier_options)
192
+ @notifier.collect_file_and_line = false
193
+ @pid = ::Process.pid
194
+ @notifier
195
+ end
196
+
197
+ # In a child process, forgets the notifier built in the parent before a fork. See #reopen.
198
+ def forget_inherited
199
+ @notifier = nil unless @pid == ::Process.pid
200
+ end
201
+
202
+ # Closes the TCP connections of the given notifier, if any: the one that a reopen replaced, when
203
+ # replaced: is true. Reports an error to the internal logger as a warning, rather than raising it.
204
+ #
205
+ # GELF::Notifier#close cannot: over TCP it raises NoMethodError, since GELF's TCP transport has no
206
+ # #close, and over UDP it closes the socket that GELF shares among every UDP notifier that sends on
207
+ # the calling thread, where the next one to send fails with IOError. Giving the notifier no address
208
+ # to send to closes the sockets of its TCP transport, and leaves that UDP socket open.
209
+ def close_notifier(notifier, replaced: false)
210
+ notifier&.addresses = []
211
+ rescue StandardError => e
212
+ logger.warn("Could not close the connection to the Graylog server#{' that a reopen replaced' if replaced}", e)
213
+ end
214
+
215
+ # Returns [Hash] a copy of the gelf_options (see Subscriber#copy_options), with the protocol of the url
216
+ # and the application as the facility, unless they name their own, for a new GELF::Notifier.
217
+ def notifier_options
218
+ options = copy_options(gelf_options)
219
+ options[:protocol] ||= (@protocol == :tcp ? GELF::Protocol::TCP : GELF::Protocol::UDP)
220
+ options[:facility] ||= application
221
+ options
222
+ end
223
+
141
224
  def default_formatter
142
225
  SemanticLogger::Formatters::Raw.new(time_format: :seconds, time_key: :timestamp)
143
226
  end
227
+
228
+ # Returns [String] the exception's message, without the whitespace around it, for an entry
229
+ # without a message. Read with Utils.exception_message, so that a #message that raises cannot
230
+ # lose the entry, and converted to valid UTF-8 first, since #strip raises for invalid bytes.
231
+ def short_message_of(exception)
232
+ Utils.encode_utf8_string(Utils.exception_message(exception)).strip
233
+ end
144
234
  end
145
235
  end
146
236
  end
@@ -42,8 +42,14 @@ module SemanticLogger
42
42
  end
43
43
 
44
44
  # Send an error notification to honeybadger
45
+ #
46
+ # Honeybadger replaces each byte of text that is not valid UTF-8 with "?", including binary text
47
+ # that holds UTF-8, such as a response body, so the data is sent as a copy with its text
48
+ # converted to valid UTF-8, as the JSON formatters write it, and a Hash or Array nested too
49
+ # deeply, or that holds itself, cut off there as they cut it off (see Utils.encode_utf8). Any
50
+ # other object is sent as it is, for Honeybadger to clean.
45
51
  def log(log)
46
- context = formatter.call(log, self)
52
+ context = format_log(log) { |data| Utils.encode_utf8(data) }
47
53
  if log.exception
48
54
  context.delete(:exception)
49
55
  ::Honeybadger.notify(log.exception, context)
@@ -17,7 +17,7 @@ module SemanticLogger
17
17
  # Parameters
18
18
  # level: [:trace | :debug | :info | :warn | :error | :fatal]
19
19
  # Override the log level for this appender.
20
- # Default: :error
20
+ # Default: :info
21
21
  #
22
22
  # formatter: [Object|Proc|Symbol|Hash]
23
23
  # An instance of a class that implements #call, or a Proc to be used to format
@@ -42,8 +42,14 @@ module SemanticLogger
42
42
  end
43
43
 
44
44
  # Send log to honeybadger events API
45
+ #
46
+ # Honeybadger replaces each byte of text that is not valid UTF-8 with "?", including binary text
47
+ # that holds UTF-8, such as a response body, so the data is sent as a copy with its text
48
+ # converted to valid UTF-8, as the JSON formatters write it, and a Hash or Array nested too
49
+ # deeply, or that holds itself, cut off there as they cut it off (see Utils.encode_utf8). Any
50
+ # other object is sent as it is, for Honeybadger to clean.
45
51
  def log(log)
46
- event = formatter.call(log, self).merge(event_type: "log")
52
+ event = format_log(log) { |data| Utils.encode_utf8(data) }.merge(event_type: "log")
47
53
 
48
54
  ::Honeybadger.event(event)
49
55
 
@@ -26,12 +26,89 @@ require "openssl"
26
26
  # url: 'http://localhost:8088/path',
27
27
  # batch: true
28
28
  # )
29
+ #
30
+ # Responses:
31
+ # A response other than 2xx raises ResponseError from #log and #batch. A 4xx other than 408 and
32
+ # 429 rejects the request itself, so #message_error? recognizes it, and an asynchronous appender
33
+ # discards the entries straight away, counted as failed. A batch rejected for what it holds (400,
34
+ # 413, or 422) is first split up and each part sent again, to find the entries at fault; any other
35
+ # such 4xx, such as 401 or 404, would reject any part too, so the whole batch is discarded at once
36
+ # (see #fails_any_batch?). Any other status is a problem with the destination, so an asynchronous
37
+ # appender backs off. After a 408, 429, or 503, which ask to send the request again later, it then
38
+ # sends the entries again, up to QueueProcessor::HeldWrite::MAX_RESENDS times, since the server did
39
+ # not store them (see #wrote_nothing?). After any other, such as a 500, 502, 504, or a redirect, it
40
+ # discards them, counted as failed. An appender that is not asynchronous reports the error.
29
41
  module SemanticLogger
30
42
  module Appender
31
43
  class Http < SemanticLogger::Subscriber
32
44
  attr_accessor :username, :compress, :header,
33
45
  :open_timeout, :read_timeout, :continue_timeout
34
- attr_reader :http, :url, :server, :port, :path, :ssl_options, :proxy_url
46
+ attr_reader :http, :url, :server, :port, :path, :query, :ssl_options, :proxy_url
47
+
48
+ # Maximum number of bytes of an error response body to write to the internal logger.
49
+ MAX_LOGGED_RESPONSE_BODY = 512
50
+
51
+ # Raised when the server answers a request with a status other than 2xx. Its message holds
52
+ # the status code, the redacted url, and the start of the response body, for the internal
53
+ # logger.
54
+ class ResponseError < StandardError
55
+ # The statuses that ask the client to send the request again later, after which the server
56
+ # has not stored it: 408 Request Timeout, 429 Too Many Requests, and 503 Service
57
+ # Unavailable. Not a 500, which often comes again for the same request, or a 502 Bad Gateway
58
+ # or 504 Gateway Timeout, which a proxy can answer once the server behind it has stored it.
59
+ RETRY_LATER_CODES = [408, 429, 503].freeze
60
+
61
+ # The 4xx statuses that reject the request for the entries it holds: 400 Bad Request,
62
+ # 413 Content Too Large, and 422 Unprocessable Content. A server can answer 400 for its own
63
+ # configuration too, such as Splunk's HTTP Event Collector for an index it does not know, which
64
+ # then rejects every part of a batch split up to find the entries at fault. A subclass that
65
+ # can tell such an answer from its body says so from #fails_any_batch?, as
66
+ # Appender::SplunkHttp does.
67
+ CONTENT_CODES = [400, 413, 422].freeze
68
+
69
+ # Returns [Integer] the status code of the response.
70
+ attr_reader :code
71
+
72
+ # Returns [String|nil] the body of the response, for a subclass that reads the server's own
73
+ # reason from it, such as Appender::SplunkHttp. Not in the message beyond its start, see
74
+ # MAX_LOGGED_RESPONSE_BODY.
75
+ attr_reader :body
76
+
77
+ def initialize(message, code:, body: nil)
78
+ super(message)
79
+ @code = code
80
+ @body = body
81
+ end
82
+
83
+ # Whether the server rejected the request itself, with a 4xx status other than 408 and
84
+ # 429, so that sending it again cannot succeed. A 408, 429, or 5xx, and a 3xx, which asks to
85
+ # send it elsewhere, are problems with the destination instead.
86
+ def rejected?
87
+ (400..499).cover?(code) && !retry_later?
88
+ end
89
+
90
+ # Whether the server asks to send the request again later (see RETRY_LATER_CODES).
91
+ def retry_later? = RETRY_LATER_CODES.include?(code)
92
+
93
+ # Whether the server rejected the request for the entries it holds (see CONTENT_CODES), so
94
+ # that it may accept a part of them. It rejects a request with any other 4xx (see
95
+ # #rejected?), such as 401 Unauthorized, 403 Forbidden, or 404 Not Found, whatever it holds.
96
+ def content_rejected?
97
+ CONTENT_CODES.include?(code)
98
+ end
99
+ end
100
+
101
+ # Describes a valid url in the error raised for an invalid one.
102
+ URL_FORMAT_HINT = "Should be similar to: 'http://hostname:port/path'".freeze
103
+
104
+ # The errors raised when the appender cannot connect to the server, so that the request was
105
+ # never sent: the server's name does not resolve, it cannot be reached, or it does not accept
106
+ # the connection, refusing it or within the open_timeout. Once connected, an error such as a
107
+ # read timeout or a reset connection may come after the server took the request.
108
+ NOT_SENT_ERRORS = [
109
+ SocketError, Net::OpenTimeout, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH,
110
+ Errno::EADDRNOTAVAIL
111
+ ].freeze
35
112
 
36
113
  # Create HTTP(S) log appender
37
114
  #
@@ -42,6 +119,9 @@ module SemanticLogger
42
119
  # To enable SSL include https in the URL.
43
120
  # Example: https://example.com/some_path
44
121
  # verify_mode will default: OpenSSL::SSL::VERIFY_PEER
122
+ # A query string is kept and sent with every request that submits log data,
123
+ # for the log servers that are configured through it.
124
+ # Example: http://example.com/some_path?source=my_app
45
125
  #
46
126
  # application: [String]
47
127
  # Name of this application to appear in log messages.
@@ -84,12 +164,13 @@ module SemanticLogger
84
164
  #
85
165
  # level: [:trace | :debug | :info | :warn | :error | :fatal]
86
166
  # Override the log level for this appender.
87
- # Default: SemanticLogger.default_level
167
+ # Default: none (:trace), so it writes every entry that a logger emits; each logger
168
+ # filters by its own level.
88
169
  #
89
170
  # formatter: [Object|Proc]
90
171
  # An instance of a class that implements #call, or a Proc to be used to format
91
172
  # the output from this appender
92
- # Default: Use the built-in formatter (See: #call)
173
+ # Default: SemanticLogger::Formatters::Json
93
174
  #
94
175
  # filter: [Regexp|Proc]
95
176
  # RegExp: Only include log messages where the class name matches the supplied.
@@ -119,7 +200,8 @@ module SemanticLogger
119
200
  &)
120
201
  @url = url
121
202
  @proxy_url = proxy_url
122
- @ssl_options = ssl
203
+ # A copy, to which an https url adds use_ssl: and verify_mode: (see Subscriber#copy_options).
204
+ @ssl_options = ssl && copy_options(ssl)
123
205
  @username = username
124
206
  @password = password
125
207
  @compress = compress
@@ -136,11 +218,10 @@ module SemanticLogger
136
218
  }.merge(header)
137
219
  @header["Content-Encoding"] = "gzip" if @compress
138
220
 
139
- uri = URI.parse(@url)
221
+ uri = parse_url(@url)
140
222
  @server = uri.host
141
- unless @server
142
- raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'")
143
- end
223
+ # The url is left out of the message, since it can hold a user and password.
224
+ raise(ArgumentError, "Invalid format for :url, it has no host. #{URL_FORMAT_HINT}") unless @server
144
225
 
145
226
  @port = uri.port
146
227
  @username = uri.user if !@username && uri.user
@@ -148,6 +229,11 @@ module SemanticLogger
148
229
  @path = uri.path
149
230
  # Path cannot be empty
150
231
  @path = "/" if @path == ""
232
+ # Query string of the url, applied to every request, see #uri_with_query.
233
+ # Kept separate from the path, because subclasses build their own request paths
234
+ # from it. For example Appender::ElasticsearchHttp appends the index name.
235
+ @query = uri.query
236
+ @query = nil if @query == ""
151
237
 
152
238
  if uri.scheme == "https"
153
239
  @ssl_options[:use_ssl] = true
@@ -157,7 +243,7 @@ module SemanticLogger
157
243
  @port ||= HTTP.http_default_port
158
244
  end
159
245
 
160
- @proxy_uri = URI.parse(@proxy_url) if @proxy_url && @proxy_url != :ENV
246
+ @proxy_uri = Utils.parse_url(@proxy_url, option: ":proxy_url") if @proxy_url && @proxy_url != :ENV
161
247
 
162
248
  @http = nil
163
249
 
@@ -165,15 +251,137 @@ module SemanticLogger
165
251
  reopen
166
252
  end
167
253
 
168
- # Re-open after process fork
254
+ # Closes the connection, if it is open, and starts a new one, such as after a log rotation, or a
255
+ # fork. When starting it fails, such as while the server cannot be reached, it raises, and the
256
+ # next request starts it (see #process_request).
257
+ #
258
+ # After a fork, the child first forgets the connection it inherited, without finishing it: it
259
+ # belongs to the parent. Finishing an https connection sends the server a TLS close_notify, which
260
+ # ends the connection for the parent too. Ruby closes the child's copy of the socket when it
261
+ # collects it, which leaves the parent's open. Net::HTTP keeps its socket to itself, so until then
262
+ # the child holds that copy open, and over plain http the server does not see the connection end
263
+ # when the parent closes it.
169
264
  def reopen
170
- # Close open connection if any
171
- begin
172
- @http&.finish
173
- rescue IOError
174
- nil
175
- end
265
+ finish_connection
266
+ build_http.start
267
+ end
268
+
269
+ # Closes the connection, if it is open, such as when the appender is removed, or add_appender
270
+ # fails to add it. The next request opens it again, see #process_request.
271
+ #
272
+ # In a forked child that has not reopened, such as one that removes the appender before its
273
+ # reopen has run, it forgets the connection it inherited instead, without finishing it, since it
274
+ # belongs to the parent (see #reopen), and the next request starts a new one.
275
+ def close
276
+ finish_connection
277
+ end
278
+
279
+ # Forward log messages to HTTP Server
280
+ def log(log)
281
+ message = format_log(log)
282
+ logger.trace(message)
283
+ post(message)
284
+ end
285
+
286
+ # Forward a batch of log messages to the HTTP Server in a single request.
287
+ # Only used when the appender is created with `batch: true`.
288
+ def batch(logs)
289
+ message = format_batch(logs)
290
+ logger.trace(message)
291
+ post(message)
292
+ end
293
+
294
+ # The HTTP appender supports batching, but only when explicitly requested
295
+ # via `batch: true`, so that the default behaviour (one request per log
296
+ # message) is preserved.
297
+ def batch_by_default?
298
+ false
299
+ end
300
+
301
+ # The whole batch is formatted into a single request before anything is sent, and the
302
+ # server answers for the request as a whole. So a batch that a format error or a rejected
303
+ # request fails (see #message_error?) is split up and sent again, to find the entries at
304
+ # fault.
305
+ def batch_all_or_nothing?
306
+ true
307
+ end
308
+
309
+ # The #batch above formats the whole batch with #format_batch, so it needs a formatter that can
310
+ # format a batch, such as the default one, rather than a formatter block. See
311
+ # Subscriber#batch_refusal.
312
+ #
313
+ # Answers only while that #batch is the one in use (see Subscriber#batch_defined_by?). A
314
+ # subclass that replaces it answers for its own, as Loki and Signalfx do, and is otherwise
315
+ # batched with any formatter, as before, such as one whose #batch formats each log message with
316
+ # #format_log. One whose #batch calls super, or that a prepended module wraps, still formats the
317
+ # whole batch with #format_batch, so it defines its own #batch_refusal, which returns
318
+ # #format_batch_refusal, as the Custom appenders section of docs/config.md shows.
319
+ def batch_refusal
320
+ format_batch_refusal if batch_defined_by?(Http)
321
+ end
322
+
323
+ # Whether the exception, raised by #log or #batch, was caused by the entries sent, rather
324
+ # than by the destination (see Subscriber#message_error?). That includes a request the
325
+ # server rejected (see ResponseError#rejected?), since sending it again cannot succeed.
326
+ def message_error?(exception)
327
+ (exception.is_a?(ResponseError) && exception.rejected?) || super
328
+ end
176
329
 
330
+ # Whether the exception, a message error, would fail any batch, whatever entries it holds (see
331
+ # Subscriber#fails_any_batch?): the server rejected the request (see ResponseError#rejected?)
332
+ # other than for the entries it holds (see ResponseError#content_rejected?), such as for its
333
+ # credentials or its url, and would reject every part of the batch too. So the batch is
334
+ # discarded at once, rather than split up. A batch that could not be formatted, or that the
335
+ # server rejected for its entries, is split up to find the entries at fault.
336
+ def fails_any_batch?(exception)
337
+ exception.is_a?(ResponseError) && exception.rejected? && !exception.content_rejected?
338
+ end
339
+
340
+ # Whether the request that raised the exception from #log or #batch stored none of its
341
+ # entries, and is worth sending again later (see Subscriber#wrote_nothing?): the server asked
342
+ # to send it again later (see ResponseError#retry_later?), or it was never sent (see
343
+ # #not_sent?). So an asynchronous appender sends the entries again once it has backed off.
344
+ #
345
+ # Decided from the class of the exception alone. Its cause, which Ruby sets to the error being
346
+ # handled when it was raised, such as a format error while a batch is split, says nothing about
347
+ # this request; #process_request raises the error of the request, not one from reconnecting.
348
+ #
349
+ # A subclass whose #log or #batch sends more than one request must override this to return
350
+ # false: a failure of a later request would otherwise have the entries of the earlier ones,
351
+ # which the server stored, sent again.
352
+ def wrote_nothing?(exception)
353
+ return exception.retry_later? if exception.is_a?(ResponseError)
354
+
355
+ not_sent?(exception)
356
+ end
357
+
358
+ # Whether the request that raised the exception from #log or #batch was never sent, because
359
+ # the appender could not connect (see NOT_SENT_ERRORS), so that the server has not answered it
360
+ # (see Subscriber#not_sent?). An asynchronous appender then sends the entries again without
361
+ # counting it towards QueueProcessor::HeldWrite::MAX_RESENDS, so that an outage loses none of
362
+ # them until the worker thread gives up after async_max_retries. Decided from the class of the
363
+ # exception alone, as for #wrote_nothing?.
364
+ def not_sent?(exception)
365
+ NOT_SENT_ERRORS.any? { |error_class| exception.is_a?(error_class) }
366
+ end
367
+
368
+ # Returns [String] the scheme, host, and any non-default port of the url. Safe to log.
369
+ # See Utils.redacted_url.
370
+ def redacted_url
371
+ @redacted_url ||= Utils.redacted_url(url)
372
+ end
373
+
374
+ # Only the redacted url, since the instance variables hold the password, the authorization
375
+ # headers, and any credential a subclass adds. See Subscriber#inspect.
376
+ def inspect_details
377
+ "url=#{redacted_url.inspect}"
378
+ end
379
+
380
+ private
381
+
382
+ # Returns [Net::HTTP] a new connection, not yet started, after making it the appender's. Records
383
+ # the process that built it, which owns its connection (see #reopen).
384
+ def build_http
177
385
  @http = if @proxy_uri
178
386
  Net::HTTP.new(server, port, @proxy_uri.host, @proxy_uri.port, @proxy_uri.user, @proxy_uri.password)
179
387
  else
@@ -191,33 +399,32 @@ module SemanticLogger
191
399
  @http.open_timeout = @open_timeout
192
400
  @http.read_timeout = @read_timeout
193
401
  @http.continue_timeout = @continue_timeout
194
- @http.start
402
+ @pid = ::Process.pid
403
+ @http
195
404
  end
196
405
 
197
- # Forward log messages to HTTP Server
198
- def log(log)
199
- message = formatter.call(log, self)
200
- logger.trace(message)
201
- post(message)
406
+ # Finishes the connection, if it is open. In a child process, first forgets the connection
407
+ # started in the parent before a fork, without finishing it. See #reopen.
408
+ #
409
+ # Used by #reopen as well as #close, rather than #reopen calling #close, so that a subclass's
410
+ # #close is not run by a reopen.
411
+ def finish_connection
412
+ forget_inherited
413
+ @http&.finish
414
+ rescue IOError
415
+ nil
202
416
  end
203
417
 
204
- # Forward a batch of log messages to the HTTP Server in a single request.
205
- # Only used when the appender is created with `batch: true`.
206
- def batch(logs)
207
- message = formatter.batch(logs, self)
208
- logger.trace(message)
209
- post(message)
418
+ # In a child process, forgets the connection started in the parent before a fork. See #reopen.
419
+ def forget_inherited
420
+ @http = nil unless @pid == ::Process.pid
210
421
  end
211
422
 
212
- # The HTTP appender supports batching, but only when explicitly requested
213
- # via `batch: true`, so that the default behaviour (one request per log
214
- # message) is preserved.
215
- def batch_by_default?
216
- false
423
+ # Returns [URI] the parsed url, without the url in the error raised for an invalid one.
424
+ def parse_url(url)
425
+ Utils.parse_url(url, hint: URL_FORMAT_HINT)
217
426
  end
218
427
 
219
- private
220
-
221
428
  # Use JSON Formatter by default
222
429
  def default_formatter
223
430
  SemanticLogger::Formatters::Json.new
@@ -233,40 +440,95 @@ module SemanticLogger
233
440
 
234
441
  # HTTP Post
235
442
  def post(body, request_uri = path)
236
- request = Net::HTTP::Post.new(request_uri, @header)
443
+ request = Net::HTTP::Post.new(uri_with_query(request_uri), @header)
237
444
  process_request(request, body)
238
445
  end
239
446
 
240
447
  # HTTP Put
241
448
  def put(body, request_uri = path)
242
- request = Net::HTTP::Put.new(request_uri, @header)
449
+ request = Net::HTTP::Put.new(uri_with_query(request_uri), @header)
243
450
  process_request(request, body)
244
451
  end
245
452
 
246
453
  # HTTP Delete
454
+ #
455
+ # The query string of the configured url is _not_ applied here: it configures how
456
+ # the server ingests log data, and servers reject it on a maintenance request.
457
+ # Appender::ElasticsearchHttp#delete_all deletes an index, and Elasticsearch fails
458
+ # the request with a 400 when it carries unrecognized parameters.
459
+ #
460
+ # A maintenance request is not log data written by the worker thread of an asynchronous
461
+ # appender, so a response other than 2xx is reported to the internal logger, and false
462
+ # returned, rather than raised.
247
463
  def delete(request_uri = path)
248
464
  request = Net::HTTP::Delete.new(request_uri, @header)
249
465
  process_request(request)
466
+ rescue ResponseError => e
467
+ logger.error(e.message)
468
+ false
469
+ end
470
+
471
+ # Applies the query string of the configured url, if any, to the uri of a request
472
+ # that submits log data.
473
+ #
474
+ # Kept out of `path` on purpose: subclasses build their own request paths from it
475
+ # (Appender::ElasticsearchHttp appends the index name and `_doc` to it), and the
476
+ # query has to stay at the end of the request uri regardless.
477
+ #
478
+ # @param request_uri [String] path of the request, `path` by default.
479
+ # @return [String]
480
+ def uri_with_query(request_uri)
481
+ return request_uri unless @query
482
+
483
+ separator = request_uri.include?("?") ? "&" : "?"
484
+ "#{request_uri}#{separator}#{@query}"
250
485
  end
251
486
 
252
487
  # Process HTTP Request
488
+ #
489
+ # Returns true when the server answers with a 2xx status. Raises ResponseError for any other
490
+ # status, so that the entries are counted as failed rather than written. Its message names
491
+ # only the redacted url, since the url can hold credentials, and the start of the body.
492
+ #
493
+ # After a RuntimeError, such as a read timeout, it connects again for the next request, and
494
+ # raises the error of this one even when connecting again fails, since only that error says
495
+ # whether the server may have stored the request (see #wrote_nothing?). Not after a request it
496
+ # could not send (see #not_sent?), such as a Net::OpenTimeout: connecting again straight away
497
+ # would fail the same way, so that a server that cannot be reached would hold up each request
498
+ # for twice its open_timeout.
253
499
  def process_request(request, body = nil)
254
500
  if body
255
501
  request.body = compress ? compress_data(body) : body
256
502
  end
257
503
  request.basic_auth(@username, @password) if @username
258
- response = @http.request(request)
259
- if response.is_a?(Net::HTTPSuccess)
260
- true
261
- else
262
- # Failures are logged to the global semantic logger failsafe logger (Usually stderr or file)
263
- logger.error("Bad HTTP response from: #{url} code: #{response.code}, #{response.body}")
264
- false
265
- end
504
+ # Net::HTTP sends a request on a connection that is not started, such as one that #close
505
+ # closed, on a new connection that it closes after the response. Starting it keeps the
506
+ # connection for the requests that follow, such as once a removed appender is added again.
507
+ # A new one is built first when the appender has none, since #close forgot the one it
508
+ # inherited in a forked child.
509
+ http = @http || build_http
510
+ http.start unless http.started?
511
+ response = http.request(request)
512
+ return true if response.is_a?(Net::HTTPSuccess)
513
+
514
+ raise ResponseError.new(
515
+ "Bad HTTP response from: #{redacted_url} code: #{response.code}, " \
516
+ "#{response.body.to_s[0, MAX_LOGGED_RESPONSE_BODY].inspect}",
517
+ code: response.code.to_i,
518
+ body: response.body
519
+ )
266
520
  rescue RuntimeError => e
267
- reopen
521
+ reconnect unless not_sent?(e)
268
522
  raise e
269
523
  end
524
+
525
+ # Connects again after a request failed. A failure to connect is reported to the internal
526
+ # logger, and left to the next request, which connects again when the connection is not open.
527
+ def reconnect
528
+ reopen
529
+ rescue StandardError => e
530
+ logger.warn("Could not connect again to #{redacted_url}", e)
531
+ end
270
532
  end
271
533
  end
272
534
  end