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.
@@ -93,48 +93,23 @@ module Libo11y
93
93
  out
94
94
  end
95
95
 
96
- # Assert specification conformance across resource, spans, logs, and metrics.
96
+ # Asserts that the recorded telemetry conforms to the specification,
97
+ # and raises an AssertionError that lists every problem:
98
+ #
99
+ # - a required tier-0 attribute missing from the resource, or a
100
+ # resource without the specification's schema URL
101
+ # - a secret-classified attribute that the code under test emitted. It
102
+ # was redacted, and the library counted the redaction, but secrets
103
+ # must never be emitted.
104
+ # - a secret-classified key in the clear on the resource, a span, a
105
+ # span event or link, a log record or a metric data point
106
+ # - a registered event that lacks a required attribute
107
+ #
108
+ # Call it before the recorder shuts down, because it collects metrics.
97
109
  def assert_conforms!
98
- res_attrs = @telemetry.resource.attributes
99
- Spec::REQUIRED_TIER0.each do |key|
100
- val = res_attrs[key]
101
- raise AssertionError, "missing required tier0 resource attribute #{key}" if val.nil? || val.to_s.empty?
102
- end
103
- if @telemetry.resource.respond_to?(:schema_url) && @telemetry.resource.schema_url != Spec::SCHEMA_URL
104
- raise AssertionError, "resource schema_url #{@telemetry.resource.schema_url} != #{Spec::SCHEMA_URL}"
105
- end
106
-
107
- policy = @telemetry.policy
108
-
109
- # Check no secret attributes leaked into resource, spans, or logs
110
- check_attrs = lambda do |surface, attrs|
111
- next if attrs.nil?
112
-
113
- attrs.each_key do |k|
114
- cls = policy.lookup(k.to_s)
115
- raise AssertionError, "secret attribute #{k} leaked on #{surface}" if cls == Spec::Sensitivity::SECRET
116
- end
117
- end
110
+ problems = resource_problems + violation_problems + cleartext_problems + event_problems
111
+ raise AssertionError, problems.join("\n") unless problems.empty?
118
112
 
119
- check_attrs.call("resource", res_attrs)
120
- spans.each do |s|
121
- check_attrs.call("span #{s.name}", s.attributes)
122
- s.events&.each { |ev| check_attrs.call("span event #{ev.name}", ev.attributes) }
123
- s.links&.each { |lk| check_attrs.call("span link", lk.attributes) }
124
- end
125
- logs.each do |lr|
126
- check_attrs.call("log #{lr.body}", lr.attributes)
127
- ev_name = lr.event_name || lr.attributes&.[](Spec::EVENT_NAME_KEY)
128
- next unless ev_name && (ev_info = Spec::EVENTS[ev_name])
129
-
130
- ev_info.required.each do |req_key|
131
- raise AssertionError, "event #{ev_name} missing required attribute #{req_key}" unless lr.attributes&.key?(req_key)
132
- end
133
- ev_info.required_templates.each do |prefix|
134
- found = lr.attributes&.keys&.any? { |k| k.start_with?("#{prefix}.") }
135
- raise AssertionError, "event #{ev_name} missing template attribute #{prefix}.*" unless found
136
- end
137
- end
138
113
  true
139
114
  end
140
115
 
@@ -143,11 +118,7 @@ module Libo11y
143
118
  span_pairs = spans.map { |s| [s.hex_trace_id, s.hex_span_id] }
144
119
  raise AssertionError, "no finished spans recorded" if span_pairs.empty?
145
120
 
146
- matched_log = logs.any? do |lr|
147
- tid = lr.trace_id&.unpack1("H*")
148
- sid = lr.span_id&.unpack1("H*")
149
- span_pairs.include?([tid, sid])
150
- end
121
+ matched_log = logs.any? { |lr| span_pairs.include?([hex(lr.trace_id), hex(lr.span_id)]) }
151
122
  raise AssertionError, "no OTLP log record correlated with any recorded span" unless matched_log
152
123
 
153
124
  matched_console = console_records.any? do |rec|
@@ -158,21 +129,93 @@ module Libo11y
158
129
  true
159
130
  end
160
131
 
161
- # Assert full three-way reconciliation: span <-> log <-> metric exemplar on the same trace_id.
162
- def assert_triangle!
132
+ # Asserts that one trace holds all three signals: a finished span, a
133
+ # log record written inside one of the trace's spans, and a metric
134
+ # exemplar recorded inside one of them. With a metric name, only that
135
+ # metric's exemplars count.
136
+ def assert_triangle!(metric_name = nil)
163
137
  assert_correlated!
164
- span_trace_ids = spans.map(&:hex_trace_id).uniq
165
- ex_list = exemplars
166
- matched_exemplar = ex_list.any? do |entry|
167
- ex = entry[:exemplar]
168
- tid = ex.trace_id&.unpack1("H*")
169
- span_trace_ids.include?(tid)
138
+ span_ids = spans.group_by(&:hex_trace_id).transform_values { |list| list.map(&:hex_span_id) }
139
+ in_trace = ->((trace_id, span_id)) { span_ids.fetch(trace_id, []).include?(span_id) }
140
+
141
+ logged = logs.map { |lr| [hex(lr.trace_id), hex(lr.span_id)] }.select(&in_trace).map(&:first)
142
+ measured = exemplars(metric_name).map { |e| [hex(e[:exemplar].trace_id), hex(e[:exemplar].span_id)] }.select(&in_trace).map(&:first)
143
+ return true if logged.intersect?(measured)
144
+
145
+ metric = metric_name ? "an exemplar of metric #{metric_name.inspect}" : "a metric exemplar"
146
+ raise AssertionError, "no trace has a span, a log record and #{metric}"
147
+ end
148
+
149
+ private
150
+
151
+ def hex(id)
152
+ id&.unpack1("H*")
153
+ end
154
+
155
+ def resource_problems
156
+ resource = @telemetry.resource
157
+ problems = Spec::REQUIRED_TIER0.select { |key| resource.attributes[key].to_s.empty? }
158
+ .map { |key| "resource missing required tier-0 attribute #{key.inspect}" }
159
+ if resource.respond_to?(:schema_url) && resource.schema_url != Spec::SCHEMA_URL
160
+ problems << "resource schema_url #{resource.schema_url.inspect} is not #{Spec::SCHEMA_URL.inspect}"
170
161
  end
171
- raise AssertionError, "no metric exemplar reconciled with any recorded trace" unless matched_exemplar
162
+ problems
163
+ end
172
164
 
173
- true
165
+ # One problem per signal on which the policy counted a redacted secret.
166
+ def violation_problems
167
+ @telemetry.force_flush
168
+ metric = metrics.find { |m| m.name == Spec::METRIC_SCHEMA_VIOLATIONS }
169
+ return [] if metric.nil?
170
+
171
+ counts = Hash.new(0)
172
+ metric.data_points.each do |dp|
173
+ counts[dp.attributes[Spec::ATTR_SIGNAL]] += dp.value if dp.attributes[Spec::ATTR_RULE] == "secret_attribute"
174
+ end
175
+ counts.sort.map do |signal, count|
176
+ source = if signal == Spec::Signal::RESOURCE
177
+ "the recorder's options put a secret-classified attribute on the resource"
178
+ else
179
+ "the code under test emitted secret-classified attributes on #{signal}"
180
+ end
181
+ "#{source} (#{count} redaction(s) counted); they were redacted, but secrets must never be emitted"
182
+ end
174
183
  end
175
184
 
185
+ def cleartext_problems
186
+ surfaces = [["resource", @telemetry.resource.attributes]]
187
+ spans.each do |span|
188
+ surfaces << ["span #{span.name.inspect}", span.attributes]
189
+ span.events&.each { |event| surfaces << ["span event #{event.name.inspect}", event.attributes] }
190
+ span.links&.each { |link| surfaces << ["a link of span #{span.name.inspect}", link.attributes] }
191
+ end
192
+ logs.each { |record| surfaces << ["log record #{record.body.inspect}", record.attributes] }
193
+ metrics.each do |metric|
194
+ Array(metric.data_points).each { |dp| surfaces << ["metric #{metric.name.inspect}", dp.attributes] }
195
+ end
196
+
197
+ surfaces.flat_map do |surface, attributes|
198
+ (attributes || {}).keys.select { |key| Spec::SENSITIVITY_OF[key.to_s] == Spec::Sensitivity::SECRET }
199
+ .map { |key| "#{surface} carries secret-classified attribute #{key.to_s.inspect} in the clear" }
200
+ end
201
+ end
202
+
203
+ def event_problems
204
+ logs.flat_map do |record|
205
+ attributes = record.attributes || {}
206
+ name = record.event_name || attributes[Spec::EVENT_NAME_KEY]
207
+ contract = name && Spec::EVENTS[name]
208
+ next [] unless contract
209
+
210
+ missing = contract.required.reject { |key| attributes.key?(key) }
211
+ missing += contract.required_templates.reject { |prefix| attributes.keys.any? { |key| key.start_with?("#{prefix}.") } }
212
+ .map { |prefix| "#{prefix}.*" }
213
+ missing.map { |key| "event #{name} missing required attribute #{key}" }
214
+ end
215
+ end
216
+
217
+ public
218
+
176
219
  def shutdown(timeout: 5.0)
177
220
  @telemetry.shutdown(timeout: timeout)
178
221
  end
@@ -194,7 +237,13 @@ module Libo11y
194
237
  # production bundle carries no web server. Add `gem "webrick"` to the
195
238
  # Gemfile group that runs the tests.
196
239
  class OtlpHttpReceiver
197
- attr_reader :port, :endpoint, :trace_requests, :metric_requests, :log_requests, :received_headers
240
+ attr_reader :port, :endpoint
241
+
242
+ # What has arrived so far, as copies taken under the lock the server
243
+ # thread writes under.
244
+ %i[trace_requests metric_requests log_requests received_headers].each do |name|
245
+ define_method(name) { @mutex.synchronize { instance_variable_get(:"@#{name}").dup } }
246
+ end
198
247
 
199
248
  def self.load_webrick
200
249
  require "webrick"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libo11y
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libo11y
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Ainslie
@@ -9,6 +9,20 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: logger
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.6'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.6'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: opentelemetry-api
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -169,14 +183,18 @@ files:
169
183
  - lib/libo11y/internal/bridges.rb
170
184
  - lib/libo11y/internal/endpoint.rb
171
185
  - lib/libo11y/internal/env.rb
186
+ - lib/libo11y/internal/histogram_buckets.rb
187
+ - lib/libo11y/internal/http_common.rb
172
188
  - lib/libo11y/internal/identity.rb
173
189
  - lib/libo11y/internal/logging_device.rb
174
190
  - lib/libo11y/internal/pii.rb
191
+ - lib/libo11y/internal/pipeline.rb
175
192
  - lib/libo11y/internal/policy.rb
176
193
  - lib/libo11y/internal/propagators.rb
177
194
  - lib/libo11y/internal/resource.rb
178
195
  - lib/libo11y/internal/runtime_metrics.rb
179
196
  - lib/libo11y/internal/sql_text.rb
197
+ - lib/libo11y/internal/summary.rb
180
198
  - lib/libo11y/options.rb
181
199
  - lib/libo11y/proc.rb
182
200
  - lib/libo11y/rack.rb