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/telemetry.rb
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "time"
|
|
4
|
-
require "
|
|
5
|
-
|
|
6
|
-
require "opentelemetry-logs-sdk"
|
|
4
|
+
require "timeout"
|
|
5
|
+
require_relative "internal/sdk_load"
|
|
7
6
|
require_relative "errors"
|
|
8
7
|
require_relative "options"
|
|
9
8
|
require_relative "spec"
|
|
10
9
|
require_relative "internal/env"
|
|
10
|
+
require_relative "internal/error_type"
|
|
11
11
|
require_relative "internal/endpoint"
|
|
12
12
|
require_relative "internal/histogram_buckets"
|
|
13
13
|
require_relative "internal/policy"
|
|
@@ -37,6 +37,7 @@ module Libo11y
|
|
|
37
37
|
shutdown_timeout:,
|
|
38
38
|
is_global:,
|
|
39
39
|
failures:,
|
|
40
|
+
injected:,
|
|
40
41
|
on_global_shutdown: nil
|
|
41
42
|
)
|
|
42
43
|
@tracer_provider = tracer_provider
|
|
@@ -50,6 +51,7 @@ module Libo11y
|
|
|
50
51
|
@shutdown_timeout = shutdown_timeout
|
|
51
52
|
@is_global = is_global
|
|
52
53
|
@failures = failures
|
|
54
|
+
@injected = injected
|
|
53
55
|
@on_global_shutdown = on_global_shutdown
|
|
54
56
|
@shutdown_mutex = Mutex.new
|
|
55
57
|
@shutdown_done = false
|
|
@@ -58,12 +60,26 @@ module Libo11y
|
|
|
58
60
|
@shutdown_in_progress = false
|
|
59
61
|
end
|
|
60
62
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
# A tracer of the handle's tracer provider.
|
|
64
|
+
#
|
|
65
|
+
# @param name [String] the instrumentation scope's name
|
|
66
|
+
# @param positional_version [String, nil] the scope's version as 0.2.0
|
|
67
|
+
# took it. `version:` wins when both are given.
|
|
68
|
+
# @param version [String, nil] the scope's version
|
|
69
|
+
# @param attributes [Hash, nil] the scope's attributes, which the
|
|
70
|
+
# sensitivity policy does not classify
|
|
71
|
+
def tracer(name, positional_version = nil, version: nil, attributes: nil)
|
|
72
|
+
@tracer_provider.tracer(name: name, version: version || positional_version, attributes: attributes)
|
|
63
73
|
end
|
|
64
74
|
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
# A meter of the handle's meter provider.
|
|
76
|
+
#
|
|
77
|
+
# @param name [String] the instrumentation scope's name
|
|
78
|
+
# @param version [String, nil] the scope's version
|
|
79
|
+
# @param attributes [Hash, nil] the scope's attributes, which the
|
|
80
|
+
# sensitivity policy does not classify
|
|
81
|
+
def meter(name, version: nil, attributes: nil)
|
|
82
|
+
@meter_provider.meter(name, version: version, attributes: attributes)
|
|
67
83
|
end
|
|
68
84
|
|
|
69
85
|
# The resolved configuration as one line of key=value pairs: the
|
|
@@ -84,14 +100,16 @@ module Libo11y
|
|
|
84
100
|
# Emit a registered (or custom) event, always force-emitted past level gates.
|
|
85
101
|
def event(name, attributes = {}, body: nil)
|
|
86
102
|
attrs = {}
|
|
87
|
-
attributes&.each { |k, v| attrs[k
|
|
88
|
-
@logger.log_with_attrs(::Logger::INFO, body || name.to_s, attrs, event_name: name.to_s, force: true)
|
|
103
|
+
attributes&.each { |k, v| attrs[Internal::SensitivityPolicy.normalize_key(k)] = v }
|
|
104
|
+
@logger.log_with_attrs(::Logger::INFO, body || name.to_s, attrs, event_name: name.to_s, force: true, seen: attributes)
|
|
89
105
|
end
|
|
90
106
|
|
|
91
107
|
# Stops logs, metrics and traces concurrently under one deadline, and
|
|
92
108
|
# returns a ShutdownReport with one error per signal that failed, missed
|
|
93
109
|
# the deadline, or had its final export rejected. It runs once: every
|
|
94
|
-
# later
|
|
110
|
+
# later caller gets the first call's report. A caller that arrives while
|
|
111
|
+
# the shutdown is running waits for it until its own deadline, and then
|
|
112
|
+
# gets a report that says the shutdown is still in progress. For each
|
|
95
113
|
# signal in the report an o11y.telemetry.dropped event goes to the
|
|
96
114
|
# console and the extra sinks.
|
|
97
115
|
#
|
|
@@ -100,8 +118,38 @@ module Libo11y
|
|
|
100
118
|
# @raise [ArgumentError] when timeout is not a number
|
|
101
119
|
def shutdown(timeout: nil)
|
|
102
120
|
secs = resolve_timeout(timeout)
|
|
121
|
+
shutdown_by(monotonic + secs, secs)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The seconds a shutdown or a flush may take. nil and a timeout that is
|
|
125
|
+
# not positive give the shutdown_timeout option.
|
|
126
|
+
#
|
|
127
|
+
# @api private
|
|
128
|
+
# @raise [ArgumentError] for a timeout that is not a finite number
|
|
129
|
+
def resolve_timeout(timeout)
|
|
130
|
+
return @shutdown_timeout if timeout.nil?
|
|
131
|
+
|
|
132
|
+
secs = Telemetry.seconds(timeout, "timeout")
|
|
133
|
+
secs.positive? ? secs : @shutdown_timeout
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Shuts down by a point on the monotonic clock that the caller has
|
|
137
|
+
# already set, as Libo11y.shutdown_on_signals does after before_shutdown
|
|
138
|
+
# has used part of the time. secs is the whole allowance, for messages.
|
|
139
|
+
#
|
|
140
|
+
# @api private
|
|
141
|
+
def shutdown_by(deadline, secs)
|
|
103
142
|
@shutdown_mutex.synchronize do
|
|
104
|
-
|
|
143
|
+
# A sink invoked from this handle's own dropped-event writer thread
|
|
144
|
+
# cannot wait for the shutdown that is waiting on that writer thread.
|
|
145
|
+
return in_progress_report(secs) if @shutdown_in_progress && Thread.current[REPORTING_SHUTDOWN_FIBER_KEY].equal?(self)
|
|
146
|
+
|
|
147
|
+
while @shutdown_in_progress
|
|
148
|
+
remaining = deadline - monotonic
|
|
149
|
+
return in_progress_report(secs) unless remaining.positive?
|
|
150
|
+
|
|
151
|
+
@shutdown_cv.wait(@shutdown_mutex, remaining)
|
|
152
|
+
end
|
|
105
153
|
return @shutdown_report if @shutdown_done
|
|
106
154
|
|
|
107
155
|
@shutdown_in_progress = true
|
|
@@ -109,7 +157,7 @@ module Libo11y
|
|
|
109
157
|
|
|
110
158
|
report = nil
|
|
111
159
|
begin
|
|
112
|
-
report = run_shutdown(secs)
|
|
160
|
+
report = run_shutdown(secs, deadline)
|
|
113
161
|
ensure
|
|
114
162
|
@shutdown_mutex.synchronize do
|
|
115
163
|
# An unexpected error leaves no report, and the next caller runs it again.
|
|
@@ -130,60 +178,188 @@ module Libo11y
|
|
|
130
178
|
# @raise [ArgumentError] when timeout is not a number
|
|
131
179
|
def force_flush(timeout: nil)
|
|
132
180
|
secs = resolve_timeout(timeout)
|
|
133
|
-
|
|
181
|
+
deadline = monotonic + secs
|
|
182
|
+
each_signal("flush", secs, deadline) { |provider| provider.force_flush(timeout: [deadline - monotonic, 0.001].max) }.empty?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def inspect
|
|
186
|
+
"#<#{self.class.name} #{@effective_config}>"
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# A number of seconds as a Float.
|
|
190
|
+
#
|
|
191
|
+
# @raise [ArgumentError] for a value that is not a finite number
|
|
192
|
+
def self.seconds(value, name)
|
|
193
|
+
secs = Float(value)
|
|
194
|
+
raise ArgumentError unless secs.finite?
|
|
195
|
+
|
|
196
|
+
secs
|
|
197
|
+
rescue ArgumentError, TypeError
|
|
198
|
+
raise ArgumentError, "libo11y: #{name} must be a finite number of seconds, got #{value.inspect}"
|
|
134
199
|
end
|
|
135
200
|
|
|
136
201
|
private
|
|
137
202
|
|
|
138
203
|
SIGNALS = %i[logs metrics traces].freeze
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
204
|
+
# The most a shutdown keeps back of its time for writing the
|
|
205
|
+
# o11y.telemetry.dropped events, in seconds: the 100 ms of Go's
|
|
206
|
+
# shutdownReserve.
|
|
207
|
+
DROPPED_EVENT_RESERVE = 0.1
|
|
208
|
+
private_constant :SIGNALS, :DROPPED_EVENT_RESERVE
|
|
209
|
+
|
|
210
|
+
def monotonic
|
|
211
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
146
212
|
end
|
|
147
213
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
214
|
+
# The providers get the time up to the deadline less a reserve, so the
|
|
215
|
+
# events that report a signal as dropped are written inside the deadline.
|
|
216
|
+
# The reserve is taken from the time that is left, which is less than
|
|
217
|
+
# secs when Libo11y.shutdown_on_signals has spent some on before_shutdown.
|
|
218
|
+
def run_shutdown(secs, deadline)
|
|
219
|
+
remaining = [deadline - monotonic, 0.0].max
|
|
220
|
+
reserve = dropped_event_reserve(remaining)
|
|
221
|
+
budget = [remaining - reserve, 0.001].max
|
|
222
|
+
left = -> { [deadline - reserve - monotonic, 0.001].max }
|
|
223
|
+
errors = each_signal("shutdown", secs, deadline - reserve) do |provider, signal|
|
|
224
|
+
steps = []
|
|
225
|
+
if signal == :metrics && provider.respond_to?(:flush_and_shutdown)
|
|
226
|
+
# Flushes and shuts down each metric reader concurrently under one
|
|
227
|
+
# deadline so a reader blocked in force_flush or shutdown does not
|
|
228
|
+
# keep healthy periodic readers alive.
|
|
229
|
+
steps << -> { provider.flush_and_shutdown(timeout: left.call) }
|
|
230
|
+
else
|
|
231
|
+
# The SDK's periodic reader makes its last collection only if its
|
|
232
|
+
# worker is already waiting, so the last collection is made here.
|
|
233
|
+
steps << -> { provider.force_flush(timeout: budget) } if signal == :metrics
|
|
234
|
+
steps << -> { provider.shutdown(timeout: left.call) }
|
|
235
|
+
end
|
|
236
|
+
# What the options injected and no provider took, as under OTEL_SDK_DISABLED.
|
|
237
|
+
steps << -> { @injected.stop_unused(signal, timeout: left.call) }
|
|
238
|
+
status = nil
|
|
239
|
+
begin
|
|
240
|
+
status = every(steps)
|
|
241
|
+
ensure
|
|
242
|
+
in_flight = (provider.respond_to?(:shutdown_in_flight?) && provider.shutdown_in_flight?) ||
|
|
243
|
+
@injected.unused_in_flight?(signal)
|
|
244
|
+
@injected.release_signal(signal) unless status == OpenTelemetry::SDK::Trace::Export::TIMEOUT || in_flight
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
report_dropped(errors, deadline)
|
|
153
248
|
@on_global_shutdown&.call if @is_global
|
|
154
249
|
ShutdownReport.new(traces_error: errors[:traces], metrics_error: errors[:metrics], logs_error: errors[:logs])
|
|
155
250
|
end
|
|
156
251
|
|
|
252
|
+
# The time kept back for the dropped events: at most a quarter of what
|
|
253
|
+
# is left of the shutdown's time.
|
|
254
|
+
def dropped_event_reserve(remaining)
|
|
255
|
+
[DROPPED_EVENT_RESERVE, remaining / 4.0].min
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
REPORTING_SHUTDOWN_FIBER_KEY = :libo11y_reporting_shutdown
|
|
259
|
+
private_constant :REPORTING_SHUTDOWN_FIBER_KEY
|
|
260
|
+
|
|
261
|
+
# Writes one o11y.telemetry.dropped event per failed signal. The OTLP log
|
|
262
|
+
# leg is already down, so they reach the console and the extra sinks
|
|
263
|
+
# only. A slow sink does not hold the shutdown past its deadline: the
|
|
264
|
+
# events are written on their own thread, which the shutdown waits for
|
|
265
|
+
# until the deadline and no longer.
|
|
266
|
+
def report_dropped(errors, deadline)
|
|
267
|
+
return if errors.empty?
|
|
268
|
+
|
|
269
|
+
writer = Thread.new do
|
|
270
|
+
Thread.current[REPORTING_SHUTDOWN_FIBER_KEY] = self
|
|
271
|
+
errors.each do |signal, error|
|
|
272
|
+
# Nothing is started after the deadline: not an event, not a sink.
|
|
273
|
+
break if monotonic >= deadline
|
|
274
|
+
|
|
275
|
+
emit_dropped(signal, error, deadline)
|
|
276
|
+
end
|
|
277
|
+
ensure
|
|
278
|
+
Thread.current[REPORTING_SHUTDOWN_FIBER_KEY] = nil
|
|
279
|
+
end
|
|
280
|
+
writer.report_on_exception = false
|
|
281
|
+
writer.join([deadline - monotonic, 0].max)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# Runs every step, also after one has raised, so a flush that fails does
|
|
285
|
+
# not keep a provider from being shut down. Returns the worst status the
|
|
286
|
+
# steps answered with, and raises the first error when there was one.
|
|
287
|
+
def every(steps)
|
|
288
|
+
first = nil
|
|
289
|
+
statuses = steps.map do |step|
|
|
290
|
+
Internal::Bridges.normalize_lifecycle_status(step.call)
|
|
291
|
+
rescue StandardError => e
|
|
292
|
+
first ||= e
|
|
293
|
+
nil
|
|
294
|
+
end
|
|
295
|
+
raise first if first
|
|
296
|
+
|
|
297
|
+
statuses.grep(Integer).max
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def in_progress_report(secs)
|
|
301
|
+
errors = SIGNALS.to_h do |signal|
|
|
302
|
+
[signal, ExporterError.new("o11y: shutdown #{signal}: shutdown is still in progress after #{secs}s")]
|
|
303
|
+
end
|
|
304
|
+
ShutdownReport.new(traces_error: errors[:traces], metrics_error: errors[:metrics], logs_error: errors[:logs])
|
|
305
|
+
end
|
|
306
|
+
|
|
157
307
|
# Runs the block for each signal's provider on its own thread, under one
|
|
158
308
|
# deadline. Returns the error of each signal that failed.
|
|
159
|
-
def each_signal(action, secs)
|
|
309
|
+
def each_signal(action, secs, deadline)
|
|
160
310
|
providers = { logs: @logger_provider, metrics: @meter_provider, traces: @tracer_provider }
|
|
161
311
|
errors = {}
|
|
162
312
|
lock = Mutex.new
|
|
163
313
|
threads = SIGNALS.to_h do |signal|
|
|
164
314
|
thread = Thread.new do
|
|
165
|
-
|
|
166
|
-
|
|
315
|
+
Thread.current[Internal::Bridges::DEADLINE_KEY] = deadline
|
|
316
|
+
status, failed = @failures.fetch(signal).during { yield providers.fetch(signal), signal }
|
|
317
|
+
original = failed.first
|
|
318
|
+
# The SDK's code for a part that ran out of time. Prefer a Timeout::Error
|
|
319
|
+
# over an earlier non-timeout export failure so an expired deadline is
|
|
320
|
+
# never masked by a prior export rejection.
|
|
321
|
+
if status.is_a?(Integer) && status == OpenTelemetry::SDK::Trace::Export::TIMEOUT
|
|
322
|
+
msg = monotonic >= deadline ? "timed out after #{secs}s" : "the provider answered with a timeout"
|
|
323
|
+
original = failed.find { |err| err.is_a?(Timeout::Error) } || Timeout::Error.new(msg)
|
|
324
|
+
end
|
|
325
|
+
cause = original&.message
|
|
167
326
|
cause ||= "the provider answered #{status}" if status.is_a?(Integer) && !status.zero?
|
|
168
|
-
lock.synchronize { errors[signal] =
|
|
327
|
+
lock.synchronize { errors[signal] = signal_error("o11y: #{action} #{signal}: #{cause}", original) } if cause
|
|
169
328
|
rescue StandardError => e
|
|
170
|
-
lock.synchronize { errors[signal] =
|
|
329
|
+
lock.synchronize { errors[signal] = signal_error("o11y: #{action} #{signal}: #{e.message}", e) }
|
|
330
|
+
ensure
|
|
331
|
+
Thread.current[Internal::Bridges::DEADLINE_KEY] = nil
|
|
171
332
|
end
|
|
172
333
|
[signal, thread]
|
|
173
334
|
end
|
|
174
335
|
|
|
175
|
-
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + secs
|
|
176
336
|
threads.each do |signal, thread|
|
|
177
|
-
|
|
178
|
-
next if thread.join([remaining, 0].max)
|
|
337
|
+
next if thread.join([deadline - monotonic, 0].max)
|
|
179
338
|
|
|
180
|
-
|
|
339
|
+
timeout = Timeout::Error.new("timed out after #{secs}s")
|
|
340
|
+
lock.synchronize { errors[signal] ||= signal_error("o11y: #{action} #{signal}: #{timeout.message}", timeout) }
|
|
181
341
|
end
|
|
182
342
|
lock.synchronize { errors.dup }
|
|
183
343
|
end
|
|
184
344
|
|
|
185
|
-
|
|
186
|
-
|
|
345
|
+
# A signal's error for the report. It wraps the error that failed the
|
|
346
|
+
# signal as its cause, when that is an error of another kind.
|
|
347
|
+
def signal_error(message, original)
|
|
348
|
+
return ExporterError.new(message) if original.nil? || original.is_a?(ExporterError)
|
|
349
|
+
|
|
350
|
+
begin
|
|
351
|
+
raise ExporterError, message, cause: original
|
|
352
|
+
rescue ExporterError => e
|
|
353
|
+
e
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# error.type names the error that failed the signal, as in Go, and not
|
|
358
|
+
# the error that wraps it for the report.
|
|
359
|
+
def emit_dropped(signal, error, deadline)
|
|
360
|
+
attributes = { Spec::ATTR_SIGNAL => signal.to_s, "error.type" => Internal::ErrorType.of(error.cause || error) }
|
|
361
|
+
@logger.log_with_attrs(::Logger::INFO, Spec::EVENT_TELEMETRY_DROPPED, attributes,
|
|
362
|
+
event_name: Spec::EVENT_TELEMETRY_DROPPED, force: true, deadline: deadline, otel: false)
|
|
187
363
|
rescue StandardError
|
|
188
364
|
nil
|
|
189
365
|
end
|
|
@@ -199,22 +375,69 @@ module Libo11y
|
|
|
199
375
|
def build(options_or_nil = nil, is_global:, **)
|
|
200
376
|
opts = Options.merge(options_or_nil, **)
|
|
201
377
|
|
|
202
|
-
# Claimed before anything is built, so a second caller fails at once
|
|
203
|
-
# and builds nothing.
|
|
204
|
-
claim_global! if is_global
|
|
205
378
|
built = []
|
|
379
|
+
injected = Internal::InjectedParts.new(opts)
|
|
380
|
+
started = false
|
|
381
|
+
claimed = false
|
|
206
382
|
begin
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
#
|
|
210
|
-
|
|
211
|
-
|
|
383
|
+
# Claimed before anything is built, so a second caller fails at
|
|
384
|
+
# once and builds nothing, while still shutting down any unowned
|
|
385
|
+
# parts it injected.
|
|
386
|
+
if is_global
|
|
387
|
+
claim_global!
|
|
388
|
+
claimed = true
|
|
389
|
+
end
|
|
390
|
+
injected.reserve!
|
|
391
|
+
telemetry = construct(opts, is_global, built, injected)
|
|
392
|
+
started = true
|
|
393
|
+
telemetry
|
|
394
|
+
ensure
|
|
395
|
+
# In an ensure, so an Interrupt or a LoadError cleans up as an
|
|
396
|
+
# ordinary error does.
|
|
397
|
+
abandon(built, injected, claimed) unless started
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# How long a failed start waits for what it built to stop, in seconds.
|
|
402
|
+
FAILED_START_CLEANUP = 1.5
|
|
403
|
+
private_constant :FAILED_START_CLEANUP
|
|
404
|
+
|
|
405
|
+
# Stops what a failed start built: each provider and the injected
|
|
406
|
+
# parts no provider had taken, each on its own thread, so that one
|
|
407
|
+
# that blocks keeps none of the others from stopping. The start waits
|
|
408
|
+
# for them briefly and no longer: a part that ignores its timeout
|
|
409
|
+
# must not hold up the error the caller is about to get. Then it
|
|
410
|
+
# frees the global claim.
|
|
411
|
+
def abandon(built, injected, is_global)
|
|
412
|
+
stops = built.map do |provider|
|
|
413
|
+
lambda do
|
|
414
|
+
status = provider.shutdown(timeout: 1.0)
|
|
415
|
+
ensure
|
|
416
|
+
in_flight = provider.respond_to?(:shutdown_in_flight?) && provider.shutdown_in_flight?
|
|
417
|
+
release_provider_signal(provider, injected) unless status == OpenTelemetry::SDK::Trace::Export::TIMEOUT || in_flight
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
stops.concat(injected.discards)
|
|
421
|
+
threads = stops.map do |stop|
|
|
422
|
+
Thread.new do
|
|
423
|
+
stop.call
|
|
212
424
|
rescue StandardError
|
|
213
425
|
nil
|
|
214
426
|
end
|
|
215
|
-
reset_global! if is_global
|
|
216
|
-
raise
|
|
217
427
|
end
|
|
428
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + FAILED_START_CLEANUP
|
|
429
|
+
all_done = threads.map { |thread| thread.join([deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max) }.all?
|
|
430
|
+
injected.release_all! if all_done && built.empty?
|
|
431
|
+
reset_global! if is_global
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def release_provider_signal(provider, injected)
|
|
435
|
+
signal = case provider
|
|
436
|
+
when Internal::Bridges::PolicyTracerProvider, Internal::Bridges::NoopTracerProvider then :traces
|
|
437
|
+
when Internal::Bridges::PolicyMeterProvider, OpenTelemetry::SDK::Metrics::MeterProvider then :metrics
|
|
438
|
+
when Internal::Bridges::QuietLoggerProvider then :logs
|
|
439
|
+
end
|
|
440
|
+
injected.release_signal(signal) if signal && injected.handed?(signal)
|
|
218
441
|
end
|
|
219
442
|
|
|
220
443
|
def reset_global!
|
|
@@ -231,7 +454,12 @@ module Libo11y
|
|
|
231
454
|
end
|
|
232
455
|
end
|
|
233
456
|
|
|
234
|
-
def construct(options, is_global, built)
|
|
457
|
+
def construct(options, is_global, built, injected)
|
|
458
|
+
shutdown_timeout = Telemetry.seconds(options.shutdown_timeout, "shutdown_timeout")
|
|
459
|
+
unless shutdown_timeout.positive?
|
|
460
|
+
raise ArgumentError, "libo11y: shutdown_timeout must be more than 0 seconds, got #{options.shutdown_timeout.inspect}"
|
|
461
|
+
end
|
|
462
|
+
|
|
235
463
|
env_map = Internal::Env.resolve_map(options.env)
|
|
236
464
|
violations = Internal::ViolationRecorder.new
|
|
237
465
|
policy = Internal::SensitivityPolicy.new(
|
|
@@ -244,7 +472,7 @@ module Libo11y
|
|
|
244
472
|
propagator = Internal::Propagators.build(options.propagators, options.baggage_egress_allow)
|
|
245
473
|
pipeline = Internal::Pipeline.new(
|
|
246
474
|
options: options, env_map: env_map, hermetic: Internal::Env.hermetic?(options.env),
|
|
247
|
-
policy: policy, resource: identity.resource, violations: violations, built: built
|
|
475
|
+
policy: policy, resource: identity.resource, violations: violations, built: built, injected: injected
|
|
248
476
|
).build
|
|
249
477
|
logger = build_logger(options, policy, pipeline)
|
|
250
478
|
|
|
@@ -266,9 +494,10 @@ module Libo11y
|
|
|
266
494
|
resource: identity.resource,
|
|
267
495
|
policy: policy,
|
|
268
496
|
effective_config: summary_line,
|
|
269
|
-
shutdown_timeout:
|
|
497
|
+
shutdown_timeout: shutdown_timeout,
|
|
270
498
|
is_global: is_global,
|
|
271
499
|
failures: pipeline.failures,
|
|
500
|
+
injected: injected,
|
|
272
501
|
on_global_shutdown: method(:reset_global!)
|
|
273
502
|
)
|
|
274
503
|
end
|
|
@@ -301,15 +530,48 @@ module Libo11y
|
|
|
301
530
|
raise CanaryRejected, "o11y: startup canary rejected by log pipeline: #{e.message}"
|
|
302
531
|
end
|
|
303
532
|
|
|
533
|
+
# The propagator first. OpenTelemetry's first global providers are
|
|
534
|
+
# proxies that hand their tracers, meters and loggers to the first
|
|
535
|
+
# provider installed, once and for good. So everything that can still
|
|
536
|
+
# fail is done before a provider is installed, and a setup that fails
|
|
537
|
+
# leaves instrumentation created before it free for the setup that
|
|
538
|
+
# succeeds.
|
|
539
|
+
GLOBALS = %i[propagation tracer_provider meter_provider logger_provider].freeze
|
|
540
|
+
private_constant :GLOBALS
|
|
541
|
+
|
|
542
|
+
# Installs the propagator and the providers as OpenTelemetry's globals.
|
|
543
|
+
# When one of them cannot be installed, the ones already replaced are
|
|
544
|
+
# put back, so a failed setup leaves the globals as it found them.
|
|
304
545
|
def install_globals(pipeline, propagator)
|
|
546
|
+
missing = GLOBALS.reject { |name| OpenTelemetry.respond_to?(:"#{name}=") }
|
|
547
|
+
raise Error, "o11y: OpenTelemetry has no global #{missing.join(", ")}" unless missing.empty?
|
|
548
|
+
|
|
549
|
+
ours = {
|
|
550
|
+
tracer_provider: pipeline.tracer_provider, meter_provider: pipeline.meter_provider,
|
|
551
|
+
logger_provider: pipeline.logger_provider, propagation: propagator
|
|
552
|
+
}
|
|
305
553
|
@global_mutex.synchronize do
|
|
554
|
+
previous = {}
|
|
555
|
+
installed = false
|
|
556
|
+
begin
|
|
557
|
+
GLOBALS.each do |name|
|
|
558
|
+
previous[name] = OpenTelemetry.public_send(name)
|
|
559
|
+
OpenTelemetry.public_send(:"#{name}=", ours.fetch(name))
|
|
560
|
+
end
|
|
561
|
+
installed = true
|
|
562
|
+
ensure
|
|
563
|
+
# In an ensure, so an Interrupt puts them back as an error does.
|
|
564
|
+
previous.each { |name, value| restore_global(name, value) } unless installed
|
|
565
|
+
end
|
|
306
566
|
@global_state = :active
|
|
307
|
-
OpenTelemetry.tracer_provider = pipeline.tracer_provider
|
|
308
|
-
OpenTelemetry.meter_provider = pipeline.meter_provider if OpenTelemetry.respond_to?(:meter_provider=)
|
|
309
|
-
OpenTelemetry.logger_provider = pipeline.logger_provider if OpenTelemetry.respond_to?(:logger_provider=)
|
|
310
|
-
OpenTelemetry.propagation = propagator
|
|
311
567
|
end
|
|
312
568
|
end
|
|
569
|
+
|
|
570
|
+
def restore_global(name, value)
|
|
571
|
+
OpenTelemetry.public_send(:"#{name}=", value)
|
|
572
|
+
rescue StandardError
|
|
573
|
+
nil
|
|
574
|
+
end
|
|
313
575
|
end
|
|
314
576
|
end
|
|
315
577
|
end
|
data/lib/libo11y/testing.rb
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "stringio"
|
|
5
5
|
require "zlib"
|
|
6
|
-
|
|
7
|
-
require "opentelemetry-metrics-sdk"
|
|
8
|
-
require "opentelemetry-logs-sdk"
|
|
6
|
+
require_relative "internal/sdk_load"
|
|
9
7
|
require "opentelemetry/exporter/otlp"
|
|
10
8
|
require "opentelemetry/exporter/otlp_metrics"
|
|
11
9
|
require "opentelemetry/exporter/otlp_logs"
|
|
@@ -20,24 +18,26 @@ module Libo11y
|
|
|
20
18
|
class Recorder
|
|
21
19
|
attr_reader :telemetry, :span_exporter, :metric_reader, :log_exporter, :console_io
|
|
22
20
|
|
|
21
|
+
# The options the recorder sets itself. Its queries and assertions read
|
|
22
|
+
# the sinks it created, and it is hermetic by contract.
|
|
23
|
+
RESERVED_OPTIONS = %i[env span_exporter metric_reader log_exporter console_writer].freeze
|
|
24
|
+
|
|
23
25
|
def initialize(
|
|
24
26
|
service_name: "test-service",
|
|
25
27
|
environment: Spec::Environment::TEST,
|
|
26
28
|
version: "1.0.0",
|
|
27
29
|
**extra_opts
|
|
28
30
|
)
|
|
31
|
+
reserved = extra_opts.keys & RESERVED_OPTIONS
|
|
32
|
+
raise ArgumentError, "libo11y: the recorder sets #{reserved.join(", ")} itself" unless reserved.empty?
|
|
33
|
+
|
|
29
34
|
@span_exporter = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
|
|
30
35
|
@metric_reader = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new
|
|
31
|
-
@metric_reader.define_singleton_method(:shutdown) do |timeout: nil|
|
|
32
|
-
_ = timeout
|
|
33
|
-
0
|
|
34
|
-
end
|
|
35
|
-
@metric_reader.define_singleton_method(:force_flush) do |timeout: nil|
|
|
36
|
-
_ = timeout
|
|
37
|
-
0
|
|
38
|
-
end
|
|
39
36
|
@log_exporter = OpenTelemetry::SDK::Logs::Export::InMemoryLogRecordExporter.new
|
|
40
37
|
@console_io = StringIO.new
|
|
38
|
+
# nil for an option means its default, here as in Options.
|
|
39
|
+
given = extra_opts.compact
|
|
40
|
+
@console_enabled = given.fetch(:console_enabled, true)
|
|
41
41
|
|
|
42
42
|
@telemetry = Libo11y.init(
|
|
43
43
|
service_name: service_name,
|
|
@@ -48,8 +48,8 @@ module Libo11y
|
|
|
48
48
|
metric_reader: @metric_reader,
|
|
49
49
|
log_exporter: @log_exporter,
|
|
50
50
|
console_writer: @console_io,
|
|
51
|
-
runtime_metrics:
|
|
52
|
-
**
|
|
51
|
+
runtime_metrics: given.fetch(:runtime_metrics, false),
|
|
52
|
+
**given.except(:runtime_metrics)
|
|
53
53
|
)
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -68,18 +68,26 @@ module Libo11y
|
|
|
68
68
|
stripped = line.strip
|
|
69
69
|
next if stripped.empty?
|
|
70
70
|
|
|
71
|
-
JSON.parse(stripped)
|
|
71
|
+
JSON.parse(stripped, max_nesting: false)
|
|
72
72
|
end
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
+
# The metrics as they stand now: what this collection returns, each
|
|
76
|
+
# metric once, with its current cumulative value. The pull exporter
|
|
77
|
+
# also keeps every earlier collection, which this leaves out.
|
|
75
78
|
def metrics
|
|
79
|
+
collected = @metric_reader.metric_snapshots.length
|
|
76
80
|
@metric_reader.pull
|
|
77
|
-
@metric_reader.metric_snapshots
|
|
81
|
+
@metric_reader.metric_snapshots.drop(collected)
|
|
78
82
|
end
|
|
79
83
|
|
|
84
|
+
# The exemplars recorded so far, with their metric and data point. The
|
|
85
|
+
# SDK hands an exemplar out in one collection only, so this reads every
|
|
86
|
+
# collection.
|
|
80
87
|
def exemplars(metric_name = nil)
|
|
88
|
+
metrics
|
|
81
89
|
out = []
|
|
82
|
-
|
|
90
|
+
@metric_reader.metric_snapshots.each do |m|
|
|
83
91
|
next if metric_name && m.name != metric_name
|
|
84
92
|
|
|
85
93
|
Array(m.data_points).each do |dp|
|
|
@@ -113,7 +121,8 @@ module Libo11y
|
|
|
113
121
|
true
|
|
114
122
|
end
|
|
115
123
|
|
|
116
|
-
#
|
|
124
|
+
# Asserts that an OTLP log record shares its trace_id and span_id with a
|
|
125
|
+
# finished span, and a console record too when the console is on.
|
|
117
126
|
def assert_correlated!
|
|
118
127
|
span_pairs = spans.map { |s| [s.hex_trace_id, s.hex_span_id] }
|
|
119
128
|
raise AssertionError, "no finished spans recorded" if span_pairs.empty?
|
|
@@ -121,7 +130,8 @@ module Libo11y
|
|
|
121
130
|
matched_log = logs.any? { |lr| span_pairs.include?([hex(lr.trace_id), hex(lr.span_id)]) }
|
|
122
131
|
raise AssertionError, "no OTLP log record correlated with any recorded span" unless matched_log
|
|
123
132
|
|
|
124
|
-
|
|
133
|
+
# A recorder whose console is off has no console records to match.
|
|
134
|
+
matched_console = !@console_enabled || console_records.any? do |rec|
|
|
125
135
|
span_pairs.include?([rec["trace_id"], rec["span_id"]])
|
|
126
136
|
end
|
|
127
137
|
raise AssertionError, "no console log record correlated with any recorded span" unless matched_console
|
|
@@ -194,8 +204,10 @@ module Libo11y
|
|
|
194
204
|
Array(metric.data_points).each { |dp| surfaces << ["metric #{metric.name.inspect}", dp.attributes] }
|
|
195
205
|
end
|
|
196
206
|
|
|
207
|
+
# The handle's policy knows the application registries' keys too.
|
|
208
|
+
policy = @telemetry.policy
|
|
197
209
|
surfaces.flat_map do |surface, attributes|
|
|
198
|
-
(attributes || {}).keys.select { |key|
|
|
210
|
+
(attributes || {}).keys.select { |key| policy.lookup(Internal::SensitivityPolicy.normalize_key(key)) == Spec::Sensitivity::SECRET }
|
|
199
211
|
.map { |key| "#{surface} carries secret-classified attribute #{key.to_s.inspect} in the clear" }
|
|
200
212
|
end
|
|
201
213
|
end
|
data/lib/libo11y/version.rb
CHANGED
data/lib/libo11y.rb
CHANGED
|
@@ -7,7 +7,7 @@ require_relative "libo11y/spec"
|
|
|
7
7
|
require_relative "libo11y/redact"
|
|
8
8
|
require_relative "libo11y/record"
|
|
9
9
|
require_relative "libo11y/detached"
|
|
10
|
-
require_relative "libo11y/
|
|
10
|
+
require_relative "libo11y/child_process"
|
|
11
11
|
require_relative "libo11y/signals"
|
|
12
12
|
require_relative "libo11y/telemetry"
|
|
13
13
|
require_relative "libo11y/rack"
|