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
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "opentelemetry/sdk"
|
|
4
|
+
require "opentelemetry-metrics-sdk"
|
|
5
|
+
require "opentelemetry-logs-sdk"
|
|
6
|
+
require_relative "bridges"
|
|
7
|
+
require_relative "env"
|
|
8
|
+
require_relative "histogram_buckets"
|
|
9
|
+
require_relative "runtime_metrics"
|
|
10
|
+
|
|
11
|
+
module Libo11y
|
|
12
|
+
module Internal
|
|
13
|
+
# Builds the tracer, meter and logger providers of one Telemetry handle
|
|
14
|
+
# from its options and the environment it was given. Every exporter is
|
|
15
|
+
# wrapped so that its failures are observed and what it sends is
|
|
16
|
+
# classified by the sensitivity policy.
|
|
17
|
+
class Pipeline
|
|
18
|
+
SIGNALS = %i[traces metrics logs].freeze
|
|
19
|
+
EXPORTER_VARIABLES = {
|
|
20
|
+
traces: "OTEL_TRACES_EXPORTER", metrics: "OTEL_METRICS_EXPORTER", logs: "OTEL_LOGS_EXPORTER"
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# failures holds one ExportFailures per signal. exporters holds what
|
|
24
|
+
# each signal exports to: "injected", "otlp", "console" or "none".
|
|
25
|
+
attr_reader :tracer_provider, :meter_provider, :logger_provider, :failures, :exporters
|
|
26
|
+
|
|
27
|
+
# @param built [Array] receives each provider as it is created, so the
|
|
28
|
+
# caller can stop the ones that exist when a later step raises
|
|
29
|
+
def initialize(options:, env_map:, hermetic:, policy:, resource:, violations:, built:)
|
|
30
|
+
@options = options
|
|
31
|
+
@env = env_map
|
|
32
|
+
@hermetic = hermetic
|
|
33
|
+
# OTEL_SDK_DISABLED beats every exporter, reader and processor, the injected ones included.
|
|
34
|
+
@disabled = !hermetic && Env.sdk_disabled?(env_map)
|
|
35
|
+
@policy = policy
|
|
36
|
+
@resource = resource
|
|
37
|
+
@violations = violations
|
|
38
|
+
@built = built
|
|
39
|
+
@failures = SIGNALS.to_h { |signal| [signal, Bridges::ExportFailures.new] }
|
|
40
|
+
@exporters = {}
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def build
|
|
44
|
+
build_traces
|
|
45
|
+
build_metrics
|
|
46
|
+
build_logs
|
|
47
|
+
self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def runtime_metrics?
|
|
51
|
+
@options.runtime_metrics && !@disabled ? true : false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def track(provider)
|
|
57
|
+
@built << provider
|
|
58
|
+
provider
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def observe(exporter, signal)
|
|
62
|
+
Bridges::ObservedExporter.new(exporter, @failures.fetch(signal), signal)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# What a signal exports to. Under HERMETIC a signal with nothing
|
|
66
|
+
# injected is off.
|
|
67
|
+
def exporter_mode(signal, injected)
|
|
68
|
+
@exporters[signal] =
|
|
69
|
+
if injected && !@disabled
|
|
70
|
+
"injected"
|
|
71
|
+
elsif @disabled || @hermetic
|
|
72
|
+
"none"
|
|
73
|
+
else
|
|
74
|
+
Env.parse_exporter_choice(@env, EXPORTER_VARIABLES.fetch(signal), default: "otlp")
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def batch_settings(prefix, schedule_delay:)
|
|
79
|
+
{
|
|
80
|
+
schedule_delay: Env.parse_int(@env, "OTEL_#{prefix}_SCHEDULE_DELAY", schedule_delay),
|
|
81
|
+
max_queue_size: Env.parse_int(@env, "OTEL_#{prefix}_MAX_QUEUE_SIZE", 2048),
|
|
82
|
+
max_export_batch_size: Env.parse_int(@env, "OTEL_#{prefix}_MAX_EXPORT_BATCH_SIZE", 512),
|
|
83
|
+
exporter_timeout: Env.parse_int(@env, "OTEL_#{prefix}_EXPORT_TIMEOUT", 30_000),
|
|
84
|
+
start_thread_on_boot: Env.start_thread_on_boot?(@env, "OTEL_RUBY_#{prefix}_START_THREAD_ON_BOOT")
|
|
85
|
+
}
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def build_traces
|
|
89
|
+
sampler = Bridges.build_sampler(@options.sampler, @env)
|
|
90
|
+
@tracer_provider = track(
|
|
91
|
+
if @disabled
|
|
92
|
+
Bridges::NoopTracerProvider.new
|
|
93
|
+
else
|
|
94
|
+
OpenTelemetry::SDK::Trace::TracerProvider.new(sampler: sampler, resource: @resource, span_limits: Env.span_limits(@env))
|
|
95
|
+
end
|
|
96
|
+
)
|
|
97
|
+
# An injected processor owns the export path, as an injected exporter
|
|
98
|
+
# does, so no exporter is built from the environment beside it.
|
|
99
|
+
processors = Array(@options.span_processors)
|
|
100
|
+
mode = exporter_mode(:traces, @options.span_exporter || !processors.empty?)
|
|
101
|
+
return if @disabled
|
|
102
|
+
|
|
103
|
+
exporting = span_export_processor(mode)
|
|
104
|
+
@tracer_provider.add_span_processor(exporting) if exporting
|
|
105
|
+
# An injected processor gets a redacted view of the finished span. A
|
|
106
|
+
# span's violations are counted once: by the exporter path when there
|
|
107
|
+
# is one, else by the first injected processor.
|
|
108
|
+
processors.each_with_index do |processor, index|
|
|
109
|
+
policy = exporting.nil? && index.zero? ? @policy : @policy.silent
|
|
110
|
+
@tracer_provider.add_span_processor(Bridges::PolicySpanProcessor.new(processor, policy))
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def span_export_processor(mode)
|
|
115
|
+
export = OpenTelemetry::SDK::Trace::Export
|
|
116
|
+
if @options.span_exporter
|
|
117
|
+
export::SimpleSpanProcessor.new(redacting(@options.span_exporter))
|
|
118
|
+
elsif mode == "otlp"
|
|
119
|
+
exporter = redacting(Bridges.build_otlp_trace_exporter(@env))
|
|
120
|
+
export::BatchSpanProcessor.new(exporter, **batch_settings("BSP", schedule_delay: 5000))
|
|
121
|
+
elsif mode == "console"
|
|
122
|
+
export::SimpleSpanProcessor.new(redacting(export::ConsoleSpanExporter.new))
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def redacting(span_exporter)
|
|
127
|
+
Bridges::PolicySpanExporter.new(observe(span_exporter, :traces), @policy)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def build_metrics
|
|
131
|
+
raw = track(OpenTelemetry::SDK::Metrics::MeterProvider.new(resource: @resource))
|
|
132
|
+
filter = @disabled ? ExemplarFilter::ALWAYS_OFF : Env.parse_exemplar_filter(@options.exemplar_filter, @env)
|
|
133
|
+
Bridges.configure_exemplar_filter(raw, filter)
|
|
134
|
+
mode = exporter_mode(:metrics, @options.metric_reader)
|
|
135
|
+
unless @disabled
|
|
136
|
+
reader = metric_reader(mode)
|
|
137
|
+
raw.add_metric_reader(reader) if reader
|
|
138
|
+
add_views(raw)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
@meter_provider = Bridges::PolicyMeterProvider.new(raw, @policy)
|
|
142
|
+
count_violations(raw)
|
|
143
|
+
RuntimeMetrics.register(@meter_provider) if runtime_metrics?
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def metric_reader(mode)
|
|
147
|
+
return @options.metric_reader if @options.metric_reader
|
|
148
|
+
|
|
149
|
+
exporter = case mode
|
|
150
|
+
when "otlp" then observe(Bridges.build_otlp_metric_exporter(@env), :metrics)
|
|
151
|
+
when "console" then OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter.new
|
|
152
|
+
end
|
|
153
|
+
return if exporter.nil?
|
|
154
|
+
|
|
155
|
+
OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader.new(
|
|
156
|
+
exporter: exporter,
|
|
157
|
+
export_interval_millis: Env.parse_int(@env, "OTEL_METRIC_EXPORT_INTERVAL", 60_000),
|
|
158
|
+
export_timeout_millis: Env.parse_int(@env, "OTEL_METRIC_EXPORT_TIMEOUT", 30_000)
|
|
159
|
+
)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# One view per histogram with boundaries: libo11y's own duration
|
|
163
|
+
# histograms, then the histogram_buckets option, which wins for a name
|
|
164
|
+
# both give. Then the views option.
|
|
165
|
+
def add_views(raw)
|
|
166
|
+
buckets = HistogramBuckets::DEFAULTS.merge((@options.histogram_buckets || {}).to_h.transform_keys(&:to_s))
|
|
167
|
+
buckets.each do |name, boundaries|
|
|
168
|
+
aggregation = OpenTelemetry::SDK::Metrics::Aggregation::ExplicitBucketHistogram.new(boundaries: Array(boundaries))
|
|
169
|
+
raw.add_view(name, type: :histogram, aggregation: aggregation)
|
|
170
|
+
end
|
|
171
|
+
Array(@options.views).each do |view|
|
|
172
|
+
arguments = view.is_a?(Hash) ? view.transform_keys(&:to_sym) : {}
|
|
173
|
+
unless arguments[:name]
|
|
174
|
+
raise ArgumentError, "libo11y: views takes Hashes of MeterProvider#add_view arguments with a :name, got #{view.inspect}"
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
raw.add_view(arguments[:name].to_s, **arguments.except(:name))
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Feeds the o11y.schema.violations counter, the violations recorded
|
|
182
|
+
# while the resource was built included. It is on the raw provider,
|
|
183
|
+
# because its own attributes are not subject to the policy.
|
|
184
|
+
def count_violations(raw)
|
|
185
|
+
counter = raw.meter("libo11y", version: Libo11y::VERSION).create_counter(
|
|
186
|
+
Spec::METRIC_SCHEMA_VIOLATIONS, unit: "{violation}", description: "Specification and sensitivity policy violations."
|
|
187
|
+
)
|
|
188
|
+
@violations.attach(lambda do |signal, rule|
|
|
189
|
+
counter.add(1, attributes: { Spec::ATTR_SIGNAL => signal.to_s, Spec::ATTR_RULE => rule.to_s })
|
|
190
|
+
end)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def build_logs
|
|
194
|
+
@logger_provider = track(
|
|
195
|
+
OpenTelemetry::SDK::Logs::LoggerProvider.new(resource: @resource, log_record_limits: Env.log_record_limits(@env))
|
|
196
|
+
)
|
|
197
|
+
mode = exporter_mode(:logs, @options.log_exporter)
|
|
198
|
+
return if @disabled
|
|
199
|
+
|
|
200
|
+
# First, so every later processor sees the classified record.
|
|
201
|
+
@logger_provider.add_log_record_processor(Bridges::PolicyLogRecordProcessor.new(@policy))
|
|
202
|
+
exporting = log_export_processor(mode)
|
|
203
|
+
@logger_provider.add_log_record_processor(exporting) if exporting
|
|
204
|
+
Array(@options.log_processors).each { |processor| @logger_provider.add_log_record_processor(processor) }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def log_export_processor(mode)
|
|
208
|
+
export = OpenTelemetry::SDK::Logs::Export
|
|
209
|
+
if @options.log_exporter
|
|
210
|
+
export::SimpleLogRecordProcessor.new(observe(@options.log_exporter, :logs))
|
|
211
|
+
elsif mode == "otlp"
|
|
212
|
+
exporter = observe(Bridges.build_otlp_log_exporter(@env), :logs)
|
|
213
|
+
export::BatchLogRecordProcessor.new(exporter, **batch_settings("BLRP", schedule_delay: 1000))
|
|
214
|
+
elsif mode == "console"
|
|
215
|
+
export::SimpleLogRecordProcessor.new(observe(export::ConsoleLogRecordExporter.new, :logs))
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "pathname"
|
|
3
4
|
require_relative "../errors"
|
|
4
5
|
require_relative "../options"
|
|
5
6
|
require_relative "../redact"
|
|
@@ -44,38 +45,50 @@ module Libo11y
|
|
|
44
45
|
|
|
45
46
|
attr_reader :pii_mode
|
|
46
47
|
|
|
48
|
+
# Loads the registries of the app_registries option and returns the
|
|
49
|
+
# sensitivity class of each key they declare. A source is a compiled
|
|
50
|
+
# registry, a Pathname, or a String. A String of one line is a path,
|
|
51
|
+
# and a String of several lines, or one that starts a flow mapping, is
|
|
52
|
+
# YAML text.
|
|
53
|
+
#
|
|
54
|
+
# @raise [InvalidAppRegistry] for a file that cannot be read, a
|
|
55
|
+
# registry that does not parse or validate, or a namespace that two
|
|
56
|
+
# sources declare
|
|
47
57
|
def self.load_app_registries(spec_sources)
|
|
48
58
|
return {} if spec_sources.nil?
|
|
49
59
|
|
|
50
60
|
sources = spec_sources.is_a?(Array) ? spec_sources : [spec_sources]
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if File.file?(src)
|
|
59
|
-
Spec::AppRegistry.load_file(src)
|
|
60
|
-
else
|
|
61
|
-
Spec::AppRegistry.compile(Spec::AppRegistry.parse(src), source: "<inline>")
|
|
62
|
-
end
|
|
63
|
-
else
|
|
64
|
-
raise InvalidAppRegistry, "o11y: invalid application registry source #{src.class}"
|
|
65
|
-
end
|
|
66
|
-
prev = by_ns[compiled.namespace]
|
|
67
|
-
if prev
|
|
68
|
-
raise InvalidAppRegistry,
|
|
69
|
-
"o11y: invalid application registry: duplicate namespace #{compiled.namespace.inspect} declared in #{prev} and #{compiled.source}"
|
|
61
|
+
declared = {}
|
|
62
|
+
sources.each_with_object({}) do |source, classes|
|
|
63
|
+
compiled = compile_app_registry(source)
|
|
64
|
+
label = compiled.source || "a compiled registry"
|
|
65
|
+
if declared.key?(compiled.namespace)
|
|
66
|
+
raise InvalidAppRegistry, "o11y: invalid application registry: duplicate namespace #{compiled.namespace.inspect} " \
|
|
67
|
+
"declared in #{declared[compiled.namespace]} and #{label}"
|
|
70
68
|
end
|
|
71
|
-
|
|
69
|
+
declared[compiled.namespace] = label
|
|
72
70
|
classes.merge!(compiled.classes)
|
|
73
|
-
rescue SystemCallError, Spec::AppRegistry::ParseError, Spec::AppRegistry::ValidationError => e
|
|
74
|
-
raise InvalidAppRegistry, "o11y: invalid application registry: #{e.message}"
|
|
75
71
|
end
|
|
76
|
-
classes
|
|
77
72
|
end
|
|
78
73
|
|
|
74
|
+
def self.compile_app_registry(source)
|
|
75
|
+
case source
|
|
76
|
+
when Spec::AppRegistry::Compiled then source
|
|
77
|
+
when Pathname then Spec::AppRegistry.load_file(source.to_s)
|
|
78
|
+
when String
|
|
79
|
+
if source.include?("\n") || source.lstrip.start_with?("{")
|
|
80
|
+
Spec::AppRegistry.compile(Spec::AppRegistry.parse_bytes(source), source: "<inline>")
|
|
81
|
+
else
|
|
82
|
+
Spec::AppRegistry.load_file(source)
|
|
83
|
+
end
|
|
84
|
+
else
|
|
85
|
+
raise InvalidAppRegistry, "o11y: invalid application registry: a source is a path, YAML text or a compiled registry, not #{source.class}"
|
|
86
|
+
end
|
|
87
|
+
rescue SystemCallError, Spec::AppRegistry::ParseError, Spec::AppRegistry::ValidationError => e
|
|
88
|
+
raise InvalidAppRegistry, "o11y: invalid application registry: #{e.message}"
|
|
89
|
+
end
|
|
90
|
+
private_class_method :compile_app_registry
|
|
91
|
+
|
|
79
92
|
def initialize(pii_mode: PIIMode::DROP, pii_hash_key: nil, app_classes: nil, recorder: nil)
|
|
80
93
|
mode = pii_mode.is_a?(Symbol) ? pii_mode : pii_mode.to_s.to_sym
|
|
81
94
|
raise Error, "o11y: invalid pii_mode #{pii_mode.inspect}" unless PIIMode::ALL.include?(mode)
|
|
@@ -29,20 +29,17 @@ module Libo11y
|
|
|
29
29
|
def filter_baggage(context)
|
|
30
30
|
return context if @allow.nil?
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
return context if
|
|
32
|
+
entries = OpenTelemetry::Baggage.raw_entries(context: context)
|
|
33
|
+
return context if entries.nil? || entries.empty?
|
|
34
34
|
|
|
35
|
-
kept = {}
|
|
36
|
-
|
|
37
|
-
kept[k] = v if @allow.call(k)
|
|
38
|
-
end
|
|
39
|
-
return context if kept.size == values.size
|
|
35
|
+
kept = entries.select { |key, _entry| @allow.call(key) }
|
|
36
|
+
return context if kept.size == entries.size
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
kept.each do |
|
|
43
|
-
|
|
38
|
+
filtered = OpenTelemetry::Baggage.clear(context: context)
|
|
39
|
+
kept.each do |key, entry|
|
|
40
|
+
filtered = OpenTelemetry::Baggage.set_value(key, entry.value, metadata: entry.metadata, context: filtered)
|
|
44
41
|
end
|
|
45
|
-
|
|
42
|
+
filtered
|
|
46
43
|
end
|
|
47
44
|
end
|
|
48
45
|
|
|
@@ -14,7 +14,7 @@ module Libo11y
|
|
|
14
14
|
UNKNOWN_VERSION = "0.0.0-unknown"
|
|
15
15
|
CGROUP_ID_RE = /\b([0-9a-f]{64})\b/i
|
|
16
16
|
|
|
17
|
-
Result = Data.define(:resource, :attributes, :service_name, :environment, :service_version, :instance_id)
|
|
17
|
+
Result = Data.define(:resource, :attributes, :service_name, :environment, :service_version, :instance_id, :version_fallback)
|
|
18
18
|
|
|
19
19
|
module_function
|
|
20
20
|
|
|
@@ -43,103 +43,47 @@ module Libo11y
|
|
|
43
43
|
{}
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
# Composes, validates and classifies the resource. Precedence, lowest to
|
|
47
|
+
# highest: what libo11y detects and the build info, then the
|
|
48
|
+
# environment (OTEL_RESOURCE_ATTRIBUTES, then OTEL_SERVICE_NAME), then
|
|
49
|
+
# the resource_attributes option, then the named options. A blank
|
|
50
|
+
# option sets nothing.
|
|
46
51
|
def build(options, env_map, policy, recorder)
|
|
47
|
-
env_res = Env.parse_resource_attributes(env_map["OTEL_RESOURCE_ATTRIBUTES"])
|
|
48
|
-
opt_res = (options.resource_attributes || {}).transform_keys(&:to_s)
|
|
49
52
|
build_info = options.build_info_enabled ? load_build_info(options.build_info) : {}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
# Environment precedence: option > OTEL_RESOURCE_ATTRIBUTES > resource_attributes
|
|
67
|
-
environment = options.environment ||
|
|
68
|
-
env_res["deployment.environment.name"] ||
|
|
69
|
-
opt_res["deployment.environment.name"]
|
|
70
|
-
environment = environment.to_s.strip
|
|
71
|
-
|
|
72
|
-
allowed_envs = Spec::ENVIRONMENT_VALUES.dup
|
|
73
|
-
Array(options.extra_environments).each { |e| allowed_envs.add(e.to_s) }
|
|
74
|
-
|
|
75
|
-
if environment.empty?
|
|
76
|
-
raise NoEnvironment if options.strict_identity
|
|
77
|
-
|
|
78
|
-
recorder&.record(Spec::Signal::RESOURCE, "identity_environment")
|
|
79
|
-
|
|
80
|
-
elsif !allowed_envs.include?(environment)
|
|
81
|
-
raise InvalidEnvironment, "o11y: deployment environment #{environment.inspect} not in the allowed set" if options.strict_identity
|
|
82
|
-
|
|
83
|
-
recorder&.record(Spec::Signal::RESOURCE, "identity_environment_invalid")
|
|
84
|
-
|
|
53
|
+
attrs = detected_attributes(build_info)
|
|
54
|
+
|
|
55
|
+
attrs.merge!(Env.parse_resource_attributes(env_map["OTEL_RESOURCE_ATTRIBUTES"]))
|
|
56
|
+
env_name = Env.value(env_map, "OTEL_SERVICE_NAME")
|
|
57
|
+
attrs["service.name"] = env_name if env_name
|
|
58
|
+
|
|
59
|
+
attrs.merge!((options.resource_attributes || {}).transform_keys(&:to_s))
|
|
60
|
+
{
|
|
61
|
+
"service.name" => options.service_name,
|
|
62
|
+
"service.namespace" => options.service_namespace,
|
|
63
|
+
"deployment.environment.name" => options.environment,
|
|
64
|
+
"service.version" => options.version,
|
|
65
|
+
"service.instance.id" => options.service_instance_id
|
|
66
|
+
}.each do |key, option|
|
|
67
|
+
text = option.to_s.strip
|
|
68
|
+
attrs[key] = text unless text.empty?
|
|
85
69
|
end
|
|
70
|
+
attrs["service.version"] ||= build_info["version"] || build_info["service.version"]
|
|
86
71
|
|
|
87
|
-
|
|
88
|
-
version = options.version ||
|
|
89
|
-
opt_res["service.version"] ||
|
|
90
|
-
env_res["service.version"] ||
|
|
91
|
-
build_info["version"] ||
|
|
92
|
-
build_info["service.version"]
|
|
93
|
-
version = version.to_s.strip
|
|
94
|
-
version_fallback = version.empty?
|
|
95
|
-
version = UNKNOWN_VERSION if version_fallback
|
|
96
|
-
|
|
97
|
-
# Instance ID
|
|
98
|
-
pod_uid = opt_res["k8s.pod.uid"] || env_res["k8s.pod.uid"] || ""
|
|
99
|
-
pod_name = opt_res["k8s.pod.name"] || env_res["k8s.pod.name"] || ""
|
|
100
|
-
k8s_ns = opt_res["k8s.namespace.name"] || env_res["k8s.namespace.name"] || ""
|
|
101
|
-
instance_id = options.service_instance_id ||
|
|
102
|
-
opt_res["service.instance.id"] ||
|
|
103
|
-
env_res["service.instance.id"] ||
|
|
104
|
-
Identity.derive_instance_id(pod_uid: pod_uid, pod_name: pod_name, namespace: k8s_ns) ||
|
|
105
|
-
Identity.random_instance_id
|
|
106
|
-
|
|
107
|
-
attrs = {
|
|
108
|
-
"telemetry.sdk.name" => "opentelemetry",
|
|
109
|
-
"telemetry.sdk.language" => "ruby",
|
|
110
|
-
"telemetry.sdk.version" => OpenTelemetry::SDK::VERSION,
|
|
111
|
-
"telemetry.distro.name" => "libo11y",
|
|
112
|
-
"telemetry.distro.version" => Libo11y::VERSION,
|
|
113
|
-
"process.runtime.name" => RUBY_ENGINE,
|
|
114
|
-
"process.runtime.version" => RUBY_VERSION,
|
|
115
|
-
"process.runtime.description" => RUBY_DESCRIPTION
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
begin
|
|
119
|
-
hostname = Socket.gethostname
|
|
120
|
-
attrs["host.name"] = hostname unless hostname.nil? || hostname.empty?
|
|
121
|
-
rescue StandardError
|
|
122
|
-
nil
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
cid = detect_container_id
|
|
126
|
-
attrs["container.id"] = cid if cid
|
|
72
|
+
enforce_identity(attrs, options, recorder)
|
|
127
73
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
attrs[
|
|
74
|
+
version_fallback = attrs["service.version"].to_s.strip.empty?
|
|
75
|
+
attrs["service.version"] = UNKNOWN_VERSION if version_fallback
|
|
76
|
+
if attrs["service.instance.id"].to_s.strip.empty?
|
|
77
|
+
attrs["service.instance.id"] = Identity.derive_instance_id(
|
|
78
|
+
pod_uid: attrs["k8s.pod.uid"].to_s, pod_name: attrs["k8s.pod.name"].to_s, namespace: attrs["k8s.namespace.name"].to_s
|
|
79
|
+
) || Identity.random_instance_id
|
|
132
80
|
end
|
|
81
|
+
attrs.delete("deployment.environment.name") if attrs["deployment.environment.name"].to_s.strip.empty?
|
|
133
82
|
|
|
134
|
-
|
|
135
|
-
attrs.
|
|
136
|
-
attrs.
|
|
137
|
-
|
|
138
|
-
attrs["service.name"] = service_name
|
|
139
|
-
attrs["service.version"] = version
|
|
140
|
-
attrs["service.instance.id"] = instance_id
|
|
141
|
-
attrs["deployment.environment.name"] = environment unless environment.empty?
|
|
142
|
-
attrs["service.namespace"] = options.service_namespace.to_s if options.service_namespace && !options.service_namespace.to_s.empty?
|
|
83
|
+
service_name = attrs["service.name"]
|
|
84
|
+
environment = attrs["deployment.environment.name"].to_s
|
|
85
|
+
version = attrs["service.version"]
|
|
86
|
+
instance_id = attrs["service.instance.id"]
|
|
143
87
|
|
|
144
88
|
# Classify the entire resource through the sensitivity policy once at construction
|
|
145
89
|
classified, = policy.classify_flat(Spec::Signal::RESOURCE, attrs)
|
|
@@ -162,10 +106,70 @@ module Libo11y
|
|
|
162
106
|
service_name: service_name,
|
|
163
107
|
environment: environment,
|
|
164
108
|
service_version: version,
|
|
165
|
-
instance_id: instance_id
|
|
109
|
+
instance_id: instance_id,
|
|
110
|
+
version_fallback: version_fallback
|
|
166
111
|
)
|
|
167
112
|
end
|
|
168
113
|
|
|
114
|
+
def detected_attributes(build_info)
|
|
115
|
+
attrs = {
|
|
116
|
+
"telemetry.sdk.name" => "opentelemetry",
|
|
117
|
+
"telemetry.sdk.language" => "ruby",
|
|
118
|
+
"telemetry.sdk.version" => OpenTelemetry::SDK::VERSION,
|
|
119
|
+
"telemetry.distro.name" => "libo11y",
|
|
120
|
+
"telemetry.distro.version" => Libo11y::VERSION,
|
|
121
|
+
"process.runtime.name" => RUBY_ENGINE,
|
|
122
|
+
"process.runtime.version" => RUBY_VERSION,
|
|
123
|
+
"process.runtime.description" => RUBY_DESCRIPTION
|
|
124
|
+
}
|
|
125
|
+
hostname = host_name
|
|
126
|
+
attrs["host.name"] = hostname if hostname
|
|
127
|
+
cid = detect_container_id
|
|
128
|
+
attrs["container.id"] = cid if cid
|
|
129
|
+
attrs["vcs.ref.head.revision"] = build_info["vcs_revision"].to_s if build_info["vcs_revision"]
|
|
130
|
+
attrs[Spec::ATTR_BUILD_TIME] = build_info["build_time"].to_s if build_info["build_time"]
|
|
131
|
+
attrs[Spec::ATTR_BUILD_MODIFIED] = build_info["build_modified"] unless build_info["build_modified"].nil?
|
|
132
|
+
attrs
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def host_name
|
|
136
|
+
name = Socket.gethostname
|
|
137
|
+
name.nil? || name.empty? ? nil : name
|
|
138
|
+
rescue StandardError
|
|
139
|
+
nil
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Applies the tier-0 identity contract. Under strict identity a
|
|
143
|
+
# violation raises. Otherwise each one is counted on
|
|
144
|
+
# o11y.schema.violations.
|
|
145
|
+
def enforce_identity(attrs, options, recorder)
|
|
146
|
+
name = attrs["service.name"].to_s.strip
|
|
147
|
+
if name.empty? || name == "unknown_service" || name.start_with?("unknown_service:")
|
|
148
|
+
raise NoServiceName if options.strict_identity
|
|
149
|
+
|
|
150
|
+
recorder&.record(Spec::Signal::RESOURCE, "identity_service_name")
|
|
151
|
+
attrs["service.name"] = "unknown_service" if name.empty?
|
|
152
|
+
else
|
|
153
|
+
attrs["service.name"] = name
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
environment = attrs["deployment.environment.name"].to_s.strip
|
|
157
|
+
allowed = Spec::ENVIRONMENT_VALUES.to_a.map(&:to_s) | Array(options.extra_environments).map(&:to_s)
|
|
158
|
+
if environment.empty?
|
|
159
|
+
raise NoEnvironment if options.strict_identity
|
|
160
|
+
|
|
161
|
+
recorder&.record(Spec::Signal::RESOURCE, "identity_environment")
|
|
162
|
+
elsif !allowed.include?(environment)
|
|
163
|
+
if options.strict_identity
|
|
164
|
+
raise InvalidEnvironment,
|
|
165
|
+
"o11y: deployment environment not in the allowed set: #{Spec::GoQuote.quote(environment)} (allowed: #{allowed.sort.join(", ")})"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
recorder&.record(Spec::Signal::RESOURCE, "identity_environment_invalid")
|
|
169
|
+
end
|
|
170
|
+
attrs["deployment.environment.name"] = environment
|
|
171
|
+
end
|
|
172
|
+
|
|
169
173
|
def normalize_otel_value(v)
|
|
170
174
|
case v
|
|
171
175
|
when String, Integer, Float, TrueClass, FalseClass then v
|
|
@@ -1,83 +1,69 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "etc"
|
|
4
|
+
|
|
3
5
|
module Libo11y
|
|
4
6
|
module Internal
|
|
5
7
|
# Zero-dependency Ruby runtime and semantic-convention process metrics.
|
|
6
8
|
module RuntimeMetrics
|
|
9
|
+
# The size of a memory page, which /proc/self/statm counts in. It is
|
|
10
|
+
# 4 KiB on most hosts and 16 or 64 KiB on some arm64 kernels.
|
|
11
|
+
PAGE_SIZE = Etc.sysconf(Etc::SC_PAGESIZE) || 4096
|
|
12
|
+
|
|
7
13
|
module_function
|
|
8
14
|
|
|
15
|
+
# Registers the observable instruments on the meter provider. A failure
|
|
16
|
+
# to register is not allowed to stop the application from starting.
|
|
9
17
|
def register(meter_provider)
|
|
10
18
|
meter = meter_provider.meter("libo11y.runtime", version: Libo11y::VERSION)
|
|
19
|
+
register_ruby(meter)
|
|
20
|
+
register_process(meter)
|
|
21
|
+
rescue StandardError
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
11
24
|
|
|
25
|
+
def register_ruby(meter)
|
|
12
26
|
meter.create_observable_counter(
|
|
13
|
-
"ruby.gc.count",
|
|
14
|
-
unit: "{collection}",
|
|
15
|
-
description: "Total Ruby garbage collections.",
|
|
16
|
-
callback: lambda do
|
|
17
|
-
GC.count
|
|
18
|
-
end
|
|
27
|
+
"ruby.gc.count", unit: "{collection}", description: "Total Ruby garbage collections.", callback: -> { GC.count }
|
|
19
28
|
)
|
|
20
|
-
|
|
21
29
|
meter.create_observable_gauge(
|
|
22
|
-
"ruby.gc.heap_live_slots",
|
|
23
|
-
|
|
24
|
-
description: "Live object slots on the Ruby heap.",
|
|
25
|
-
callback: lambda do
|
|
26
|
-
GC.stat(:heap_live_slots)
|
|
27
|
-
end
|
|
30
|
+
"ruby.gc.heap_live_slots", unit: "{slot}", description: "Live object slots on the Ruby heap.",
|
|
31
|
+
callback: -> { GC.stat(:heap_live_slots) }
|
|
28
32
|
)
|
|
33
|
+
end
|
|
29
34
|
|
|
35
|
+
def register_process(meter)
|
|
30
36
|
meter.create_observable_gauge(
|
|
31
|
-
"process.thread.count",
|
|
32
|
-
unit: "{thread}",
|
|
33
|
-
description: "Number of active Ruby threads.",
|
|
34
|
-
callback: lambda do
|
|
35
|
-
Thread.list.size
|
|
36
|
-
end
|
|
37
|
+
"process.thread.count", unit: "{thread}", description: "Number of active Ruby threads.", callback: -> { Thread.list.size }
|
|
37
38
|
)
|
|
38
|
-
|
|
39
39
|
meter.create_observable_counter(
|
|
40
|
-
"process.cpu.time",
|
|
41
|
-
|
|
42
|
-
description: "Process CPU time in seconds.",
|
|
43
|
-
callback: lambda do
|
|
44
|
-
t = Process.times
|
|
45
|
-
t.utime + t.stime
|
|
46
|
-
end
|
|
40
|
+
"process.cpu.time", unit: "s", description: "Process CPU time in seconds.",
|
|
41
|
+
callback: -> { Process.times.then { |times| times.utime + times.stime } }
|
|
47
42
|
)
|
|
48
|
-
|
|
49
43
|
if File.readable?("/proc/self/statm")
|
|
50
44
|
meter.create_observable_gauge(
|
|
51
|
-
"process.memory.usage",
|
|
52
|
-
unit: "By",
|
|
53
|
-
description: "Resident set size in bytes.",
|
|
54
|
-
callback: lambda do
|
|
55
|
-
read_rss_bytes
|
|
56
|
-
end
|
|
45
|
+
"process.memory.usage", unit: "By", description: "Resident set size in bytes.", callback: -> { read_rss_bytes }
|
|
57
46
|
)
|
|
58
47
|
end
|
|
59
|
-
|
|
60
48
|
return unless File.directory?("/proc/self/fd")
|
|
61
49
|
|
|
62
50
|
meter.create_observable_gauge(
|
|
63
|
-
"process.unix.file_descriptor.count",
|
|
64
|
-
|
|
65
|
-
description: "Open file descriptors.",
|
|
66
|
-
callback: lambda do
|
|
67
|
-
Dir.children("/proc/self/fd").size
|
|
68
|
-
rescue StandardError
|
|
69
|
-
0
|
|
70
|
-
end
|
|
51
|
+
"process.unix.file_descriptor.count", unit: "{file_descriptor}", description: "Open file descriptors.",
|
|
52
|
+
callback: -> { open_file_descriptors }
|
|
71
53
|
)
|
|
72
|
-
|
|
73
|
-
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def open_file_descriptors
|
|
57
|
+
Dir.children("/proc/self/fd").size
|
|
58
|
+
rescue SystemCallError
|
|
59
|
+
0
|
|
74
60
|
end
|
|
75
61
|
|
|
76
62
|
def read_rss_bytes
|
|
77
63
|
parts = File.read("/proc/self/statm").split
|
|
78
64
|
return 0 if parts.length < 2
|
|
79
65
|
|
|
80
|
-
parts[1].to_i *
|
|
66
|
+
parts[1].to_i * PAGE_SIZE
|
|
81
67
|
rescue StandardError
|
|
82
68
|
0
|
|
83
69
|
end
|