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
@@ -13,15 +13,71 @@ module SemanticLogger
13
13
  #
14
14
  # All timestamps are ISO8601 formatteed
15
15
  # All user supplied values are escaped and surrounded by double quotes to avoid ambiguious message delimeters
16
- # `tags` are treated as keys with boolean values. Tag names are not formatted or validated;
17
- # ensure you use valid logfmt format for tag names.
18
- # `named_tags` are flattened are merged into the top level message field. Any conflicting fields are overridden.
19
- # `payload` values take precedence over `tags` and `named_tags`. Any conflicting fields are overridden.
16
+ # `tags` are treated as keys with boolean values.
17
+ # In every key (tags, named tags, payload), spaces, control and format characters, `=`, and `"`
18
+ # are replaced with `_`, so that untrusted data cannot inject fields or lines, or disguise a key.
19
+ # `tags`, `named_tags`, and `payload` are flattened into the top level fields. A key named like
20
+ # one of the entry's own fields (the time key, `level`, `name`, `message`, `duration`,
21
+ # `duration_ms`, `tag`) or an exception field is prefixed with its source, as in `payload.level`,
22
+ # so that logged data cannot replace those fields. So is a key that starts with `tags.`,
23
+ # `named_tags.`, or `payload.`, so that it cannot forge another source's field. Other keys that
24
+ # are the same once encoded, such as `:user` and `"user"`, or `"a b"` and `"a=b"`, are written
25
+ # once, with the value from the later source: `payload` values take precedence over
26
+ # `named_tags`, and `named_tags` over `tags`.
27
+ # An exception is emitted as `exception_name`, `exception_message`, `stack_trace`, and `cause`,
28
+ # and with `log_exception_details` also `exception_details`.
20
29
  #
21
30
  # Futher Reading https://brandur.org/logfmt
22
31
  class Logfmt < Raw
32
+ # The entry's own fields in the Raw hash, written in this order after the time key.
33
+ ENTRY_FIELDS = %i[level name message duration duration_ms].freeze
34
+
35
+ # The field that tells an entry with an exception from one without.
36
+ TAG_FIELD = "tag".freeze
37
+
38
+ # The exception's fields in the Raw hash, mapped to their logfmt names, so that the exception's
39
+ # name and message do not replace the entry's own.
40
+ EXCEPTION_FIELDS = Utils.make_shareable(
41
+ {
42
+ name: "exception_name",
43
+ message: "exception_message",
44
+ details: "exception_details",
45
+ stack_trace: "stack_trace",
46
+ cause: "cause"
47
+ }.freeze
48
+ )
49
+
50
+ # Prefixes that mark a key from logged data with its source, when the key alone would name
51
+ # one of the entry's own fields, or look like a key marked this way.
52
+ TAGS_PREFIX = "tags.".freeze
53
+ NAMED_TAGS_PREFIX = "named_tags.".freeze
54
+ PAYLOAD_PREFIX = "payload.".freeze
55
+ SOURCE_PREFIXES = [TAGS_PREFIX, NAMED_TAGS_PREFIX, PAYLOAD_PREFIX].freeze
56
+
57
+ # Characters that cannot appear in a logfmt key: spaces and control characters (including
58
+ # NEL), which would end the key or the line, the `=` and `"` delimiters, LINE SEPARATOR and
59
+ # PARAGRAPH SEPARATOR, which some readers treat as newlines, and the invisible format
60
+ # characters, such as ZERO WIDTH SPACE and RIGHT-TO-LEFT OVERRIDE, which would let a key pass
61
+ # for another one, such as "level" followed by a ZERO WIDTH SPACE for "level".
62
+ UNSAFE_KEY_CHARS = /[\p{Cc}\p{Cf} ="\u2028\u2029]/
63
+
64
+ # The ASCII subset of UNSAFE_KEY_CHARS, which is much faster to match against the ASCII keys
65
+ # that make up nearly every log event.
66
+ UNSAFE_ASCII_KEY_CHARS = /[\x00-\x20="\x7f]/
67
+
68
+ # The most Symbol keys to remember the names of, so that untrusted keys cannot grow the cache
69
+ # without bound.
70
+ PLAIN_SYMBOL_CACHE_SIZE = 512
71
+
23
72
  def initialize(time_format: :iso_8601, time_key: :timestamp, **args)
24
73
  super
74
+ name_entry_fields
75
+ end
76
+
77
+ # Also writes and reserves the new time key in place of the old one.
78
+ def time_key=(time_key)
79
+ super
80
+ name_entry_fields
25
81
  end
26
82
 
27
83
  def call(log, logger)
@@ -32,8 +88,12 @@ module SemanticLogger
32
88
 
33
89
  private
34
90
 
91
+ # @parsed maps the logfmt name of each field to its value. Logged data is added after the
92
+ # entry's own fields, and a field with the same name as an earlier one replaces its value, so
93
+ # the payload takes precedence over named tags, and named tags over tags.
35
94
  def raw_to_logfmt
36
- @parsed = @raw.slice(time_key, :level, :name, :message, :duration, :duration_ms).merge(tag: "success")
95
+ @parsed = @raw.slice(*@entry_keys).transform_keys(@entry_names)
96
+ @parsed[TAG_FIELD] = "success"
37
97
  handle_tags
38
98
  handle_payload
39
99
  handle_exception
@@ -42,37 +102,111 @@ module SemanticLogger
42
102
  end
43
103
 
44
104
  def handle_tags
45
- tags = @raw.fetch(:tags) { [] }.
46
- each_with_object({}) { |tag, accum| accum[tag] = true }
47
-
48
- @parsed = @parsed.merge(tags).
49
- merge(@raw.fetch(:named_tags) { {} })
105
+ @raw[:tags]&.each { |tag| add_field(TAGS_PREFIX, tag, true) }
106
+ @raw[:named_tags]&.each_pair { |key, value| add_field(NAMED_TAGS_PREFIX, key, value) }
50
107
  end
51
108
 
52
109
  def handle_payload
53
110
  return unless @raw.key? :payload
54
111
 
55
- @parsed = @parsed.merge(@raw[:payload])
112
+ payload = @raw[:payload]
113
+ # A payload is normally a Hash, but a log call can supply any value, so emit anything else
114
+ # as a single field rather than failing to format the whole entry.
115
+ if payload.is_a?(Hash)
116
+ payload.each_pair { |key, value| add_field(PAYLOAD_PREFIX, key, value) }
117
+ else
118
+ add_field(PAYLOAD_PREFIX, "payload", payload)
119
+ end
56
120
  end
57
121
 
58
122
  def handle_exception
59
123
  return unless @raw.key? :exception
60
124
 
61
- @parsed[:tag] = "exception"
62
- @parsed = @parsed.merge(@raw[:exception])
125
+ @parsed[TAG_FIELD] = "exception"
126
+ @raw[:exception].each_pair { |key, value| @parsed[EXCEPTION_FIELDS[key] || logfmt_key(key)] = value }
127
+ end
128
+
129
+ # Adds a field from logged data, under the name returned by #field_name.
130
+ def add_field(prefix, key, value)
131
+ name = (key.is_a?(Symbol) && @plain_symbols[key]) || field_name(prefix, key)
132
+ @parsed[name] = value
133
+ end
134
+
135
+ # Returns [String] the logfmt name of a key from logged data: the key encoded by #logfmt_key,
136
+ # prefixed with its source, as in "payload.level", when it names one of the entry's own fields
137
+ # or starts with a source's prefix, so that untrusted data cannot replace or forge that field,
138
+ # or a field from another source.
139
+ def field_name(prefix, key)
140
+ name = key.is_a?(Symbol) ? key.name : key.to_s
141
+ name = logfmt_key(name) unless plain_name?(name)
142
+ return prefix + name if @reserved_names.key?(name) || name.start_with?(*SOURCE_PREFIXES)
143
+
144
+ remember_symbol(key, name) if key.is_a?(Symbol)
145
+ name
146
+ end
147
+
148
+ # Remembers the name of a Symbol key that is not reserved, while there is room. The cache is
149
+ # replaced rather than updated, since a formatter can be shared by several threads.
150
+ def remember_symbol(symbol, name)
151
+ plain_symbols = @plain_symbols
152
+ @plain_symbols = plain_symbols.merge(symbol => name).freeze if plain_symbols.size < PLAIN_SYMBOL_CACHE_SIZE
63
153
  end
64
154
 
65
155
  def flatten_log
66
- flattened = @parsed.map do |key, value|
67
- case value
68
- when Hash, Array
69
- "#{key}=#{Utils.to_json(value.to_s)}"
70
- else
71
- "#{key}=#{Utils.to_json(value)}"
72
- end
156
+ line = +""
157
+ @parsed.each_pair do |name, value|
158
+ line << " " unless line.empty?
159
+ line << name << "=" << format_value(value)
160
+ end
161
+ line
162
+ end
163
+
164
+ # Integers, true, false, and nil have a single JSON form, so they skip the cost of `to_json`.
165
+ # Strings, the most common values, are checked first.
166
+ def format_value(value)
167
+ return Utils.to_json(value) if value.is_a?(String)
168
+
169
+ case value
170
+ when Integer, true, false
171
+ value.to_s
172
+ when nil
173
+ "null"
174
+ else
175
+ # The JSON form rather than #inspect, which can print an object's instance variables
176
+ # and has no depth limit. A structure is quoted, since its JSON holds spaces and quotes.
177
+ json = Utils.to_json(value)
178
+ json.start_with?("{", "[") ? Utils.to_json(json) : json
73
179
  end
180
+ end
181
+
182
+ # Names the entry's own fields from the time key, and reserves those names and the
183
+ # exception's, so that logged data cannot use them.
184
+ def name_entry_fields
185
+ @entry_names = [time_key, *ENTRY_FIELDS].to_h { |key| [key, logfmt_key(key)] }.freeze
186
+ @entry_keys = @entry_names.keys.freeze
187
+ @reserved_names = [*@entry_names.values, TAG_FIELD, *EXCEPTION_FIELDS.values].
188
+ to_h { |name| [name, true] }.freeze
189
+ # Symbol keys from logged data that need no prefix, mapped to their logfmt names.
190
+ @plain_symbols = {}.freeze
191
+ end
74
192
 
75
- flattened.join(" ")
193
+ # Whether the name is already a valid logfmt key, made of ASCII characters that are all safe.
194
+ def plain_name?(name)
195
+ !name.empty? && name.ascii_only? && !name.match?(UNSAFE_ASCII_KEY_CHARS)
196
+ end
197
+
198
+ # Returns [String] the key as valid UTF-8, with every character that would break the logfmt
199
+ # structure replaced by an underscore. Keys come from tags, named tags, and payloads, which
200
+ # can hold untrusted data; unescaped, they could inject fields or forge an entire line.
201
+ def logfmt_key(key)
202
+ key = key.is_a?(Symbol) ? key.name : key.to_s
203
+ unsafe = UNSAFE_ASCII_KEY_CHARS
204
+ unless key.ascii_only?
205
+ key = Utils.encode_utf8_string(key)
206
+ unsafe = UNSAFE_KEY_CHARS
207
+ end
208
+ key = key.gsub(unsafe, "_") if key.match?(unsafe)
209
+ key.empty? ? "_" : key
76
210
  end
77
211
  end
78
212
  end
@@ -0,0 +1,282 @@
1
+ module SemanticLogger
2
+ module Formatters
3
+ class Loki < Base
4
+ # The labels of the stream of one log entry, as Loki accepts them: Loki rejects the whole
5
+ # push, with every entry in it, for a label name it does not accept, or past one of its
6
+ # limits, and keeps only one value of a label name.
7
+ #
8
+ # It knows the labels written so far, a Hash of their names, as Strings, and their values, to
9
+ # which the formatter has written its own labels first (see Loki#build_stream), whether a
10
+ # name may still be a Symbol, the names that the formatter writes to the structured metadata
11
+ # instead of as labels, the labels still to be written under another name, Loki's limits,
12
+ # and how many labels there were when it finished them. It writes each name as a String (see
13
+ # #string_names), adds the named tags and the context to the labels (see #add), and then
14
+ # writes those that need another name and takes out those past Loki's limits (see #finish),
15
+ # which the formatter writes to the structured metadata instead. It names them by the
16
+ # formatter's rules (see Loki.loki_name and Loki.free_name), and converts their values with
17
+ # the converter the formatter gives it.
18
+ class Labels
19
+ # Matches a label name that Loki accepts: an ASCII letter or an underscore, followed by
20
+ # ASCII letters, digits, and underscores. Loki rejects the whole push for any other, such
21
+ # as `request-id`, `1st`, or an empty name, with "couldn't parse labels".
22
+ LABEL_NAME = /\A[A-Za-z_][A-Za-z0-9_]*\z/
23
+
24
+ # The name of a label whose key is empty, with the prefix that Loki puts before a name that
25
+ # starts with a digit.
26
+ EMPTY_LABEL_NAME = "key_".freeze
27
+
28
+ # The label that Loki adds to a stream that does not have it, and does not count toward
29
+ # max_label_names_per_series, even when the stream has it.
30
+ SERVICE_NAME = "service_name".freeze
31
+
32
+ # Parameters
33
+ # labels: [Hash]
34
+ # The labels written so far, which this adds to, and takes the labels past Loki's
35
+ # limits from.
36
+ # loki_value: [#call]
37
+ # Returns a value as Loki stores it, a String: the formatter's #loki_value, which a
38
+ # subclass of the formatter may override.
39
+ # max_names, max_name_length, max_value_length: [Integer]
40
+ # Loki's limits, see Loki#initialize.
41
+ # reserved: [Array<String>]
42
+ # The names that the formatter writes to the structured metadata instead of as labels,
43
+ # such as `thread` (see Loki#initialize). A label of that name is written under another
44
+ # name, since Loki would show the structured metadata as `thread_extracted` beside it.
45
+ # symbols: [true|false]
46
+ # Whether a label may have a name that is not a String, such as a Symbol that a
47
+ # subclass of the formatter written for v5.2 writes its own label under, as v5.2 did.
48
+ # The names are then written as Strings (see #string_names) before the first label is
49
+ # added, and as the labels are finished.
50
+ # Default: false, as the formatter's own methods write every label under a String.
51
+ def initialize(labels, loki_value, max_names, max_name_length, max_value_length, reserved:,
52
+ symbols: false)
53
+ @labels = labels
54
+ @loki_value = loki_value
55
+ @max_names = max_names
56
+ @max_name_length = max_name_length
57
+ @max_value_length = max_value_length
58
+ @reserved = reserved
59
+ @symbols = symbols
60
+ @unchecked = symbols
61
+ @renamed = nil
62
+ @finished_size = nil
63
+ end
64
+
65
+ # Whether these are the labels of the supplied Hash: the same Hash, not an equal one.
66
+ def of?(labels)
67
+ @labels.equal?(labels)
68
+ end
69
+
70
+ # Whether #finish has written and limited these labels, and no label was added since, as a
71
+ # subclass of the formatter may add one after its #build_stream called super, even under
72
+ # the name of another label, such as `:level` beside `"level"`. A label whose value was
73
+ # replaced since is not seen.
74
+ def finished?
75
+ @finished_size == @labels.size
76
+ end
77
+
78
+ # Adds the label of a named tag or a value of the context, with its value as Loki stores it
79
+ # (see #initialize), unless the value is nil, which Loki would store as the text "null".
80
+ #
81
+ # Loki rejects the whole push for a label name it does not accept (see LABEL_NAME), so such
82
+ # a name is replaced (see #label_name_for). Loki keeps one value of a label name, so a label
83
+ # whose name another label already has is written once when that label has the same value,
84
+ # as Loki stores it, and otherwise under another name, as is a label of a reserved name.
85
+ # These are written after the others, by #finish, so that the name they get is not one that
86
+ # a later label has as its own.
87
+ def add(key, value)
88
+ return if value.nil?
89
+
90
+ string_names if @unchecked
91
+ name = key.is_a?(Symbol) ? key.name : key.to_s
92
+ value = @loki_value.call(value)
93
+ if !label_name?(name)
94
+ (@renamed ||= []) << [label_name_for(name), value]
95
+ elsif @labels.key?(name) || @reserved.include?(name)
96
+ (@renamed ||= []) << [name, value]
97
+ else
98
+ @labels[name] = value
99
+ end
100
+ end
101
+
102
+ # Writes the labels whose names are taken, reserved, or were replaced (see #add), after the
103
+ # others, so that a label written under its own name keeps it (see #write_renamed), and
104
+ # then takes out the labels past Loki's limits (see #limit). A label written under a
105
+ # Symbol since, as by a subclass of the formatter, is first written under a String (see
106
+ # #string_names).
107
+ #
108
+ # It can finish them again, after more labels were added (see #finished?).
109
+ #
110
+ # Returns [Array<Array(String, String)>|nil] the labels taken out, each its name and its
111
+ # value as Loki stores it, or nil when there are none.
112
+ def finish
113
+ string_names if @symbols
114
+ write_renamed if @renamed
115
+ moved = limit
116
+ @finished_size = @labels.size
117
+ moved
118
+ end
119
+
120
+ private
121
+
122
+ # Writes each label whose name is not a String under its name as a String, in place and in
123
+ # the same order, such as the `:host` of a subclass's own #host, or the `{pid: pid}` of
124
+ # v5.2's #build_stream. Otherwise #add would not find its name among the labels, and would
125
+ # write a named tag of that name beside it, so the JSON would have the name twice, which
126
+ # json 3 refuses to write. When a label written before it has that name, it is written
127
+ # after the others, as a named tag whose name is taken is (see #write_renamed), with a
128
+ # number unless it has the same value. So #add, #write_renamed, #limit, and #fit? find
129
+ # every label by its name as a String.
130
+ #
131
+ # It runs before the first named tag or context key is added, so that the labels are
132
+ # numbered in the order they are written in, and again as they are finished, for a label
133
+ # written since. Nearly every stream has String names alone, which Hash#any? checks without
134
+ # allocating.
135
+ def string_names
136
+ @unchecked = false
137
+ return unless @labels.any? { |name, _value| !name.is_a?(String) }
138
+
139
+ written = @labels.to_a
140
+ @labels.clear
141
+ written.each do |name, value|
142
+ name = name.is_a?(Symbol) ? name.name : name.to_s
143
+ if @labels.key?(name)
144
+ (@renamed ||= []) << [name, @loki_value.call(value)]
145
+ else
146
+ @labels[name] = value
147
+ end
148
+ end
149
+ end
150
+
151
+ # Writes the labels whose names are taken, reserved, or were replaced (see #add) in two
152
+ # passes: first each whose name is free, and then each of the rest, under the name that
153
+ # Loki.free_name gives it, which is never a reserved name, or not at all when the label of
154
+ # that name, or of a numbered name before it, has the same value (see #same_value?). So the
155
+ # number a label gets is never the name that another of them is written under, such as
156
+ # `host_2` for a named tag `host-2`, whatever the order of their keys. Only Loki.free_name
157
+ # compares the values, so that the value of each label a label is compared with is
158
+ # converted once.
159
+ def write_renamed
160
+ pending = @renamed.reject do |name, value|
161
+ next false if @labels.key?(name) || @reserved.include?(name)
162
+
163
+ @labels[name] = value
164
+ true
165
+ end
166
+ @renamed = nil
167
+ pending.each do |name, value|
168
+ name = Loki.free_name(@labels, name, reserved: @reserved) { |held| same_value?(held, value) }
169
+ @labels[name] = value if name
170
+ end
171
+ end
172
+
173
+ # Whether a label's value, as Loki stores it, is the supplied value. A value that is not a
174
+ # String, such as the `level` Symbol or the `pid` Integer of the formatter's own labels, is
175
+ # compared as Loki stores it.
176
+ def same_value?(held, value)
177
+ @loki_value.call(held) == value
178
+ end
179
+
180
+ # Whether Loki accepts the name as a label name. A name with characters outside ASCII never
181
+ # is, and matching it could raise, when it holds invalid UTF-8.
182
+ def label_name?(name)
183
+ name.ascii_only? && name.match?(LABEL_NAME)
184
+ end
185
+
186
+ # Returns [String] a name that Loki accepts for a label whose name it does not: the name
187
+ # under which Loki stores structured metadata of that name (see Loki.loki_name), such as
188
+ # `request_id` for `request-id`, and `key_1st` for `1st`, or EMPTY_LABEL_NAME for an empty
189
+ # name.
190
+ def label_name_for(name)
191
+ name = Loki.loki_name(name)
192
+ name.empty? ? EMPTY_LABEL_NAME : name
193
+ end
194
+
195
+ # Takes out each label for which Loki would reject the whole push, with every entry in it,
196
+ # so that the push is accepted, and the value is kept in the structured metadata: a label
197
+ # whose name is longer than max_name_length, or whose value, as Loki stores it, is longer
198
+ # than max_value_length, and each label past max_names, in the order they are written (see
199
+ # Loki#build_stream).
200
+ #
201
+ # As Loki 3.7.10 does, a label with an empty value, which Loki leaves out, is neither
202
+ # checked nor counted, and SERVICE_NAME is not counted.
203
+ #
204
+ # Returns [Array<Array(String, String)>|nil] the labels taken out, each its name as a String
205
+ # and its value as Loki stores it, or nil when there are none.
206
+ def limit
207
+ return if @labels.size <= @max_names && fit?
208
+
209
+ moved = nil
210
+ count = 0
211
+ @labels.delete_if do |name, value|
212
+ next false if empty_value?(value) ||
213
+ (label_fits?(name, value) && (name == SERVICE_NAME || (count += 1) <= @max_names))
214
+
215
+ (moved ||= []) << [name, @loki_value.call(value)]
216
+ end
217
+ moved
218
+ end
219
+
220
+ # Whether Loki accepts the length of each label's name and value (see #label_fits?). A
221
+ # label with an empty value fits, whatever the length of its name, since Loki leaves it out.
222
+ #
223
+ # Every entry is checked, so an ASCII String, as most values are, is checked here, without
224
+ # another method call.
225
+ def fit?
226
+ max_name = @max_name_length
227
+ max_value = @max_value_length
228
+ @labels.each_pair do |name, value|
229
+ next if value.is_a?(String) && value.ascii_only? && value.bytesize <= max_value &&
230
+ (name.bytesize <= max_name || value.empty?)
231
+ return false unless label_fits?(name, value) || empty_value?(value)
232
+ end
233
+ true
234
+ end
235
+
236
+ # Whether the value is empty, which Loki leaves out. A value that is neither a String nor a
237
+ # Symbol, such as the `pid` Integer or the tags, is never written as an empty value.
238
+ def empty_value?(value)
239
+ (value.is_a?(String) || value.is_a?(Symbol)) && value.empty?
240
+ end
241
+
242
+ # Whether Loki accepts the length of the label's name, a String, and of its value, as Loki
243
+ # stores it, in bytes, in UTF-8 without invalid bytes, as Utils.to_json writes them.
244
+ #
245
+ # The `level` Symbol and the `pid` Integer of every entry are read as text without
246
+ # converting them, which first checks whether a value is a structure, and the tags of
247
+ # nearly every entry that has them are not written as JSON only to measure them (see
248
+ # #short_strings?).
249
+ def label_fits?(name, value)
250
+ return false if name.bytesize > @max_name_length
251
+ return true if short_strings?(value)
252
+
253
+ text =
254
+ case value
255
+ when String then value
256
+ when Symbol then value.name
257
+ when Integer then value.to_s
258
+ else @loki_value.call(value)
259
+ end
260
+ text = Utils.encode_utf8_string(text) unless text.ascii_only?
261
+ text.bytesize <= @max_value_length
262
+ end
263
+
264
+ # Whether the value is an Array of Strings, such as the tags, whose JSON cannot be longer
265
+ # than max_value_length: JSON writes each byte of a String as at most 6 bytes, as for a
266
+ # control character, such as `\u0001`, with 2 quotes around it and a comma after it, all
267
+ # within brackets.
268
+ def short_strings?(value)
269
+ return false unless value.is_a?(Array)
270
+
271
+ size = 2
272
+ value.each do |item|
273
+ return false unless item.is_a?(String)
274
+
275
+ size += (item.bytesize * 6) + 3
276
+ end
277
+ size <= @max_value_length
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end