flare 0.9.0 → 0.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd5b5fab580387a959687183e1cc45f029b5a240aa8f2550dc45e865982b5005
4
- data.tar.gz: a4822ce2fa543a98d67fae77654d6e5fd5ddf5f920299cd1c254519b42d90bbc
3
+ metadata.gz: 172caa3536661f2161fd2da2789bfbfc5f8279c9c99968887c27b04ed2ad61d6
4
+ data.tar.gz: 4f740703c091264639a9286a24a52c8bfeebb86104b711ba662621eaf34eb67f
5
5
  SHA512:
6
- metadata.gz: 03cb32b349f10120fad4094db69878de6bbe94d243e7cc8a908091b8e99306e7d6d7acc92313088ea70a4a8ff56d920ab510ab24bd2f669168c79d0c0b7af943
7
- data.tar.gz: c1016c807d3b6eddd2db368292381f84beac2e8712b025e5047fa0a5f6a752a9606d185839c6b2793d6161f53efa86373aac15b3e50b5f22bc2bd2fcfb165945
6
+ metadata.gz: 0e0e7eb55e0dc38e8ba690b6152430f0da2c5cfdc52ab82dfe32c6f0ca3da3cf7dd14b4f51d15065f95433f7c69370d5695e55592bb370b0b677e16686f452d3
7
+ data.tar.gz: a8a1db7116653857570e1f717a176a040cce4ec258345736bd65746161d9e2f2a1f84d6e5389e8dc352a95d305172e42d3c9c40ed71d091d3abb57a946d1a975
@@ -108,6 +108,7 @@ module Flare
108
108
 
109
109
  def initialize(filters = [])
110
110
  @parameter_filter = ActiveSupport::ParameterFilter.new(DEFAULT_FILTERS + filters)
111
+ @credential_filter = ActiveSupport::ParameterFilter.new(DEFAULT_FILTERS)
111
112
  end
112
113
 
113
114
  # Whether filter_parameters covers a param name like "user[password]".
@@ -133,7 +134,8 @@ module Flare
133
134
  private
134
135
 
135
136
  def attribute(name, value, system)
136
- return value if KEPT.include?(name) || KEPT_PREFIX.match?(name)
137
+ return value if KEPT.include?(name)
138
+ return kept_prefix(name, value) if KEPT_PREFIX.match?(name)
137
139
  return FILTERED if OPAQUE.include?(name) || OPAQUE_PREFIX.match?(name)
138
140
 
139
141
  case value
@@ -146,6 +148,17 @@ module Flare
146
148
  FILTERED
147
149
  end
148
150
 
151
+ # The trace view and rules read these whatever the app filters, since its
152
+ # list can hold words like "code" or "system". Names that hold credentials
153
+ # or contents, like messaging.message.token or messaging.message.body,
154
+ # still don't go out.
155
+ def kept_prefix(name, value)
156
+ leaf = name.split(".").last
157
+ return FILTERED if OPAQUE.include?(leaf)
158
+
159
+ [name, leaf].all? { |key| @credential_filter.filter_param(key, value) == value } ? value : FILTERED
160
+ end
161
+
149
162
  def text_attribute(name, value, system)
150
163
  return statement(value, system) if STATEMENTS.include?(name)
151
164
  return SqlMask.operation_word(value) || SqlMask::PLACEHOLDER if OPERATIONS.include?(name)
@@ -5,6 +5,7 @@ require "logger"
5
5
  require "opentelemetry/sdk"
6
6
 
7
7
  require_relative "deadline"
8
+ require_relative "metric_span_processor"
8
9
 
9
10
  module Flare
10
11
  # BSP-shaped span processor whose filter is `sampled OR marked` instead
@@ -114,6 +115,7 @@ module Flare
114
115
 
115
116
  return unless sampled || marked
116
117
 
118
+ MetricSpanProcessor.note_handled_job(span) if @slo_check
117
119
  span_data = span.respond_to?(:to_span_data) ? span.to_span_data : span
118
120
  complete = owner_finished || sampled_completion_span?(span_data)
119
121
 
@@ -37,6 +37,15 @@ module Flare
37
37
 
38
38
  ACTIVE_JOB_ERROR_STATUS = /\AUnexpected ActiveJob Error (\S+)\z/
39
39
 
40
+ # The id of the job span on this thread whose error discard_on or
41
+ # retry_on handled.
42
+ HANDLED_JOB_KEY = :__flare_handled_job_span_id
43
+
44
+ # The ActiveJob instrumentation's child spans for an error the job
45
+ # handled: discard_on dropped it, retry_on retried it, or a retry_on
46
+ # block took the last one.
47
+ HANDLED_JOB_EVENTS = [" discard", " enqueue_retry", " retry_stopped"].freeze
48
+
40
49
  # Where a statement names the table it writes, by its first word. Every
41
50
  # other statement is named for the table it reads FROM.
42
51
  INTO_TABLE = /\bINTO\s+[`"]?(\w+)[`"]?/i
@@ -66,6 +75,7 @@ module Flare
66
75
  return unless span.end_timestamp && span.start_timestamp
67
76
 
68
77
  detect_forking
78
+ self.class.note_handled_job(span)
69
79
 
70
80
  if web_request?(span) || background_job?(span)
71
81
  record_entry_point_metric(span)
@@ -183,10 +193,23 @@ module Flare
183
193
  key = web_key(span)
184
194
  [key, http_error?(span)] if key
185
195
  elsif background_job?(span)
186
- [job_key(span), span_error?(span)]
196
+ [job_key(span), span_error?(span) && !handled_job_error?(span)]
187
197
  end
188
198
  end
189
199
 
200
+ # A job span records its own exception only when one escaped the job, as
201
+ # when retries run out without a block or a discard_on block raises, so
202
+ # that still counts as a failure.
203
+ def handled_job_error?(span)
204
+ handled_id = Thread.current[HANDLED_JOB_KEY]
205
+ return false unless handled_id
206
+
207
+ # Finished SpanData (FilteringSpanProcessor's SLO check) has span_id;
208
+ # a live span only has its context.
209
+ span_id = span.respond_to?(:span_id) ? span.span_id : span.context.span_id
210
+ handled_id == span_id && self.class.exception_event(span).nil?
211
+ end
212
+
190
213
  def web_key(span)
191
214
  target = span.attributes[Flare::TRANSACTION_NAME_ATTRIBUTE]
192
215
 
@@ -362,11 +385,25 @@ module Flare
362
385
  # A job's retried exception is recorded on its enqueue_retry child span,
363
386
  # so the process span only names it in the status description.
364
387
  def self.exception_type(span)
365
- event = span.events&.reverse_each&.find { |e| e.name == "exception" }
366
- event&.attributes&.[]("exception.type") ||
388
+ exception_event(span)&.attributes&.[]("exception.type") ||
367
389
  span.status&.description.to_s[ACTIVE_JOB_ERROR_STATUS, 1]
368
390
  end
369
391
 
392
+ # discard_on and retry_on rescue the job's exception, but the ActiveJob
393
+ # instrumentation still marks the job span failed from their child span,
394
+ # which ends first on the same thread. The trace processor's SLO check
395
+ # calls this too, since metrics can be off while tracing is on.
396
+ def self.note_handled_job(span)
397
+ return unless span.name.to_s.end_with?(*HANDLED_JOB_EVENTS) && span.attributes["messaging.system"] == "active_job"
398
+
399
+ Thread.current[HANDLED_JOB_KEY] = span.parent_span_id
400
+ end
401
+
402
+ # The last exception the span recorded itself.
403
+ def self.exception_event(span)
404
+ span.events&.reverse_each&.find { |e| e.name == "exception" }
405
+ end
406
+
370
407
  # Subclasses of an ignored class match when the class is loaded.
371
408
  def ignored_exception_type?(type)
372
409
  klass = ActiveSupport::Inflector.safe_constantize(type)
@@ -18,7 +18,11 @@ module Flare
18
18
  # "duration_ms": <int>,
19
19
  # "spans": [
20
20
  # { "id", "parent_id", "name", "started_at",
21
- # "duration_ms", "attributes" }
21
+ # "duration_ms", "attributes",
22
+ # "kind", # "client", "server", "internal", "producer" or "consumer"
23
+ # "status", # "ok" or "error", left out when unset
24
+ # "events" } # [{ "name" => "exception", "timestamp", "attributes" => { "exception.type" } }],
25
+ # # left out when none
22
26
  # ]
23
27
  # }
24
28
  #
@@ -30,6 +34,18 @@ module Flare
30
34
  class TraceBlob
31
35
  ZERO_SPAN_ID = ("\x00".b * 8).freeze
32
36
  ROOT_NAME_LIMIT = 255
37
+ # Exceptions recorded in a trace go out as events on the span that raised
38
+ # them, up to this many per trace. The root's go first, and its last one
39
+ # always goes, so the exception that ended the trace is never crowded
40
+ # out. Only the class goes with each: other events and attributes can
41
+ # carry any value at any size.
42
+ EXCEPTIONS_LIMIT = 32
43
+ # Only the code: instrumentation often puts the exception's message in the
44
+ # status description, and messages are where personal data ends up.
45
+ STATUS_CODES = {
46
+ OpenTelemetry::Trace::Status::OK => "ok",
47
+ OpenTelemetry::Trace::Status::ERROR => "error"
48
+ }.freeze
33
49
 
34
50
  def self.build(trace_id:, spans:)
35
51
  return nil if spans.nil? || spans.empty?
@@ -44,6 +60,7 @@ module Flare
44
60
 
45
61
  def to_h
46
62
  root = find_root
63
+ @exceptions = exceptions_by_span(root)
47
64
  UTF8.scrub({
48
65
  "trace_id" => hexify(@trace_id),
49
66
  "trace_rule_id" => rule_id_from_spans,
@@ -104,7 +121,7 @@ module Flare
104
121
  end
105
122
 
106
123
  def span_to_h(span)
107
- {
124
+ hash = {
108
125
  "id" => hexify(span.span_id),
109
126
  "parent_id" => root?(span) ? nil : hexify(span.parent_span_id),
110
127
  "name" => @filter.name(span.name),
@@ -112,6 +129,47 @@ module Flare
112
129
  "duration_ms" => duration_ms(span),
113
130
  "attributes" => @filter.attributes(span.attributes)
114
131
  }
132
+ # The SDK's kinds are symbols; anything else is left out rather than guessed at.
133
+ hash["kind"] = span.kind.name if span.kind.is_a?(Symbol)
134
+ status = STATUS_CODES[span.status&.code]
135
+ hash["status"] = status if status
136
+ events = @exceptions[span]
137
+ hash["events"] = events if events
138
+ hash
139
+ end
140
+
141
+ def exceptions_by_span(root)
142
+ by_span = {}.compare_by_identity
143
+ root_events = root ? exception_events(root) : []
144
+ kept = root_events.first(EXCEPTIONS_LIMIT - 1)
145
+ kept << root_events.last if root_events.length >= EXCEPTIONS_LIMIT
146
+ by_span[root] = kept.map { |event| exception_to_h(event) } if kept.any?
147
+
148
+ left = EXCEPTIONS_LIMIT - kept.length
149
+ @spans.each do |span|
150
+ break if left.zero?
151
+ next if span.equal?(root)
152
+
153
+ events = exception_events(span).first(left)
154
+ next if events.empty?
155
+
156
+ left -= events.length
157
+ by_span[span] = events.map { |event| exception_to_h(event) }
158
+ end
159
+ by_span
160
+ end
161
+
162
+ def exception_events(span)
163
+ Array(span.events).select { |event| event.name == "exception" }
164
+ end
165
+
166
+ def exception_to_h(event)
167
+ type = event.attributes&.[]("exception.type")
168
+ {
169
+ "name" => "exception",
170
+ "timestamp" => iso(event.timestamp),
171
+ "attributes" => type ? { "exception.type" => type.to_s[0, ROOT_NAME_LIMIT] } : {}
172
+ }
115
173
  end
116
174
 
117
175
  def hexify(bytes)
data/lib/flare/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flare
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-29 00:00:00.000000000 Z
11
+ date: 2026-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties