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
|
@@ -395,7 +395,13 @@ module Libo11y
|
|
|
395
395
|
end
|
|
396
396
|
|
|
397
397
|
def sanitize(raw)
|
|
398
|
-
|
|
398
|
+
text = raw.to_s
|
|
399
|
+
bytes = begin
|
|
400
|
+
text.encode(Encoding::UTF_8).b
|
|
401
|
+
rescue EncodingError
|
|
402
|
+
text.b
|
|
403
|
+
end
|
|
404
|
+
sanitize_bytes(bytes)
|
|
399
405
|
end
|
|
400
406
|
|
|
401
407
|
def sanitize_bytes(raw)
|
data/lib/libo11y/internal/env.rb
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "openssl"
|
|
4
|
-
require "
|
|
5
|
-
|
|
4
|
+
require "uri"
|
|
5
|
+
require_relative "sdk_load"
|
|
6
|
+
require_relative "policy"
|
|
6
7
|
require_relative "../errors"
|
|
7
8
|
|
|
8
9
|
module Libo11y
|
|
@@ -13,11 +14,21 @@ module Libo11y
|
|
|
13
14
|
|
|
14
15
|
module_function
|
|
15
16
|
|
|
17
|
+
# @raise [ArgumentError] for an env option that is neither nil, a Hash
|
|
18
|
+
# nor HERMETIC
|
|
16
19
|
def resolve_map(env_opt)
|
|
17
20
|
return {} if env_opt.equal?(Libo11y::HERMETIC)
|
|
18
|
-
return ENV.to_h if env_opt.nil?
|
|
19
21
|
|
|
20
|
-
env_opt.
|
|
22
|
+
raw = if env_opt.nil?
|
|
23
|
+
ENV.to_h
|
|
24
|
+
elsif env_opt.is_a?(Hash)
|
|
25
|
+
env_opt
|
|
26
|
+
else
|
|
27
|
+
raise ArgumentError, "libo11y: env must be a Hash or Libo11y::HERMETIC, got #{env_opt.class}"
|
|
28
|
+
end
|
|
29
|
+
raw.each_with_object({}) do |(k, v), out|
|
|
30
|
+
out[SensitivityPolicy.utf8_safe(k.to_s)] = SensitivityPolicy.utf8_safe(v.to_s)
|
|
31
|
+
end
|
|
21
32
|
end
|
|
22
33
|
|
|
23
34
|
# The first of the named variables that is set to more than blanks,
|
|
@@ -25,7 +36,7 @@ module Libo11y
|
|
|
25
36
|
# therefore falls back to the general one.
|
|
26
37
|
def value(env_map, *names)
|
|
27
38
|
names.each do |name|
|
|
28
|
-
raw = env_map[name].to_s.strip
|
|
39
|
+
raw = SensitivityPolicy.utf8_safe(env_map[name].to_s).strip
|
|
29
40
|
return raw unless raw.empty?
|
|
30
41
|
end
|
|
31
42
|
nil
|
|
@@ -36,13 +47,15 @@ module Libo11y
|
|
|
36
47
|
end
|
|
37
48
|
|
|
38
49
|
def sdk_disabled?(env_map)
|
|
39
|
-
env_map["OTEL_SDK_DISABLED"].to_s.strip.downcase == "true"
|
|
50
|
+
SensitivityPolicy.utf8_safe(env_map["OTEL_SDK_DISABLED"].to_s).strip.downcase == "true"
|
|
40
51
|
end
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
# The pairs of a "k=v,k2=v2" list, as OTEL_RESOURCE_ATTRIBUTES and the
|
|
54
|
+
# OTLP header variables hold one. Values are percent-decoded, and a
|
|
55
|
+
# pair with no "=" or no key is skipped.
|
|
56
|
+
def each_pair(raw)
|
|
57
|
+
return if raw.nil? || raw.strip.empty?
|
|
44
58
|
|
|
45
|
-
out = {}
|
|
46
59
|
raw.split(",").each do |pair|
|
|
47
60
|
k, v = pair.split("=", 2)
|
|
48
61
|
next if k.nil? || v.nil?
|
|
@@ -50,41 +63,47 @@ module Libo11y
|
|
|
50
63
|
key = k.strip
|
|
51
64
|
next if key.empty?
|
|
52
65
|
|
|
53
|
-
|
|
66
|
+
yield key, decode_percent(v.strip)
|
|
54
67
|
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def parse_pairs(raw)
|
|
71
|
+
out = {}
|
|
72
|
+
each_pair(raw) { |key, value| out[key] = value }
|
|
55
73
|
out
|
|
56
74
|
end
|
|
57
75
|
|
|
76
|
+
def parse_resource_attributes(raw)
|
|
77
|
+
parse_pairs(raw)
|
|
78
|
+
end
|
|
79
|
+
|
|
58
80
|
# Decodes %XX escapes as the bytes of UTF-8 text. A % that starts no
|
|
59
81
|
# escape stays, and bytes that are not UTF-8 are replaced.
|
|
60
82
|
def decode_percent(str)
|
|
61
83
|
str.b.gsub(/%([0-9A-Fa-f]{2})/) { Regexp.last_match(1).hex.chr }.force_encoding(Encoding::UTF_8).scrub
|
|
62
84
|
end
|
|
63
85
|
|
|
86
|
+
# The headers of an OTLP header variable. A header name is not
|
|
87
|
+
# case-sensitive, so of two that differ in case only the later stays.
|
|
64
88
|
def parse_headers(raw)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
k, v = pair.split("=", 2)
|
|
70
|
-
next if k.nil? || v.nil?
|
|
71
|
-
|
|
72
|
-
key = k.strip
|
|
73
|
-
next if key.empty?
|
|
74
|
-
|
|
75
|
-
out[key] = decode_percent(v.strip)
|
|
89
|
+
headers = {}
|
|
90
|
+
each_pair(raw) do |name, value|
|
|
91
|
+
headers.delete_if { |known, _| known.casecmp?(name) }
|
|
92
|
+
headers[name] = value
|
|
76
93
|
end
|
|
77
|
-
|
|
94
|
+
headers
|
|
78
95
|
end
|
|
79
96
|
|
|
80
97
|
def parse_exporter_choice(env_map, var_name, default:)
|
|
81
98
|
raw = env_map[var_name]
|
|
82
99
|
return default if raw.nil? || raw.strip.empty?
|
|
83
100
|
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
parts = raw.split(",", -1).map { |part| part.strip.downcase }
|
|
102
|
+
if parts.length != 1 || !SUPPORTED_EXPORTERS.include?(parts.first)
|
|
103
|
+
raise UnsupportedProtocol, "o11y: unsupported exporter #{raw.strip.inspect} in #{var_name}"
|
|
104
|
+
end
|
|
86
105
|
|
|
87
|
-
first
|
|
106
|
+
parts.first
|
|
88
107
|
end
|
|
89
108
|
|
|
90
109
|
def parse_protocol(env_map, signal_var)
|
|
@@ -99,19 +118,54 @@ module Libo11y
|
|
|
99
118
|
proto
|
|
100
119
|
end
|
|
101
120
|
|
|
121
|
+
# The URL a signal's OTLP exporter sends to: the signal's own variable
|
|
122
|
+
# as given, else the general endpoint with the signal's path after its
|
|
123
|
+
# own, else the default.
|
|
124
|
+
#
|
|
125
|
+
# @raise [ExporterError] for an endpoint that is not an absolute http
|
|
126
|
+
# or https URL. The message names the variable and never the value,
|
|
127
|
+
# which may hold credentials.
|
|
102
128
|
def signal_endpoint(env_map, signal_var, path_suffix)
|
|
103
|
-
specific = env_map
|
|
104
|
-
|
|
129
|
+
specific = value(env_map, signal_var)
|
|
130
|
+
if specific
|
|
131
|
+
uri = checked_endpoint(specific, signal_var)
|
|
132
|
+
# OTLP/HTTP sends to the root path when a signal's URL has none.
|
|
133
|
+
uri.path = "/" if uri.path.empty?
|
|
134
|
+
return uri.to_s
|
|
135
|
+
end
|
|
105
136
|
|
|
106
|
-
base = env_map
|
|
107
|
-
|
|
108
|
-
|
|
137
|
+
base = value(env_map, "OTEL_EXPORTER_OTLP_ENDPOINT")
|
|
138
|
+
return "http://localhost:4318#{path_suffix}" if base.nil?
|
|
139
|
+
|
|
140
|
+
uri = checked_endpoint(base, "OTEL_EXPORTER_OTLP_ENDPOINT")
|
|
141
|
+
stop = uri.path.bytesize
|
|
142
|
+
stop -= 1 while stop.positive? && uri.path.getbyte(stop - 1) == 47
|
|
143
|
+
uri.path = "#{uri.path.byteslice(0, stop)}#{path_suffix}"
|
|
144
|
+
uri.to_s
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The parsed endpoint. The error is raised outside the rescue of the
|
|
148
|
+
# parser's own error, so it has no cause: that error's message quotes
|
|
149
|
+
# the value.
|
|
150
|
+
def checked_endpoint(raw, variable)
|
|
151
|
+
uri = begin
|
|
152
|
+
URI.parse(raw)
|
|
153
|
+
rescue URI::InvalidURIError
|
|
154
|
+
nil
|
|
155
|
+
end
|
|
156
|
+
return uri if uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
|
|
157
|
+
|
|
158
|
+
raise ExporterError, "o11y: #{variable} is not an absolute http or https URL"
|
|
109
159
|
end
|
|
110
160
|
|
|
161
|
+
# The headers a signal's OTLP exporter sends: the general ones, with
|
|
162
|
+
# the signal's own laid over them. A header name is not case-sensitive,
|
|
163
|
+
# so the signal's header replaces a general one of the same name in
|
|
164
|
+
# another case. Two of them would go out as one header with both values.
|
|
111
165
|
def signal_headers(env_map, signal_var)
|
|
112
166
|
base = parse_headers(env_map["OTEL_EXPORTER_OTLP_HEADERS"])
|
|
113
167
|
specific = parse_headers(env_map[signal_var])
|
|
114
|
-
base.merge(specific)
|
|
168
|
+
base.reject { |name, _| specific.keys.any? { |own| own.casecmp?(name) } }.merge(specific)
|
|
115
169
|
end
|
|
116
170
|
|
|
117
171
|
def signal_timeout(env_map, signal_var, default: 10.0)
|
|
@@ -119,7 +173,7 @@ module Libo11y
|
|
|
119
173
|
return default if raw.nil?
|
|
120
174
|
|
|
121
175
|
ms = Float(raw)
|
|
122
|
-
ms.positive? ? ms / 1000.0 : default
|
|
176
|
+
ms.finite? && ms.positive? ? ms / 1000.0 : default
|
|
123
177
|
rescue ArgumentError
|
|
124
178
|
default
|
|
125
179
|
end
|
|
@@ -146,8 +200,16 @@ module Libo11y
|
|
|
146
200
|
}
|
|
147
201
|
end
|
|
148
202
|
|
|
203
|
+
# The exemplar filter: the option, else OTEL_METRICS_EXEMPLAR_FILTER,
|
|
204
|
+
# else trace_based.
|
|
205
|
+
#
|
|
206
|
+
# @raise [ArgumentError] for an option that is not one of ExemplarFilter::ALL
|
|
149
207
|
def parse_exemplar_filter(opt_filter, env_map)
|
|
150
|
-
|
|
208
|
+
unless opt_filter.nil?
|
|
209
|
+
return opt_filter if ExemplarFilter::ALL.include?(opt_filter)
|
|
210
|
+
|
|
211
|
+
raise ArgumentError, "libo11y: exemplar_filter must be one of #{ExemplarFilter::ALL.join(", ")}, got #{opt_filter.inspect}"
|
|
212
|
+
end
|
|
151
213
|
|
|
152
214
|
raw = env_map["OTEL_METRICS_EXEMPLAR_FILTER"]
|
|
153
215
|
return ExemplarFilter::TRACE_BASED if raw.nil? || raw.strip.empty?
|
|
@@ -157,36 +219,85 @@ module Libo11y
|
|
|
157
219
|
when "always_on" then ExemplarFilter::ALWAYS_ON
|
|
158
220
|
when "always_off" then ExemplarFilter::ALWAYS_OFF
|
|
159
221
|
else
|
|
160
|
-
|
|
222
|
+
# As the Go and Python implementations: a name that is not known
|
|
223
|
+
# does not stop the service.
|
|
224
|
+
OpenTelemetry.logger.warn("libo11y: OTEL_METRICS_EXEMPLAR_FILTER=#{raw.inspect} names no exemplar filter, using trace_based")
|
|
225
|
+
ExemplarFilter::TRACE_BASED
|
|
161
226
|
end
|
|
162
227
|
end
|
|
163
228
|
|
|
229
|
+
SAMPLERS = %w[always_on always_off traceidratio parentbased_always_on parentbased_always_off parentbased_traceidratio].freeze
|
|
230
|
+
DEFAULT_SAMPLER = "parentbased_always_on"
|
|
231
|
+
|
|
232
|
+
# The name of the sampler OTEL_TRACES_SAMPLER selects: the default when
|
|
233
|
+
# the variable is unset or names no sampler libo11y knows, which is the
|
|
234
|
+
# sampler that then runs.
|
|
235
|
+
def sampler_name(env_map)
|
|
236
|
+
name = value(env_map, "OTEL_TRACES_SAMPLER").to_s.strip.downcase
|
|
237
|
+
SAMPLERS.include?(name) ? name : DEFAULT_SAMPLER
|
|
238
|
+
end
|
|
239
|
+
|
|
164
240
|
# The span limits, from env_map and the SDK's defaults. The SDK's own
|
|
165
241
|
# defaults read the process environment when the SDK loads, so they are
|
|
166
242
|
# never used.
|
|
167
243
|
def span_limits(env_map)
|
|
168
|
-
|
|
169
|
-
general_length = "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT"
|
|
170
|
-
OpenTelemetry::SDK::Trace::SpanLimits.new(
|
|
171
|
-
attribute_count_limit: count_limit(env_map, "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", general_count),
|
|
172
|
-
attribute_length_limit: length_limit(env_map, "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", general_length),
|
|
173
|
-
event_count_limit: count_limit(env_map, "OTEL_SPAN_EVENT_COUNT_LIMIT"),
|
|
174
|
-
link_count_limit: count_limit(env_map, "OTEL_SPAN_LINK_COUNT_LIMIT"),
|
|
175
|
-
event_attribute_count_limit: count_limit(env_map, "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"),
|
|
176
|
-
event_attribute_length_limit: length_limit(env_map, "OTEL_EVENT_ATTRIBUTE_VALUE_LENGTH_LIMIT", general_length),
|
|
177
|
-
link_attribute_count_limit: count_limit(env_map, "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT")
|
|
178
|
-
)
|
|
244
|
+
OpenTelemetry::SDK::Trace::SpanLimits.new(**limits(env_map, SPAN_LIMIT_VARIABLES))
|
|
179
245
|
end
|
|
180
246
|
|
|
181
247
|
def log_record_limits(env_map)
|
|
182
|
-
OpenTelemetry::SDK::Logs::LogRecordLimits.new(
|
|
183
|
-
attribute_count_limit: count_limit(env_map, "OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT", "OTEL_ATTRIBUTE_COUNT_LIMIT"),
|
|
184
|
-
attribute_length_limit: length_limit(env_map, "OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT", "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT")
|
|
185
|
-
)
|
|
248
|
+
OpenTelemetry::SDK::Logs::LogRecordLimits.new(**limits(env_map, LOG_RECORD_LIMIT_VARIABLES))
|
|
186
249
|
end
|
|
187
250
|
|
|
188
|
-
#
|
|
189
|
-
|
|
251
|
+
# The variables each limit is read from, the first that is set.
|
|
252
|
+
SPAN_LIMIT_VARIABLES = {
|
|
253
|
+
attribute_count_limit: %w[OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT OTEL_ATTRIBUTE_COUNT_LIMIT],
|
|
254
|
+
attribute_length_limit: %w[OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT],
|
|
255
|
+
event_count_limit: %w[OTEL_SPAN_EVENT_COUNT_LIMIT],
|
|
256
|
+
link_count_limit: %w[OTEL_SPAN_LINK_COUNT_LIMIT],
|
|
257
|
+
event_attribute_count_limit: %w[OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT],
|
|
258
|
+
event_attribute_length_limit: %w[OTEL_EVENT_ATTRIBUTE_VALUE_LENGTH_LIMIT OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT],
|
|
259
|
+
link_attribute_count_limit: %w[OTEL_LINK_ATTRIBUTE_COUNT_LIMIT]
|
|
260
|
+
}.freeze
|
|
261
|
+
LOG_RECORD_LIMIT_VARIABLES = {
|
|
262
|
+
attribute_count_limit: %w[OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT OTEL_ATTRIBUTE_COUNT_LIMIT],
|
|
263
|
+
attribute_length_limit: %w[OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT]
|
|
264
|
+
}.freeze
|
|
265
|
+
# The least value length limit the SDK accepts.
|
|
266
|
+
MIN_VALUE_LENGTH = 32
|
|
267
|
+
|
|
268
|
+
def limits(env_map, variables)
|
|
269
|
+
variables.to_h do |limit, names|
|
|
270
|
+
[limit, limit.end_with?("length_limit") ? length_limit(env_map, *names) : count_limit(env_map, *names)]
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# The limit variables of env_map that hold a value the SDK refuses,
|
|
275
|
+
# each once: its name, its value, and :count or :length for the kind
|
|
276
|
+
# of limit, which says what applies in its place.
|
|
277
|
+
def refused_limits(env_map)
|
|
278
|
+
[SPAN_LIMIT_VARIABLES, LOG_RECORD_LIMIT_VARIABLES].flat_map(&:to_a).filter_map do |limit, names|
|
|
279
|
+
name = names.find { |candidate| value(env_map, candidate) }
|
|
280
|
+
next if name.nil?
|
|
281
|
+
|
|
282
|
+
length = limit.end_with?("length_limit")
|
|
283
|
+
number = Integer(value(env_map, name), 10, exception: false)
|
|
284
|
+
next if number && number >= (length ? MIN_VALUE_LENGTH : 1)
|
|
285
|
+
|
|
286
|
+
[name, value(env_map, name), length ? :length : :count]
|
|
287
|
+
end.uniq
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# The variable a value length limit is read from and the limit, or
|
|
291
|
+
# nil when none applies.
|
|
292
|
+
def length_limit_source(env_map, names)
|
|
293
|
+
limit = length_limit(env_map, *names)
|
|
294
|
+
limit && [names.find { |candidate| value(env_map, candidate) }, limit]
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# A count limit: the first of the named variables that is set, when
|
|
298
|
+
# it holds a positive integer, else the OpenTelemetry default of 128.
|
|
299
|
+
# A first variable that holds anything else does not fall through to
|
|
300
|
+
# the next, as in the OpenTelemetry Go SDK.
|
|
190
301
|
def count_limit(env_map, *names)
|
|
191
302
|
count = Integer(value(env_map, *names) || "", 10)
|
|
192
303
|
count.positive? ? count : 128
|
|
@@ -194,23 +305,30 @@ module Libo11y
|
|
|
194
305
|
128
|
|
195
306
|
end
|
|
196
307
|
|
|
197
|
-
# A value length limit, or nil for none. The SDK accepts 32 or more
|
|
308
|
+
# A value length limit, or nil for none. The SDK accepts 32 or more,
|
|
309
|
+
# so a smaller limit is not applied, which Pipeline warns of.
|
|
198
310
|
def length_limit(env_map, *names)
|
|
199
311
|
length = Integer(value(env_map, *names) || "", 10)
|
|
200
|
-
length >=
|
|
312
|
+
length >= MIN_VALUE_LENGTH ? length : nil
|
|
201
313
|
rescue ArgumentError
|
|
202
314
|
nil
|
|
203
315
|
end
|
|
204
316
|
|
|
205
317
|
# Whether an OTLP exporter verifies the collector's certificate. It
|
|
206
|
-
# does, unless
|
|
207
|
-
#
|
|
208
|
-
#
|
|
318
|
+
# does, unless OpenTelemetry Ruby's variable that turns verification
|
|
319
|
+
# off is "true" in env_map and the one that turns it on is not. The
|
|
320
|
+
# upstream trace and metric exporters turn verification off when the
|
|
321
|
+
# first variable is set to anything, "false" included. libo11y reads
|
|
322
|
+
# it as the upstream log exporter does, for all three signals. The
|
|
323
|
+
# exporters' own default reads the process environment, so libo11y
|
|
324
|
+
# always passes this one.
|
|
209
325
|
def ssl_verify_mode(env_map)
|
|
210
|
-
|
|
211
|
-
|
|
326
|
+
off = flag?(env_map, "OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_NONE") && !flag?(env_map, "OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_PEER")
|
|
327
|
+
off ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
|
328
|
+
end
|
|
212
329
|
|
|
213
|
-
|
|
330
|
+
def flag?(env_map, name)
|
|
331
|
+
env_map[name].to_s.strip.casecmp?("true")
|
|
214
332
|
end
|
|
215
333
|
|
|
216
334
|
# OpenTelemetry Ruby's switch for starting a batch processor's worker
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Libo11y
|
|
4
|
+
module Internal
|
|
5
|
+
# The error.type of an error.
|
|
6
|
+
module ErrorType
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# The name of the error's class. A class without a name, such as one
|
|
10
|
+
# made with Class.new, gives the nearest ancestor that has one, so the
|
|
11
|
+
# value is always a name and never an object's address.
|
|
12
|
+
def of(error)
|
|
13
|
+
error.class.ancestors.find { |ancestor| ancestor.is_a?(Class) && ancestor.name }.name
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -16,6 +16,48 @@ module Libo11y
|
|
|
16
16
|
"http.client.request.duration" => HTTP,
|
|
17
17
|
"db.client.operation.duration" => DB
|
|
18
18
|
}.freeze
|
|
19
|
+
|
|
20
|
+
# The instrumentation scopes of the integrations that record them.
|
|
21
|
+
RACK_SCOPE = "libo11y.rack"
|
|
22
|
+
HTTP_SCOPE = "libo11y.http"
|
|
23
|
+
DB_SCOPE = "libo11y.db"
|
|
24
|
+
|
|
25
|
+
# The scope of each histogram, which a view may select by.
|
|
26
|
+
SCOPES = {
|
|
27
|
+
"http.server.request.duration" => RACK_SCOPE,
|
|
28
|
+
"http.client.request.duration" => HTTP_SCOPE,
|
|
29
|
+
"db.client.operation.duration" => DB_SCOPE
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
Stream = Struct.new(:name, :instrument_kind, :unit, :instrumentation_scope)
|
|
33
|
+
Scope = Struct.new(:name, :version)
|
|
34
|
+
private_constant :Stream, :Scope
|
|
35
|
+
|
|
36
|
+
module_function
|
|
37
|
+
|
|
38
|
+
# Whether a view, given as the arguments of MeterProvider#add_view
|
|
39
|
+
# with its :name, selects the duration histogram of that name. The
|
|
40
|
+
# SDK's own rules decide: name pattern, kind, unit, and the meter's
|
|
41
|
+
# name and version. A histogram from the histogram_buckets option has
|
|
42
|
+
# no known meter, so its name and its kind decide.
|
|
43
|
+
#
|
|
44
|
+
# The SDK calls the class that holds those rules internal. Should a
|
|
45
|
+
# release change it so that it cannot be asked, the name decides and
|
|
46
|
+
# a warning says so.
|
|
47
|
+
def selected_by?(view, name)
|
|
48
|
+
registered = OpenTelemetry::SDK::Metrics::View::RegisteredView.new(view[:name], **view.except(:name))
|
|
49
|
+
scope = SCOPES[name]
|
|
50
|
+
# The SDK takes a view without a name for one that selects by its
|
|
51
|
+
# other fields alone.
|
|
52
|
+
return (view[:name].nil? || registered.name_match(name)) && [nil, :histogram].include?(view[:type]) if scope.nil?
|
|
53
|
+
|
|
54
|
+
begin
|
|
55
|
+
registered.match_instrument?(Stream.new(name, :histogram, "s", Scope.new(scope, Libo11y::VERSION)))
|
|
56
|
+
rescue StandardError => e
|
|
57
|
+
OpenTelemetry.logger.warn("libo11y: could not ask the SDK whether a view selects #{name} (#{e.class}), so its name decides")
|
|
58
|
+
registered.name_match(name)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
19
61
|
end
|
|
20
62
|
end
|
|
21
63
|
end
|
|
@@ -26,8 +26,17 @@ module Libo11y
|
|
|
26
26
|
nil
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
INSTANCE_LOCK = Mutex.new
|
|
30
|
+
private_constant :INSTANCE_LOCK
|
|
31
|
+
|
|
32
|
+
# The random service.instance.id of this process: one UUIDv4 for every
|
|
33
|
+
# handle the process builds, as the specification asks. A forked child
|
|
34
|
+
# is another process and draws its own.
|
|
29
35
|
def random_instance_id
|
|
30
|
-
|
|
36
|
+
INSTANCE_LOCK.synchronize do
|
|
37
|
+
@process_instance = [Process.pid, SecureRandom.uuid] unless @process_instance&.first == Process.pid
|
|
38
|
+
@process_instance.last
|
|
39
|
+
end
|
|
31
40
|
end
|
|
32
41
|
end
|
|
33
42
|
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "timeout"
|
|
4
|
+
require_relative "bridges"
|
|
5
|
+
require_relative "quiet_inspect"
|
|
6
|
+
|
|
7
|
+
module Libo11y
|
|
8
|
+
module Internal
|
|
9
|
+
# The exporters, processors and reader the options inject. libo11y owns
|
|
10
|
+
# them from the moment a handle starts to build. A provider that takes
|
|
11
|
+
# one shuts it down with itself. One that no provider took, because
|
|
12
|
+
# OTEL_SDK_DISABLED switched the SDK off or the start failed first, is
|
|
13
|
+
# shut down here, once.
|
|
14
|
+
class InjectedParts
|
|
15
|
+
include QuietInspect
|
|
16
|
+
|
|
17
|
+
def initialize(options)
|
|
18
|
+
@parts_by_role = {
|
|
19
|
+
traces_exporter: [options.span_exporter].compact,
|
|
20
|
+
traces_processor: Array(options.span_processors).compact,
|
|
21
|
+
metrics: [options.metric_reader].compact,
|
|
22
|
+
logs_exporter: [options.log_exporter].compact,
|
|
23
|
+
logs_processor: Array(options.log_processors).compact
|
|
24
|
+
}
|
|
25
|
+
@parts = {
|
|
26
|
+
traces: @parts_by_role[:traces_exporter] + @parts_by_role[:traces_processor],
|
|
27
|
+
metrics: @parts_by_role[:metrics],
|
|
28
|
+
logs: @parts_by_role[:logs_exporter] + @parts_by_role[:logs_processor]
|
|
29
|
+
}
|
|
30
|
+
@handed = {}
|
|
31
|
+
@stopped = {}.compare_by_identity
|
|
32
|
+
@unused_in_flight = Hash.new(0)
|
|
33
|
+
@mutex = Mutex.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Whether any unused-part shutdown worker for signal is still running.
|
|
37
|
+
def unused_in_flight?(signal)
|
|
38
|
+
@mutex.synchronize { @unused_in_flight[signal].positive? }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Atomically claims every injected part for this handle before any
|
|
42
|
+
# provider is built, raising ArgumentError if another active handle or
|
|
43
|
+
# provider already owns one of them.
|
|
44
|
+
def reserve!
|
|
45
|
+
Bridges::Ownership.reserve_parts!(@parts_by_role, self)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Marks the parts of signal as owned by the provider built around them.
|
|
49
|
+
def hand_over(signal)
|
|
50
|
+
@mutex.synchronize { @handed[signal] = true }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Whether signal's injected parts were handed over to its provider.
|
|
54
|
+
def handed?(signal)
|
|
55
|
+
@mutex.synchronize { @handed[signal] == true }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Releases ownership entries held by this handle for one signal once its
|
|
59
|
+
# shutdown thread has finished.
|
|
60
|
+
def release_signal(signal)
|
|
61
|
+
Bridges::Ownership.release_parts(@parts.fetch(signal), self)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Releases ownership entries held by this handle for one part once its
|
|
65
|
+
# discard thread has finished.
|
|
66
|
+
def release_part(part)
|
|
67
|
+
Bridges::Ownership.release_parts([part], self)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Releases ownership entries held by this handle after shutdown or abandon.
|
|
71
|
+
def release_all!
|
|
72
|
+
Bridges::Ownership.release_parts(@parts.values.flatten, self)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Shuts down the parts of signal that no provider owns, each once,
|
|
76
|
+
# concurrently under the shared monotonic deadline so a blocked part
|
|
77
|
+
# never starves healthy siblings. Every part's ownership stays claimed
|
|
78
|
+
# until its own shutdown thread exits.
|
|
79
|
+
def stop_unused(signal, timeout:)
|
|
80
|
+
parts = unclaimed(signal)
|
|
81
|
+
return nil if parts.empty?
|
|
82
|
+
|
|
83
|
+
deadline = Thread.current[Bridges::DEADLINE_KEY] || (monotonic + timeout)
|
|
84
|
+
errors = Array.new(parts.length)
|
|
85
|
+
@mutex.synchronize { @unused_in_flight[signal] += parts.length }
|
|
86
|
+
threads = spawn_unused_workers(signal, parts, deadline, errors)
|
|
87
|
+
join_unused_workers(parts, threads, deadline, errors)
|
|
88
|
+
collected = errors.compact
|
|
89
|
+
raise collected.first if collected.length == 1
|
|
90
|
+
raise Timeout::Error, collected.map(&:message).join("; ") if collected.any?(Timeout::Error)
|
|
91
|
+
raise ExporterError, collected.map(&:message).join("; ") unless collected.empty?
|
|
92
|
+
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# One callable for each part no provider owns, which shuts that part
|
|
97
|
+
# down, for a start that failed. Nothing was registered with them, so
|
|
98
|
+
# there is nothing to wait for. The caller runs each apart from the
|
|
99
|
+
# others, so a part that blocks holds up none of them.
|
|
100
|
+
def discards
|
|
101
|
+
@parts.keys.flat_map { |signal| unclaimed(signal) }.map do |part|
|
|
102
|
+
lambda do
|
|
103
|
+
if part.respond_to?(:shutdown)
|
|
104
|
+
Bridges.takes_timeout?(part, :shutdown) ? part.shutdown(timeout: 0) : part.shutdown
|
|
105
|
+
end
|
|
106
|
+
ensure
|
|
107
|
+
release_part(part)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def monotonic
|
|
115
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def spawn_unused_workers(signal, parts, deadline, errors)
|
|
119
|
+
parts.each_with_index.map do |part, idx|
|
|
120
|
+
Thread.new do
|
|
121
|
+
Thread.current[Bridges::DEADLINE_KEY] = deadline
|
|
122
|
+
next unless part.respond_to?(:shutdown)
|
|
123
|
+
|
|
124
|
+
rem = [deadline - monotonic, 0.001].max
|
|
125
|
+
status = Bridges.takes_timeout?(part, :shutdown) ? part.shutdown(timeout: rem) : part.shutdown
|
|
126
|
+
code = Bridges.normalize_lifecycle_status(status)
|
|
127
|
+
if code == OpenTelemetry::SDK::Trace::Export::TIMEOUT
|
|
128
|
+
errors[idx] = Timeout::Error.new("the injected #{part.class} timed out")
|
|
129
|
+
elsif code != OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
130
|
+
errors[idx] = ExporterError.new("the injected #{part.class} answered #{status.inspect}")
|
|
131
|
+
end
|
|
132
|
+
rescue StandardError => e
|
|
133
|
+
errors[idx] = e
|
|
134
|
+
ensure
|
|
135
|
+
Thread.current[Bridges::DEADLINE_KEY] = nil
|
|
136
|
+
release_part(part)
|
|
137
|
+
@mutex.synchronize { @unused_in_flight[signal] -= 1 }
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def join_unused_workers(parts, threads, deadline, errors)
|
|
143
|
+
threads.each_with_index do |thread, idx|
|
|
144
|
+
rem = [deadline - monotonic, 0].max
|
|
145
|
+
next if thread.join(rem)
|
|
146
|
+
|
|
147
|
+
errors[idx] ||= Timeout::Error.new("the injected #{parts[idx].class} timed out")
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def unclaimed(signal)
|
|
152
|
+
@mutex.synchronize do
|
|
153
|
+
next [] if @handed[signal]
|
|
154
|
+
|
|
155
|
+
# One at a time, so a part listed twice is claimed once. Any part
|
|
156
|
+
# already owned by another active handle or provider is left alone;
|
|
157
|
+
# any unowned part is atomically claimed before its shutdown runs so
|
|
158
|
+
# no other handle can acquire it while shutdown is in progress.
|
|
159
|
+
@parts.fetch(signal).select do |part|
|
|
160
|
+
next false if @stopped[part]
|
|
161
|
+
next false unless Bridges::Ownership.try_claim_part!(part, self, role: role_for(signal, part))
|
|
162
|
+
|
|
163
|
+
@stopped[part] = true
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def role_for(signal, part)
|
|
169
|
+
return :metrics if signal == :metrics
|
|
170
|
+
|
|
171
|
+
exp_role = signal == :traces ? :traces_exporter : :logs_exporter
|
|
172
|
+
return exp_role if @parts_by_role[exp_role].any? { |known| known.equal?(part) }
|
|
173
|
+
|
|
174
|
+
signal == :traces ? :traces_processor : :logs_processor
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|