libo11y 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +22 -0
- data/LICENSE +202 -0
- data/README.md +64 -0
- data/exe/libo11y +32 -0
- data/lib/libo11y/db.rb +160 -0
- data/lib/libo11y/detached.rb +90 -0
- data/lib/libo11y/errors.rb +52 -0
- data/lib/libo11y/http_client.rb +98 -0
- data/lib/libo11y/internal/bridges.rb +427 -0
- data/lib/libo11y/internal/endpoint.rb +422 -0
- data/lib/libo11y/internal/env.rb +158 -0
- data/lib/libo11y/internal/identity.rb +34 -0
- data/lib/libo11y/internal/logging_device.rb +266 -0
- data/lib/libo11y/internal/pii.rb +198 -0
- data/lib/libo11y/internal/policy.rb +286 -0
- data/lib/libo11y/internal/propagators.rb +87 -0
- data/lib/libo11y/internal/resource.rb +178 -0
- data/lib/libo11y/internal/runtime_metrics.rb +86 -0
- data/lib/libo11y/internal/sql_text.rb +442 -0
- data/lib/libo11y/options.rb +90 -0
- data/lib/libo11y/proc.rb +50 -0
- data/lib/libo11y/rack.rb +201 -0
- data/lib/libo11y/record.rb +63 -0
- data/lib/libo11y/redact.rb +224 -0
- data/lib/libo11y/signals.rb +47 -0
- data/lib/libo11y/spec/app_registry.rb +430 -0
- data/lib/libo11y/spec/generated/goprint.rb +721 -0
- data/lib/libo11y/spec/generated/spec.rb +363 -0
- data/lib/libo11y/spec/generated/tables.rb +785 -0
- data/lib/libo11y/spec/go_quote.rb +58 -0
- data/lib/libo11y/spec/yaml_v3.rb +468 -0
- data/lib/libo11y/spec.rb +12 -0
- data/lib/libo11y/telemetry.rb +450 -0
- data/lib/libo11y/testing.rb +261 -0
- data/lib/libo11y/version.rb +5 -0
- data/lib/libo11y.rb +46 -0
- metadata +222 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Libo11y
|
|
4
|
+
module Internal
|
|
5
|
+
module Endpoint
|
|
6
|
+
WITHHELD = "unparseable"
|
|
7
|
+
|
|
8
|
+
HEX_UPPER = "0123456789ABCDEF"
|
|
9
|
+
HOST_OK = "!$&'()*+,;=:<>\"[]"
|
|
10
|
+
GEN_DELIMS = "$&+,/:;=?@"
|
|
11
|
+
UNRESERVED_PUNCT = "-_.~"
|
|
12
|
+
FRAG_OK = "!()*"
|
|
13
|
+
USERINFO_PUNCT = "-._:~!$&'()*+,;=%@"
|
|
14
|
+
VALID_ENC_EXTRA = "!$&'()*+,;=:@[]%"
|
|
15
|
+
|
|
16
|
+
HOST_OK_BYTES = HOST_OK.bytes.to_h { |b| [b, true] }.freeze
|
|
17
|
+
GEN_DELIMS_BYTES = GEN_DELIMS.bytes.to_h { |b| [b, true] }.freeze
|
|
18
|
+
UNRESERVED_BYTES = UNRESERVED_PUNCT.bytes.to_h { |b| [b, true] }.freeze
|
|
19
|
+
FRAG_OK_BYTES = FRAG_OK.bytes.to_h { |b| [b, true] }.freeze
|
|
20
|
+
USERINFO_BYTES = USERINFO_PUNCT.bytes.to_h { |b| [b, true] }.freeze
|
|
21
|
+
VALID_ENC_BYTES = VALID_ENC_EXTRA.bytes.to_h { |b| [b, true] }.freeze
|
|
22
|
+
|
|
23
|
+
class InvalidURLError < StandardError; end
|
|
24
|
+
|
|
25
|
+
ParsedUrl = Struct.new(:scheme, :opaque_text, :has_user, :host, :path, :raw_path, :omit_host)
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
def alnum?(c)
|
|
30
|
+
c.between?(0x30, 0x39) || c.between?(0x41, 0x5A) || c.between?(0x61, 0x7A)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def hex?(c)
|
|
34
|
+
c.between?(0x30, 0x39) || c.between?(0x41, 0x46) || c.between?(0x61, 0x66)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def unhex(c)
|
|
38
|
+
return c - 0x30 if c.between?(0x30, 0x39)
|
|
39
|
+
return c - 0x61 + 10 if c.between?(0x61, 0x66)
|
|
40
|
+
|
|
41
|
+
c - 0x41 + 10
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def should_escape?(c, mode)
|
|
45
|
+
return false if alnum?(c)
|
|
46
|
+
return false if %i[host zone].include?(mode) && HOST_OK_BYTES[c]
|
|
47
|
+
return false if UNRESERVED_BYTES[c]
|
|
48
|
+
|
|
49
|
+
if GEN_DELIMS_BYTES[c]
|
|
50
|
+
return c == 0x3F if mode == :path
|
|
51
|
+
return [0x40, 0x2F, 0x3F, 0x3A].include?(c) if mode == :user_password
|
|
52
|
+
|
|
53
|
+
return mode != :fragment
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
!(mode == :fragment && FRAG_OK_BYTES[c])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def unescape(s, mode)
|
|
60
|
+
n = 0
|
|
61
|
+
i = 0
|
|
62
|
+
len = s.bytesize
|
|
63
|
+
while i < len
|
|
64
|
+
c = s.getbyte(i)
|
|
65
|
+
if c == 0x25
|
|
66
|
+
n += 1
|
|
67
|
+
raise InvalidURLError if i + 2 >= len || !hex?(s.getbyte(i + 1)) || !hex?(s.getbyte(i + 2))
|
|
68
|
+
|
|
69
|
+
pct25 = s.byteslice(i, 3) == "%25"
|
|
70
|
+
raise InvalidURLError if mode == :host && unhex(s.getbyte(i + 1)) < 8 && !pct25
|
|
71
|
+
|
|
72
|
+
if mode == :zone
|
|
73
|
+
v = (unhex(s.getbyte(i + 1)) << 4) | unhex(s.getbyte(i + 2))
|
|
74
|
+
raise InvalidURLError if !pct25 && v != 0x20 && should_escape?(v, :host)
|
|
75
|
+
end
|
|
76
|
+
i += 3
|
|
77
|
+
else
|
|
78
|
+
raise InvalidURLError if %i[host zone].include?(mode) && c < 0x80 && should_escape?(c, mode)
|
|
79
|
+
|
|
80
|
+
i += 1
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
return s if n.zero?
|
|
84
|
+
|
|
85
|
+
out = String.new(encoding: Encoding::BINARY, capacity: len - (2 * n))
|
|
86
|
+
i = 0
|
|
87
|
+
while i < len
|
|
88
|
+
if s.getbyte(i) == 0x25
|
|
89
|
+
out << ((unhex(s.getbyte(i + 1)) << 4) | unhex(s.getbyte(i + 2)))
|
|
90
|
+
i += 3
|
|
91
|
+
else
|
|
92
|
+
out << s.getbyte(i)
|
|
93
|
+
i += 1
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
out
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def escape(s, mode)
|
|
100
|
+
count = 0
|
|
101
|
+
s.each_byte { |c| count += 1 if should_escape?(c, mode) }
|
|
102
|
+
return s if count.zero?
|
|
103
|
+
|
|
104
|
+
out = String.new(encoding: Encoding::BINARY, capacity: s.bytesize + (2 * count))
|
|
105
|
+
s.each_byte do |c|
|
|
106
|
+
if should_escape?(c, mode)
|
|
107
|
+
out << 0x25 << HEX_UPPER.getbyte(c >> 4) << HEX_UPPER.getbyte(c & 0x0F)
|
|
108
|
+
else
|
|
109
|
+
out << c
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
out
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def valid_encoded?(s, mode)
|
|
116
|
+
s.each_byte do |c|
|
|
117
|
+
next if VALID_ENC_BYTES[c]
|
|
118
|
+
return false if should_escape?(c, mode)
|
|
119
|
+
end
|
|
120
|
+
true
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def valid_optional_port?(port)
|
|
124
|
+
return true if port.empty?
|
|
125
|
+
return false unless port.getbyte(0) == 0x3A
|
|
126
|
+
|
|
127
|
+
i = 1
|
|
128
|
+
while i < port.bytesize
|
|
129
|
+
c = port.getbyte(i)
|
|
130
|
+
return false unless c.between?(0x30, 0x39)
|
|
131
|
+
|
|
132
|
+
i += 1
|
|
133
|
+
end
|
|
134
|
+
true
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def valid_userinfo?(s)
|
|
138
|
+
s.each_byte do |c|
|
|
139
|
+
return false unless alnum?(c) || USERINFO_BYTES[c]
|
|
140
|
+
end
|
|
141
|
+
true
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def get_scheme(raw)
|
|
145
|
+
i = 0
|
|
146
|
+
len = raw.bytesize
|
|
147
|
+
while i < len
|
|
148
|
+
c = raw.getbyte(i)
|
|
149
|
+
if c.between?(0x41, 0x5A) || c.between?(0x61, 0x7A)
|
|
150
|
+
i += 1
|
|
151
|
+
next
|
|
152
|
+
elsif c.between?(0x30, 0x39) || c == 0x2B || c == 0x2D || c == 0x2E
|
|
153
|
+
return ["".b, raw] if i.zero?
|
|
154
|
+
|
|
155
|
+
i += 1
|
|
156
|
+
next
|
|
157
|
+
elsif c == 0x3A
|
|
158
|
+
raise InvalidURLError if i.zero?
|
|
159
|
+
|
|
160
|
+
return [raw.byteslice(0, i), raw.byteslice(i + 1, len - i - 1)]
|
|
161
|
+
else
|
|
162
|
+
return ["".b, raw]
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
["".b, raw]
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def valid_ipv4?(s)
|
|
169
|
+
val = 0
|
|
170
|
+
pos = 0
|
|
171
|
+
dig_len = 0
|
|
172
|
+
len = s.bytesize
|
|
173
|
+
i = 0
|
|
174
|
+
while i < len
|
|
175
|
+
c = s.getbyte(i)
|
|
176
|
+
if c.between?(0x30, 0x39)
|
|
177
|
+
return false if dig_len == 1 && val.zero?
|
|
178
|
+
|
|
179
|
+
val = (val * 10) + (c - 0x30)
|
|
180
|
+
dig_len += 1
|
|
181
|
+
return false if val > 255
|
|
182
|
+
elsif c == 0x2E
|
|
183
|
+
return false if i.zero? || i == len - 1 || s.getbyte(i - 1) == 0x2E
|
|
184
|
+
return false if pos == 3
|
|
185
|
+
|
|
186
|
+
pos += 1
|
|
187
|
+
val = 0
|
|
188
|
+
dig_len = 0
|
|
189
|
+
else
|
|
190
|
+
return false
|
|
191
|
+
end
|
|
192
|
+
i += 1
|
|
193
|
+
end
|
|
194
|
+
pos == 3
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def parse_ipv6?(data)
|
|
198
|
+
s = data
|
|
199
|
+
pct = s.index("%".b)
|
|
200
|
+
if pct
|
|
201
|
+
return false if pct + 1 == s.bytesize
|
|
202
|
+
|
|
203
|
+
s = s.byteslice(0, pct)
|
|
204
|
+
end
|
|
205
|
+
ellipsis = nil
|
|
206
|
+
if s.bytesize >= 2 && s.getbyte(0) == 0x3A && s.getbyte(1) == 0x3A
|
|
207
|
+
ellipsis = 0
|
|
208
|
+
s = s.byteslice(2, s.bytesize - 2)
|
|
209
|
+
return true if s.empty?
|
|
210
|
+
end
|
|
211
|
+
i = 0
|
|
212
|
+
while i < 16
|
|
213
|
+
off = 0
|
|
214
|
+
acc = 0
|
|
215
|
+
while off < s.bytesize
|
|
216
|
+
c = s.getbyte(off)
|
|
217
|
+
break unless hex?(c)
|
|
218
|
+
|
|
219
|
+
acc = (acc << 4) + unhex(c)
|
|
220
|
+
return false if off > 3 || acc > 0xFFFF
|
|
221
|
+
|
|
222
|
+
off += 1
|
|
223
|
+
end
|
|
224
|
+
return false if off.zero?
|
|
225
|
+
|
|
226
|
+
if off < s.bytesize && s.getbyte(off) == 0x2E
|
|
227
|
+
return false if ellipsis.nil? && i != 12
|
|
228
|
+
return false if i + 4 > 16 || !valid_ipv4?(s)
|
|
229
|
+
|
|
230
|
+
s = "".b
|
|
231
|
+
i += 4
|
|
232
|
+
break
|
|
233
|
+
end
|
|
234
|
+
i += 2
|
|
235
|
+
s = s.byteslice(off, s.bytesize - off)
|
|
236
|
+
break if s.empty?
|
|
237
|
+
return false if s.getbyte(0) != 0x3A || s.bytesize == 1
|
|
238
|
+
|
|
239
|
+
s = s.byteslice(1, s.bytesize - 1)
|
|
240
|
+
next unless s.getbyte(0) == 0x3A
|
|
241
|
+
return false unless ellipsis.nil?
|
|
242
|
+
|
|
243
|
+
ellipsis = i
|
|
244
|
+
s = s.byteslice(1, s.bytesize - 1)
|
|
245
|
+
break if s.empty?
|
|
246
|
+
end
|
|
247
|
+
return false unless s.empty?
|
|
248
|
+
|
|
249
|
+
(i < 16) == !ellipsis.nil?
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def ipv6_literal?(s)
|
|
253
|
+
s.each_byte do |c|
|
|
254
|
+
return false if [0x2E, 0x25].include?(c)
|
|
255
|
+
return parse_ipv6?(s) if c == 0x3A
|
|
256
|
+
end
|
|
257
|
+
false
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def parse_host(scheme, host)
|
|
261
|
+
open_idx = host.rindex("[".b)
|
|
262
|
+
if open_idx
|
|
263
|
+
raise InvalidURLError if open_idx.positive?
|
|
264
|
+
|
|
265
|
+
close_idx = host.rindex("]".b)
|
|
266
|
+
raise InvalidURLError unless close_idx
|
|
267
|
+
|
|
268
|
+
colon_port = host.byteslice(close_idx + 1, host.bytesize - close_idx - 1)
|
|
269
|
+
raise InvalidURLError unless valid_optional_port?(colon_port)
|
|
270
|
+
|
|
271
|
+
port = unescape(colon_port, :host)
|
|
272
|
+
hostname = host.byteslice(1, close_idx - 1)
|
|
273
|
+
zone = hostname.index("%25".b)
|
|
274
|
+
unescaped = if zone
|
|
275
|
+
unescape(hostname.byteslice(0, zone), :host) +
|
|
276
|
+
unescape(hostname.byteslice(zone, hostname.bytesize - zone), :zone)
|
|
277
|
+
else
|
|
278
|
+
unescape(hostname, :host)
|
|
279
|
+
end
|
|
280
|
+
raise InvalidURLError unless ipv6_literal?(unescaped)
|
|
281
|
+
|
|
282
|
+
return "[".b + unescaped + "]".b + port
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
first = host.index(":".b)
|
|
286
|
+
if first
|
|
287
|
+
idx = first
|
|
288
|
+
last = host.rindex(":".b)
|
|
289
|
+
idx = last if last != first && scheme != "http" && scheme != "https"
|
|
290
|
+
raise InvalidURLError unless valid_optional_port?(host.byteslice(idx, host.bytesize - idx))
|
|
291
|
+
end
|
|
292
|
+
unescape(host, :host)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def parse_authority(url, authority)
|
|
296
|
+
at = authority.rindex("@".b)
|
|
297
|
+
host_part = at ? authority.byteslice(at + 1, authority.bytesize - at - 1) : authority
|
|
298
|
+
url.host = parse_host(url.scheme, host_part)
|
|
299
|
+
return unless at
|
|
300
|
+
|
|
301
|
+
userinfo = authority.byteslice(0, at)
|
|
302
|
+
raise InvalidURLError unless valid_userinfo?(userinfo)
|
|
303
|
+
|
|
304
|
+
colon = userinfo.index(":".b)
|
|
305
|
+
if colon
|
|
306
|
+
unescape(userinfo.byteslice(0, colon), :user_password)
|
|
307
|
+
unescape(userinfo.byteslice(colon + 1, userinfo.bytesize - colon - 1), :user_password)
|
|
308
|
+
else
|
|
309
|
+
unescape(userinfo, :user_password)
|
|
310
|
+
end
|
|
311
|
+
url.has_user = true
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def parse_reference(raw)
|
|
315
|
+
raw.each_byte { |c| raise InvalidURLError if c < 0x20 || c == 0x7F }
|
|
316
|
+
url = ParsedUrl.new("", "".b, false, "".b, "".b, "".b, false)
|
|
317
|
+
if raw == "*".b
|
|
318
|
+
url.path = "*".b
|
|
319
|
+
return url
|
|
320
|
+
end
|
|
321
|
+
scheme_bytes, rest = get_scheme(raw)
|
|
322
|
+
url.scheme = scheme_bytes.force_encoding(Encoding::US_ASCII).downcase
|
|
323
|
+
if rest.end_with?("?".b) && rest.count("?".b) == 1
|
|
324
|
+
rest = rest.byteslice(0, rest.bytesize - 1)
|
|
325
|
+
elsif (q = rest.index("?".b))
|
|
326
|
+
rest = rest.byteslice(0, q)
|
|
327
|
+
end
|
|
328
|
+
unless rest.start_with?("/".b)
|
|
329
|
+
unless url.scheme.empty?
|
|
330
|
+
url.opaque_text = rest
|
|
331
|
+
return url
|
|
332
|
+
end
|
|
333
|
+
slash = rest.index("/".b)
|
|
334
|
+
segment = slash ? rest.byteslice(0, slash) : rest
|
|
335
|
+
raise InvalidURLError if segment.include?(":".b)
|
|
336
|
+
end
|
|
337
|
+
if (!url.scheme.empty? || !rest.start_with?("///".b)) && rest.start_with?("//".b)
|
|
338
|
+
authority = rest.byteslice(2, rest.bytesize - 2)
|
|
339
|
+
rest = "".b
|
|
340
|
+
if (slash = authority.index("/".b))
|
|
341
|
+
rest = authority.byteslice(slash, authority.bytesize - slash)
|
|
342
|
+
authority = authority.byteslice(0, slash)
|
|
343
|
+
end
|
|
344
|
+
parse_authority(url, authority)
|
|
345
|
+
elsif !url.scheme.empty? && rest.start_with?("/".b)
|
|
346
|
+
url.omit_host = true
|
|
347
|
+
end
|
|
348
|
+
url.path = unescape(rest, :path)
|
|
349
|
+
escaped = escape(url.path, :path)
|
|
350
|
+
url.raw_path = rest == escaped ? "".b : rest
|
|
351
|
+
url
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def parse(raw)
|
|
355
|
+
h = raw.index("#".b)
|
|
356
|
+
url = parse_reference(h ? raw.byteslice(0, h) : raw)
|
|
357
|
+
unescape(raw.byteslice(h + 1, raw.bytesize - h - 1), :fragment) if h && h + 1 < raw.bytesize
|
|
358
|
+
url
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def escaped_path(url)
|
|
362
|
+
if !url.raw_path.empty? && valid_encoded?(url.raw_path, :path)
|
|
363
|
+
begin
|
|
364
|
+
p = unescape(url.raw_path, :path)
|
|
365
|
+
rescue InvalidURLError
|
|
366
|
+
p = nil
|
|
367
|
+
end
|
|
368
|
+
return url.raw_path if p == url.path
|
|
369
|
+
end
|
|
370
|
+
return "*".b if url.path == "*".b
|
|
371
|
+
|
|
372
|
+
escape(url.path, :path)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
def format_url(url)
|
|
376
|
+
out = "".b
|
|
377
|
+
out << url.scheme.b << ":".b unless url.scheme.empty?
|
|
378
|
+
unless url.opaque_text.empty?
|
|
379
|
+
out << url.opaque_text
|
|
380
|
+
return out
|
|
381
|
+
end
|
|
382
|
+
if (!url.scheme.empty? || !url.host.empty?) && !(url.omit_host && url.host.empty?)
|
|
383
|
+
out << "//".b if !url.host.empty? || !url.path.empty?
|
|
384
|
+
out << escape(url.host, :host) unless url.host.empty?
|
|
385
|
+
end
|
|
386
|
+
path = escaped_path(url)
|
|
387
|
+
out << "/".b if !path.empty? && path.getbyte(0) != 0x2F && !url.host.empty?
|
|
388
|
+
if out.empty?
|
|
389
|
+
slash = path.index("/".b)
|
|
390
|
+
segment = slash ? path.byteslice(0, slash) : path
|
|
391
|
+
out << "./".b if segment.include?(":".b)
|
|
392
|
+
end
|
|
393
|
+
out << path
|
|
394
|
+
out
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
def sanitize(raw)
|
|
398
|
+
sanitize_bytes(raw.to_s.encode(Encoding::UTF_8).b)
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def sanitize_bytes(raw)
|
|
402
|
+
schemeless = !raw.include?("://".b)
|
|
403
|
+
data = schemeless ? "//".b + raw : raw
|
|
404
|
+
begin
|
|
405
|
+
url = parse(data)
|
|
406
|
+
rescue InvalidURLError
|
|
407
|
+
return WITHHELD
|
|
408
|
+
end
|
|
409
|
+
return WITHHELD if url.host.empty?
|
|
410
|
+
|
|
411
|
+
ats = data.count("@".b)
|
|
412
|
+
return WITHHELD if ats > 1 || (ats == 1 && !url.has_user)
|
|
413
|
+
|
|
414
|
+
out = format_url(url)
|
|
415
|
+
return WITHHELD if out.include?("@".b)
|
|
416
|
+
|
|
417
|
+
out = out.byteslice(2, out.bytesize - 2) if schemeless && out.start_with?("//".b)
|
|
418
|
+
out.force_encoding(Encoding::US_ASCII)
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module Libo11y
|
|
6
|
+
module Internal
|
|
7
|
+
module Env
|
|
8
|
+
SUPPORTED_EXPORTERS = %w[otlp console none].freeze
|
|
9
|
+
SUPPORTED_PROTOCOLS = %w[http/protobuf].freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def resolve_map(env_opt)
|
|
14
|
+
return {} if env_opt.equal?(Libo11y::HERMETIC)
|
|
15
|
+
return ENV.to_h if env_opt.nil?
|
|
16
|
+
|
|
17
|
+
env_opt.transform_keys(&:to_s).transform_values(&:to_s)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def hermetic?(env_opt)
|
|
21
|
+
env_opt.equal?(Libo11y::HERMETIC)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def sdk_disabled?(env_map)
|
|
25
|
+
env_map["OTEL_SDK_DISABLED"].to_s.strip.downcase == "true"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def parse_resource_attributes(raw)
|
|
29
|
+
return {} if raw.nil? || raw.strip.empty?
|
|
30
|
+
|
|
31
|
+
out = {}
|
|
32
|
+
raw.split(",").each do |pair|
|
|
33
|
+
k, v = pair.split("=", 2)
|
|
34
|
+
next if k.nil? || v.nil?
|
|
35
|
+
|
|
36
|
+
key = k.strip
|
|
37
|
+
next if key.empty?
|
|
38
|
+
|
|
39
|
+
out[key] = decode_percent(v.strip)
|
|
40
|
+
end
|
|
41
|
+
out
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def decode_percent(str)
|
|
45
|
+
str.gsub(/%([0-9A-Fa-f]{2})/) { Regexp.last_match(1).to_i(16).chr(Encoding::UTF_8) }
|
|
46
|
+
rescue StandardError
|
|
47
|
+
str
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def parse_headers(raw)
|
|
51
|
+
return {} if raw.nil? || raw.strip.empty?
|
|
52
|
+
|
|
53
|
+
out = {}
|
|
54
|
+
raw.split(",").each do |pair|
|
|
55
|
+
k, v = pair.split("=", 2)
|
|
56
|
+
next if k.nil? || v.nil?
|
|
57
|
+
|
|
58
|
+
key = k.strip
|
|
59
|
+
next if key.empty?
|
|
60
|
+
|
|
61
|
+
out[key] = decode_percent(v.strip)
|
|
62
|
+
end
|
|
63
|
+
out
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def parse_exporter_choice(env_map, var_name, default:)
|
|
67
|
+
raw = env_map[var_name]
|
|
68
|
+
return default if raw.nil? || raw.strip.empty?
|
|
69
|
+
|
|
70
|
+
first = raw.split(",").first.to_s.strip.downcase
|
|
71
|
+
raise UnsupportedProtocol, "o11y: unsupported exporter #{first.inspect} in #{var_name}" unless SUPPORTED_EXPORTERS.include?(first)
|
|
72
|
+
|
|
73
|
+
first
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def parse_protocol(env_map, signal_var)
|
|
77
|
+
raw = env_map[signal_var] || env_map["OTEL_EXPORTER_OTLP_PROTOCOL"]
|
|
78
|
+
return "http/protobuf" if raw.nil? || raw.strip.empty?
|
|
79
|
+
|
|
80
|
+
proto = raw.strip.downcase
|
|
81
|
+
unless SUPPORTED_PROTOCOLS.include?(proto)
|
|
82
|
+
raise UnsupportedProtocol, "o11y: unsupported OTLP protocol #{proto.inspect} (supported: http/protobuf)"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
proto
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def signal_endpoint(env_map, signal_var, path_suffix)
|
|
89
|
+
specific = env_map[signal_var]
|
|
90
|
+
return specific.strip unless specific.nil? || specific.strip.empty?
|
|
91
|
+
|
|
92
|
+
base = env_map["OTEL_EXPORTER_OTLP_ENDPOINT"]
|
|
93
|
+
base = "http://localhost:4318" if base.nil? || base.strip.empty?
|
|
94
|
+
"#{base.strip.sub(%r{/+\z}, "")}#{path_suffix}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def signal_headers(env_map, signal_var)
|
|
98
|
+
base = parse_headers(env_map["OTEL_EXPORTER_OTLP_HEADERS"])
|
|
99
|
+
specific = parse_headers(env_map[signal_var])
|
|
100
|
+
base.merge(specific)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def signal_timeout(env_map, signal_var, default: 10.0)
|
|
104
|
+
raw = env_map[signal_var] || env_map["OTEL_EXPORTER_OTLP_TIMEOUT"]
|
|
105
|
+
return default if raw.nil? || raw.strip.empty?
|
|
106
|
+
|
|
107
|
+
ms = Float(raw.strip)
|
|
108
|
+
ms.positive? ? ms / 1000.0 : default
|
|
109
|
+
rescue ArgumentError
|
|
110
|
+
default
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def signal_compression(env_map, signal_var)
|
|
114
|
+
raw = env_map[signal_var] || env_map["OTEL_EXPORTER_OTLP_COMPRESSION"]
|
|
115
|
+
return "gzip" if raw.nil? || raw.strip.empty?
|
|
116
|
+
|
|
117
|
+
val = raw.strip.downcase
|
|
118
|
+
return "gzip" if val == "gzip"
|
|
119
|
+
return "none" if val == "none"
|
|
120
|
+
|
|
121
|
+
raise UnsupportedProtocol, "o11y: unsupported OTLP compression #{val.inspect}"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def signal_tls(env_map, prefix)
|
|
125
|
+
{
|
|
126
|
+
certificate_file: env_map["#{prefix}_CERTIFICATE"] || env_map["OTEL_EXPORTER_OTLP_CERTIFICATE"],
|
|
127
|
+
client_certificate_file: env_map["#{prefix}_CLIENT_CERTIFICATE"] || env_map["OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE"],
|
|
128
|
+
client_key_file: env_map["#{prefix}_CLIENT_KEY"] || env_map["OTEL_EXPORTER_OTLP_CLIENT_KEY"]
|
|
129
|
+
}.compact
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def parse_exemplar_filter(opt_filter, env_map)
|
|
133
|
+
return opt_filter unless opt_filter.nil?
|
|
134
|
+
|
|
135
|
+
raw = env_map["OTEL_METRICS_EXEMPLAR_FILTER"]
|
|
136
|
+
return ExemplarFilter::TRACE_BASED if raw.nil? || raw.strip.empty?
|
|
137
|
+
|
|
138
|
+
case raw.strip.downcase
|
|
139
|
+
when "trace_based" then ExemplarFilter::TRACE_BASED
|
|
140
|
+
when "always_on" then ExemplarFilter::ALWAYS_ON
|
|
141
|
+
when "always_off" then ExemplarFilter::ALWAYS_OFF
|
|
142
|
+
else
|
|
143
|
+
raise ExporterError, "o11y: unsupported exemplar filter #{raw.inspect}"
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def parse_int(env_map, key, default)
|
|
148
|
+
raw = env_map[key]
|
|
149
|
+
return default if raw.nil? || raw.strip.empty?
|
|
150
|
+
|
|
151
|
+
val = Integer(raw.strip, 10)
|
|
152
|
+
val.positive? ? val : default
|
|
153
|
+
rescue ArgumentError
|
|
154
|
+
default
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module Libo11y
|
|
7
|
+
module Internal
|
|
8
|
+
module Identity
|
|
9
|
+
INSTANCE_ID_NAMESPACE_BYTES = ["4d63009a8d0f11eeaad74c796ed8e320"].pack("H*").freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def uuid_v5(namespace_bytes, name)
|
|
14
|
+
hash = Digest::SHA1.digest(namespace_bytes + name.encode(Encoding::UTF_8).b)
|
|
15
|
+
bytes = hash.byteslice(0, 16).bytes
|
|
16
|
+
bytes[6] = (bytes[6] & 0x0F) | 0x50
|
|
17
|
+
bytes[8] = (bytes[8] & 0x3F) | 0x80
|
|
18
|
+
hex = bytes.pack("C*").unpack1("H*")
|
|
19
|
+
"#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def derive_instance_id(pod_uid: "", pod_name: "", namespace: "")
|
|
23
|
+
return uuid_v5(INSTANCE_ID_NAMESPACE_BYTES, pod_uid) unless pod_uid.nil? || pod_uid.empty?
|
|
24
|
+
return uuid_v5(INSTANCE_ID_NAMESPACE_BYTES, "#{namespace}/#{pod_name}") unless pod_name.nil? || pod_name.empty?
|
|
25
|
+
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def random_instance_id
|
|
30
|
+
SecureRandom.uuid
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|