libo11y 0.2.0 → 0.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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +170 -21
  3. data/README.md +77 -8
  4. data/lib/libo11y/{proc.rb → child_process.rb} +12 -2
  5. data/lib/libo11y/db.rb +22 -10
  6. data/lib/libo11y/detached.rb +241 -29
  7. data/lib/libo11y/http_client.rb +48 -15
  8. data/lib/libo11y/internal/bridges.rb +3553 -255
  9. data/lib/libo11y/internal/endpoint.rb +7 -1
  10. data/lib/libo11y/internal/env.rb +177 -59
  11. data/lib/libo11y/internal/error_type.rb +17 -0
  12. data/lib/libo11y/internal/histogram_buckets.rb +42 -0
  13. data/lib/libo11y/internal/http_common.rb +2 -0
  14. data/lib/libo11y/internal/identity.rb +10 -1
  15. data/lib/libo11y/internal/injected.rb +178 -0
  16. data/lib/libo11y/internal/logging_device.rb +141 -32
  17. data/lib/libo11y/internal/path_match.rb +148 -0
  18. data/lib/libo11y/internal/pii.rb +185 -65
  19. data/lib/libo11y/internal/pipeline.rb +141 -34
  20. data/lib/libo11y/internal/policy.rb +556 -52
  21. data/lib/libo11y/internal/propagators.rb +29 -6
  22. data/lib/libo11y/internal/quiet_inspect.rb +20 -0
  23. data/lib/libo11y/internal/resource.rb +187 -52
  24. data/lib/libo11y/internal/sdk_load.rb +64 -0
  25. data/lib/libo11y/internal/sql_text.rb +365 -67
  26. data/lib/libo11y/internal/summary.rb +2 -1
  27. data/lib/libo11y/options.rb +159 -1
  28. data/lib/libo11y/rack.rb +257 -44
  29. data/lib/libo11y/record.rb +70 -7
  30. data/lib/libo11y/redact.rb +87 -21
  31. data/lib/libo11y/signals.rb +70 -29
  32. data/lib/libo11y/spec/app_registry.rb +97 -61
  33. data/lib/libo11y/spec/yaml_v3.rb +179 -111
  34. data/lib/libo11y/telemetry.rb +315 -53
  35. data/lib/libo11y/testing.rb +31 -19
  36. data/lib/libo11y/version.rb +1 -1
  37. data/lib/libo11y.rb +1 -1
  38. metadata +94 -39
@@ -58,11 +58,18 @@ module Libo11y
58
58
  MAX_DEPTH = 10_000
59
59
 
60
60
  class Handler < Psych::Handler
61
- attr_reader :docs
62
-
63
- def initialize
64
- super
61
+ # double_quoted holds, for each double-quoted scalar, the index of
62
+ # its document and libyaml's location of it.
63
+ attr_reader :docs, :double_quoted
64
+
65
+ # @param stand [Stand, nil] what stands in for the backslash of \' in
66
+ # the text being parsed
67
+ def initialize(stand = nil)
68
+ super()
69
+ @stand = stand
65
70
  @docs = []
71
+ @double_quoted = []
72
+ @location = nil
66
73
  @stack = []
67
74
  @anchors = {}
68
75
  @aliases = {}.compare_by_identity
@@ -91,14 +98,23 @@ module Libo11y
91
98
  )
92
99
  end
93
100
 
101
+ # Psych calls this before each event with where the event is.
102
+ def event_location(start_line, start_column, end_line, end_column)
103
+ @location = [start_line, start_column, end_line, end_column]
104
+ end
105
+
94
106
  def scalar(value, anchor, tag, plain, quoted, style) # rubocop:disable Metrics/ParameterLists -- Psych::Handler's signature
107
+ double_quoted = style == Psych::Nodes::Scalar::DOUBLE_QUOTED
108
+ @double_quoted << [@docs.length, *@location] if double_quoted && @location
95
109
  quoted_or_block = quoted || !plain || (style != Psych::Nodes::Scalar::PLAIN)
96
- node = ScalarNode.new(tag: tag, value: value, quoted_or_block: quoted_or_block, anchor: anchor)
110
+ anchor = restore(anchor)
111
+ node = ScalarNode.new(tag: restore(tag), value: restore(value, decoded: double_quoted), quoted_or_block: quoted_or_block, anchor: anchor)
97
112
  @anchors[anchor] = node if anchor
98
113
  attach(node)
99
114
  end
100
115
 
101
116
  def alias(anchor)
117
+ anchor = restore(anchor)
102
118
  target = @anchors[anchor]
103
119
  raise SyntaxError, "unknown anchor '#{anchor}' referenced" unless target
104
120
 
@@ -110,7 +126,8 @@ module Libo11y
110
126
  def start_sequence(anchor, tag, _implicit, _style)
111
127
  raise SyntaxError, "the document nests too deeply" if @stack.length >= MAX_DEPTH
112
128
 
113
- node = SequenceNode.new(tag: tag, items: [], anchor: anchor)
129
+ anchor = restore(anchor)
130
+ node = SequenceNode.new(tag: restore(tag), items: [], anchor: anchor)
114
131
  @anchors[anchor] = node if anchor
115
132
  attach(node)
116
133
  @stack << [:seq, node]
@@ -123,7 +140,8 @@ module Libo11y
123
140
  def start_mapping(anchor, tag, _implicit, _style)
124
141
  raise SyntaxError, "the document nests too deeply" if @stack.length >= MAX_DEPTH
125
142
 
126
- node = MappingNode.new(tag: tag, pairs: [], anchor: anchor)
143
+ anchor = restore(anchor)
144
+ node = MappingNode.new(tag: restore(tag), pairs: [], anchor: anchor)
127
145
  @anchors[anchor] = node if anchor
128
146
  attach(node)
129
147
  @stack << [:map, node, nil]
@@ -135,6 +153,20 @@ module Libo11y
135
153
 
136
154
  private
137
155
 
156
+ # Undoes the stand-in for \' in a string Psych gives. decoded says
157
+ # that the string is the value of a double-quoted scalar. There
158
+ # libyaml has decoded the stand-in to its character, which nothing
159
+ # else in the text can produce, and that character is removed.
160
+ # Anywhere else the stand-in is still its own text, which the
161
+ # original did not hold, and it becomes the backslash again. A
162
+ # double-quoted value is never searched for the text: escapes such
163
+ # as \x5c can spell it there without it being a stand-in.
164
+ def restore(text, decoded: false)
165
+ return text if @stand.nil? || text.nil?
166
+
167
+ decoded ? text.delete(@stand.character) : text.gsub(@stand.escape) { "\\" }
168
+ end
169
+
138
170
  def attach(node)
139
171
  if @stack.empty?
140
172
  @doc_root = node
@@ -156,17 +188,15 @@ module Libo11y
156
188
 
157
189
  def decode_input(raw)
158
190
  bytes = raw.b
159
- text = if bytes.start_with?("\xFF\xFE".b)
160
- decode_utf(bytes.byteslice(2, bytes.bytesize - 2), Encoding::UTF_16LE)
161
- elsif bytes.start_with?("\xFE\xFF".b)
162
- decode_utf(bytes.byteslice(2, bytes.bytesize - 2), Encoding::UTF_16BE)
163
- elsif bytes.start_with?("\xEF\xBB\xBF".b)
164
- decode_utf(bytes.byteslice(3, bytes.bytesize - 3), Encoding::UTF_8)
165
- else
166
- decode_utf(bytes, Encoding::UTF_8)
167
- end
168
- check_characters(text)
169
- text
191
+ if bytes.start_with?("\xFF\xFE".b)
192
+ decode_utf(bytes.byteslice(2, bytes.bytesize - 2), Encoding::UTF_16LE)
193
+ elsif bytes.start_with?("\xFE\xFF".b)
194
+ decode_utf(bytes.byteslice(2, bytes.bytesize - 2), Encoding::UTF_16BE)
195
+ elsif bytes.start_with?("\xEF\xBB\xBF".b)
196
+ decode_utf(bytes.byteslice(3, bytes.bytesize - 3), Encoding::UTF_8)
197
+ else
198
+ decode_utf(bytes, Encoding::UTF_8)
199
+ end
170
200
  end
171
201
 
172
202
  def decode_utf(bytes, enc)
@@ -178,117 +208,155 @@ module Libo11y
178
208
  raise SyntaxError, "invalid #{enc.name} input"
179
209
  end
180
210
 
181
- def check_characters(text)
182
- return unless (m = INVALID_CHAR_RE.match(text))
211
+ # The line breaks libyaml counts in the positions it reports.
212
+ LINE_BREAK = /\r\n|[\r\n\u0085\u2028\u2029]/
213
+ private_constant :LINE_BREAK
214
+
215
+ # What stands in for the backslash of every \' in the text while Psych
216
+ # parses it: a \u escape of a private-use character that the text does
217
+ # not hold, as an escape and as the character it names.
218
+ Stand = Struct.new(:escape, :character)
219
+ private_constant :Stand
220
+
221
+ # Parses the documents of text. Returns the documents and, when the
222
+ # second document does not parse, the SyntaxError for it. An error in
223
+ # the first document is raised: it wins over everything else. Nothing
224
+ # after the second document matters, as in Go, which decodes the first
225
+ # document and then looks at the next one only. The one exception is a
226
+ # character that may not be in a YAML stream: yaml.v3 reads the whole
227
+ # input before it parses, and refuses such a character wherever it is.
228
+ #
229
+ # Psych reads YAML as libyaml does, and yaml.v3 differs in two escapes
230
+ # of a double-quoted scalar: it knows \' and does not know \/. Which
231
+ # text is a double-quoted scalar is libyaml's own answer, taken from
232
+ # its scalar events, so a quote in a plain or block scalar is never
233
+ # mistaken for one.
234
+ def parse_documents(text)
235
+ bad = INVALID_CHAR_RE.match(text)
236
+ raise SyntaxError, format("control characters are not allowed: U+%04X", bad[0].ord) if bad
237
+
238
+ source, stand = stand_in_for_apostrophe_escapes(text)
239
+ handler = Handler.new(stand)
240
+ failure = run_parser(source, handler)
241
+ starts = line_starts(source)
183
242
 
184
- cp = m[0].ord
185
- raise SyntaxError, format("control characters are not allowed: U+%04X", cp)
243
+ first = escape_problem_in(source, starts, handler, 0)
244
+ raise first if first
245
+ raise failure if failure && handler.docs.empty?
246
+
247
+ second = escape_problem_in(source, starts, handler, 1)
248
+ second ||= failure if handler.docs.length == 1
249
+ [handler.docs, second]
186
250
  end
187
251
 
188
- # Adjust libyaml's YAML 1.1 double-quoted escapes to match Go's yaml.v3:
189
- # - Reject `\/` (yaml.v3 rejects escaped slash; libyaml accepts it)
190
- # - Rewrite `\'` inside double-quoted strings to `'` (yaml.v3 accepts it; libyaml rejects it)
191
- # - Reject `\uD800`-`\uDFFF` surrogate escapes
192
- # Checks the escapes of double-quoted scalars as yaml.v3 does, before
193
- # the text reaches Psych: \/ is not an escape, \' is one, and \u may
194
- # not name a surrogate. A quote opens a scalar only where a scalar can
195
- # start, so an apostrophe inside a plain scalar ("the user's id") or
196
- # anywhere in a block scalar is ordinary text.
197
- def preprocess_double_quoted_escapes(text)
198
- out = String.new(encoding: Encoding::UTF_8, capacity: text.bytesize)
199
- quote = nil # the quote character of an open quoted scalar, which may span lines
200
- block_indent = nil # the indentation of the line that opened a block scalar
201
- text.each_line do |line|
202
- if quote.nil? && block_indent
203
- if line.strip.empty? || line[/\A */].length > block_indent
204
- out << line
205
- next
206
- end
207
- block_indent = nil
208
- end
209
- quote, opens_block = scan_escapes(line, quote, out)
210
- block_indent = line[/\A */].length if opens_block
252
+ # The first escape yaml.v3 refuses in a double-quoted scalar of
253
+ # document number index, as a SyntaxError, or nil.
254
+ def escape_problem_in(source, starts, handler, index)
255
+ handler.double_quoted.each do |document, *location|
256
+ next unless document == index
257
+
258
+ message = escape_problem(source, starts, location)
259
+ return SyntaxError.new(message) if message
211
260
  end
212
- out
261
+ nil
213
262
  end
214
263
 
215
- # Copies one line to out with its double-quoted escapes checked.
216
- # Returns the quote still open at the end of the line, and whether the
217
- # line opens a block scalar.
218
- def scan_escapes(line, quote, out)
219
- i = 0
220
- while i < line.length
221
- ch = line[i]
222
- if quote == "'"
223
- out << ch
224
- if ch == "'" && line[i + 1] == "'"
225
- out << "'"
226
- i += 1
227
- elsif ch == "'"
228
- quote = nil
229
- end
230
- elsif quote == "\""
231
- if ch == "\\"
232
- i = copy_escape(line, i, out)
233
- next
234
- end
235
- out << ch
236
- quote = nil if ch == "\""
237
- elsif ch == "#" && (i.zero? || [" ", "\t"].include?(line[i - 1]))
238
- out << line[i..]
239
- break
264
+ # Runs Psych over source. Returns the error that stopped the parse, or nil.
265
+ def run_parser(source, handler)
266
+ Psych::Parser.new(handler).parse(source)
267
+ nil
268
+ rescue SyntaxError => e
269
+ e
270
+ rescue Psych::Exception => e
271
+ SyntaxError.new(e.message)
272
+ end
273
+
274
+ # libyaml refuses \', which yaml.v3 reads as an apostrophe. So the
275
+ # backslash of each \' in text becomes a \u escape that libyaml takes,
276
+ # and the apostrophe stays where it is: a single-quoted scalar still
277
+ # ends at it, and a plain or block scalar still holds it. The handler
278
+ # undoes this in every string it is given. In a double-quoted scalar
279
+ # the escape decodes to a character that is then removed, which leaves
280
+ # the apostrophe. Anywhere else the escape is plain text and is turned
281
+ # back into the backslash. The text is parsed once, however many
282
+ # escapes it has.
283
+ def stand_in_for_apostrophe_escapes(text)
284
+ return [text, nil] unless text.include?("\\'")
285
+
286
+ code = (0xE000..0xF8FF).find do |candidate|
287
+ !text.include?(candidate.chr(Encoding::UTF_8)) && !text.match?(/#{format("%04X", candidate)}/i)
288
+ end
289
+ raise SyntaxError, "the text uses every private-use character" if code.nil?
290
+
291
+ stand = Stand.new(format("\\u%04X", code), code.chr(Encoding::UTF_8))
292
+ # Only a backslash that follows an even number of backslashes: after
293
+ # an odd number it is itself escaped, and libyaml takes \\' as it is.
294
+ out = String.new(encoding: text.encoding)
295
+ pos = 0
296
+ while (hit = text.byteindex("\\'", pos))
297
+ run_start = hit
298
+ run_start -= 1 while run_start.positive? && text.getbyte(run_start - 1) == 92
299
+ if (hit + 1 - run_start).odd?
300
+ out << text.byteslice(pos, hit - pos) << stand.escape << "'"
240
301
  else
241
- quote = ch if ["'", "\""].include?(ch) && scalar_start?(line, i)
242
- out << ch
302
+ out << text.byteslice(pos, hit + 2 - pos)
243
303
  end
244
- i += 1
304
+ pos = hit + 2
245
305
  end
246
- [quote, quote.nil? && block_header?(line)]
306
+ out << text.byteslice(pos, text.bytesize - pos)
307
+ [out, stand]
247
308
  end
248
309
 
249
- # Copies the escape that starts at i, and returns the index after it.
250
- def copy_escape(line, i, out)
251
- nxt = line[i + 1]
252
- raise SyntaxError, "unterminated escape in double-quoted scalar" if nxt.nil? && !line.end_with?("\n")
253
- raise SyntaxError, "found unknown escape character '/'" if nxt == "/"
310
+ # The index of the first character of each line.
311
+ def line_starts(source)
312
+ starts = [0]
313
+ source.scan(LINE_BREAK) { starts << Regexp.last_match.end(0) }
314
+ starts
315
+ end
254
316
 
255
- if nxt == "'"
256
- out << "'"
257
- else
258
- hex = line[i + 2, 4].to_s
259
- if nxt == "u" && /\A[0-9a-fA-F]{4}\z/.match?(hex) && hex.to_i(16).between?(0xD800, 0xDFFF)
260
- raise SyntaxError, "found invalid Unicode character escape code"
317
+ # The message for an escape yaml.v3 refuses in the double-quoted
318
+ # scalar at location, or nil. The location is libyaml's: it starts at
319
+ # the scalar's tag or anchor, when it has one, and ends after the
320
+ # closing quote.
321
+ def escape_problem(source, starts, location)
322
+ from_line, from_column, to_line, to_column = location
323
+ raw = source[(starts.fetch(from_line) + from_column)...(starts.fetch(to_line) + to_column)]
324
+ opening = opening_quote(raw)
325
+ return nil if opening.nil?
326
+
327
+ i = opening + 1
328
+ while i < raw.length
329
+ if raw[i] == "\\"
330
+ return "found unknown escape character '/'" if raw[i + 1] == "/"
331
+ return "found invalid Unicode character escape code" if surrogate_escape?(raw, i)
332
+
333
+ i += 1
261
334
  end
262
-
263
- out << "\\" << nxt.to_s
335
+ i += 1
264
336
  end
265
- i + 2
337
+ nil
266
338
  end
267
339
 
268
- # Whether a quote at index i opens a quoted scalar: it is the first
269
- # thing in a value, a sequence entry or a flow collection.
270
- def scalar_start?(line, i)
271
- before = line[0, i].rstrip
272
- return true if before.empty? || before.end_with?("[", "{", ",")
273
-
274
- before.end_with?(":", "-", "?") && before.length < i
275
- end
276
-
277
- # Whether the line's value is a block scalar header, such as "key: |" or "- >-".
278
- def block_header?(line)
279
- code = line.sub(/(\A|\s)#.*\z/m, "").rstrip
280
- code.match?(/(\A\s*|[:-]\s+)[|>][-+0-9]*\z/)
340
+ # The index of the quote that opens the scalar in raw. Before it may
341
+ # stand the scalar's tag and anchor, white space, and comments, each
342
+ # of which runs to the end of its line and may hold a quote of its own.
343
+ def opening_quote(raw)
344
+ i = 0
345
+ while i < raw.length
346
+ case raw[i]
347
+ when "\"" then return i
348
+ when "#" then i = raw.index(LINE_BREAK, i) || raw.length
349
+ when "!", "&" then i = raw.index(/\s/, i) || raw.length
350
+ when /\s/ then i += 1
351
+ else return nil
352
+ end
353
+ end
354
+ nil
281
355
  end
282
356
 
283
- def parse_documents(text)
284
- check_characters(text)
285
- normalized = preprocess_double_quoted_escapes(text)
286
- handler = Handler.new
287
- parser = Psych::Parser.new(handler)
288
- parser.parse(normalized)
289
- handler.docs
290
- rescue Psych::Exception => e
291
- raise SyntaxError, e.message
357
+ def surrogate_escape?(raw, i)
358
+ hex = raw[i + 2, 4].to_s
359
+ raw[i + 1] == "u" && hex.match?(/\A\h{4}\z/) && hex.to_i(16).between?(0xD800, 0xDFFF)
292
360
  end
293
361
 
294
362
  def short_tag(tag)