logbrew-sdk 0.1.18 → 0.1.20
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/README.md +31 -5
- data/lib/logbrew/automatic_delivery.rb +27 -8
- data/lib/logbrew/http_client_tracing.rb +3 -1
- data/lib/logbrew/issue_diagnostics.rb +2 -1
- data/lib/logbrew/rails_integration.rb +49 -8
- data/lib/logbrew/request_scope.rb +70 -0
- data/lib/logbrew/sidekiq.rb +1 -1
- data/lib/logbrew/version.rb +1 -1
- data/lib/logbrew/worker_lifecycle.rb +1 -1
- data/lib/logbrew.rb +27 -13
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a0f4d5c699a2ad04533c601925d0f7f58c4271f16f345ffbf7f65f68ea5fde8f
|
|
4
|
+
data.tar.gz: 2683b8e1ebdb4a2af2c3db7765ce6f54ff16a925833c5adb03a533618fac54be
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d5045b002e7ea063d305484d4a641c9ad40fe9d3f284339f8f3361b342b5bb940c50d6c3913776d2943f015e5c14aaa2f495aa8b65f656d0f204a825ea708f5e
|
|
7
|
+
data.tar.gz: 60563aa13772a2e8d75d3632ea0fa26204edd8c627bb03a656fa079cef58e569de24cb1dd6d866259e7fa26c053e54c43b9e0859b1fbaa0501ed36f633d986a4
|
data/README.md
CHANGED
|
@@ -51,9 +51,20 @@ Escaped request failures include exception identity,
|
|
|
51
51
|
`rails.middleware` with `handled: false`, and up to 32 newest-first sanitized
|
|
52
52
|
frames. Handled Rails reports use `rails.error_reporter` with `handled: true`.
|
|
53
53
|
It does not record concrete request paths, query strings, request or response
|
|
54
|
-
bodies, arbitrary headers, authorization values, cookies,
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
bodies, arbitrary headers, authorization values, cookies, exception messages,
|
|
55
|
+
raw backtrace text, source snippets, locals, arguments, or absolute paths.
|
|
56
|
+
Exception messages and raw backtrace text are separate opt-ins.
|
|
57
|
+
|
|
58
|
+
To see which user hit an error, call `LogBrew.set_user` with your app's opaque
|
|
59
|
+
user ID, for example in a `before_action`. Every issue, log, span, and metric
|
|
60
|
+
of that request then carries the user. Pass an ID, never an email address or a
|
|
61
|
+
name. Outside a request, the call does nothing and returns `false`:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
class ApplicationController < ActionController::Base
|
|
65
|
+
before_action { LogBrew.set_user(current_user.id.to_s) if current_user }
|
|
66
|
+
end
|
|
67
|
+
```
|
|
57
68
|
|
|
58
69
|
ActiveJob needs no extra initializer. Each enqueue and execution emits a
|
|
59
70
|
producer or worker span, including bounded adapter identity, retry count, and
|
|
@@ -79,6 +90,13 @@ gem, startup, and out-of-trace logs remain local. Messages are limited to 2,048
|
|
|
79
90
|
characters and include `messageState: captured` or `messageState: truncated`.
|
|
80
91
|
Log messages are application-provided telemetry and can contain sensitive
|
|
81
92
|
values, so enable this only after the application applies its normal filtering.
|
|
93
|
+
Each captured message in a request also becomes a breadcrumb, so an issue shows
|
|
94
|
+
the last 64 log lines that led to it. Messages longer than 512 characters are
|
|
95
|
+
cut in the breadcrumb. Add your own breadcrumbs with `LogBrew.add_breadcrumb`.
|
|
96
|
+
|
|
97
|
+
Set `LOGBREW_CAPTURE_REQUEST_METRICS=true` to record an `http.server.duration`
|
|
98
|
+
histogram in milliseconds for each request. Its metadata holds only the method,
|
|
99
|
+
route template, status code, status class, service, environment, and release.
|
|
82
100
|
|
|
83
101
|
The explicit Rails capture settings are:
|
|
84
102
|
|
|
@@ -95,6 +113,7 @@ The explicit Rails capture settings are:
|
|
|
95
113
|
| `LOGBREW_FLUSH_THRESHOLD` | `100` | Queue size from 1 to 1000 that requests an earlier flush |
|
|
96
114
|
| `LOGBREW_CAPTURE_EXCEPTION_MESSAGES` | `false` | Opt in to exception message capture |
|
|
97
115
|
| `LOGBREW_CAPTURE_RAILS_LOGS` | `false` | Opt in to bounded application-origin logs on active request and job traces |
|
|
116
|
+
| `LOGBREW_CAPTURE_REQUEST_METRICS` | `false` | Opt in to one request duration metric per request |
|
|
98
117
|
| `LOGBREW_INCLUDE_EXCEPTION_BACKTRACE` | `false` | Opt in to raw exception backtrace text; sanitized structured frames are always captured |
|
|
99
118
|
|
|
100
119
|
`LOGBREW_API_KEY` and `LOGBREW_INGEST_KEY` are not Rails aliases. If either is
|
|
@@ -580,7 +599,7 @@ client.metric(
|
|
|
580
599
|
)
|
|
581
600
|
```
|
|
582
601
|
|
|
583
|
-
Supported metric kinds are `counter`, `gauge`, and `histogram`. Counters and histograms require `delta` or `cumulative` temporality and non-negative values; gauges require `instant` temporality and may be negative. An optional `description` gives people and investigation tools the stable meaning of the measurement. Keep it generic, single-line, between 1 and 1,024 Unicode scalar values, and free of identifiers, personal data, or changing values. It is not a query dimension. Keep metadata low-cardinality and primitive. This SDK does not
|
|
602
|
+
Supported metric kinds are `counter`, `gauge`, and `histogram`. Counters and histograms require `delta` or `cumulative` temporality and non-negative values; gauges require `instant` temporality and may be negative. An optional `description` gives people and investigation tools the stable meaning of the measurement. Keep it generic, single-line, between 1 and 1,024 Unicode scalar values, and free of identifiers, personal data, or changing values. It is not a query dimension. Keep metadata low-cardinality and primitive. The Rails integration records request duration with `LOGBREW_CAPTURE_REQUEST_METRICS=true`. This SDK does not collect Ruby runtime, Rack, or database metrics automatically.
|
|
584
603
|
|
|
585
604
|
## Product And Network Timelines
|
|
586
605
|
|
|
@@ -724,7 +743,7 @@ client.shutdown
|
|
|
724
743
|
|
|
725
744
|
The worker starts only after accepted queue work exists, then wakes when the queue reaches `flush_threshold` or `flush_interval` expires. The defaults are 1 second and 100 events, so a single event is readable in about a second while bursts still share one request. Restart-hydrated persistent work wakes it immediately. Automatic and manual sends share the existing serialized flush, immutable failed prefix, accepted-prefix acknowledgement, batch bounds, and persistence format; no second queue or transport path is created.
|
|
726
745
|
|
|
727
|
-
Retryable network, `408`, and `5xx` failures retain the exact failed body and use capped equal-jitter backoff. Authentication (`401`/`403`), quota (`429`, described below), validation (`400`/`422`), and other non-retryable responses pause automatic sends without dropping queued work. `recover_automatic_delivery` performs one explicit synchronous flush through the owned transport and resumes scheduling only after success. Calling `flush(transport)` directly provides the same explicit recovery boundary. `stop_automatic_delivery` joins the worker without draining or discarding work; a later manual flush remains available.
|
|
746
|
+
Retryable network, `408`, and `5xx` failures retain the exact failed body and use capped equal-jitter backoff. Authentication (`401`/`403`), quota (`429`, described below), validation (`400`/`422`), and other non-retryable responses pause automatic sends without dropping queued work. A paused delivery resends the same batch on its own as a probe: the first probe waits up to `retry_max_delay`, each failed probe doubles the wait up to 5 minutes (or `retry_max_delay` when it is longer), and the first accepted probe resumes normal delivery. During the pause `delivery_health` reports the `paused` state, the reason, and the probe delay in `retry_delay_ms`. A persistent queue failure does not probe and waits for explicit recovery. `recover_automatic_delivery` performs one explicit synchronous flush through the owned transport at once and resumes scheduling only after success. Calling `flush(transport)` directly provides the same explicit recovery boundary. `stop_automatic_delivery` joins the worker without draining or discarding work; a later manual flush remains available.
|
|
728
747
|
|
|
729
748
|
A `429` response with `code: "usage_limit_exceeded"` in its JSON body pauses with the `quota` reason, even when it includes `Retry-After`. Any other `429` with a readable `Retry-After` value (delta-seconds or an HTTP date; a past date counts as zero) or with `code: "rate_limited"` starts a timed wait instead: the client waits for the `Retry-After` delay, or its usual retry backoff when the header is missing, kept between 1 second and 1 hour plus up to 10 percent of random jitter, and capped at 1 hour. The failed batch stays first in the queue and is sent again unchanged when the wait ends, without any application call, while new events keep queueing within the usual limits. During the wait `delivery_health` reports the `retrying` state with `retry_delay_ms`, not a pause. A `429` with neither pauses as before. `HttpTransport` reads at most 4 KiB of a `429` body only to find `code` and never keeps or reports it. A custom transport can request the same handling by returning `LogBrew::TransportResponse.new(429, 1, retry_after: seconds, code: code)`. `flush` still raises `transport_error` for any `429`.
|
|
730
749
|
|
|
@@ -881,6 +900,12 @@ is omitted unless `include_exception_backtrace: true` is set; sanitized
|
|
|
881
900
|
structured frames remain available either way. Events queue by default; pass
|
|
882
901
|
`transport:` plus `flush_on_response: true` when each response should flush.
|
|
883
902
|
|
|
903
|
+
Inside a request, `LogBrew.set_user("user_123")` adds the user to every event
|
|
904
|
+
of that request. `LogBrew.add_breadcrumb(category: "checkout", level: "info",
|
|
905
|
+
message: "Cart loaded")` adds a breadcrumb to the request's next issue. An issue
|
|
906
|
+
keeps the newest 64 breadcrumbs. Breadcrumbs you pass to `client.issue` replace
|
|
907
|
+
them.
|
|
908
|
+
|
|
884
909
|
## Rails Error Subscriber
|
|
885
910
|
|
|
886
911
|
The automatic Rails integration already subscribes to handled Rails errors.
|
|
@@ -931,6 +956,7 @@ unhandled request exceptions are not captured twice.
|
|
|
931
956
|
- `LogBrew::IssueDiagnostics` builds typed exception identity, mechanism/handled state, basename-only structured frames, and bounded ordered breadcrumbs without raw exception internals.
|
|
932
957
|
- `LogBrew::HttpTransport` sends queued batches through Ruby's standard `Net::HTTP` with configurable endpoint, headers, timeout, and app-owned HTTP client support.
|
|
933
958
|
- `LogBrew::RackMiddleware` captures Rack request spans and unhandled app exceptions without requiring Rails or Rack at runtime.
|
|
959
|
+
- `LogBrew.set_user` and `LogBrew.add_breadcrumb` add the user and breadcrumbs to the events of the current request.
|
|
934
960
|
- `LogBrew::RailsErrorSubscriber` captures handled/manual Rails error reports without requiring Rails at runtime.
|
|
935
961
|
- `LogBrew::Rails` automatically installs privacy-bounded Rails request spans, handled-error issues, per-process delivery, health access, and idempotent shutdown when the canonical server key is configured.
|
|
936
962
|
- `shutdown(transport)` flushes queued events and rejects later writes.
|
|
@@ -28,6 +28,8 @@ module LogBrew
|
|
|
28
28
|
PAUSE_REASONS = %w[authentication quota validation nonretryable].freeze
|
|
29
29
|
RATE_LIMIT_MIN_WAIT = 1.0
|
|
30
30
|
RATE_LIMIT_MAX_WAIT = 3600.0
|
|
31
|
+
PAUSE_PROBE_MAX_WAIT = 300.0
|
|
32
|
+
STORAGE_ERROR_CODES = %w[persistent_queue_error persistence_commit_error].freeze
|
|
31
33
|
|
|
32
34
|
attr_reader :transport
|
|
33
35
|
|
|
@@ -68,6 +70,14 @@ module LogBrew
|
|
|
68
70
|
end
|
|
69
71
|
private_class_method :validate_positive_integer
|
|
70
72
|
|
|
73
|
+
# Doubles from retry_max_delay with each consecutive failure up to 5 minutes, or up to
|
|
74
|
+
# retry_max_delay when it is longer, then keeps half to all of that ceiling as jitter.
|
|
75
|
+
def self.pause_probe_delay(retry_max_delay, failures, jitter = Random.rand)
|
|
76
|
+
maximum = [retry_max_delay, PAUSE_PROBE_MAX_WAIT].max
|
|
77
|
+
ceiling = [retry_max_delay * (2.0**[failures - 1, 0].max), maximum].min
|
|
78
|
+
(ceiling / 2.0) + (ceiling / 2.0 * jitter)
|
|
79
|
+
end
|
|
80
|
+
|
|
71
81
|
def initialize(
|
|
72
82
|
client:,
|
|
73
83
|
transport:,
|
|
@@ -134,8 +144,10 @@ module LogBrew
|
|
|
134
144
|
if @observed_queue_count.zero?
|
|
135
145
|
@wake_requested = false
|
|
136
146
|
@next_interval_deadline = nil
|
|
137
|
-
@
|
|
138
|
-
|
|
147
|
+
unless @state == "paused"
|
|
148
|
+
@retry_deadline = nil
|
|
149
|
+
@retry_delay = nil
|
|
150
|
+
end
|
|
139
151
|
end
|
|
140
152
|
@condition.signal
|
|
141
153
|
end
|
|
@@ -186,8 +198,7 @@ module LogBrew
|
|
|
186
198
|
@pause_reason = previous.fetch(1)
|
|
187
199
|
@wake_requested = false
|
|
188
200
|
@next_interval_deadline = nil
|
|
189
|
-
|
|
190
|
-
@retry_deadline = nil
|
|
201
|
+
schedule_pause_probe(error)
|
|
191
202
|
elsif previous.fetch(0) == "stopped"
|
|
192
203
|
@state = "stopped"
|
|
193
204
|
@wake_requested = false
|
|
@@ -375,12 +386,12 @@ module LogBrew
|
|
|
375
386
|
now = monotonic_time
|
|
376
387
|
retry_ready = !@retry_deadline.nil? && now >= @retry_deadline
|
|
377
388
|
interval_ready = !@next_interval_deadline.nil? && now >= @next_interval_deadline
|
|
378
|
-
if @state != "paused" && (@wake_requested ||
|
|
389
|
+
if retry_ready || (@state != "paused" && (@wake_requested || interval_ready))
|
|
379
390
|
@wake_requested = false
|
|
380
391
|
@retry_deadline = nil
|
|
381
392
|
@retry_delay = nil if retry_ready
|
|
382
393
|
@next_interval_deadline = nil
|
|
383
|
-
@state = retry_ready ? "retrying" : "running"
|
|
394
|
+
@state = retry_ready ? "retrying" : "running" unless @state == "paused"
|
|
384
395
|
@in_flight = true
|
|
385
396
|
return @generation
|
|
386
397
|
end
|
|
@@ -414,12 +425,20 @@ module LogBrew
|
|
|
414
425
|
@condition.signal
|
|
415
426
|
else
|
|
416
427
|
@pause_reason = pause_reason(error)
|
|
417
|
-
@retry_delay = nil
|
|
418
|
-
@retry_deadline = nil
|
|
419
428
|
@state = "paused"
|
|
429
|
+
schedule_pause_probe(error)
|
|
420
430
|
end
|
|
421
431
|
end
|
|
422
432
|
|
|
433
|
+
# A paused delivery resends its batch on its own; a storage failure waits for explicit recovery,
|
|
434
|
+
# because another send could lose or repeat persisted events.
|
|
435
|
+
def schedule_pause_probe(error)
|
|
436
|
+
storage = error.is_a?(SdkError) && STORAGE_ERROR_CODES.include?(error.code)
|
|
437
|
+
@retry_delay = storage ? nil : self.class.pause_probe_delay(@retry_max_delay, @consecutive_failures)
|
|
438
|
+
@retry_deadline = @retry_delay.nil? ? nil : monotonic_time + @retry_delay
|
|
439
|
+
@condition.signal
|
|
440
|
+
end
|
|
441
|
+
|
|
423
442
|
def schedule_pending_work
|
|
424
443
|
if @observed_queue_count.zero?
|
|
425
444
|
@wake_requested = false
|
|
@@ -244,7 +244,9 @@ module LogBrew
|
|
|
244
244
|
end
|
|
245
245
|
|
|
246
246
|
def normalize_host(host)
|
|
247
|
-
value = host.to_s.strip.downcase
|
|
247
|
+
value = host.to_s.strip.downcase
|
|
248
|
+
last_label_end = value.rindex(/[^.]/)
|
|
249
|
+
value = last_label_end.nil? ? "" : value[0..last_label_end]
|
|
248
250
|
return nil if value.empty? || value.bytesize > 253
|
|
249
251
|
return nil if value.match?(/\A[0-9.]+\z/)
|
|
250
252
|
return nil unless value.match?(/\A[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?\z/)
|
|
@@ -24,6 +24,7 @@ module LogBrew
|
|
|
24
24
|
MAX_BREADCRUMB_DATA_FIELDS = 8
|
|
25
25
|
MAX_BREADCRUMB_DATA_STRING = 256
|
|
26
26
|
MAX_COORDINATE = 2_147_483_647
|
|
27
|
+
MAX_BACKTRACE_LINE = MAX_FRAME_FILENAME + MAX_FRAME_FUNCTION + 64
|
|
27
28
|
|
|
28
29
|
MACHINE_NAME = /\A[A-Za-z][A-Za-z0-9_.:-]{0,63}\z/.freeze
|
|
29
30
|
DATA_KEY = /\A[A-Za-z][A-Za-z0-9_.-]{0,63}\z/.freeze
|
|
@@ -611,7 +612,7 @@ module LogBrew
|
|
|
611
612
|
end
|
|
612
613
|
|
|
613
614
|
def frame_from_backtrace_line(line)
|
|
614
|
-
return nil unless line.is_a?(String) && line.valid_encoding?
|
|
615
|
+
return nil unless line.is_a?(String) && line.valid_encoding? && line.length <= MAX_BACKTRACE_LINE
|
|
615
616
|
|
|
616
617
|
match = line.match(/\A(.+):(\d+)(?::in [`'](.+?)[`'])?\z/)
|
|
617
618
|
return nil unless match
|
|
@@ -21,7 +21,7 @@ module LogBrew
|
|
|
21
21
|
ATTRIBUTES = %i[
|
|
22
22
|
enabled api_key service_name app_environment rails_version release endpoint
|
|
23
23
|
request_timeout flush_interval flush_threshold capture_exception_messages
|
|
24
|
-
capture_rails_logs include_exception_backtrace
|
|
24
|
+
capture_rails_logs capture_request_metrics include_exception_backtrace
|
|
25
25
|
].freeze
|
|
26
26
|
private_constant :ATTRIBUTES
|
|
27
27
|
attr_reader(*ATTRIBUTES.drop(1).reject { |name| name.to_s.start_with?("capture_", "include_") })
|
|
@@ -89,6 +89,7 @@ module LogBrew
|
|
|
89
89
|
false
|
|
90
90
|
),
|
|
91
91
|
capture_rails_logs: boolean_value(environment, "LOGBREW_CAPTURE_RAILS_LOGS", false),
|
|
92
|
+
capture_request_metrics: boolean_value(environment, "LOGBREW_CAPTURE_REQUEST_METRICS", false),
|
|
92
93
|
include_exception_backtrace: boolean_value(
|
|
93
94
|
environment,
|
|
94
95
|
"LOGBREW_INCLUDE_EXCEPTION_BACKTRACE",
|
|
@@ -111,6 +112,7 @@ module LogBrew
|
|
|
111
112
|
flush_threshold: DEFAULT_FLUSH_THRESHOLD,
|
|
112
113
|
capture_exception_messages: false,
|
|
113
114
|
capture_rails_logs: false,
|
|
115
|
+
capture_request_metrics: false,
|
|
114
116
|
include_exception_backtrace: false
|
|
115
117
|
)
|
|
116
118
|
end
|
|
@@ -221,6 +223,10 @@ module LogBrew
|
|
|
221
223
|
@capture_rails_logs
|
|
222
224
|
end
|
|
223
225
|
|
|
226
|
+
def capture_request_metrics?
|
|
227
|
+
@capture_request_metrics
|
|
228
|
+
end
|
|
229
|
+
|
|
224
230
|
def include_exception_backtrace?
|
|
225
231
|
@include_exception_backtrace
|
|
226
232
|
end
|
|
@@ -382,6 +388,7 @@ module LogBrew
|
|
|
382
388
|
# Adds one bounded, trace-aware LogBrew sink without replacing the app logger.
|
|
383
389
|
module ApplicationLogCapture
|
|
384
390
|
MAX_MESSAGE_CHARACTERS = 2_048
|
|
391
|
+
MAX_BREADCRUMB_CHARACTERS = 512
|
|
385
392
|
SINK_IVAR = :@logbrew_rails_log_sink
|
|
386
393
|
ROOT_IVAR = :@logbrew_rails_application_root
|
|
387
394
|
GUARD_KEY = :logbrew_rails_log_capture
|
|
@@ -391,7 +398,7 @@ module LogBrew
|
|
|
391
398
|
LOGGER_WRAPPER_PATH = %r{/active_support/(?:broadcast_logger|tagged_logging|logger(?:_silence|_thread_safe_level)?)\.rb\z}
|
|
392
399
|
GEM_ROOTS = (defined?(::Gem) ? ::Gem.path : []).map { |root| "#{File.expand_path(root)}#{File::SEPARATOR}" }.freeze
|
|
393
400
|
LEVELS = LogBrew::Logger::SEVERITY_TO_LOGBREW_LEVEL
|
|
394
|
-
private_constant :MAX_MESSAGE_CHARACTERS, :SINK_IVAR, :ROOT_IVAR, :GUARD_KEY, :ORIGIN_KEY,
|
|
401
|
+
private_constant :MAX_MESSAGE_CHARACTERS, :MAX_BREADCRUMB_CHARACTERS, :SINK_IVAR, :ROOT_IVAR, :GUARD_KEY, :ORIGIN_KEY,
|
|
395
402
|
:LIBRARY_ROOT, :LOGGER_PATH, :LOGGER_WRAPPER_PATH, :GEM_ROOTS, :LEVELS
|
|
396
403
|
|
|
397
404
|
class Sink < ::Logger
|
|
@@ -486,14 +493,22 @@ module LogBrew
|
|
|
486
493
|
"rubySeverity" => ::Logger::SEV_LABEL[severity.to_i] || "ANY",
|
|
487
494
|
"messageState" => truncated ? "truncated" : "captured"
|
|
488
495
|
)
|
|
496
|
+
level = LEVELS.fetch(severity.to_i, severity.to_i >= ::Logger::FATAL ? "critical" : "info")
|
|
489
497
|
client.log(
|
|
490
498
|
"ruby_rails_log_#{SecureRandom.hex(8)}",
|
|
491
499
|
Time.now.utc.iso8601(6),
|
|
492
500
|
message: text[0, MAX_MESSAGE_CHARACTERS],
|
|
493
|
-
level:
|
|
501
|
+
level: level,
|
|
494
502
|
logger: "rails",
|
|
495
503
|
metadata: metadata
|
|
496
504
|
)
|
|
505
|
+
LogBrew.add_breadcrumb(
|
|
506
|
+
category: "logging",
|
|
507
|
+
type: "debug",
|
|
508
|
+
level: level,
|
|
509
|
+
message: text.gsub(/[[:cntrl:]]/, " ")[0, MAX_BREADCRUMB_CHARACTERS],
|
|
510
|
+
data: { "logger" => "rails" }
|
|
511
|
+
)
|
|
497
512
|
rescue StandardError => error
|
|
498
513
|
runtime.report_error("application_log_capture", error)
|
|
499
514
|
ensure
|
|
@@ -574,7 +589,7 @@ module LogBrew
|
|
|
574
589
|
result = Trace.with_context(@context) { yield }
|
|
575
590
|
finish
|
|
576
591
|
result
|
|
577
|
-
rescue Exception => error
|
|
592
|
+
rescue Exception => error
|
|
578
593
|
finish(error)
|
|
579
594
|
raise
|
|
580
595
|
end
|
|
@@ -691,7 +706,7 @@ module LogBrew
|
|
|
691
706
|
@logbrew_perform_operation = operation
|
|
692
707
|
begin
|
|
693
708
|
Trace.with_context(operation.context) { super }
|
|
694
|
-
rescue Exception => error
|
|
709
|
+
rescue Exception => error
|
|
695
710
|
operation.finish(error)
|
|
696
711
|
operation.capture_terminal_issue(error)
|
|
697
712
|
raise
|
|
@@ -772,19 +787,44 @@ module LogBrew
|
|
|
772
787
|
# Internal Rack adapter that replaces concrete request paths with Rails
|
|
773
788
|
# route templates while reusing the core request/error lifecycle.
|
|
774
789
|
class RailsRackMiddleware < LogBrew::RackMiddleware
|
|
790
|
+
def initialize(app, capture_request_metrics: false, **options)
|
|
791
|
+
super(app, **options)
|
|
792
|
+
@capture_request_metrics = capture_request_metrics
|
|
793
|
+
end
|
|
794
|
+
|
|
775
795
|
private
|
|
776
796
|
|
|
777
|
-
def capture_request_span(
|
|
797
|
+
def capture_request_span(env, status_code, elapsed_ms, status, timestamp)
|
|
778
798
|
super.tap do
|
|
779
|
-
RequestOperations.snapshot[1].sort_by(&:last).each do |duration_ms, kind, name, metadata, error,
|
|
799
|
+
RequestOperations.snapshot[1].sort_by(&:last).each do |duration_ms, kind, name, metadata, error, operation_timestamp|
|
|
780
800
|
LogBrew::OperationTracing.capture_span(
|
|
781
801
|
@client, kind, name, LogBrew::OperationTracing.child_context, nil,
|
|
782
|
-
{ duration_ms: duration_ms, timestamp:
|
|
802
|
+
{ duration_ms: duration_ms, timestamp: operation_timestamp, source: "rails.active_support", system: kind == "database" ? "active_record" : "rails", operation: name, metadata: metadata, on_error: @on_error }, error
|
|
783
803
|
)
|
|
784
804
|
end
|
|
805
|
+
capture_request_metric(env, status_code, elapsed_ms) if @capture_request_metrics
|
|
785
806
|
end
|
|
786
807
|
end
|
|
787
808
|
|
|
809
|
+
def capture_request_metric(env, status_code, elapsed_ms)
|
|
810
|
+
@client.metric(
|
|
811
|
+
next_event_id("metric"),
|
|
812
|
+
logbrew_timestamp(6),
|
|
813
|
+
name: "http.server.duration",
|
|
814
|
+
description: "Duration of one completed server request.",
|
|
815
|
+
kind: "histogram",
|
|
816
|
+
value: elapsed_ms,
|
|
817
|
+
unit: "ms",
|
|
818
|
+
temporality: "delta",
|
|
819
|
+
metadata: @metadata.merge(
|
|
820
|
+
"method" => request_method(env),
|
|
821
|
+
"routeTemplate" => route_template(env),
|
|
822
|
+
"statusCode" => status_code,
|
|
823
|
+
"statusCodeClass" => "#{status_code.to_i / 100}xx"
|
|
824
|
+
)
|
|
825
|
+
)
|
|
826
|
+
end
|
|
827
|
+
|
|
788
828
|
def exception_mechanism_type
|
|
789
829
|
"rails.middleware"
|
|
790
830
|
end
|
|
@@ -918,6 +958,7 @@ module LogBrew
|
|
|
918
958
|
metadata: @runtime.metadata,
|
|
919
959
|
include_exception_message: @runtime.configuration.capture_exception_messages?,
|
|
920
960
|
include_exception_backtrace: @runtime.configuration.include_exception_backtrace?,
|
|
961
|
+
capture_request_metrics: @runtime.configuration.capture_request_metrics?,
|
|
921
962
|
on_error: ->(error) { @runtime.report_error("request_capture", error) }
|
|
922
963
|
)
|
|
923
964
|
@adapter_client = active_client
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LogBrew
|
|
4
|
+
# The user and breadcrumbs of one request. LogBrew::RackMiddleware opens a
|
|
5
|
+
# fresh scope for each request, so one request's state never reaches another.
|
|
6
|
+
module RequestScope
|
|
7
|
+
KEY = :logbrew_request_scope
|
|
8
|
+
State = Struct.new(:subject, :breadcrumbs, :truncated)
|
|
9
|
+
private_constant :KEY, :State
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def open
|
|
14
|
+
previous = Thread.current[KEY]
|
|
15
|
+
Thread.current[KEY] = State.new(nil, [], false)
|
|
16
|
+
yield
|
|
17
|
+
ensure
|
|
18
|
+
Thread.current[KEY] = previous
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def set_user(id, kind)
|
|
22
|
+
subject = TelemetryContext.create.with_subject(id: id, kind: kind).build
|
|
23
|
+
state = Thread.current[KEY]
|
|
24
|
+
return false if state.nil?
|
|
25
|
+
|
|
26
|
+
state.subject = subject
|
|
27
|
+
true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_breadcrumb(**breadcrumb)
|
|
31
|
+
state = Thread.current[KEY]
|
|
32
|
+
return false if state.nil?
|
|
33
|
+
|
|
34
|
+
state.breadcrumbs << IssueDiagnostics.breadcrumb(timestamp: Time.now.utc.iso8601(3), **breadcrumb)
|
|
35
|
+
if state.breadcrumbs.length > IssueDiagnostics::MAX_BREADCRUMBS
|
|
36
|
+
state.breadcrumbs.shift
|
|
37
|
+
state.truncated = true
|
|
38
|
+
end
|
|
39
|
+
true
|
|
40
|
+
rescue SdkError
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def subject_context
|
|
45
|
+
Thread.current[KEY]&.subject
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def with_breadcrumbs(issue)
|
|
49
|
+
state = Thread.current[KEY]
|
|
50
|
+
return issue if state.nil? || state.breadcrumbs.empty? || issue.key?("breadcrumbs")
|
|
51
|
+
|
|
52
|
+
issue["breadcrumbs"] = state.breadcrumbs.map(&:dup)
|
|
53
|
+
issue["breadcrumbsTruncated"] = true if state.truncated
|
|
54
|
+
issue
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Attach an opaque, app-owned user ID to every event of the current request.
|
|
59
|
+
# Pass an ID, never an email address or a name. Outside a request the call
|
|
60
|
+
# does nothing and returns false. An invalid ID raises SdkError.
|
|
61
|
+
def self.set_user(id, kind: "user")
|
|
62
|
+
RequestScope.set_user(id, kind)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Record one breadcrumb for the issue of the current request. Outside a
|
|
66
|
+
# request, or for an invalid breadcrumb, the call does nothing and returns false.
|
|
67
|
+
def self.add_breadcrumb(category:, type: nil, level: nil, message: nil, data: nil)
|
|
68
|
+
RequestScope.add_breadcrumb(category: category, type: type, level: level, message: message, data: data)
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/logbrew/sidekiq.rb
CHANGED
data/lib/logbrew/version.rb
CHANGED
data/lib/logbrew.rb
CHANGED
|
@@ -80,6 +80,7 @@ module LogBrew
|
|
|
80
80
|
require_relative "logbrew/telemetry_resource"
|
|
81
81
|
require_relative "logbrew/telemetry_context"
|
|
82
82
|
require_relative "logbrew/telemetry"
|
|
83
|
+
require_relative "logbrew/request_scope"
|
|
83
84
|
|
|
84
85
|
require_relative "logbrew/event_batcher"
|
|
85
86
|
require_relative "logbrew/persistent_event_store"
|
|
@@ -478,16 +479,18 @@ module LogBrew
|
|
|
478
479
|
end
|
|
479
480
|
|
|
480
481
|
def call(env)
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
482
|
+
RequestScope.open do
|
|
483
|
+
started_at = monotonic_time
|
|
484
|
+
started_timestamp, timestamp_error = request_timestamp
|
|
485
|
+
response = begin
|
|
486
|
+
@app.call(env)
|
|
487
|
+
rescue StandardError => error
|
|
488
|
+
capture_request(env, exception_status_code(error), started_at, started_timestamp, timestamp_error, error)
|
|
489
|
+
raise
|
|
490
|
+
end
|
|
491
|
+
capture_request(env, rack_status(response), started_at, started_timestamp, timestamp_error)
|
|
492
|
+
response
|
|
488
493
|
end
|
|
489
|
-
capture_request(env, rack_status(response), started_at, started_timestamp, timestamp_error)
|
|
490
|
-
response
|
|
491
494
|
end
|
|
492
495
|
|
|
493
496
|
private
|
|
@@ -901,10 +904,13 @@ module LogBrew
|
|
|
901
904
|
DEFAULT_AUTOMATIC_RETRY_BASE_DELAY = 0.25
|
|
902
905
|
DEFAULT_AUTOMATIC_RETRY_MAX_DELAY = 30.0
|
|
903
906
|
|
|
907
|
+
SERVER_API_KEY_VARIABLE = "LOGBREW_SERVER_API_KEY"
|
|
908
|
+
|
|
909
|
+
# A nil or blank api_key reads LOGBREW_SERVER_API_KEY.
|
|
904
910
|
def self.create(
|
|
905
|
-
api_key:,
|
|
906
911
|
sdk_name:,
|
|
907
912
|
sdk_version:,
|
|
913
|
+
api_key: nil,
|
|
908
914
|
max_retries: 2,
|
|
909
915
|
max_queue_size: BoundedEventQueue::DEFAULT_MAX_SIZE,
|
|
910
916
|
max_queue_bytes: BoundedEventQueue::DEFAULT_MAX_BYTES,
|
|
@@ -915,7 +921,7 @@ module LogBrew
|
|
|
915
921
|
context: nil,
|
|
916
922
|
capture_runtime_context: true
|
|
917
923
|
)
|
|
918
|
-
|
|
924
|
+
api_key = resolve_server_api_key(api_key)
|
|
919
925
|
Validation.require_non_empty("sdk_name", sdk_name)
|
|
920
926
|
Validation.require_non_empty("sdk_version", sdk_version)
|
|
921
927
|
raise SdkError.new("validation_error", "max_retries must be non-negative") if max_retries.negative?
|
|
@@ -951,10 +957,10 @@ module LogBrew
|
|
|
951
957
|
end
|
|
952
958
|
|
|
953
959
|
def self.create_automatic(
|
|
954
|
-
api_key:,
|
|
955
960
|
sdk_name:,
|
|
956
961
|
sdk_version:,
|
|
957
962
|
transport:,
|
|
963
|
+
api_key: nil,
|
|
958
964
|
flush_interval: DEFAULT_AUTOMATIC_FLUSH_INTERVAL,
|
|
959
965
|
flush_threshold: DEFAULT_AUTOMATIC_FLUSH_THRESHOLD,
|
|
960
966
|
retry_base_delay: DEFAULT_AUTOMATIC_RETRY_BASE_DELAY,
|
|
@@ -987,6 +993,12 @@ module LogBrew
|
|
|
987
993
|
client
|
|
988
994
|
end
|
|
989
995
|
|
|
996
|
+
def self.resolve_server_api_key(api_key)
|
|
997
|
+
[api_key, ENV.fetch(SERVER_API_KEY_VARIABLE, nil)].find { |key| key.is_a?(String) && !key.strip.empty? } ||
|
|
998
|
+
raise(SdkError.new("configuration_error", "set #{SERVER_API_KEY_VARIABLE} or pass api_key"))
|
|
999
|
+
end
|
|
1000
|
+
private_class_method :resolve_server_api_key
|
|
1001
|
+
|
|
990
1002
|
def initialize(
|
|
991
1003
|
api_key:,
|
|
992
1004
|
sdk:,
|
|
@@ -1100,7 +1112,7 @@ module LogBrew
|
|
|
1100
1112
|
end
|
|
1101
1113
|
|
|
1102
1114
|
def issue(id, timestamp, attributes)
|
|
1103
|
-
push_event("issue", id, timestamp, validate_issue(attributes), event_context(attributes))
|
|
1115
|
+
push_event("issue", id, timestamp, RequestScope.with_breadcrumbs(validate_issue(attributes)), event_context(attributes))
|
|
1104
1116
|
end
|
|
1105
1117
|
|
|
1106
1118
|
def log(id, timestamp, attributes)
|
|
@@ -1178,6 +1190,8 @@ module LogBrew
|
|
|
1178
1190
|
Validation.require_non_empty("event id", id)
|
|
1179
1191
|
Validation.require_timestamp(timestamp)
|
|
1180
1192
|
ambient_context = Telemetry.current_context
|
|
1193
|
+
user_context = RequestScope.subject_context
|
|
1194
|
+
ambient_context = TelemetryContext.merge(user_context, ambient_context) unless user_context.nil?
|
|
1181
1195
|
active_trace = defined?(LogBrew::Trace) ? LogBrew::Trace.current : nil
|
|
1182
1196
|
ambient_context = TelemetryContext.with_trace(ambient_context, active_trace) unless active_trace.nil?
|
|
1183
1197
|
merged_context = TelemetryContext.merge(
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: logbrew-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.20
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- LogBrew
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Public LogBrew Ruby SDK with typed issue diagnostics, automatic Rails
|
|
14
14
|
request/error capture, and standard-library delivery.
|
|
@@ -41,6 +41,7 @@ files:
|
|
|
41
41
|
- lib/logbrew/queue_carrier.rb
|
|
42
42
|
- lib/logbrew/rails.rb
|
|
43
43
|
- lib/logbrew/rails_integration.rb
|
|
44
|
+
- lib/logbrew/request_scope.rb
|
|
44
45
|
- lib/logbrew/sidekiq.rb
|
|
45
46
|
- lib/logbrew/span_events.rb
|
|
46
47
|
- lib/logbrew/support_ticket.rb
|
|
@@ -52,11 +53,12 @@ files:
|
|
|
52
53
|
- lib/logbrew/traceparent.rb
|
|
53
54
|
- lib/logbrew/version.rb
|
|
54
55
|
- lib/logbrew/worker_lifecycle.rb
|
|
55
|
-
homepage: https://
|
|
56
|
+
homepage: https://logbrew.co
|
|
56
57
|
licenses:
|
|
57
58
|
- MIT
|
|
58
59
|
metadata:
|
|
59
60
|
source_code_uri: https://github.com/LogBrewCo/sdk
|
|
61
|
+
documentation_uri: https://docs.logbrew.co
|
|
60
62
|
post_install_message:
|
|
61
63
|
rdoc_options: []
|
|
62
64
|
require_paths:
|