rails-profiler 0.46.0 → 0.47.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 +21 -0
- data/app/controllers/profiler/api/slave_proxy_controller.rb +2 -0
- data/app/controllers/profiler/api/stream_controller.rb +69 -0
- data/app/controllers/profiler/api/toolbar_controller.rb +3 -2
- data/app/controllers/profiler/application_controller.rb +16 -0
- data/config/routes.rb +1 -0
- data/lib/profiler/collectors/enqueued_jobs_collector.rb +92 -0
- data/lib/profiler/configuration.rb +12 -3
- data/lib/profiler/job_profiler.rb +3 -1
- data/lib/profiler/live/event_stream.rb +96 -0
- data/lib/profiler/live/signature.rb +47 -0
- data/lib/profiler/live/streams.rb +48 -0
- data/lib/profiler/railtie.rb +3 -1
- data/lib/profiler/sse/event_bus.rb +12 -1
- data/lib/profiler/sse/redis_event_bus.rb +10 -1
- data/lib/profiler/storage/base_store.rb +26 -1
- data/lib/profiler/storage/file_store.rb +8 -0
- data/lib/profiler/storage/sqlite_store.rb +7 -0
- data/lib/profiler/version.rb +1 -1
- data/lib/profiler.rb +2 -0
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4534b3dd777b1379a70f6bd6f0eb770dae0f3ad8e84e5a251063259ddcfd7d08
|
|
4
|
+
data.tar.gz: bda18dfec9a07bf1341f0317edce91f7d0b076c42de43203a2a04ad354af17f6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5db82256a78d4caac6d74fad99cb94a3773b25b4d2a2b8c4dafd4a279cd02437e0fae119a2fb8f33a5bc214e425682f7cdc27dd60daa347dfb08334579726be2
|
|
7
|
+
data.tar.gz: ffcddd382edbccc0f7090fc2d9bd38d9dae60f20e0ecdb0fb78d5a03429bda11e96498833bf0312a8c61b1c303fd2c6b7265bd94724dfa4be5f940665e3a1017
|
data/CHANGELOG.md
CHANGED
|
@@ -19,6 +19,27 @@ every commit of every tag interval is accounted for one way or the other.
|
|
|
19
19
|
|
|
20
20
|
## [Unreleased]
|
|
21
21
|
|
|
22
|
+
## [0.47.0] - 2026-11-07
|
|
23
|
+
|
|
24
|
+
<!-- stamped -->
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- The jobs a request or a job puts on a queue are recorded when they are enqueued (`EnqueuedJobsCollector`, `enqueued_jobs` in the profile data) and listed by the toolbar route as `enqueued_jobs` until they start and get a profile of their own, matched by job id. In a custom `config.collectors` list, add `Profiler::Collectors::EnqueuedJobsCollector` to get them.
|
|
29
|
+
- `GET /_profiler/api/stream`: a server-sent events stream that announces, as it happens, a change to any of the profiles a client follows or to what is under them (AJAX calls, jobs, jobs of jobs), including a job saved by another process. `config.live_events` (true in development only) turns it on and `config.live_events_max_streams` (2) caps the threads it holds; over the cap, with too few Puma threads, or when off, it answers 503 with `Retry-After`. The toolbar route returns a `live_cursor` for the client to pass back.
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- A job's save is now announced to every profile above it, up to five levels, not only to the one that enqueued it, so the request at the top sees the end of a job of a job.
|
|
34
|
+
|
|
35
|
+
## [0.46.1] - 2026-10-28
|
|
36
|
+
|
|
37
|
+
<!-- stamped -->
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
|
|
41
|
+
- A release that did not complete now blocks the branch pipelines: the `release:backlog` job fails, naming the version and what to do, while master stamps a version without a tag (`fix/release-*` branches are exempt). The release job no longer dies silently of SIGPIPE (status 141) when several versions are waiting.
|
|
42
|
+
|
|
22
43
|
## [0.46.0] - 2026-10-28
|
|
23
44
|
|
|
24
45
|
<!-- stamped -->
|
|
@@ -11,6 +11,8 @@ module Profiler
|
|
|
11
11
|
# is refused, so the request stays under the slave's API.
|
|
12
12
|
segments = params[:path].to_s.split("/", -1).map { |segment| Cluster::Security.escape_segment(segment) }
|
|
13
13
|
full_path = "/_profiler/api/#{segments.join("/")}"
|
|
14
|
+
# A stream never ends before its lifetime: the proxy reads one JSON answer.
|
|
15
|
+
return head :not_found if segments.first == "stream"
|
|
14
16
|
|
|
15
17
|
result = case request.method
|
|
16
18
|
when "GET"
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Profiler
|
|
4
|
+
module Api
|
|
5
|
+
# Server-sent events: tells a client which of the profiles it follows changed, as it happens
|
|
6
|
+
# (see Profiler::Live::EventStream). One request is one open stream, so it holds a server
|
|
7
|
+
# thread: Profiler::Live::Streams caps them, a Puma with too few threads is refused, and the
|
|
8
|
+
# client falls back to asking Api::EventsController when this answers 503.
|
|
9
|
+
class StreamController < Profiler::ApplicationController
|
|
10
|
+
MAX_TOKENS = 20
|
|
11
|
+
# A stream must leave threads to the application itself.
|
|
12
|
+
MIN_SPARE_THREADS = 2
|
|
13
|
+
|
|
14
|
+
def show
|
|
15
|
+
return unavailable("live events are off") unless Profiler.configuration.live_events
|
|
16
|
+
# A HEAD gets the headers only: the server drops its body without iterating or closing it.
|
|
17
|
+
return head(:ok, content_type: "text/event-stream") if request.head?
|
|
18
|
+
return unavailable("the profiler storage is unavailable") if Profiler.storage.is_a?(Profiler::Storage::Unavailable)
|
|
19
|
+
|
|
20
|
+
tokens = params[:tokens].to_s.split(",").uniq.select { |t| Profiler::Storage::Token.valid?(t) }.first(MAX_TOKENS)
|
|
21
|
+
return render(json: { error: "tokens is required" }, status: :bad_request) if tokens.empty?
|
|
22
|
+
|
|
23
|
+
limit = stream_limit
|
|
24
|
+
return unavailable("this server has too few threads for a live stream") if limit < 1
|
|
25
|
+
lease = Profiler::Live::Streams.acquire(limit)
|
|
26
|
+
return unavailable("too many open streams") unless lease
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
response.headers["Content-Type"] = "text/event-stream"
|
|
30
|
+
response.headers["Cache-Control"] = "no-cache"
|
|
31
|
+
response.headers["X-Accel-Buffering"] = "no"
|
|
32
|
+
self.response_body = Profiler::Live::EventStream.new(
|
|
33
|
+
storage: Profiler.storage, tokens: tokens, lease: lease, cursors: cursors
|
|
34
|
+
)
|
|
35
|
+
rescue StandardError
|
|
36
|
+
Profiler::Live::Streams.release(lease)
|
|
37
|
+
raise
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def unavailable(reason)
|
|
44
|
+
response.headers["Retry-After"] = "30"
|
|
45
|
+
render json: { error: reason }, status: :service_unavailable
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# since=token:cursor,token:cursor
|
|
49
|
+
def cursors
|
|
50
|
+
params[:since].to_s.split(",").to_h do |pair|
|
|
51
|
+
token, cursor = pair.split(":", 2)
|
|
52
|
+
[token, cursor.to_s[0, 40]]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The configured cap, lowered so that the application keeps MIN_SPARE_THREADS threads when
|
|
57
|
+
# Puma says how many it has.
|
|
58
|
+
def stream_limit
|
|
59
|
+
limit = Profiler.configuration.live_events_max_streams.to_i
|
|
60
|
+
max_threads = begin
|
|
61
|
+
request.env["puma.config"]&.options&.[](:max_threads)
|
|
62
|
+
rescue StandardError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
max_threads ? [limit, max_threads.to_i - MIN_SPARE_THREADS].min : limit
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -21,8 +21,9 @@ module Profiler
|
|
|
21
21
|
|
|
22
22
|
# The bodies as stored (gzip+base64 past compress_body_threshold): the toolbar shows none.
|
|
23
23
|
render json: {
|
|
24
|
-
profile: profile.to_h.merge(child_jobs: build_child_jobs(profile)),
|
|
25
|
-
events_cursor: events_cursor
|
|
24
|
+
profile: profile.to_h.merge(child_jobs: build_child_jobs(profile), enqueued_jobs: build_enqueued_jobs(profile)),
|
|
25
|
+
events_cursor: events_cursor,
|
|
26
|
+
live_cursor: Profiler.configuration.live_events ? Profiler::Live::Signature.of(Profiler.storage, params[:token]) : nil
|
|
26
27
|
}
|
|
27
28
|
end
|
|
28
29
|
end
|
|
@@ -124,6 +124,22 @@ module Profiler
|
|
|
124
124
|
end
|
|
125
125
|
end
|
|
126
126
|
|
|
127
|
+
# The jobs this profile put on a queue that have no profile yet, because none has started:
|
|
128
|
+
# from the enqueued-jobs collector. One that started is in build_child_jobs instead.
|
|
129
|
+
def build_enqueued_jobs(profile)
|
|
130
|
+
started = child_profiles(profile).filter_map { |p| (p.collector_data("job") || {})["job_id"] }
|
|
131
|
+
jobs = (profile.collector_data("enqueued_jobs") || {})["jobs"]
|
|
132
|
+
Array(jobs).reject { |entry| started.include?(entry["job_id"]) }.map do |entry|
|
|
133
|
+
{
|
|
134
|
+
job_class: entry["job_class"],
|
|
135
|
+
job_id: entry["job_id"],
|
|
136
|
+
queue: entry["queue"],
|
|
137
|
+
enqueued_at: entry["enqueued_at"],
|
|
138
|
+
scheduled_at: entry["scheduled_at"]
|
|
139
|
+
}
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
127
143
|
def build_parent_summary(profile)
|
|
128
144
|
return nil unless profile.parent_token
|
|
129
145
|
|
data/config/routes.rb
CHANGED
|
@@ -65,6 +65,7 @@ Profiler::Engine.routes.draw do
|
|
|
65
65
|
get "test_runner/runs/:id/stream", to: "test_runner#stream", as: :test_runner_run_stream
|
|
66
66
|
delete "test_runner/runs/:id", to: "test_runner#destroy"
|
|
67
67
|
get "events/:token", to: "events#show", as: :profile_events
|
|
68
|
+
get "stream", to: "stream#show", as: :live_stream
|
|
68
69
|
|
|
69
70
|
# Cluster endpoints (master-side), routed only on a node with cluster_master set
|
|
70
71
|
constraints(->(_request) { Profiler.configuration.cluster_master? }) do
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require_relative "base_collector"
|
|
5
|
+
|
|
6
|
+
module Profiler
|
|
7
|
+
module Collectors
|
|
8
|
+
# The ActiveJob jobs a request (or a job) puts on a queue. A job only gets a profile of its own
|
|
9
|
+
# once it starts, so until then the profile that enqueued it is the only place that knows it
|
|
10
|
+
# exists. The toolbar route lists those that have not started yet from here.
|
|
11
|
+
class EnqueuedJobsCollector < BaseCollector
|
|
12
|
+
MAX_JOBS = 100
|
|
13
|
+
|
|
14
|
+
def initialize(profile)
|
|
15
|
+
super
|
|
16
|
+
@jobs = []
|
|
17
|
+
@subscriptions = []
|
|
18
|
+
@mutex = Mutex.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def name
|
|
22
|
+
"enqueued_jobs"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Data for the toolbar route and the API only: no tab of its own.
|
|
26
|
+
def tab_config
|
|
27
|
+
{ key: name, label: "Enqueued jobs", icon: "", priority: 100, enabled: false, default_active: false }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def subscribe
|
|
31
|
+
return unless defined?(ActiveSupport::Notifications)
|
|
32
|
+
return unless Profiler.configuration.track_jobs
|
|
33
|
+
|
|
34
|
+
# enqueue_at is the event of a job scheduled for later (set(wait:), set(wait_until:)).
|
|
35
|
+
%w[enqueue.active_job enqueue_at.active_job].each do |event|
|
|
36
|
+
@subscriptions << subscribe_notification(event) do |_name, _started, _finished, _id, payload|
|
|
37
|
+
job = payload[:job]
|
|
38
|
+
next if job.nil? || payload[:aborted] || payload[:exception_object]
|
|
39
|
+
|
|
40
|
+
record(job)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
# perform_all_later (Rails 7.1+) announces its jobs together, with no event per job.
|
|
44
|
+
@subscriptions << subscribe_notification("enqueue_all.active_job") do |_name, _started, _finished, _id, payload|
|
|
45
|
+
next if payload[:exception_object]
|
|
46
|
+
|
|
47
|
+
Array(payload[:jobs]).each { |job| record(job) unless job.respond_to?(:successfully_enqueued?) && !job.successfully_enqueued? }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Jobs enqueued from a thread that outlives the request still land here until collect.
|
|
52
|
+
def collect_from_any_thread?
|
|
53
|
+
true
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def unsubscribe
|
|
57
|
+
unsubscribe_notifications(@subscriptions)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def collect
|
|
61
|
+
unsubscribe
|
|
62
|
+
jobs = @mutex.synchronize { @jobs.map { |entry| entry.transform_keys(&:to_s) } }
|
|
63
|
+
store_data("jobs" => jobs)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def has_data?
|
|
67
|
+
@mutex.synchronize { @jobs.any? }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def record(job)
|
|
73
|
+
entry = {
|
|
74
|
+
job_id: job.job_id,
|
|
75
|
+
job_class: job.class.name,
|
|
76
|
+
queue: job.queue_name.to_s,
|
|
77
|
+
enqueued_at: Time.now.utc.iso8601(3),
|
|
78
|
+
scheduled_at: scheduled_at(job)
|
|
79
|
+
}
|
|
80
|
+
@mutex.synchronize { @jobs << entry if @jobs.size < MAX_JOBS }
|
|
81
|
+
rescue StandardError => e
|
|
82
|
+
Profiler.log_error_once(:enqueued_jobs_record, "EnqueuedJobsCollector: could not record an enqueued job", e)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def scheduled_at(job)
|
|
86
|
+
at = job.respond_to?(:scheduled_at) ? job.scheduled_at : nil
|
|
87
|
+
at = Time.at(at) if at.is_a?(Numeric)
|
|
88
|
+
at&.utc&.iso8601(3)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -30,7 +30,7 @@ module Profiler
|
|
|
30
30
|
|
|
31
31
|
# Options whose default depends on the Rails environment: the railtie sets them only when
|
|
32
32
|
# the application has not assigned them itself (see #default).
|
|
33
|
-
RAILS_DEFAULTED = %i[enabled storage track_tests track_runner max_profiles].freeze
|
|
33
|
+
RAILS_DEFAULTED = %i[enabled storage track_tests track_runner live_events max_profiles].freeze
|
|
34
34
|
|
|
35
35
|
BACKEND_LOCK = Mutex.new
|
|
36
36
|
|
|
@@ -54,7 +54,7 @@ module Profiler
|
|
|
54
54
|
:track_http, :slow_http_threshold, :http_backtrace_depth,
|
|
55
55
|
:otel_enabled, :otel_service_name, :otel_otlp_endpoint,
|
|
56
56
|
:track_jobs,
|
|
57
|
-
:track_console, :runner_skip,
|
|
57
|
+
:track_console, :runner_skip, :live_events_max_streams,
|
|
58
58
|
:apply_env_overrides_when_disabled, :restrict_storage_permissions,
|
|
59
59
|
:test_runner_allow_undiscovered_files, :allow_explain_outside_development,
|
|
60
60
|
:test_runner_kill_grace_seconds,
|
|
@@ -72,7 +72,7 @@ module Profiler
|
|
|
72
72
|
:regression_query_threshold, :regression_cache_threshold,
|
|
73
73
|
:replay_enabled, :replay_max_count, :replay_timeout
|
|
74
74
|
|
|
75
|
-
attr_reader :authorize_block, :enabled, :track_tests, :track_runner, :max_profiles, :editor_url_scheme, :editor_path_map
|
|
75
|
+
attr_reader :authorize_block, :enabled, :track_tests, :track_runner, :live_events, :max_profiles, :editor_url_scheme, :editor_path_map
|
|
76
76
|
|
|
77
77
|
def initialize
|
|
78
78
|
@assigned = []
|
|
@@ -130,6 +130,10 @@ module Profiler
|
|
|
130
130
|
@track_jobs = true
|
|
131
131
|
@track_console = true
|
|
132
132
|
@track_runner = false
|
|
133
|
+
# GET /_profiler/api/stream: pushes the changes of the profiles a client follows, instead of
|
|
134
|
+
# the client asking again and again. Each open stream holds a server thread, hence the cap.
|
|
135
|
+
@live_events = false
|
|
136
|
+
@live_events_max_streams = 2
|
|
133
137
|
# Runner executions left out: Regexps matched against the code or script path, Strings
|
|
134
138
|
# compared to it whole.
|
|
135
139
|
@runner_skip = [/\AProfiler\b/]
|
|
@@ -334,6 +338,11 @@ module Profiler
|
|
|
334
338
|
@enabled = value
|
|
335
339
|
end
|
|
336
340
|
|
|
341
|
+
def live_events=(value)
|
|
342
|
+
@assigned |= [:live_events]
|
|
343
|
+
@live_events = value
|
|
344
|
+
end
|
|
345
|
+
|
|
337
346
|
def track_runner=(value)
|
|
338
347
|
@assigned |= [:track_runner]
|
|
339
348
|
@track_runner = value
|
|
@@ -13,6 +13,7 @@ require_relative "collectors/exception_collector"
|
|
|
13
13
|
require_relative "collectors/env_collector"
|
|
14
14
|
require_relative "collectors/flamegraph_collector"
|
|
15
15
|
require_relative "collectors/mailer_collector"
|
|
16
|
+
require_relative "collectors/enqueued_jobs_collector"
|
|
16
17
|
|
|
17
18
|
module Profiler
|
|
18
19
|
class JobProfiler
|
|
@@ -25,7 +26,8 @@ module Profiler
|
|
|
25
26
|
Collectors::ExceptionCollector,
|
|
26
27
|
Collectors::EnvCollector,
|
|
27
28
|
Collectors::FlameGraphCollector,
|
|
28
|
-
Collectors::MailerCollector
|
|
29
|
+
Collectors::MailerCollector,
|
|
30
|
+
Collectors::EnqueuedJobsCollector
|
|
29
31
|
].freeze
|
|
30
32
|
|
|
31
33
|
def self.profile(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil, &block)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "signature"
|
|
5
|
+
require_relative "streams"
|
|
6
|
+
|
|
7
|
+
module Profiler
|
|
8
|
+
module Live
|
|
9
|
+
# The body of GET /_profiler/api/stream: a text/event-stream that says which of the profiles it
|
|
10
|
+
# was given changed, as it happens. It answers to a cursor per profile (Signature): a profile
|
|
11
|
+
# whose cursor is not the current one is announced at once, so a client reconnecting misses
|
|
12
|
+
# nothing. The thread serving it sleeps between checks, which is why Streams caps them.
|
|
13
|
+
#
|
|
14
|
+
# The storage is only read when its live_marker moved (a save, from any process), plus a full
|
|
15
|
+
# check every FULL_CHECK_TICKS as a safety net.
|
|
16
|
+
class EventStream
|
|
17
|
+
LIFETIME = 300
|
|
18
|
+
TICK = 1.0
|
|
19
|
+
HEARTBEAT = 5
|
|
20
|
+
FULL_CHECK_TICKS = 10
|
|
21
|
+
|
|
22
|
+
def initialize(storage:, tokens:, lease:, cursors: {}, lifetime: LIFETIME, tick: TICK)
|
|
23
|
+
@storage = storage
|
|
24
|
+
@lease = lease
|
|
25
|
+
@tokens = tokens
|
|
26
|
+
@cursors = cursors.dup
|
|
27
|
+
@lifetime = lifetime
|
|
28
|
+
@tick = tick
|
|
29
|
+
@released = false
|
|
30
|
+
@mutex = Mutex.new
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def each
|
|
34
|
+
# Puma and Rails 7.x ask for an enumerator first (to_ary through the response buffer).
|
|
35
|
+
return enum_for(:each) unless block_given?
|
|
36
|
+
|
|
37
|
+
yield "retry: 3000\n: ready\n\n"
|
|
38
|
+
started = clock
|
|
39
|
+
last_beat = started
|
|
40
|
+
last_marker = nil
|
|
41
|
+
ticks = 0
|
|
42
|
+
loop do
|
|
43
|
+
marker = @storage.live_marker
|
|
44
|
+
if marker != last_marker || (ticks % FULL_CHECK_TICKS).zero?
|
|
45
|
+
last_marker = marker
|
|
46
|
+
changed.each { |frame| yield frame }
|
|
47
|
+
end
|
|
48
|
+
ticks += 1
|
|
49
|
+
break if clock - started >= @lifetime
|
|
50
|
+
|
|
51
|
+
sleep @tick
|
|
52
|
+
if clock - last_beat >= HEARTBEAT
|
|
53
|
+
yield ": ping\n\n"
|
|
54
|
+
last_beat = clock
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
yield "event: end\ndata: {}\n\n"
|
|
58
|
+
rescue StandardError => e
|
|
59
|
+
# A closed connection ends the stream; anything else is worth a line in the log.
|
|
60
|
+
Profiler.log_error_once(:live_stream, "Live::EventStream: stopped", e) unless client_gone?(e)
|
|
61
|
+
ensure
|
|
62
|
+
release
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def close
|
|
66
|
+
release
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def changed
|
|
72
|
+
@tokens.filter_map do |token|
|
|
73
|
+
signature = Signature.of(@storage, token)
|
|
74
|
+
next if @cursors[token] == signature
|
|
75
|
+
|
|
76
|
+
@cursors[token] = signature
|
|
77
|
+
"event: update\ndata: #{JSON.generate(token: token, cursor: signature)}\n\n"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# What a server raises when the client closed the connection (Puma::ConnectionError, ...).
|
|
82
|
+
def client_gone?(error)
|
|
83
|
+
error.is_a?(IOError) || error.is_a?(SystemCallError) || error.class.name.to_s.end_with?("ConnectionError")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def release
|
|
87
|
+
first = @mutex.synchronize { !@released && (@released = true) }
|
|
88
|
+
Streams.release(@lease) if first
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def clock
|
|
92
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Profiler
|
|
6
|
+
module Live
|
|
7
|
+
# What a page shows of a profile and of everything under it (the AJAX calls and the jobs it
|
|
8
|
+
# started, and their own jobs): a short opaque string that changes when any of it does. Read
|
|
9
|
+
# from the storage, so a save made by another process (a Sidekiq worker) is seen like any other.
|
|
10
|
+
module Signature
|
|
11
|
+
MAX_DEPTH = 5
|
|
12
|
+
MAX_PROFILES = 100
|
|
13
|
+
MAX_CHILDREN = 50
|
|
14
|
+
GONE = "gone"
|
|
15
|
+
|
|
16
|
+
def self.of(storage, token)
|
|
17
|
+
seen = {}
|
|
18
|
+
root = storage.load(token)
|
|
19
|
+
walk(storage, root, 0, seen) if root
|
|
20
|
+
return GONE if seen.empty?
|
|
21
|
+
|
|
22
|
+
Digest::SHA1.hexdigest(seen.sort.inspect)[0, 16]
|
|
23
|
+
rescue StandardError => e
|
|
24
|
+
Profiler.log_error_once(:live_signature, "Live::Signature: could not read a profile", e)
|
|
25
|
+
GONE
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The children come loaded from find_by_parent: they are used as they are, not read again.
|
|
29
|
+
def self.walk(storage, profile, depth, seen)
|
|
30
|
+
token = profile.token
|
|
31
|
+
return if seen.key?(token) || seen.size >= MAX_PROFILES
|
|
32
|
+
|
|
33
|
+
children = depth < MAX_DEPTH ? storage.find_by_parent(token).last(MAX_CHILDREN) : []
|
|
34
|
+
seen[token] = [
|
|
35
|
+
profile.status,
|
|
36
|
+
profile.finished_at&.to_f,
|
|
37
|
+
(profile.collector_data("job") || {})["status"],
|
|
38
|
+
(profile.collector_data("http") || {})["total_requests"],
|
|
39
|
+
Array((profile.collector_data("enqueued_jobs") || {})["jobs"]).size,
|
|
40
|
+
children.map(&:token).sort
|
|
41
|
+
]
|
|
42
|
+
children.each { |child| walk(storage, child, depth + 1, seen) }
|
|
43
|
+
end
|
|
44
|
+
private_class_method :walk
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Profiler
|
|
4
|
+
module Live
|
|
5
|
+
# The live event streams this process holds open. Each one keeps a server thread until it
|
|
6
|
+
# ends, so they are counted and capped (config.live_events_max_streams). A lease older than
|
|
7
|
+
# MAX_AGE is dropped on the next acquire: a body that a middleware or the server threw away
|
|
8
|
+
# without iterating or closing it (Rack::Head does) cannot keep a slot for good.
|
|
9
|
+
module Streams
|
|
10
|
+
MUTEX = Mutex.new
|
|
11
|
+
MAX_AGE = 330
|
|
12
|
+
@leases = {}
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
# A lease when a slot was taken (to give back with release), nil over the limit.
|
|
16
|
+
def acquire(limit)
|
|
17
|
+
MUTEX.synchronize do
|
|
18
|
+
now = clock
|
|
19
|
+
@leases.delete_if { |_lease, taken| now - taken > MAX_AGE }
|
|
20
|
+
return nil if @leases.size >= limit
|
|
21
|
+
|
|
22
|
+
lease = Object.new
|
|
23
|
+
@leases[lease] = now
|
|
24
|
+
lease
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def release(lease)
|
|
29
|
+
MUTEX.synchronize { @leases.delete(lease) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def open_count
|
|
33
|
+
MUTEX.synchronize { @leases.size }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def reset!
|
|
37
|
+
MUTEX.synchronize { @leases.clear }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def clock
|
|
43
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/profiler/railtie.rb
CHANGED
|
@@ -16,6 +16,7 @@ module Profiler
|
|
|
16
16
|
config.default(:storage, development_or_test ? :file : :memory)
|
|
17
17
|
config.default(:track_tests, Rails.env.test?)
|
|
18
18
|
config.default(:track_runner, Rails.env.development?)
|
|
19
|
+
config.default(:live_events, Rails.env.development?)
|
|
19
20
|
# One test profile per example: no cap on the count in the test environment, so that the
|
|
20
21
|
# report at the end of a suite sees all of them.
|
|
21
22
|
config.default(:max_profiles, Rails.env.test? ? nil : 100)
|
|
@@ -61,7 +62,8 @@ module Profiler
|
|
|
61
62
|
Profiler::Collectors::RoutesCollector,
|
|
62
63
|
Profiler::Collectors::I18nCollector,
|
|
63
64
|
Profiler::Collectors::EnvCollector,
|
|
64
|
-
Profiler::Collectors::MailerCollector
|
|
65
|
+
Profiler::Collectors::MailerCollector,
|
|
66
|
+
Profiler::Collectors::EnqueuedJobsCollector
|
|
65
67
|
]
|
|
66
68
|
end
|
|
67
69
|
end
|
|
@@ -21,12 +21,20 @@ module Profiler
|
|
|
21
21
|
|
|
22
22
|
def initialize
|
|
23
23
|
@versions = {}
|
|
24
|
+
@generation = 0
|
|
24
25
|
@mutex = Mutex.new
|
|
25
26
|
end
|
|
26
27
|
|
|
28
|
+
# Moves on every save this process announces, whatever the profile: a cheap way for a live
|
|
29
|
+
# stream to know that something was saved, before it looks at what.
|
|
30
|
+
def generation
|
|
31
|
+
@mutex.synchronize { @generation }
|
|
32
|
+
end
|
|
33
|
+
|
|
27
34
|
def broadcast(token)
|
|
28
35
|
now = Process.clock_gettime(Process::CLOCK_REALTIME, :microsecond)
|
|
29
36
|
@mutex.synchronize do
|
|
37
|
+
@generation += 1
|
|
30
38
|
# Strictly increasing within the process, even for two saves in one microsecond.
|
|
31
39
|
version = [now, @versions.delete(token).to_i + 1].max
|
|
32
40
|
@versions[token] = version
|
|
@@ -41,7 +49,10 @@ module Profiler
|
|
|
41
49
|
end
|
|
42
50
|
|
|
43
51
|
def reset!
|
|
44
|
-
@mutex.synchronize
|
|
52
|
+
@mutex.synchronize do
|
|
53
|
+
@versions.clear
|
|
54
|
+
@generation = 0
|
|
55
|
+
end
|
|
45
56
|
end
|
|
46
57
|
end
|
|
47
58
|
end
|
|
@@ -14,18 +14,27 @@ module Profiler
|
|
|
14
14
|
include Singleton
|
|
15
15
|
|
|
16
16
|
KEY_PREFIX = "profiler:events"
|
|
17
|
+
GENERATION_KEY = "profiler:events:generation"
|
|
17
18
|
# As long as RedisStore keeps a profile by default: past that, nobody looks at its page.
|
|
18
19
|
TTL = 24 * 60 * 60
|
|
19
20
|
|
|
20
21
|
def broadcast(token)
|
|
21
22
|
key = key(token)
|
|
22
|
-
version, _expire = redis.multi do |transaction|
|
|
23
|
+
version, _expire, _generation = redis.multi do |transaction|
|
|
23
24
|
transaction.incr(key)
|
|
24
25
|
transaction.expire(key, TTL)
|
|
26
|
+
transaction.incr(GENERATION_KEY)
|
|
25
27
|
end
|
|
26
28
|
version
|
|
27
29
|
end
|
|
28
30
|
|
|
31
|
+
# Moves on every save of any process: see EventBus#generation.
|
|
32
|
+
def generation
|
|
33
|
+
redis.get(GENERATION_KEY).to_i
|
|
34
|
+
rescue StandardError
|
|
35
|
+
0
|
|
36
|
+
end
|
|
37
|
+
|
|
29
38
|
# The version of the last save of +token+, 0 when none is known or Redis cannot be read.
|
|
30
39
|
def version(token)
|
|
31
40
|
redis.get(key(token)).to_i
|
|
@@ -14,10 +14,18 @@ module Profiler
|
|
|
14
14
|
Token.validate!(token)
|
|
15
15
|
result = do_save(token, profile)
|
|
16
16
|
broadcast_event(token)
|
|
17
|
-
|
|
17
|
+
broadcast_ancestors(profile)
|
|
18
18
|
result
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
# Something that changes whenever a profile is saved, by any process sharing the storage, and
|
|
22
|
+
# that is cheap to read: the live event stream only looks at the profiles once it moved.
|
|
23
|
+
# The announced saves of the event bus count for the stores that are not shared through a
|
|
24
|
+
# file; a store that is overrides this.
|
|
25
|
+
def live_marker
|
|
26
|
+
Profiler::SSE.current.generation
|
|
27
|
+
end
|
|
28
|
+
|
|
21
29
|
def do_save(token, profile)
|
|
22
30
|
raise NotImplementedError, "#{self.class} must implement #do_save"
|
|
23
31
|
end
|
|
@@ -85,6 +93,23 @@ module Profiler
|
|
|
85
93
|
profile.profile_type == "job" && Token.valid?(profile.parent_token)
|
|
86
94
|
end
|
|
87
95
|
|
|
96
|
+
# A job's save is recorded for the profile that enqueued it and, going up, for each job that
|
|
97
|
+
# enqueued that one: the toolbar of the request lists the whole chain.
|
|
98
|
+
MAX_ANCESTORS = 5
|
|
99
|
+
|
|
100
|
+
def broadcast_ancestors(profile)
|
|
101
|
+
current = profile
|
|
102
|
+
MAX_ANCESTORS.times do
|
|
103
|
+
break unless job_with_parent?(current)
|
|
104
|
+
|
|
105
|
+
broadcast_event(current.parent_token)
|
|
106
|
+
current = load(current.parent_token)
|
|
107
|
+
break if current.nil?
|
|
108
|
+
end
|
|
109
|
+
rescue StandardError => e
|
|
110
|
+
Profiler.log_error_once(:store_broadcast_ancestors, "#{self.class.name.split("::").last}: could not announce a saved job to the profiles above it", e)
|
|
111
|
+
end
|
|
112
|
+
|
|
88
113
|
def broadcast_event(token)
|
|
89
114
|
Profiler::SSE.current.broadcast(token)
|
|
90
115
|
rescue StandardError => e
|
|
@@ -128,6 +128,14 @@ module Profiler
|
|
|
128
128
|
end
|
|
129
129
|
end
|
|
130
130
|
|
|
131
|
+
# The index is appended to by every process that saves a profile.
|
|
132
|
+
def live_marker
|
|
133
|
+
stat = File.stat(@index_path)
|
|
134
|
+
[stat.mtime.to_i, stat.mtime.nsec, stat.size, super]
|
|
135
|
+
rescue SystemCallError
|
|
136
|
+
super
|
|
137
|
+
end
|
|
138
|
+
|
|
131
139
|
def find_by_parent(parent_token)
|
|
132
140
|
return [] unless Token.valid?(parent_token)
|
|
133
141
|
|
|
@@ -155,6 +155,13 @@ module Profiler
|
|
|
155
155
|
rows.filter_map { |row| row_to_summary(row) }
|
|
156
156
|
end
|
|
157
157
|
|
|
158
|
+
# data_version moves when another connection, of another process, commits.
|
|
159
|
+
def live_marker
|
|
160
|
+
[@db.get_first_value("PRAGMA data_version"), super]
|
|
161
|
+
rescue StandardError
|
|
162
|
+
super
|
|
163
|
+
end
|
|
164
|
+
|
|
158
165
|
def find_by_parent(parent_token)
|
|
159
166
|
return [] unless Token.valid?(parent_token)
|
|
160
167
|
|
data/lib/profiler/version.rb
CHANGED
data/lib/profiler.rb
CHANGED
|
@@ -304,6 +304,8 @@ require_relative "profiler/collectors/routes_collector"
|
|
|
304
304
|
require_relative "profiler/collectors/i18n_collector"
|
|
305
305
|
require_relative "profiler/collectors/env_collector"
|
|
306
306
|
require_relative "profiler/collectors/mailer_collector"
|
|
307
|
+
require_relative "profiler/collectors/enqueued_jobs_collector"
|
|
308
|
+
require_relative "profiler/live/event_stream"
|
|
307
309
|
|
|
308
310
|
require_relative "profiler/storage/token"
|
|
309
311
|
require_relative "profiler/storage/private_files"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails-profiler
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.47.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sébastien Duplessy
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -131,6 +131,7 @@ files:
|
|
|
131
131
|
- app/controllers/profiler/api/profiles_controller.rb
|
|
132
132
|
- app/controllers/profiler/api/replays_controller.rb
|
|
133
133
|
- app/controllers/profiler/api/slave_proxy_controller.rb
|
|
134
|
+
- app/controllers/profiler/api/stream_controller.rb
|
|
134
135
|
- app/controllers/profiler/api/test_runner_controller.rb
|
|
135
136
|
- app/controllers/profiler/api/tests_controller.rb
|
|
136
137
|
- app/controllers/profiler/api/toolbar_controller.rb
|
|
@@ -171,6 +172,7 @@ files:
|
|
|
171
172
|
- lib/profiler/collectors/console_collector.rb
|
|
172
173
|
- lib/profiler/collectors/database_collector.rb
|
|
173
174
|
- lib/profiler/collectors/dump_collector.rb
|
|
175
|
+
- lib/profiler/collectors/enqueued_jobs_collector.rb
|
|
174
176
|
- lib/profiler/collectors/env_collector.rb
|
|
175
177
|
- lib/profiler/collectors/exception_collector.rb
|
|
176
178
|
- lib/profiler/collectors/flamegraph_collector.rb
|
|
@@ -204,6 +206,9 @@ files:
|
|
|
204
206
|
- lib/profiler/instrumentation/sidekiq_middleware.rb
|
|
205
207
|
- lib/profiler/instrumentation/thread_context_propagation.rb
|
|
206
208
|
- lib/profiler/job_profiler.rb
|
|
209
|
+
- lib/profiler/live/event_stream.rb
|
|
210
|
+
- lib/profiler/live/signature.rb
|
|
211
|
+
- lib/profiler/live/streams.rb
|
|
207
212
|
- lib/profiler/local_request.rb
|
|
208
213
|
- lib/profiler/mcp/body_formatter.rb
|
|
209
214
|
- lib/profiler/mcp/file_cache.rb
|