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
@@ -5,7 +5,10 @@ module Libo11y
5
5
  # SQL statement text for telemetry: a lexer that removes every literal and
6
6
  # comment, and a summary of the statement's operation and table. It is a
7
7
  # port of go/o11ysql/internal/sqltext and works on bytes as that does, so
8
- # both give the same text for the same statement.
8
+ # both give the same sanitized text for the same statement. In summary
9
+ # tokenization, Ruby also treats form feed (\f) as whitespace and compares
10
+ # qualified table names by unquoted component boundaries with optional
11
+ # whitespace around '.' (see docs/PARITY.md).
9
12
  module SqlText
10
13
  DIALECTS = %i[standard mysql sqlite].freeze
11
14
  MAX_COLLECTION_BYTES = 128
@@ -19,6 +22,23 @@ module Libo11y
19
22
  ).freeze
20
23
  UPDATE_CLAUSE = Set.new(%w[FOR KEY DO THEN OR BEFORE AFTER OF]).freeze
21
24
  TABLE_MODIFIERS = Set.new(%w[ONLY LOW_PRIORITY IGNORE]).freeze
25
+ # Clause keywords, which cannot be a table alias written without AS.
26
+ CLAUSE_KEYWORDS = Set.new(
27
+ %w[
28
+ WHERE JOIN INNER LEFT RIGHT FULL CROSS NATURAL OUTER ON USING GROUP ORDER HAVING LIMIT OFFSET FETCH FOR
29
+ UNION INTERSECT EXCEPT SET VALUES SELECT RETURNING WINDOW INTO FROM TABLESAMPLE
30
+ ]
31
+ ).freeze
32
+ # Strictly reserved clause keywords across PostgreSQL, MySQL, and SQLite
33
+ # that cannot be unquoted table names after a spaced dot (such as
34
+ # malformed `FROM t1 . JOIN t1`), while preserving valid unquoted
35
+ # qualified names like `main . offset` or `db . full`.
36
+ SPACED_DOT_RESERVED_KEYWORDS = Set.new(
37
+ %w[
38
+ WHERE JOIN INNER LEFT RIGHT CROSS NATURAL OUTER ON USING GROUP ORDER HAVING
39
+ UNION INTERSECT EXCEPT SELECT FROM INTO
40
+ ]
41
+ ).freeze
22
42
 
23
43
  QUOTE = "'".ord
24
44
  DQUOTE = '"'.ord
@@ -47,10 +67,21 @@ module Libo11y
47
67
 
48
68
  module_function
49
69
 
50
- # Returns query with every literal replaced by "?" and every comment by
51
- # one space. Names, placeholders and operators stay. The result has the
52
- # encoding of query, and bytes that are not UTF-8 pass through as part
53
- # of a name.
70
+ # Returns [text, safe]. The text is query with every literal replaced
71
+ # by "?" and every comment by one space. Names, placeholders and
72
+ # operators stay. The text has the encoding of query, and bytes that
73
+ # are not UTF-8 pass through as part of a name.
74
+ #
75
+ # safe is false when the statement holds a construct whose literal or
76
+ # comment boundaries depend on server settings or a server version the
77
+ # client cannot see. Sanitizing then fails closed: the text ends at
78
+ # that construct, and the caller records no db.query.text. Those are:
79
+ #
80
+ # - an odd run of backslashes before a quote in a plain PostgreSQL
81
+ # '...' string (standard_conforming_strings) or a MySQL '...' or
82
+ # "..." string (NO_BACKSLASH_ESCAPES, ANSI_QUOTES)
83
+ # - a MySQL /*! ... */ or MariaDB /*M! ... */ versioned comment, whose
84
+ # body a server runs as SQL or skips, depending on its version
54
85
  def sanitize(query, dialect = :standard)
55
86
  raise ArgumentError, "libo11y: unknown SQL dialect #{dialect.inspect}" unless DIALECTS.include?(dialect)
56
87
 
@@ -59,21 +90,22 @@ module Libo11y
59
90
  mysql = dialect == :mysql
60
91
  sqlite = dialect == :sqlite
61
92
  pg = !mysql && !sqlite
62
- in_versioned = false # inside a MySQL /*! ... */ comment, whose body is SQL
63
93
  i = 0
64
94
  n = s.bytesize
65
95
  while i < n
66
96
  c = s.getbyte(i)
67
97
  nx = s.getbyte(i + 1)
68
- if c == QUOTE
69
- i, = skip_quoted(s, i, QUOTE, mysql)
98
+ if c == QUOTE || (c == DQUOTE && !pg)
70
99
  out << "?"
71
- elsif pg && [69, 101].include?(c) && nx == QUOTE # E'...' escape string
100
+ if sqlite
101
+ i, = skip_quoted(s, i, c, false)
102
+ else
103
+ i, ambiguous = skip_quoted_checked(s, i, c)
104
+ return [out.force_encoding(query.encoding), false] if ambiguous
105
+ end
106
+ elsif pg && [69, 101].include?(c) && nx == QUOTE # E'...' always uses backslash escapes
72
107
  i, = skip_quoted(s, i + 1, QUOTE, true)
73
108
  out << "?"
74
- elsif c == DQUOTE && !pg
75
- i, = skip_quoted(s, i, DQUOTE, mysql)
76
- out << "?"
77
109
  elsif c == DQUOTE || (c == BACKTICK && !pg) || (c == LBRACKET && sqlite)
78
110
  finish, closed = skip_quoted_name(s, i)
79
111
  # An unterminated name may hold anything.
@@ -83,18 +115,10 @@ module Libo11y
83
115
  i = skip_line(s, i)
84
116
  out << " "
85
117
  elsif c == SLASH && nx == STAR
86
- finish = mysql ? skip_versioned_open(s, i) : nil
87
- if finish
88
- in_versioned = true
89
- i = finish
90
- else
91
- i = skip_block(s, i, pg)
92
- end
93
- out << " "
94
- elsif in_versioned && c == STAR && nx == SLASH
95
- in_versioned = false
96
- i += 2
97
118
  out << " "
119
+ return [out.force_encoding(query.encoding), false] if mysql && versioned_comment?(s, i)
120
+
121
+ i = skip_block(s, i, pg)
98
122
  elsif pg && c == DOLLAR
99
123
  finish = skip_dollar_quoted(s, i)
100
124
  if finish
@@ -130,16 +154,19 @@ module Libo11y
130
154
  i += 1
131
155
  end
132
156
  end
133
- out.force_encoding(query.encoding)
157
+ [out.force_encoding(query.encoding), true]
134
158
  end
135
159
 
136
160
  # Returns [operation, collection] for sanitized statement text. The
137
161
  # operation is the first keyword, upper case, when it is a known one.
138
- # The collection is the one table the statement names, and is empty for
139
- # a join of different tables, a subquery, or a table function. Both are
140
- # empty when the statement starts with anything else.
141
- def summarize(sanitized)
142
- toks = tokenize(sanitized.b)
162
+ # The collection is the one table the statement names, without quotes,
163
+ # SQLite's [brackets] included under :sqlite. It is empty for a join of
164
+ # different tables, a list of tables with or without aliases, a
165
+ # subquery, or a table function. Both are empty when the statement
166
+ # starts with anything else.
167
+ def summarize(sanitized, dialect = :standard)
168
+ sqlite = dialect == :sqlite
169
+ toks = tokenize(sanitized.b, sqlite)
143
170
  return ["", ""] if toks.empty?
144
171
 
145
172
  operation = toks[0].upcase.force_encoding(Encoding::UTF_8)
@@ -147,9 +174,11 @@ module Libo11y
147
174
  return [operation, ""] if operation == "SHOW"
148
175
 
149
176
  subquery = [] # per open parenthesis: whether it holds a subquery
150
- collection = ""
177
+ seen_join = false # top-level JOIN seen, so a later USING (col, ...) can be a join column list
178
+ collection_parts = nil
151
179
  last = toks.length
152
- (0...(last - 1)).each do |k|
180
+ k = -1
181
+ while (k += 1) < last - 1
153
182
  tok = toks[k]
154
183
  if tok == "("
155
184
  subquery << %w[SELECT WITH].include?(toks[k + 1].upcase)
@@ -161,25 +190,200 @@ module Libo11y
161
190
  next if !subquery.empty? && !subquery.last # inside a call or a column list
162
191
 
163
192
  keyword = tok.upcase
164
- next unless names_table?(keyword, toks, k)
193
+ ctx = { op: operation, toks: toks, in_subquery: !subquery.empty?, seen_join: seen_join, sqlite: sqlite }
194
+ next unless names_table?(keyword, k, ctx)
195
+
196
+ seen_join = true if keyword == "JOIN" && subquery.empty?
165
197
 
166
198
  j = k + 1
167
199
  j = skip_if_exists(toks, j) if keyword == "TABLE"
168
- j += 2 if keyword == "UPDATE" && toks[j].casecmp?("OR") # SQLite's UPDATE OR IGNORE t
200
+ j += 2 if keyword == "UPDATE" && j < last && toks[j].casecmp?("OR") # SQLite's UPDATE OR IGNORE t
169
201
  j += 1 while j < last && TABLE_MODIFIERS.include?(toks[j].upcase)
170
202
  # A subquery or something unrecognized.
171
- return [operation, ""] if j >= last || !name_start?(toks[j].getbyte(0))
203
+ return [operation, ""] if j >= last || !name?(toks[j], sqlite)
204
+
205
+ after, several = table_follower(keyword, toks, j, sqlite)
172
206
  # A list of tables, or a table function.
173
- return [operation, ""] if j + 1 < last && (toks[j + 1] == "," || (toks[j + 1] == "(" && %w[FROM JOIN].include?(keyword)))
207
+ return [operation, ""] if several
208
+
209
+ parts = unquote_name_parts(toks[j], sqlite)
210
+ return [operation, ""] if !collection_parts.nil? && collection_parts != parts
211
+
212
+ collection_parts = parts
213
+ k = after - 1 if after > k
214
+ end
215
+ return [operation, ""] if collection_parts.nil?
216
+
217
+ scrubbed = to_valid_utf8(collection_parts.join("."))
218
+ return [operation, ""] if scrubbed.bytesize > MAX_COLLECTION_BYTES
219
+
220
+ [operation, scrubbed]
221
+ end
222
+
223
+ # Matches Go's strings.ToValidUTF8(raw, "\uFFFD"): replaces each
224
+ # contiguous run of invalid UTF-8 bytes with one U+FFFD while preserving
225
+ # valid U+FFFD characters already present in the input.
226
+ def to_valid_utf8(raw)
227
+ str = raw.encoding == Encoding::UTF_8 ? raw : String.new(raw.b, encoding: Encoding::UTF_8)
228
+ return str.dup if str.valid_encoding?
229
+
230
+ out = +""
231
+ in_invalid = false
232
+ str.each_char do |ch|
233
+ if ch.valid_encoding?
234
+ out << ch
235
+ in_invalid = false
236
+ elsif !in_invalid
237
+ out << "\uFFFD"
238
+ in_invalid = true
239
+ end
240
+ end
241
+ out
242
+ end
243
+
244
+ # Looks past the table name at toks[j] for the comma of a list of
245
+ # tables, directly or after an alias, column alias list, SQLite
246
+ # INDEXED BY / NOT INDEXED hint, MySQL index hint, or TABLESAMPLE
247
+ # clause, and for the parenthesis of a table function. Returns the
248
+ # index past the skipped tokens, which keeps summarize linear, and
249
+ # whether the table is one of several or a table function.
250
+ def table_follower(keyword, toks, j, sqlite)
251
+ last = toks.length
252
+ nxt = j + 1
253
+ return [nxt, false] if nxt >= last
254
+ return [nxt, true] if toks[nxt] == "," # a list of tables
255
+
256
+ table_clause = %w[FROM JOIN USING].include?(keyword)
257
+ return [nxt, true] if toks[nxt] == "(" && table_clause # a table function
258
+ return [nxt, false] unless table_clause || keyword == "UPDATE"
259
+
260
+ after, unclosed = skip_table_hints(toks, nxt, sqlite)
261
+ return [last, true] if unclosed
262
+ return [after, after < last && toks[after] == ","] if after != nxt
263
+
264
+ after_alias = skip_table_alias(toks, nxt, sqlite)
265
+ return [nxt, false] if after_alias.nil?
266
+
267
+ nxt = after_alias
268
+
269
+ if nxt < last && toks[nxt] == "(" && table_clause
270
+ # A flat list of column aliases: FROM users u(a, b), orders o
271
+ close = nxt + 1
272
+ close += 1 while close < last && toks[close] != ")" && toks[close] != "("
273
+ nxt = close + 1 if close < last && toks[close] == ")"
274
+ end
275
+ nxt, unclosed = skip_table_hints(toks, nxt, sqlite)
276
+ return [last, true] if unclosed
277
+
278
+ [nxt, nxt < last && toks[nxt] == ","]
279
+ end
280
+
281
+ # Advances past SQLite index hints, MySQL index hints, and TABLESAMPLE
282
+ # clauses between a table name (or its alias) and a table-list comma.
283
+ def skip_table_hints(toks, idx, sqlite)
284
+ loop do
285
+ prev = idx
286
+ idx = skip_sqlite_index_hint(toks, idx) if sqlite
287
+ idx, unclosed = skip_mysql_index_hints(toks, idx)
288
+ return [toks.length, true] if unclosed
289
+
290
+ idx, unclosed = skip_table_sample(toks, idx)
291
+ return [toks.length, true] if unclosed
292
+
293
+ return [idx, false] if idx == prev
294
+ end
295
+ end
296
+
297
+ def skip_mysql_index_hints(toks, idx)
298
+ last = toks.length
299
+ while idx + 2 < last
300
+ break unless %w[USE FORCE IGNORE].include?(toks[idx].upcase)
301
+ break unless %w[INDEX KEY].include?(toks[idx + 1].upcase)
302
+
303
+ pos = skip_mysql_hint_scope(toks, idx + 2)
304
+ return [idx, false] if pos.nil?
305
+
306
+ after, unclosed = skip_balanced_parens(toks, pos)
307
+ return [last, true] if unclosed
308
+ return [idx, false] if after == pos
309
+
310
+ idx = after
311
+ end
312
+ [idx, false]
313
+ end
314
+
315
+ def skip_mysql_hint_scope(toks, pos)
316
+ last = toks.length
317
+ return pos unless pos < last && toks[pos].casecmp?("FOR")
318
+
319
+ pos += 1
320
+ return pos + 1 if pos < last && toks[pos].casecmp?("JOIN")
321
+ return pos + 2 if pos + 1 < last && %w[ORDER GROUP].include?(toks[pos].upcase) && toks[pos + 1].casecmp?("BY")
322
+
323
+ nil
324
+ end
325
+
326
+ def skip_table_sample(toks, idx)
327
+ last = toks.length
328
+ return [idx, false] if idx + 2 >= last || !toks[idx].casecmp?("TABLESAMPLE")
329
+
330
+ pos = idx + 1
331
+ pos += 1 if name?(toks[pos], false)
332
+ after, unclosed = skip_balanced_parens(toks, pos)
333
+ return [last, true] if unclosed
334
+ return [idx, false] if after == pos
174
335
 
175
- name = unquote_name(toks[j])
176
- return [operation, ""] if !collection.empty? && collection != name
336
+ pos = after
337
+ if pos < last && toks[pos].casecmp?("REPEATABLE")
338
+ rep, rep_unclosed = skip_balanced_parens(toks, pos + 1)
339
+ return [last, true] if rep_unclosed
177
340
 
178
- collection = name
341
+ pos = rep if rep > pos + 1
179
342
  end
180
- return [operation, ""] if collection.bytesize > MAX_COLLECTION_BYTES
343
+ [pos, false]
344
+ end
345
+
346
+ def skip_balanced_parens(toks, pos)
347
+ last = toks.length
348
+ return [pos, false] if pos >= last || toks[pos] != "("
349
+
350
+ depth = 1
351
+ idx = pos + 1
352
+ while idx < last
353
+ depth += 1 if toks[idx] == "("
354
+ depth -= 1 if toks[idx] == ")"
355
+ return [idx + 1, false] if depth.zero?
356
+
357
+ idx += 1
358
+ end
359
+ [last, true]
360
+ end
361
+
362
+ # Under :sqlite, sanitize replaces double-quoted tokens (including
363
+ # double-quoted table aliases) with ?, and a bind parameter cannot
364
+ # follow a table name in FROM/JOIN/UPDATE.
365
+ def alias_token?(tok, sqlite)
366
+ name?(tok, sqlite) || (sqlite && tok == "?")
367
+ end
368
+
369
+ def skip_table_alias(toks, nxt, sqlite)
370
+ if toks[nxt].casecmp?("AS")
371
+ return nil if nxt + 1 >= toks.length || !alias_token?(toks[nxt + 1], sqlite)
372
+
373
+ nxt + 2
374
+ elsif alias_token?(toks[nxt], sqlite) && !CLAUSE_KEYWORDS.include?(toks[nxt].upcase)
375
+ nxt + 1
376
+ end
377
+ end
378
+
379
+ # Advances past a SQLite INDEXED BY <name|?> or NOT INDEXED clause at
380
+ # toks[idx], returning idx unchanged when neither is present.
381
+ def skip_sqlite_index_hint(toks, idx)
382
+ last = toks.length
383
+ return idx + 3 if idx + 2 < last && toks[idx].casecmp?("INDEXED") && toks[idx + 1].casecmp?("BY") && alias_token?(toks[idx + 2], true)
384
+ return idx + 2 if idx + 1 < last && toks[idx].casecmp?("NOT") && toks[idx + 1].casecmp?("INDEXED")
181
385
 
182
- [operation, String.new(collection, encoding: Encoding::UTF_8).scrub]
386
+ idx
183
387
  end
184
388
 
185
389
  # Returns the longest prefix of text that has at most max_bytes bytes
@@ -193,16 +397,48 @@ module Libo11y
193
397
  text.byteslice(0, cut)
194
398
  end
195
399
 
196
- def names_table?(keyword, toks, k)
400
+ def names_table?(keyword, k, ctx)
401
+ toks = ctx[:toks]
197
402
  prev = k.positive? ? toks[k - 1].upcase : ""
198
403
  case keyword
199
404
  when "JOIN", "INTO", "TABLE" then true
200
405
  when "FROM" then prev != "DISTINCT" # a IS DISTINCT FROM b compares values
201
406
  when "UPDATE" then !UPDATE_CLAUSE.include?(prev)
407
+ when "USING" then using_names_table?(k, ctx)
202
408
  else false
203
409
  end
204
410
  end
205
411
 
412
+ def using_names_table?(k, ctx)
413
+ return false if !%w[DELETE MERGE].include?(ctx[:op]) || ctx[:in_subquery]
414
+
415
+ toks = ctx[:toks]
416
+ return true unless k + 1 < toks.length && toks[k + 1] == "("
417
+
418
+ # A top-level USING (...) in DELETE or MERGE can only be a
419
+ # JOIN ... USING (col, ...) column list after a top-level JOIN and when
420
+ # the parenthesized group is a closed flat identifier list. Any other
421
+ # parenthesized USING source names a table source so summarize fails closed.
422
+ !ctx[:seen_join] || !flat_column_list?(toks, k + 1, ctx[:sqlite])
423
+ end
424
+
425
+ def flat_column_list?(toks, open_idx, sqlite)
426
+ last = toks.length
427
+ return false if open_idx >= last || toks[open_idx] != "("
428
+
429
+ idx = open_idx + 1
430
+ loop do
431
+ return false if idx >= last || !name?(toks[idx], sqlite)
432
+
433
+ idx += 1
434
+ return false if idx >= last
435
+ return true if toks[idx] == ")"
436
+ return false if toks[idx] != ","
437
+
438
+ idx += 1
439
+ end
440
+ end
441
+
206
442
  def skip_if_exists(toks, j)
207
443
  last = toks.length
208
444
  return j if j >= last || !toks[j].casecmp?("IF")
@@ -214,37 +450,57 @@ module Libo11y
214
450
  last
215
451
  end
216
452
 
217
- def unquote_name(tok)
218
- return tok.dup unless tok.include?('"') || tok.include?("`")
219
-
220
- out = +"".b
453
+ # Splits a possibly qualified name into unquoted components, turning a
454
+ # doubled quote inside a quoted part back into one. Keeping component
455
+ # boundaries prevents `"a.b".c` and `a."b.c"` from comparing equal, and
456
+ # skipping unquoted whitespace around `.` handles `main . x`.
457
+ def unquote_name_parts(tok, sqlite)
458
+ parts = []
221
459
  i = 0
222
460
  n = tok.bytesize
461
+ while i < n
462
+ i += 1 while i < n && space?(tok.getbyte(i))
463
+ break if i >= n
464
+
465
+ part, i = read_unquoted_part(tok, i, sqlite)
466
+ parts << part
467
+ i += 1 while i < n && space?(tok.getbyte(i))
468
+ i += 1 if i < n && tok.getbyte(i) == DOT
469
+ end
470
+ parts
471
+ end
472
+
473
+ def read_unquoted_part(tok, i, sqlite)
474
+ part = +"".b
475
+ n = tok.bytesize
223
476
  while i < n
224
477
  q = tok.getbyte(i)
225
- if q != DQUOTE && q != BACKTICK
226
- out << q.chr
478
+ break if q == DOT || space?(q)
479
+
480
+ bracket = q == LBRACKET && sqlite
481
+ unless bracket || q == DQUOTE || q == BACKTICK
482
+ part << q.chr
227
483
  i += 1
228
484
  next
229
485
  end
230
- finish, closed = skip_quoted(tok, i, q, false)
486
+ finish, closed = bracket ? skip_quoted_name(tok, i) : skip_quoted(tok, i, q, false)
231
487
  inner = tok.byteslice(i + 1, (closed ? finish - 1 : finish) - (i + 1))
232
- out << inner.gsub(q.chr * 2, q.chr)
488
+ part << (bracket ? inner : inner.gsub(q.chr * 2, q.chr))
233
489
  i = finish
234
490
  end
235
- out
491
+ [part, i]
236
492
  end
237
493
 
238
- def tokenize(s)
494
+ def tokenize(s, sqlite)
239
495
  toks = []
240
496
  i = 0
241
497
  n = s.bytesize
242
498
  while i < n
243
499
  c = s.getbyte(i)
244
- if [SPACE, 9, 10, 13].include?(c)
500
+ if space?(c)
245
501
  i += 1
246
- elsif name_start?(c)
247
- finish = skip_name(s, i)
502
+ elsif name_start?(c, sqlite)
503
+ finish = skip_name(s, i, sqlite)
248
504
  toks << s.byteslice(i, finish - i)
249
505
  i = finish
250
506
  else
@@ -255,23 +511,40 @@ module Libo11y
255
511
  toks
256
512
  end
257
513
 
258
- def skip_name(s, i)
514
+ # The end of a name that may be qualified and quoted, such as users,
515
+ # "Users", public."Users", main . x, or SQLite's [users].
516
+ def skip_name(s, i, sqlite)
259
517
  n = s.bytesize
260
518
  loop do
261
519
  c = s.getbyte(i)
262
- i = if [DQUOTE, BACKTICK].include?(c)
263
- skip_quoted(s, i, c, false).first
520
+ i = if c == DQUOTE || c == BACKTICK || (sqlite && c == LBRACKET)
521
+ skip_quoted_name(s, i).first
264
522
  else
265
523
  skip_ident(s, i)
266
524
  end
267
- return i unless i + 1 < n && s.getbyte(i) == DOT && name_start?(s.getbyte(i + 1))
525
+ j = i
526
+ j += 1 while j < n && space?(s.getbyte(j))
527
+ return i unless j < n && s.getbyte(j) == DOT
268
528
 
269
- i += 1
529
+ k = j + 1
530
+ k += 1 while k < n && space?(s.getbyte(k))
531
+ return i unless k < n && name_start?(s.getbyte(k), sqlite)
532
+
533
+ if (j > i || k > j + 1) && ident_start?(s.getbyte(k))
534
+ word = s.byteslice(k, skip_ident(s, k) - k).upcase
535
+ return i if SPACED_DOT_RESERVED_KEYWORDS.include?(word)
536
+ end
537
+
538
+ i = k
270
539
  end
271
540
  end
272
541
 
273
- def name_start?(c)
274
- ident_start?(c) || c == DQUOTE || c == BACKTICK
542
+ def name?(tok, sqlite)
543
+ !tok.empty? && name_start?(tok.getbyte(0), sqlite)
544
+ end
545
+
546
+ def name_start?(c, sqlite)
547
+ ident_start?(c) || c == DQUOTE || c == BACKTICK || (sqlite && c == LBRACKET)
275
548
  end
276
549
 
277
550
  def ident_start?(c)
@@ -309,6 +582,33 @@ module Libo11y
309
582
  i
310
583
  end
311
584
 
585
+ # Scans a string whose closing quote a server setting can move
586
+ # (standard_conforming_strings, NO_BACKSLASH_ESCAPES, ANSI_QUOTES).
587
+ # Returns the index after the string and false, or the end of the text
588
+ # and true when an odd run of backslashes stands before a quote: the
589
+ # two server modes then disagree on where the string ends.
590
+ def skip_quoted_checked(s, i, quote)
591
+ n = s.bytesize
592
+ slashes = 0
593
+ j = i + 1
594
+ while j < n
595
+ c = s.getbyte(j)
596
+ if c == BACKSLASH
597
+ slashes += 1
598
+ elsif c == quote
599
+ return [n, true] if slashes.odd?
600
+ return [j + 1, false] unless j + 1 < n && s.getbyte(j + 1) == quote
601
+
602
+ j += 1
603
+ slashes = 0
604
+ else
605
+ slashes = 0
606
+ end
607
+ j += 1
608
+ end
609
+ [n, false]
610
+ end
611
+
312
612
  # Returns the index after the literal that opens at i, and whether it
313
613
  # was closed. A doubled quote is an escaped quote. With backslash, a
314
614
  # backslash escapes the next byte.
@@ -369,14 +669,12 @@ module Libo11y
369
669
  n
370
670
  end
371
671
 
372
- # Returns the index after a MySQL /*!12345 or MariaDB /*M!12345 opener,
373
- # or nil when the comment at i is an ordinary one.
374
- def skip_versioned_open(s, i)
672
+ # Whether the comment at i opens a MySQL /*! or a MariaDB /*M!
673
+ # versioned comment.
674
+ def versioned_comment?(s, i)
375
675
  j = i + 2
376
676
  j += 1 if s.getbyte(j) == 77
377
- return nil unless s.getbyte(j) == BANG
378
-
379
- skip_digits(s, j + 1)
677
+ s.getbyte(j) == BANG
380
678
  end
381
679
 
382
680
  # Returns the index after the dollar-quoted string that opens at i, or
@@ -48,7 +48,8 @@ module Libo11y
48
48
  def sampler(options, env_map)
49
49
  return options.sampler.description if options.sampler
50
50
 
51
- (Env.value(env_map, "OTEL_TRACES_SAMPLER") || DEFAULT_SAMPLER).downcase
51
+ # The sampler that runs, so an unknown name is reported as the default.
52
+ Env.sampler_name(env_map)
52
53
  end
53
54
  end
54
55
  end