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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +170 -21
- data/README.md +77 -8
- data/lib/libo11y/{proc.rb → child_process.rb} +12 -2
- data/lib/libo11y/db.rb +22 -10
- data/lib/libo11y/detached.rb +241 -29
- data/lib/libo11y/http_client.rb +48 -15
- data/lib/libo11y/internal/bridges.rb +3553 -255
- data/lib/libo11y/internal/endpoint.rb +7 -1
- data/lib/libo11y/internal/env.rb +177 -59
- data/lib/libo11y/internal/error_type.rb +17 -0
- data/lib/libo11y/internal/histogram_buckets.rb +42 -0
- data/lib/libo11y/internal/http_common.rb +2 -0
- data/lib/libo11y/internal/identity.rb +10 -1
- data/lib/libo11y/internal/injected.rb +178 -0
- data/lib/libo11y/internal/logging_device.rb +141 -32
- data/lib/libo11y/internal/path_match.rb +148 -0
- data/lib/libo11y/internal/pii.rb +185 -65
- data/lib/libo11y/internal/pipeline.rb +141 -34
- data/lib/libo11y/internal/policy.rb +556 -52
- data/lib/libo11y/internal/propagators.rb +29 -6
- data/lib/libo11y/internal/quiet_inspect.rb +20 -0
- data/lib/libo11y/internal/resource.rb +187 -52
- data/lib/libo11y/internal/sdk_load.rb +64 -0
- data/lib/libo11y/internal/sql_text.rb +365 -67
- data/lib/libo11y/internal/summary.rb +2 -1
- data/lib/libo11y/options.rb +159 -1
- data/lib/libo11y/rack.rb +257 -44
- data/lib/libo11y/record.rb +70 -7
- data/lib/libo11y/redact.rb +87 -21
- data/lib/libo11y/signals.rb +70 -29
- data/lib/libo11y/spec/app_registry.rb +97 -61
- data/lib/libo11y/spec/yaml_v3.rb +179 -111
- data/lib/libo11y/telemetry.rb +315 -53
- data/lib/libo11y/testing.rb +31 -19
- data/lib/libo11y/version.rb +1 -1
- data/lib/libo11y.rb +1 -1
- metadata +94 -39
data/lib/libo11y/detached.rb
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "opentelemetry"
|
|
4
|
+
require "uri"
|
|
4
5
|
require_relative "errors"
|
|
6
|
+
require_relative "internal/pii"
|
|
5
7
|
|
|
6
8
|
module Libo11y
|
|
7
9
|
# Trace propagation for work that runs detached from the request that
|
|
@@ -12,6 +14,26 @@ module Libo11y
|
|
|
12
14
|
# The longest text form of a carrier, in bytes.
|
|
13
15
|
MAX_CARRIER_BYTES = 32 * 1024
|
|
14
16
|
|
|
17
|
+
# Trace context extraction and carrier text encoding shared by Carrier's
|
|
18
|
+
# class and instance methods.
|
|
19
|
+
module CarrierCodec
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def extract_span_context(traceparent, tracestate)
|
|
23
|
+
headers = { "traceparent" => traceparent, "tracestate" => tracestate }.reject { |_, v| v.empty? }
|
|
24
|
+
context = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator.extract(headers, context: OpenTelemetry::Context.empty)
|
|
25
|
+
OpenTelemetry::Trace.current_span(context).context
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def encode_text(traceparent, tracestate, baggage)
|
|
29
|
+
return "" if traceparent.empty?
|
|
30
|
+
|
|
31
|
+
fields = { "baggage" => baggage, "traceparent" => traceparent, "tracestate" => tracestate }
|
|
32
|
+
fields.reject { |_, value| value.empty? }.map { |key, value| "#{key}=#{Query.escape(value)}" }.join("&")
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
private_constant :CarrierCodec
|
|
36
|
+
|
|
15
37
|
# A captured span context and baggage. Its text form is the URL-encoded
|
|
16
38
|
# query every libo11y implementation writes, so a carrier a Ruby service
|
|
17
39
|
# captured starts a linked span in a Go or Python worker, and the other
|
|
@@ -32,7 +54,7 @@ module Libo11y
|
|
|
32
54
|
end
|
|
33
55
|
|
|
34
56
|
fields = Query.parse(text)
|
|
35
|
-
context =
|
|
57
|
+
context = CarrierCodec.extract_span_context(fields["traceparent"].to_s, fields["tracestate"].to_s)
|
|
36
58
|
raise InvalidCarrier, "o11y: invalid carrier: no valid traceparent" unless context.valid?
|
|
37
59
|
|
|
38
60
|
from_span_context(context, Baggage.valid_members(fields["baggage"].to_s))
|
|
@@ -47,16 +69,23 @@ module Libo11y
|
|
|
47
69
|
context = OpenTelemetry::Trace.context_with_span(span, parent_context: OpenTelemetry::Context.empty)
|
|
48
70
|
OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator.inject(headers, context: context)
|
|
49
71
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
72
|
+
tp = headers["traceparent"].to_s
|
|
73
|
+
ts = headers["tracestate"].to_s
|
|
74
|
+
bag = baggage.to_s
|
|
75
|
+
ts = "" if CarrierCodec.encode_text(tp, ts, bag).bytesize > MAX_CARRIER_BYTES
|
|
76
|
+
bag = "" if CarrierCodec.encode_text(tp, ts, bag).bytesize > MAX_CARRIER_BYTES
|
|
77
|
+
new(tp, ts, bag)
|
|
54
78
|
end
|
|
55
79
|
|
|
56
80
|
def initialize(traceparent, tracestate, baggage)
|
|
57
|
-
@traceparent = traceparent.dup.freeze
|
|
58
|
-
@tracestate = tracestate.dup.freeze
|
|
59
|
-
@baggage = baggage.dup.freeze
|
|
81
|
+
@traceparent = traceparent.to_s.dup.freeze
|
|
82
|
+
@tracestate = tracestate.to_s.dup.freeze
|
|
83
|
+
@baggage = baggage.to_s.dup.freeze
|
|
84
|
+
encoded_size = marshal_text.bytesize
|
|
85
|
+
if encoded_size > MAX_CARRIER_BYTES
|
|
86
|
+
raise InvalidCarrier, "o11y: invalid carrier: #{encoded_size} bytes exceeds the #{MAX_CARRIER_BYTES} byte limit"
|
|
87
|
+
end
|
|
88
|
+
|
|
60
89
|
freeze
|
|
61
90
|
end
|
|
62
91
|
|
|
@@ -67,17 +96,12 @@ module Libo11y
|
|
|
67
96
|
|
|
68
97
|
# The captured span context. It is invalid for the zero carrier.
|
|
69
98
|
def span_context
|
|
70
|
-
|
|
71
|
-
context = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator.extract(headers, context: OpenTelemetry::Context.empty)
|
|
72
|
-
OpenTelemetry::Trace.current_span(context).context
|
|
99
|
+
CarrierCodec.extract_span_context(@traceparent, @tracestate)
|
|
73
100
|
end
|
|
74
101
|
|
|
75
102
|
# The text form: a URL-encoded query with its keys in order.
|
|
76
103
|
def marshal_text
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
fields = { "baggage" => @baggage, "traceparent" => @traceparent, "tracestate" => @tracestate }
|
|
80
|
-
fields.reject { |_, value| value.empty? }.map { |key, value| "#{key}=#{Query.escape(value)}" }.join("&")
|
|
104
|
+
CarrierCodec.encode_text(@traceparent, @tracestate, @baggage)
|
|
81
105
|
end
|
|
82
106
|
alias to_s marshal_text
|
|
83
107
|
|
|
@@ -116,33 +140,214 @@ module Libo11y
|
|
|
116
140
|
next if pair.empty?
|
|
117
141
|
|
|
118
142
|
key, value = pair.split("=", 2)
|
|
119
|
-
|
|
143
|
+
# Every value is unescaped, so a bad escape in a repeated field is
|
|
144
|
+
# refused, as Go's url.ParseQuery refuses it.
|
|
145
|
+
unescaped = unescape(value.to_s)
|
|
146
|
+
fields[unescape(key)] ||= unescaped
|
|
120
147
|
end
|
|
121
148
|
end
|
|
122
149
|
end
|
|
123
150
|
private_constant :Query
|
|
124
151
|
|
|
125
|
-
# W3C baggage header values.
|
|
152
|
+
# W3C baggage header values, read as Go's validBaggage reads them.
|
|
126
153
|
module Baggage
|
|
127
|
-
|
|
128
|
-
|
|
154
|
+
MAX_BYTES = 8192
|
|
155
|
+
MAX_MEMBERS = 64
|
|
156
|
+
KEY = /\A[!#$%&'*+\-.^_`|~0-9A-Za-z]+\z/
|
|
157
|
+
VALUE = /\A[\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]*\z/
|
|
158
|
+
VALUE_ESCAPE = /[^\x21\x23\x24\x26-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]/n
|
|
159
|
+
BAD_PERCENT = /%(?![0-9A-Fa-f]{2})/
|
|
160
|
+
private_constant :KEY, :VALUE, :VALUE_ESCAPE, :BAD_PERCENT
|
|
129
161
|
|
|
130
162
|
module_function
|
|
131
163
|
|
|
164
|
+
# W3C optional whitespace is a space or a tab. String#strip would also
|
|
165
|
+
# take a newline or a NUL, and so let one into a member.
|
|
166
|
+
def trim_ows(text)
|
|
167
|
+
start = 0
|
|
168
|
+
stop = text.bytesize
|
|
169
|
+
start += 1 while start < stop && [32, 9].include?(text.getbyte(start))
|
|
170
|
+
stop -= 1 while stop > start && [32, 9].include?(text.getbyte(stop - 1))
|
|
171
|
+
text.byteslice(start, stop - start)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Replaces each invalid UTF-8 byte individually with U+FFFD, matching
|
|
175
|
+
# Go's utf8.DecodeRuneInString loop in baggage.Parse.
|
|
176
|
+
def replace_invalid_utf8_bytes(str)
|
|
177
|
+
return str if str.encoding == Encoding::UTF_8 && str.valid_encoding?
|
|
178
|
+
|
|
179
|
+
bytes = str.b
|
|
180
|
+
len = bytes.bytesize
|
|
181
|
+
out = +"".b
|
|
182
|
+
idx = 0
|
|
183
|
+
while idx < len
|
|
184
|
+
byte = bytes.getbyte(idx)
|
|
185
|
+
if byte < 0x80
|
|
186
|
+
out << byte
|
|
187
|
+
idx += 1
|
|
188
|
+
else
|
|
189
|
+
width = Internal::PII.utf8_char_width(bytes, idx, len)
|
|
190
|
+
if width.zero?
|
|
191
|
+
out << "\xEF\xBF\xBD".b
|
|
192
|
+
idx += 1
|
|
193
|
+
else
|
|
194
|
+
out << bytes.byteslice(idx, width)
|
|
195
|
+
idx += width
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
out.force_encoding(Encoding::UTF_8)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def value_escape(str)
|
|
203
|
+
str.b.gsub(VALUE_ESCAPE) { |byte| format("%%%02X", byte.ord) }
|
|
204
|
+
end
|
|
205
|
+
|
|
132
206
|
# The members of a baggage value that are valid on their own, in
|
|
133
|
-
# order, the
|
|
207
|
+
# order. A value over 8192 bytes is dropped whole, as the W3C baggage
|
|
208
|
+
# propagator drops it. Parsing scans left-to-right like Go's
|
|
209
|
+
# baggage.Parse, stopping before reading past 64 distinct keys or when
|
|
210
|
+
# the re-encoded members exceed 8192 bytes. A header whose members are
|
|
211
|
+
# all valid within those limits is returned unchanged; otherwise the
|
|
212
|
+
# first original segment matching each kept member is returned in order,
|
|
213
|
+
# matching Go's validBaggage.
|
|
134
214
|
def valid_members(header)
|
|
215
|
+
return "" if header.empty? || header.bytesize > MAX_BYTES
|
|
216
|
+
|
|
217
|
+
segments = header.split(",", -1)
|
|
218
|
+
by_key = {}
|
|
219
|
+
sizes = {}
|
|
220
|
+
total_bytes = 0
|
|
221
|
+
had_error = false
|
|
222
|
+
|
|
223
|
+
segments.each do |raw|
|
|
224
|
+
if by_key.size >= MAX_MEMBERS
|
|
225
|
+
had_error = true
|
|
226
|
+
break
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
parsed = parse_member(raw)
|
|
230
|
+
if parsed.nil?
|
|
231
|
+
had_error = true
|
|
232
|
+
next
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
key, canonical = parsed
|
|
236
|
+
member_bytes = canonical.bytesize
|
|
237
|
+
existing = by_key.key?(key)
|
|
238
|
+
member_bytes += 1 if !existing && !by_key.empty?
|
|
239
|
+
new_total = existing ? total_bytes - sizes[key] + member_bytes : total_bytes + member_bytes
|
|
240
|
+
if new_total > MAX_BYTES
|
|
241
|
+
had_error = true
|
|
242
|
+
break
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
by_key[key] = canonical
|
|
246
|
+
sizes[key] = member_bytes
|
|
247
|
+
total_bytes = new_total
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
return "" if by_key.empty?
|
|
251
|
+
return header unless had_error
|
|
252
|
+
|
|
135
253
|
seen = {}
|
|
136
|
-
kept =
|
|
137
|
-
|
|
254
|
+
kept = []
|
|
255
|
+
segments.each do |raw|
|
|
256
|
+
parsed = parse_member(raw)
|
|
257
|
+
next if parsed.nil?
|
|
138
258
|
|
|
139
|
-
key
|
|
140
|
-
next
|
|
259
|
+
key, canonical = parsed
|
|
260
|
+
next if seen[key] || by_key[key] != canonical
|
|
141
261
|
|
|
142
262
|
seen[key] = true
|
|
263
|
+
kept << raw
|
|
143
264
|
end
|
|
144
265
|
kept.join(",")
|
|
145
266
|
end
|
|
267
|
+
|
|
268
|
+
# Restores valid baggage members into context under the same W3C limits
|
|
269
|
+
# and last-duplicate-wins rules as unmarshal_text and Go's restoreBaggage.
|
|
270
|
+
def restore(header, context)
|
|
271
|
+
normalized = valid_members(header)
|
|
272
|
+
return context if normalized.empty?
|
|
273
|
+
|
|
274
|
+
OpenTelemetry::Baggage.build(context: context) do |builder|
|
|
275
|
+
normalized.split(",").each do |raw|
|
|
276
|
+
pair, raw_meta = raw.split(";", 2)
|
|
277
|
+
key, value = pair.to_s.split("=", 2)
|
|
278
|
+
next if value.nil?
|
|
279
|
+
|
|
280
|
+
meta = restore_metadata(raw_meta)
|
|
281
|
+
decoded = replace_invalid_utf8_bytes(URI.decode_uri_component(trim_ows(value)))
|
|
282
|
+
builder.set_value(trim_ows(key), decoded, metadata: meta)
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Strips empty property segments while preserving raw non-empty segments,
|
|
288
|
+
# matching Go's otel/baggage normalizeMetadata for wire filtering and
|
|
289
|
+
# 8192-byte size checks.
|
|
290
|
+
def normalize_metadata(raw_meta)
|
|
291
|
+
return nil if raw_meta.nil? || raw_meta.empty?
|
|
292
|
+
|
|
293
|
+
cleaned = raw_meta.split(";", -1).reject(&:empty?).join(";")
|
|
294
|
+
cleaned.empty? ? nil : cleaned
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Normalizes property whitespace and percent-encoding for restored
|
|
298
|
+
# baggage metadata, matching Go's Property.String().
|
|
299
|
+
def restore_metadata(raw_meta)
|
|
300
|
+
return nil if raw_meta.nil? || raw_meta.empty?
|
|
301
|
+
|
|
302
|
+
cleaned = raw_meta.split(";", -1).filter_map do |segment|
|
|
303
|
+
next if segment.empty?
|
|
304
|
+
|
|
305
|
+
key, value = trim_ows(segment).split("=", 2)
|
|
306
|
+
if value.nil?
|
|
307
|
+
trim_ows(key)
|
|
308
|
+
else
|
|
309
|
+
decoded = replace_invalid_utf8_bytes(URI.decode_uri_component(trim_ows(value)))
|
|
310
|
+
"#{trim_ows(key)}=#{value_escape(decoded)}"
|
|
311
|
+
end
|
|
312
|
+
end.join(";")
|
|
313
|
+
cleaned.empty? ? nil : cleaned
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# Parses one list member into [key, canonical_encoded_string], or nil
|
|
317
|
+
# when the member is not valid W3C baggage.
|
|
318
|
+
def parse_member(raw)
|
|
319
|
+
pair, *properties = raw.split(";", -1)
|
|
320
|
+
key, value = pair.to_s.split("=", 2)
|
|
321
|
+
return nil if value.nil?
|
|
322
|
+
|
|
323
|
+
key = trim_ows(key)
|
|
324
|
+
value = trim_ows(value)
|
|
325
|
+
return nil unless KEY.match?(key) && VALUE.match?(value) && !BAD_PERCENT.match?(value)
|
|
326
|
+
return nil unless properties.all? { |property| valid_property?(property) }
|
|
327
|
+
|
|
328
|
+
decoded = replace_invalid_utf8_bytes(URI.decode_uri_component(value))
|
|
329
|
+
canonical = "#{key}=#{value_escape(decoded)}"
|
|
330
|
+
meta = normalize_metadata(raw.split(";", 2)[1])
|
|
331
|
+
canonical = "#{canonical};#{meta}" if meta
|
|
332
|
+
[key, canonical]
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# A property is literally empty (allowed and stripped by Go's
|
|
336
|
+
# baggage.Parse), a key, or a key and a value. A whitespace-only
|
|
337
|
+
# segment is rejected, matching Go's validateMetadata.
|
|
338
|
+
def valid_property?(property)
|
|
339
|
+
return true if property.empty?
|
|
340
|
+
|
|
341
|
+
trimmed = trim_ows(property)
|
|
342
|
+
return false if trimmed.empty?
|
|
343
|
+
|
|
344
|
+
key, value = trimmed.split("=", 2)
|
|
345
|
+
return false if key.nil? || !KEY.match?(trim_ows(key))
|
|
346
|
+
return true if value.nil?
|
|
347
|
+
|
|
348
|
+
trimmed_val = trim_ows(value)
|
|
349
|
+
VALUE.match?(trimmed_val) && !BAD_PERCENT.match?(trimmed_val)
|
|
350
|
+
end
|
|
146
351
|
end
|
|
147
352
|
private_constant :Baggage
|
|
148
353
|
|
|
@@ -157,9 +362,16 @@ module Libo11y
|
|
|
157
362
|
span_context = OpenTelemetry::Trace.current_span(context).context
|
|
158
363
|
return Carrier::ZERO unless span_context.valid?
|
|
159
364
|
|
|
160
|
-
|
|
161
|
-
propagator(telemetry)
|
|
162
|
-
|
|
365
|
+
staged = {}
|
|
366
|
+
prop = propagator(telemetry)
|
|
367
|
+
baggage = begin
|
|
368
|
+
prop.inject(staged, context: context)
|
|
369
|
+
staged["baggage"].to_s
|
|
370
|
+
rescue StandardError => e
|
|
371
|
+
OpenTelemetry.handle_error(exception: e, message: "libo11y: #{prop.class} raised in inject")
|
|
372
|
+
""
|
|
373
|
+
end
|
|
374
|
+
Carrier.from_span_context(span_context, baggage)
|
|
163
375
|
end
|
|
164
376
|
|
|
165
377
|
# Returns context with the carrier's baggage merged over its own. The
|
|
@@ -167,7 +379,7 @@ module Libo11y
|
|
|
167
379
|
def restore_baggage(carrier, context: OpenTelemetry::Context.current)
|
|
168
380
|
return context if carrier.baggage.empty?
|
|
169
381
|
|
|
170
|
-
|
|
382
|
+
Baggage.restore(carrier.baggage, context)
|
|
171
383
|
end
|
|
172
384
|
|
|
173
385
|
# Starts a new root span linked to the carrier's span, with the carrier's
|
|
@@ -185,7 +397,7 @@ module Libo11y
|
|
|
185
397
|
|
|
186
398
|
provider = telemetry ? telemetry.tracer_provider : OpenTelemetry.tracer_provider
|
|
187
399
|
OpenTelemetry::Context.with_current(context) do
|
|
188
|
-
provider.tracer(tracer_name, Libo11y::VERSION).in_span(span_name, links: links, kind: kind, attributes: attributes, &)
|
|
400
|
+
provider.tracer(name: tracer_name, version: Libo11y::VERSION).in_span(span_name, links: links, kind: kind, attributes: attributes, &)
|
|
189
401
|
end
|
|
190
402
|
end
|
|
191
403
|
|
data/lib/libo11y/http_client.rb
CHANGED
|
@@ -4,14 +4,17 @@ require "net/http"
|
|
|
4
4
|
require "uri"
|
|
5
5
|
require "opentelemetry"
|
|
6
6
|
require_relative "internal/http_common"
|
|
7
|
+
require_relative "internal/quiet_inspect"
|
|
7
8
|
|
|
8
9
|
module Libo11y
|
|
9
10
|
# An instrumented Net::HTTP client. It records a client span and the
|
|
10
11
|
# http.client.request.duration histogram for each request and injects the
|
|
11
12
|
# trace context into the request headers. It patches nothing globally.
|
|
12
13
|
class HttpClient
|
|
14
|
+
include Internal::QuietInspect
|
|
15
|
+
|
|
13
16
|
HTTP_DURATION_BUCKETS = Internal::HttpCommon::DURATION_BUCKETS
|
|
14
|
-
SCOPE_NAME =
|
|
17
|
+
SCOPE_NAME = Internal::HistogramBuckets::HTTP_SCOPE
|
|
15
18
|
|
|
16
19
|
# @param telemetry [Libo11y::Telemetry, nil] the handle to record
|
|
17
20
|
# through. Without one the client uses the global providers.
|
|
@@ -24,16 +27,18 @@ module Libo11y
|
|
|
24
27
|
@peer_service = peer_service
|
|
25
28
|
@mutex = Mutex.new
|
|
26
29
|
@duration = nil
|
|
30
|
+
@duration_provider = nil
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
# Sends one request and returns the Net::HTTPResponse.
|
|
30
34
|
#
|
|
31
35
|
# @param method [Symbol, String] the request method, such as :get. A
|
|
32
36
|
# method outside the standard set is sent as given and recorded as
|
|
33
|
-
# _OTHER.
|
|
37
|
+
# _OTHER. A Symbol is sent in upper case, and a String as written.
|
|
34
38
|
def request(method, url, headers: {}, body: nil, open_timeout: 5, read_timeout: 15)
|
|
35
39
|
uri = url.is_a?(URI) ? url : URI.parse(url.to_s)
|
|
36
|
-
|
|
40
|
+
# A method token is case-sensitive, so a String goes out as written.
|
|
41
|
+
verb = method.is_a?(Symbol) ? method.to_s.upcase : method.to_s
|
|
37
42
|
normalized, original = Internal::HttpCommon.normalize_method(verb)
|
|
38
43
|
|
|
39
44
|
attrs = { "http.request.method" => normalized, "server.address" => uri.host.to_s, "server.port" => uri.port }
|
|
@@ -54,11 +59,13 @@ module Libo11y
|
|
|
54
59
|
response
|
|
55
60
|
rescue StandardError => e
|
|
56
61
|
span.record_exception(e)
|
|
57
|
-
failed(span, metric_attrs, e
|
|
62
|
+
failed(span, metric_attrs, Internal::ErrorType.of(e))
|
|
58
63
|
raise
|
|
64
|
+
ensure
|
|
65
|
+
# Inside the client span's context, so the measurement's exemplar names this span.
|
|
66
|
+
duration.record(Internal::HttpCommon.monotonic - started, attributes: metric_attrs)
|
|
59
67
|
end
|
|
60
68
|
ensure
|
|
61
|
-
duration.record(Internal::HttpCommon.monotonic - started, attributes: metric_attrs)
|
|
62
69
|
span.finish
|
|
63
70
|
end
|
|
64
71
|
end
|
|
@@ -71,16 +78,40 @@ module Libo11y
|
|
|
71
78
|
metric_attrs["error.type"] = error_type
|
|
72
79
|
end
|
|
73
80
|
|
|
81
|
+
W3C_PROPAGATION_FIELDS = %w[traceparent tracestate baggage].freeze
|
|
82
|
+
private_constant :W3C_PROPAGATION_FIELDS
|
|
83
|
+
|
|
74
84
|
# The caller's headers with the trace context of the active span. A
|
|
75
|
-
#
|
|
85
|
+
# header the propagator owns that the caller passed, in any case, never
|
|
86
|
+
# goes out as it came: to a destination propagate_fn refuses none is
|
|
87
|
+
# sent, and to any other only what the propagator writes, which for
|
|
88
|
+
# baggage is what the egress filter lets out.
|
|
76
89
|
def outbound_headers(headers, uri)
|
|
77
|
-
|
|
90
|
+
propagator = @telemetry ? @telemetry.propagator : OpenTelemetry.propagation
|
|
91
|
+
owned = (W3C_PROPAGATION_FIELDS + propagator_fields(propagator)).uniq
|
|
92
|
+
out = headers.to_h { |key, value| [key.to_s, value.to_s] }.reject { |key, _| owned.any? { |name| name.casecmp?(key) } }
|
|
78
93
|
return out unless @propagate_fn.nil? || @propagate_fn.call(uri)
|
|
79
94
|
|
|
80
|
-
|
|
81
|
-
injected
|
|
82
|
-
|
|
83
|
-
|
|
95
|
+
injected = inject_headers(propagator)
|
|
96
|
+
out.merge(injected)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def propagator_fields(propagator)
|
|
100
|
+
return [] unless propagator.respond_to?(:fields)
|
|
101
|
+
|
|
102
|
+
Array(propagator.fields).map(&:to_s)
|
|
103
|
+
rescue StandardError => e
|
|
104
|
+
OpenTelemetry.handle_error(exception: e, message: "libo11y: #{propagator.class} raised in fields")
|
|
105
|
+
[]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def inject_headers(propagator)
|
|
109
|
+
staged = {}
|
|
110
|
+
propagator.inject(staged)
|
|
111
|
+
staged
|
|
112
|
+
rescue StandardError => e
|
|
113
|
+
OpenTelemetry.handle_error(exception: e, message: "libo11y: #{propagator.class} raised in inject")
|
|
114
|
+
{}
|
|
84
115
|
end
|
|
85
116
|
|
|
86
117
|
def send_request(uri, verb, headers, body, open_timeout:, read_timeout:)
|
|
@@ -103,17 +134,19 @@ module Libo11y
|
|
|
103
134
|
|
|
104
135
|
def tracer
|
|
105
136
|
provider = @telemetry ? @telemetry.tracer_provider : OpenTelemetry.tracer_provider
|
|
106
|
-
provider.tracer(SCOPE_NAME, Libo11y::VERSION)
|
|
137
|
+
provider.tracer(name: SCOPE_NAME, version: Libo11y::VERSION)
|
|
107
138
|
end
|
|
108
139
|
|
|
109
140
|
def duration
|
|
141
|
+
provider = @telemetry ? @telemetry.meter_provider : OpenTelemetry.meter_provider
|
|
110
142
|
@mutex.synchronize do
|
|
111
|
-
@
|
|
112
|
-
|
|
113
|
-
provider.meter(SCOPE_NAME, version: Libo11y::VERSION).create_histogram(
|
|
143
|
+
unless @duration_provider.equal?(provider)
|
|
144
|
+
@duration = provider.meter(SCOPE_NAME, version: Libo11y::VERSION).create_histogram(
|
|
114
145
|
"http.client.request.duration", unit: "s", description: "Duration of HTTP client requests."
|
|
115
146
|
)
|
|
147
|
+
@duration_provider = provider
|
|
116
148
|
end
|
|
149
|
+
@duration
|
|
117
150
|
end
|
|
118
151
|
end
|
|
119
152
|
end
|