libo11y 0.1.0 → 0.2.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 +71 -1
- data/README.md +7 -4
- data/exe/libo11y +8 -4
- data/lib/libo11y/db.rb +2 -1
- data/lib/libo11y/detached.rb +158 -51
- data/lib/libo11y/http_client.rb +83 -61
- data/lib/libo11y/internal/bridges.rb +107 -3
- data/lib/libo11y/internal/env.rb +91 -16
- data/lib/libo11y/internal/histogram_buckets.rb +21 -0
- data/lib/libo11y/internal/http_common.rb +39 -0
- data/lib/libo11y/internal/logging_device.rb +81 -90
- data/lib/libo11y/internal/pipeline.rb +220 -0
- data/lib/libo11y/internal/policy.rb +36 -23
- data/lib/libo11y/internal/propagators.rb +8 -11
- data/lib/libo11y/internal/resource.rb +96 -92
- data/lib/libo11y/internal/runtime_metrics.rb +32 -46
- data/lib/libo11y/internal/summary.rb +55 -0
- data/lib/libo11y/options.rb +16 -2
- data/lib/libo11y/proc.rb +6 -6
- data/lib/libo11y/rack.rb +175 -135
- data/lib/libo11y/record.rb +11 -7
- data/lib/libo11y/spec/app_registry.rb +6 -5
- data/lib/libo11y/spec/yaml_v3.rb +81 -59
- data/lib/libo11y/telemetry.rb +185 -320
- data/lib/libo11y/testing.rb +105 -56
- data/lib/libo11y/version.rb +1 -1
- metadata +19 -1
|
@@ -15,6 +15,80 @@ require_relative "policy"
|
|
|
15
15
|
module Libo11y
|
|
16
16
|
module Internal
|
|
17
17
|
module Bridges
|
|
18
|
+
# The tracer provider under OTEL_SDK_DISABLED=true. Its spans record
|
|
19
|
+
# nothing and mint no trace identity, and an inbound trace context still
|
|
20
|
+
# passes through them.
|
|
21
|
+
class NoopTracerProvider < OpenTelemetry::Trace::TracerProvider
|
|
22
|
+
def add_span_processor(_processor); end
|
|
23
|
+
|
|
24
|
+
def force_flush(timeout: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
25
|
+
OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def shutdown(timeout: nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
29
|
+
OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Collects one signal's export failures while a shutdown, a flush or the
|
|
34
|
+
# startup canary runs. An export that fails at any other time is not
|
|
35
|
+
# kept.
|
|
36
|
+
class ExportFailures
|
|
37
|
+
def initialize
|
|
38
|
+
@mutex = Mutex.new
|
|
39
|
+
@windows = []
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def record(error)
|
|
43
|
+
@mutex.synchronize { @windows.each { |window| window << error } }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Runs the block. Returns its value and the failures recorded while it
|
|
47
|
+
# ran, on any thread.
|
|
48
|
+
def during
|
|
49
|
+
window = []
|
|
50
|
+
@mutex.synchronize { @windows << window }
|
|
51
|
+
begin
|
|
52
|
+
[yield, window]
|
|
53
|
+
ensure
|
|
54
|
+
@mutex.synchronize { @windows.delete_if { |open| open.equal?(window) } }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Wraps a span, metric or log record exporter and records every export
|
|
60
|
+
# that fails, times out or raises.
|
|
61
|
+
class ObservedExporter
|
|
62
|
+
SUCCESS = OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
63
|
+
TIMEOUT = OpenTelemetry::SDK::Trace::Export::TIMEOUT
|
|
64
|
+
|
|
65
|
+
def initialize(inner, failures, signal)
|
|
66
|
+
@inner = inner
|
|
67
|
+
@failures = failures
|
|
68
|
+
@signal = signal
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def export(data, timeout: nil)
|
|
72
|
+
result = @inner.export(data, timeout: timeout)
|
|
73
|
+
unless result == SUCCESS
|
|
74
|
+
outcome = result == TIMEOUT ? "timed out" : "failed"
|
|
75
|
+
@failures.record(ExporterError.new("the #{@signal} export #{outcome}"))
|
|
76
|
+
end
|
|
77
|
+
result
|
|
78
|
+
rescue StandardError => e
|
|
79
|
+
@failures.record(e)
|
|
80
|
+
raise
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def method_missing(name, ...)
|
|
84
|
+
@inner.public_send(name, ...)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def respond_to_missing?(name, include_private = false)
|
|
88
|
+
@inner.respond_to?(name, include_private)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
18
92
|
# Wraps any span exporter so span, event, and link attributes pass through SensitivityPolicy once.
|
|
19
93
|
class PolicySpanExporter
|
|
20
94
|
def initialize(inner, policy)
|
|
@@ -387,40 +461,70 @@ module Libo11y
|
|
|
387
461
|
end
|
|
388
462
|
end
|
|
389
463
|
|
|
464
|
+
# Makes an OTLP exporter write the specification's schema URL on each
|
|
465
|
+
# resource it sends. The upstream exporters do not write a schema URL.
|
|
466
|
+
# Each one builds its request and hands the bytes to its private
|
|
467
|
+
# send_bytes, so that method is the one place to add it for all three.
|
|
468
|
+
# An exporter without that method is returned as it is, and the wire
|
|
469
|
+
# test then fails.
|
|
470
|
+
def stamp_schema_url(exporter, request_class, field)
|
|
471
|
+
return exporter unless exporter.respond_to?(:send_bytes, true)
|
|
472
|
+
|
|
473
|
+
stamp = Module.new do
|
|
474
|
+
define_method(:send_bytes) do |bytes, timeout:|
|
|
475
|
+
unless bytes.nil?
|
|
476
|
+
request = request_class.decode(bytes)
|
|
477
|
+
request.public_send(field).each { |entry| entry.schema_url = Spec::SCHEMA_URL if entry.schema_url.empty? }
|
|
478
|
+
bytes = request_class.encode(request)
|
|
479
|
+
end
|
|
480
|
+
super(bytes, timeout: timeout)
|
|
481
|
+
end
|
|
482
|
+
private :send_bytes
|
|
483
|
+
end
|
|
484
|
+
exporter.singleton_class.prepend(stamp)
|
|
485
|
+
exporter
|
|
486
|
+
end
|
|
487
|
+
|
|
390
488
|
def build_otlp_trace_exporter(env_map)
|
|
391
489
|
Env.parse_protocol(env_map, "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL")
|
|
392
490
|
tls = Env.signal_tls(env_map, "OTEL_EXPORTER_OTLP_TRACES")
|
|
393
|
-
OpenTelemetry::Exporter::OTLP::Exporter.new(
|
|
491
|
+
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
|
|
394
492
|
endpoint: Env.signal_endpoint(env_map, "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "/v1/traces"),
|
|
395
493
|
headers: Env.signal_headers(env_map, "OTEL_EXPORTER_OTLP_TRACES_HEADERS"),
|
|
396
494
|
compression: Env.signal_compression(env_map, "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION"),
|
|
397
495
|
timeout: Env.signal_timeout(env_map, "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"),
|
|
496
|
+
ssl_verify_mode: Env.ssl_verify_mode(env_map),
|
|
398
497
|
**tls
|
|
399
498
|
)
|
|
499
|
+
stamp_schema_url(exporter, Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest, :resource_spans)
|
|
400
500
|
end
|
|
401
501
|
|
|
402
502
|
def build_otlp_metric_exporter(env_map)
|
|
403
503
|
Env.parse_protocol(env_map, "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL")
|
|
404
504
|
tls = Env.signal_tls(env_map, "OTEL_EXPORTER_OTLP_METRICS")
|
|
405
|
-
OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(
|
|
505
|
+
exporter = OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new(
|
|
406
506
|
endpoint: Env.signal_endpoint(env_map, "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", "/v1/metrics"),
|
|
407
507
|
headers: Env.signal_headers(env_map, "OTEL_EXPORTER_OTLP_METRICS_HEADERS"),
|
|
408
508
|
compression: Env.signal_compression(env_map, "OTEL_EXPORTER_OTLP_METRICS_COMPRESSION"),
|
|
409
509
|
timeout: Env.signal_timeout(env_map, "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT"),
|
|
510
|
+
ssl_verify_mode: Env.ssl_verify_mode(env_map),
|
|
410
511
|
**tls
|
|
411
512
|
)
|
|
513
|
+
stamp_schema_url(exporter, Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest, :resource_metrics)
|
|
412
514
|
end
|
|
413
515
|
|
|
414
516
|
def build_otlp_log_exporter(env_map)
|
|
415
517
|
Env.parse_protocol(env_map, "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL")
|
|
416
518
|
tls = Env.signal_tls(env_map, "OTEL_EXPORTER_OTLP_LOGS")
|
|
417
|
-
OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new(
|
|
519
|
+
exporter = OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new(
|
|
418
520
|
endpoint: Env.signal_endpoint(env_map, "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", "/v1/logs"),
|
|
419
521
|
headers: Env.signal_headers(env_map, "OTEL_EXPORTER_OTLP_LOGS_HEADERS"),
|
|
420
522
|
compression: Env.signal_compression(env_map, "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION"),
|
|
421
523
|
timeout: Env.signal_timeout(env_map, "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT"),
|
|
524
|
+
ssl_verify_mode: Env.ssl_verify_mode(env_map),
|
|
422
525
|
**tls
|
|
423
526
|
)
|
|
527
|
+
stamp_schema_url(exporter, Opentelemetry::Proto::Collector::Logs::V1::ExportLogsServiceRequest, :resource_logs)
|
|
424
528
|
end
|
|
425
529
|
end
|
|
426
530
|
end
|
data/lib/libo11y/internal/env.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "opentelemetry/sdk"
|
|
5
|
+
require "opentelemetry-logs-sdk"
|
|
3
6
|
require_relative "../errors"
|
|
4
7
|
|
|
5
8
|
module Libo11y
|
|
@@ -17,6 +20,17 @@ module Libo11y
|
|
|
17
20
|
env_opt.transform_keys(&:to_s).transform_values(&:to_s)
|
|
18
21
|
end
|
|
19
22
|
|
|
23
|
+
# The first of the named variables that is set to more than blanks,
|
|
24
|
+
# with its surrounding blanks removed. An empty per-signal variable
|
|
25
|
+
# therefore falls back to the general one.
|
|
26
|
+
def value(env_map, *names)
|
|
27
|
+
names.each do |name|
|
|
28
|
+
raw = env_map[name].to_s.strip
|
|
29
|
+
return raw unless raw.empty?
|
|
30
|
+
end
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
20
34
|
def hermetic?(env_opt)
|
|
21
35
|
env_opt.equal?(Libo11y::HERMETIC)
|
|
22
36
|
end
|
|
@@ -41,10 +55,10 @@ module Libo11y
|
|
|
41
55
|
out
|
|
42
56
|
end
|
|
43
57
|
|
|
58
|
+
# Decodes %XX escapes as the bytes of UTF-8 text. A % that starts no
|
|
59
|
+
# escape stays, and bytes that are not UTF-8 are replaced.
|
|
44
60
|
def decode_percent(str)
|
|
45
|
-
str.gsub(/%([0-9A-Fa-f]{2})/) { Regexp.last_match(1).
|
|
46
|
-
rescue StandardError
|
|
47
|
-
str
|
|
61
|
+
str.b.gsub(/%([0-9A-Fa-f]{2})/) { Regexp.last_match(1).hex.chr }.force_encoding(Encoding::UTF_8).scrub
|
|
48
62
|
end
|
|
49
63
|
|
|
50
64
|
def parse_headers(raw)
|
|
@@ -74,10 +88,10 @@ module Libo11y
|
|
|
74
88
|
end
|
|
75
89
|
|
|
76
90
|
def parse_protocol(env_map, signal_var)
|
|
77
|
-
raw = env_map
|
|
78
|
-
return "http/protobuf" if raw.nil?
|
|
91
|
+
raw = value(env_map, signal_var, "OTEL_EXPORTER_OTLP_PROTOCOL")
|
|
92
|
+
return "http/protobuf" if raw.nil?
|
|
79
93
|
|
|
80
|
-
proto = raw.
|
|
94
|
+
proto = raw.downcase
|
|
81
95
|
unless SUPPORTED_PROTOCOLS.include?(proto)
|
|
82
96
|
raise UnsupportedProtocol, "o11y: unsupported OTLP protocol #{proto.inspect} (supported: http/protobuf)"
|
|
83
97
|
end
|
|
@@ -101,32 +115,35 @@ module Libo11y
|
|
|
101
115
|
end
|
|
102
116
|
|
|
103
117
|
def signal_timeout(env_map, signal_var, default: 10.0)
|
|
104
|
-
raw = env_map
|
|
105
|
-
return default if raw.nil?
|
|
118
|
+
raw = value(env_map, signal_var, "OTEL_EXPORTER_OTLP_TIMEOUT")
|
|
119
|
+
return default if raw.nil?
|
|
106
120
|
|
|
107
|
-
ms = Float(raw
|
|
121
|
+
ms = Float(raw)
|
|
108
122
|
ms.positive? ? ms / 1000.0 : default
|
|
109
123
|
rescue ArgumentError
|
|
110
124
|
default
|
|
111
125
|
end
|
|
112
126
|
|
|
113
127
|
def signal_compression(env_map, signal_var)
|
|
114
|
-
raw = env_map
|
|
115
|
-
return "gzip" if raw.nil?
|
|
128
|
+
raw = value(env_map, signal_var, "OTEL_EXPORTER_OTLP_COMPRESSION")
|
|
129
|
+
return "gzip" if raw.nil?
|
|
116
130
|
|
|
117
|
-
val = raw.
|
|
131
|
+
val = raw.downcase
|
|
118
132
|
return "gzip" if val == "gzip"
|
|
119
133
|
return "none" if val == "none"
|
|
120
134
|
|
|
121
135
|
raise UnsupportedProtocol, "o11y: unsupported OTLP compression #{val.inspect}"
|
|
122
136
|
end
|
|
123
137
|
|
|
138
|
+
# The TLS files of one signal's exporter. Every key is present, as nil
|
|
139
|
+
# when unset, because an exporter reads the process environment for a
|
|
140
|
+
# keyword it is not given.
|
|
124
141
|
def signal_tls(env_map, prefix)
|
|
125
142
|
{
|
|
126
|
-
certificate_file: env_map
|
|
127
|
-
client_certificate_file: env_map
|
|
128
|
-
client_key_file: env_map
|
|
129
|
-
}
|
|
143
|
+
certificate_file: value(env_map, "#{prefix}_CERTIFICATE", "OTEL_EXPORTER_OTLP_CERTIFICATE"),
|
|
144
|
+
client_certificate_file: value(env_map, "#{prefix}_CLIENT_CERTIFICATE", "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE"),
|
|
145
|
+
client_key_file: value(env_map, "#{prefix}_CLIENT_KEY", "OTEL_EXPORTER_OTLP_CLIENT_KEY")
|
|
146
|
+
}
|
|
130
147
|
end
|
|
131
148
|
|
|
132
149
|
def parse_exemplar_filter(opt_filter, env_map)
|
|
@@ -144,6 +161,64 @@ module Libo11y
|
|
|
144
161
|
end
|
|
145
162
|
end
|
|
146
163
|
|
|
164
|
+
# The span limits, from env_map and the SDK's defaults. The SDK's own
|
|
165
|
+
# defaults read the process environment when the SDK loads, so they are
|
|
166
|
+
# never used.
|
|
167
|
+
def span_limits(env_map)
|
|
168
|
+
general_count = "OTEL_ATTRIBUTE_COUNT_LIMIT"
|
|
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
|
+
)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
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
|
+
)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# A count limit: the first of the named variables that holds a
|
|
189
|
+
# positive integer, else the OpenTelemetry default of 128.
|
|
190
|
+
def count_limit(env_map, *names)
|
|
191
|
+
count = Integer(value(env_map, *names) || "", 10)
|
|
192
|
+
count.positive? ? count : 128
|
|
193
|
+
rescue ArgumentError
|
|
194
|
+
128
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# A value length limit, or nil for none. The SDK accepts 32 or more.
|
|
198
|
+
def length_limit(env_map, *names)
|
|
199
|
+
length = Integer(value(env_map, *names) || "", 10)
|
|
200
|
+
length >= 32 ? length : nil
|
|
201
|
+
rescue ArgumentError
|
|
202
|
+
nil
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Whether an OTLP exporter verifies the collector's certificate. It
|
|
206
|
+
# does, unless env_map holds OpenTelemetry Ruby's variable that turns
|
|
207
|
+
# verification off. The exporters' own default reads the process
|
|
208
|
+
# environment, so libo11y always passes this one.
|
|
209
|
+
def ssl_verify_mode(env_map)
|
|
210
|
+
return OpenSSL::SSL::VERIFY_PEER if env_map.key?("OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_PEER")
|
|
211
|
+
return OpenSSL::SSL::VERIFY_NONE if env_map.key?("OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_NONE")
|
|
212
|
+
|
|
213
|
+
OpenSSL::SSL::VERIFY_PEER
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# OpenTelemetry Ruby's switch for starting a batch processor's worker
|
|
217
|
+
# when the processor is built, read from env_map.
|
|
218
|
+
def start_thread_on_boot?(env_map, name)
|
|
219
|
+
!env_map[name].to_s.match?(/false/i)
|
|
220
|
+
end
|
|
221
|
+
|
|
147
222
|
def parse_int(env_map, key, default)
|
|
148
223
|
raw = env_map[key]
|
|
149
224
|
return default if raw.nil? || raw.strip.empty?
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Libo11y
|
|
4
|
+
module Internal
|
|
5
|
+
# Bucket boundaries, in seconds, of the duration histograms libo11y
|
|
6
|
+
# records. The SDK's default boundaries start at 0, 5, 10, which suits
|
|
7
|
+
# milliseconds and puts nearly every request in the first bucket.
|
|
8
|
+
module HistogramBuckets
|
|
9
|
+
HTTP = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0].freeze
|
|
10
|
+
DB = [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0].freeze
|
|
11
|
+
|
|
12
|
+
# By instrument name. The SDK ignores the boundaries an instrument
|
|
13
|
+
# advises, so the builder registers a view for each of these.
|
|
14
|
+
DEFAULTS = {
|
|
15
|
+
"http.server.request.duration" => HTTP,
|
|
16
|
+
"http.client.request.duration" => HTTP,
|
|
17
|
+
"db.client.operation.duration" => DB
|
|
18
|
+
}.freeze
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "histogram_buckets"
|
|
4
|
+
|
|
5
|
+
module Libo11y
|
|
6
|
+
module Internal
|
|
7
|
+
# What the HTTP server and client instrumentation share.
|
|
8
|
+
module HttpCommon
|
|
9
|
+
# The request methods the semantic conventions name. Any other method
|
|
10
|
+
# is recorded as _OTHER, so a caller cannot grow the metric's series.
|
|
11
|
+
KNOWN_METHODS = %w[CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE].freeze
|
|
12
|
+
METHOD_OTHER = "_OTHER"
|
|
13
|
+
DURATION_BUCKETS = HistogramBuckets::HTTP
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# Maps a request method onto the known set. Returns the value for
|
|
18
|
+
# http.request.method and, when the method was not already written
|
|
19
|
+
# that way, the method as given, for http.request.method_original.
|
|
20
|
+
def normalize_method(raw)
|
|
21
|
+
return [raw, nil] if KNOWN_METHODS.include?(raw)
|
|
22
|
+
return [METHOD_OTHER, nil] if raw.empty?
|
|
23
|
+
|
|
24
|
+
upper = raw.upcase
|
|
25
|
+
[KNOWN_METHODS.include?(upper) ? upper : METHOD_OTHER, raw]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The span name: the method, or HTTP for _OTHER, and the route when one is known.
|
|
29
|
+
def span_name(method, route = nil)
|
|
30
|
+
prefix = method == METHOD_OTHER ? "HTTP" : method
|
|
31
|
+
route.nil? || route.empty? ? prefix : "#{prefix} #{route}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def monotonic
|
|
35
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -5,20 +5,26 @@ require "logger"
|
|
|
5
5
|
require "opentelemetry"
|
|
6
6
|
require_relative "../spec"
|
|
7
7
|
require_relative "bridges"
|
|
8
|
+
require_relative "pii"
|
|
8
9
|
|
|
9
10
|
module Libo11y
|
|
10
11
|
FORCE_EMIT_FIBER_KEY = :libo11y_force_emit
|
|
11
12
|
REENTRY_FIBER_KEY = :libo11y_logging_reentry
|
|
12
13
|
|
|
14
|
+
# Runs the block with every log record in it passing the logger's level,
|
|
15
|
+
# for records that must always be written, such as audit records. Returns
|
|
16
|
+
# the block's value.
|
|
17
|
+
#
|
|
18
|
+
# @raise [ArgumentError] without a block
|
|
13
19
|
def self.force_emit
|
|
14
|
-
|
|
15
|
-
Thread.current[FORCE_EMIT_FIBER_KEY] = true
|
|
16
|
-
return true unless block_given?
|
|
20
|
+
raise ArgumentError, "libo11y: force_emit needs a block" unless block_given?
|
|
17
21
|
|
|
22
|
+
previous = Thread.current[FORCE_EMIT_FIBER_KEY]
|
|
23
|
+
Thread.current[FORCE_EMIT_FIBER_KEY] = true
|
|
18
24
|
begin
|
|
19
25
|
yield
|
|
20
26
|
ensure
|
|
21
|
-
Thread.current[FORCE_EMIT_FIBER_KEY] =
|
|
27
|
+
Thread.current[FORCE_EMIT_FIBER_KEY] = previous
|
|
22
28
|
end
|
|
23
29
|
end
|
|
24
30
|
|
|
@@ -45,7 +51,7 @@ module Libo11y
|
|
|
45
51
|
otel_logger: nil,
|
|
46
52
|
console_writer: $stdout,
|
|
47
53
|
console_enabled: true,
|
|
48
|
-
|
|
54
|
+
log_level: ::Logger::INFO,
|
|
49
55
|
attr_replacer: nil,
|
|
50
56
|
extra_sinks: []
|
|
51
57
|
)
|
|
@@ -54,11 +60,10 @@ module Libo11y
|
|
|
54
60
|
@otel_logger = otel_logger
|
|
55
61
|
@console_writer = console_writer
|
|
56
62
|
@console_enabled = console_enabled
|
|
57
|
-
@console_level = console_level
|
|
58
63
|
@attr_replacer = attr_replacer
|
|
59
64
|
@extra_sinks = Array(extra_sinks).dup
|
|
60
65
|
@mutex = Mutex.new
|
|
61
|
-
self.level =
|
|
66
|
+
self.level = log_level
|
|
62
67
|
end
|
|
63
68
|
|
|
64
69
|
def add(severity, message = nil, progname = nil, &)
|
|
@@ -66,16 +71,9 @@ module Libo11y
|
|
|
66
71
|
return true if Thread.current[REENTRY_FIBER_KEY]
|
|
67
72
|
|
|
68
73
|
force = Libo11y.force_emit?
|
|
69
|
-
return true if !force && severity <
|
|
70
|
-
|
|
71
|
-
if message.nil?
|
|
72
|
-
if block_given?
|
|
73
|
-
message = yield
|
|
74
|
-
else
|
|
75
|
-
message = progname
|
|
76
|
-
@progname
|
|
77
|
-
end
|
|
78
|
-
end
|
|
74
|
+
return true if !force && severity < level
|
|
75
|
+
|
|
76
|
+
message = block_given? ? yield : progname if message.nil?
|
|
79
77
|
|
|
80
78
|
body, attrs = extract_message_and_attrs(message)
|
|
81
79
|
emit_record(
|
|
@@ -145,84 +143,83 @@ module Libo11y
|
|
|
145
143
|
|
|
146
144
|
def emit_record(severity:, body:, attrs:, event_name:, force:, raise_on_sink_error:)
|
|
147
145
|
return if Thread.current[REENTRY_FIBER_KEY]
|
|
146
|
+
# One level gates every sink. force_emit and events pass it.
|
|
147
|
+
return unless force || severity >= level
|
|
148
148
|
|
|
149
149
|
Thread.current[REENTRY_FIBER_KEY] = true
|
|
150
150
|
begin
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
trace_id = span_ctx.valid? ? span_ctx.hex_trace_id : nil
|
|
156
|
-
span_id = span_ctx.valid? ? span_ctx.hex_span_id : nil
|
|
157
|
-
|
|
158
|
-
full_attrs = {}
|
|
159
|
-
attrs&.each { |k, v| full_attrs[k.to_s] = v }
|
|
160
|
-
full_attrs[Spec::EVENT_NAME_KEY] = event_name if event_name && !event_name.empty?
|
|
161
|
-
|
|
162
|
-
classified = @policy.classify_log_attributes(full_attrs, replacer: @attr_replacer)
|
|
163
|
-
# trace_id and span_id are reserved: they come from the active span
|
|
164
|
-
# only. Removed after the replacer, which could bring them back.
|
|
165
|
-
classified.delete("trace_id")
|
|
166
|
-
classified.delete("span_id")
|
|
167
|
-
|
|
168
|
-
# Console leg
|
|
169
|
-
if @console_enabled && @console_writer && (force || severity >= @console_level)
|
|
170
|
-
line = build_console_line(now, sev_text, body, trace_id, span_id, classified)
|
|
171
|
-
write_console(line, raise_on_sink_error: raise_on_sink_error)
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
# OTLP leg
|
|
175
|
-
if @otel_logger && (force || severity >= @console_level)
|
|
176
|
-
emit_otel(now, sev_num, sev_text, body, event_name, classified, raise_on_sink_error: raise_on_sink_error)
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# Extra sinks
|
|
180
|
-
@extra_sinks.each do |sink|
|
|
181
|
-
next unless force || severity >= @console_level
|
|
182
|
-
|
|
183
|
-
record_hash = {
|
|
184
|
-
timestamp: now,
|
|
185
|
-
observed_timestamp: now,
|
|
186
|
-
severity_number: sev_num,
|
|
187
|
-
severity_text: sev_text,
|
|
188
|
-
body: body,
|
|
189
|
-
event_name: event_name,
|
|
190
|
-
trace_id: trace_id,
|
|
191
|
-
span_id: span_id,
|
|
192
|
-
attributes: classified
|
|
193
|
-
}
|
|
194
|
-
begin
|
|
195
|
-
if sink.respond_to?(:call)
|
|
196
|
-
sink.call(record_hash)
|
|
197
|
-
elsif sink.respond_to?(:emit)
|
|
198
|
-
sink.emit(record_hash)
|
|
199
|
-
end
|
|
200
|
-
rescue StandardError
|
|
201
|
-
raise if raise_on_sink_error
|
|
202
|
-
end
|
|
203
|
-
end
|
|
151
|
+
record = build_record(severity, body, attrs, event_name)
|
|
152
|
+
write_console(raise_on_sink_error: raise_on_sink_error) { console_line(record) } if @console_enabled && @console_writer
|
|
153
|
+
emit_otel(record, raise_on_sink_error: raise_on_sink_error) if @otel_logger
|
|
154
|
+
@extra_sinks.each { |sink| emit_extra(sink, record, raise_on_sink_error: raise_on_sink_error) }
|
|
204
155
|
ensure
|
|
205
156
|
Thread.current[REENTRY_FIBER_KEY] = false
|
|
206
157
|
end
|
|
207
158
|
end
|
|
208
159
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
160
|
+
# The record every sink gets: the trace identity of the active span,
|
|
161
|
+
# and the attributes after the replacer and the policy, classified once.
|
|
162
|
+
def build_record(severity, body, attrs, event_name)
|
|
163
|
+
now = Time.now.utc
|
|
164
|
+
severity_number, severity_text = SEVERITY_TO_OTEL.fetch(severity, [9, "INFO"])
|
|
165
|
+
span_context = OpenTelemetry::Trace.current_span.context
|
|
166
|
+
|
|
167
|
+
attributes = attrs.to_h { |key, value| [key.to_s, value] }
|
|
168
|
+
attributes[Spec::EVENT_NAME_KEY] = event_name if event_name && !event_name.empty?
|
|
169
|
+
classified = @policy.classify_log_attributes(attributes, replacer: @attr_replacer)
|
|
170
|
+
# trace_id and span_id are reserved: they come from the active span
|
|
171
|
+
# only. Removed after the replacer, which could bring them back.
|
|
172
|
+
classified.delete("trace_id")
|
|
173
|
+
classified.delete("span_id")
|
|
174
|
+
|
|
175
|
+
{
|
|
176
|
+
timestamp: now,
|
|
177
|
+
observed_timestamp: now,
|
|
178
|
+
severity_number: severity_number,
|
|
179
|
+
severity_text: severity_text,
|
|
180
|
+
body: body,
|
|
181
|
+
event_name: event_name,
|
|
182
|
+
trace_id: span_context.valid? ? span_context.hex_trace_id : nil,
|
|
183
|
+
span_id: span_context.valid? ? span_context.hex_span_id : nil,
|
|
184
|
+
attributes: classified
|
|
214
185
|
}
|
|
215
|
-
|
|
216
|
-
payload["span_id"] = span_id if span_id
|
|
217
|
-
attrs.each do |k, v|
|
|
218
|
-
next if %w[time level msg trace_id span_id].include?(k)
|
|
186
|
+
end
|
|
219
187
|
|
|
220
|
-
|
|
188
|
+
def emit_extra(sink, record, raise_on_sink_error:)
|
|
189
|
+
if sink.respond_to?(:call)
|
|
190
|
+
sink.call(record)
|
|
191
|
+
elsif sink.respond_to?(:emit)
|
|
192
|
+
sink.emit(record)
|
|
193
|
+
end
|
|
194
|
+
rescue StandardError
|
|
195
|
+
raise if raise_on_sink_error
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def console_line(record)
|
|
199
|
+
payload = { "time" => record[:timestamp].iso8601(6), "level" => record[:severity_text], "msg" => record[:body] }
|
|
200
|
+
payload["trace_id"] = record[:trace_id] if record[:trace_id]
|
|
201
|
+
payload["span_id"] = record[:span_id] if record[:span_id]
|
|
202
|
+
record[:attributes].each do |key, value|
|
|
203
|
+
payload[key] = json_safe(value) unless %w[time level msg trace_id span_id].include?(key)
|
|
221
204
|
end
|
|
222
205
|
"#{JSON.generate(payload)}\n"
|
|
223
206
|
end
|
|
224
207
|
|
|
225
|
-
|
|
208
|
+
# JSON has no NaN and no infinity, so those floats are written as the
|
|
209
|
+
# strings Go's slog writes: "NaN", "+Inf" and "-Inf".
|
|
210
|
+
def json_safe(value)
|
|
211
|
+
case value
|
|
212
|
+
when Float then value.finite? ? value : PII.canonical_float(value)
|
|
213
|
+
when Hash then value.transform_values { |v| json_safe(v) }
|
|
214
|
+
when Array then value.map { |v| json_safe(v) }
|
|
215
|
+
else value
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Encodes and writes one line. A failure to encode or to write stays
|
|
220
|
+
# in this sink, so the record still reaches the others.
|
|
221
|
+
def write_console(raise_on_sink_error:)
|
|
222
|
+
line = yield
|
|
226
223
|
@mutex.synchronize do
|
|
227
224
|
@console_writer.write(line)
|
|
228
225
|
end
|
|
@@ -230,16 +227,10 @@ module Libo11y
|
|
|
230
227
|
raise if raise_on_sink_error
|
|
231
228
|
end
|
|
232
229
|
|
|
233
|
-
def emit_otel(
|
|
234
|
-
flat = flatten_for_otel(classified)
|
|
230
|
+
def emit_otel(record, raise_on_sink_error:)
|
|
235
231
|
@otel_logger.on_emit(
|
|
236
|
-
timestamp:
|
|
237
|
-
|
|
238
|
-
severity_number: sev_num,
|
|
239
|
-
severity_text: sev_text,
|
|
240
|
-
body: body,
|
|
241
|
-
attributes: flat,
|
|
242
|
-
event_name: event_name,
|
|
232
|
+
**record.slice(:timestamp, :observed_timestamp, :severity_number, :severity_text, :body, :event_name),
|
|
233
|
+
attributes: flatten_for_otel(record[:attributes]),
|
|
243
234
|
context: Bridges::PolicyLogRecordProcessor.mark_classified(OpenTelemetry::Context.current)
|
|
244
235
|
)
|
|
245
236
|
rescue StandardError
|