catpm 0.1.2 → 0.1.4
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 +3 -0
- data/lib/catpm/flusher.rb +58 -30
- data/lib/catpm/lifecycle.rb +3 -23
- data/lib/catpm/middleware.rb +2 -0
- data/lib/catpm/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 722853b2fcfcd675b06e698948f58e7b169041f966371e7699ced35b1ae83b8f
|
|
4
|
+
data.tar.gz: 867594a5c8631535eafb45cfdd66626c4403dea1d550e29c6b7103e7c9893500
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c686066f30115d154c8d05e0a9a47d69c09a4221e02a2c46550c144426914da2aa34f2e6df7bd601326f7e43b009823a9956224afa10aa28536eb0f5e1495021
|
|
7
|
+
data.tar.gz: 29581810d90caf496eb63f8e28493bbed6783919f48c134cada3fc7141540a74dc4bb84c8d933b28a0f44d5d6a7510fa514a686163794bc66b056c548e64a879
|
data/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
gem build catpm.gemspec
|
|
2
|
+
gem push catpm-0.1.2.gem
|
|
3
|
+
|
|
1
4
|
# Catpm
|
|
2
5
|
|
|
3
6
|
Lightweight, self-hosted performance monitoring for Rails. Track requests, background jobs, errors, and custom traces — all stored in your existing database. No external services, no Redis, no extra infrastructure.
|
data/lib/catpm/flusher.rb
CHANGED
|
@@ -12,28 +12,54 @@ module Catpm
|
|
|
12
12
|
@last_cleanup_at = Time.now
|
|
13
13
|
@running = false
|
|
14
14
|
@thread = nil
|
|
15
|
+
@pid = nil
|
|
16
|
+
@mutex = Mutex.new
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def start
|
|
18
|
-
|
|
20
|
+
@mutex.synchronize do
|
|
21
|
+
# After fork(), threads are dead but @running may still be true
|
|
22
|
+
if @pid && @pid != Process.pid
|
|
23
|
+
@running = false
|
|
24
|
+
@thread = nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
return if @running
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
@running = true
|
|
30
|
+
@pid = Process.pid
|
|
31
|
+
@thread = Thread.new do
|
|
32
|
+
while @running
|
|
33
|
+
sleep(effective_interval)
|
|
34
|
+
flush_cycle if @running
|
|
35
|
+
end
|
|
36
|
+
rescue => e
|
|
37
|
+
Catpm.config.error_handler.call(e)
|
|
38
|
+
retry if @running
|
|
25
39
|
end
|
|
26
|
-
rescue => e
|
|
27
|
-
Catpm.config.error_handler.call(e)
|
|
28
|
-
retry if @running
|
|
29
40
|
end
|
|
30
41
|
end
|
|
31
42
|
|
|
43
|
+
# Cheap check called from middleware on every request.
|
|
44
|
+
# Detects fork (Puma, Unicorn, etc.) and restarts the thread.
|
|
45
|
+
def ensure_running!
|
|
46
|
+
return if @running && @thread&.alive? && @pid == Process.pid
|
|
47
|
+
|
|
48
|
+
start
|
|
49
|
+
end
|
|
50
|
+
|
|
32
51
|
def stop(timeout: Catpm.config.shutdown_timeout)
|
|
33
|
-
|
|
52
|
+
thread = nil
|
|
34
53
|
|
|
35
|
-
@
|
|
36
|
-
|
|
54
|
+
@mutex.synchronize do
|
|
55
|
+
return unless @running
|
|
56
|
+
|
|
57
|
+
@running = false
|
|
58
|
+
thread = @thread
|
|
59
|
+
@thread = nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
thread&.join(timeout)
|
|
37
63
|
flush_cycle # Final flush
|
|
38
64
|
end
|
|
39
65
|
|
|
@@ -44,29 +70,29 @@ module Catpm
|
|
|
44
70
|
events = @buffer.drain
|
|
45
71
|
return if events.empty?
|
|
46
72
|
|
|
47
|
-
|
|
73
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
|
74
|
+
ActiveRecord::Base.transaction do
|
|
75
|
+
perf_events, custom_events = events.partition { |e| e.is_a?(Catpm::Event) }
|
|
48
76
|
|
|
49
|
-
|
|
50
|
-
|
|
77
|
+
if perf_events.any?
|
|
78
|
+
buckets, samples, errors = aggregate(perf_events)
|
|
51
79
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
adapter.persist_buckets(buckets)
|
|
80
|
+
adapter = Catpm::Adapter.current
|
|
81
|
+
adapter.persist_buckets(buckets)
|
|
55
82
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
83
|
+
bucket_map = build_bucket_map(buckets)
|
|
84
|
+
samples = rotate_samples(samples)
|
|
85
|
+
adapter.persist_samples(samples, bucket_map)
|
|
86
|
+
adapter.persist_errors(errors)
|
|
87
|
+
end
|
|
62
88
|
|
|
63
|
-
|
|
64
|
-
|
|
89
|
+
if custom_events.any?
|
|
90
|
+
event_buckets, event_samples = aggregate_custom_events(custom_events)
|
|
65
91
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
92
|
+
adapter = Catpm::Adapter.current
|
|
93
|
+
adapter.persist_event_buckets(event_buckets)
|
|
94
|
+
adapter.persist_event_samples(event_samples)
|
|
95
|
+
end
|
|
70
96
|
end
|
|
71
97
|
end
|
|
72
98
|
|
|
@@ -75,8 +101,10 @@ module Catpm
|
|
|
75
101
|
|
|
76
102
|
maybe_cleanup
|
|
77
103
|
rescue => e
|
|
104
|
+
events&.each { |ev| @buffer.push(ev) }
|
|
78
105
|
@circuit.record_failure
|
|
79
106
|
Catpm.config.error_handler.call(e)
|
|
107
|
+
Rails.logger.error("[catpm] flush error: #{e.class}: #{e.message}\n#{e.backtrace&.first(5)&.join("\n")}")
|
|
80
108
|
end
|
|
81
109
|
|
|
82
110
|
def reset!
|
data/lib/catpm/lifecycle.rb
CHANGED
|
@@ -10,17 +10,11 @@ module Catpm
|
|
|
10
10
|
initialize_flusher
|
|
11
11
|
apply_patches
|
|
12
12
|
|
|
13
|
-
#
|
|
14
|
-
# For forking servers,
|
|
15
|
-
#
|
|
13
|
+
# Start the flusher in the current process.
|
|
14
|
+
# For forking servers (Puma, Passenger, Unicorn, etc.),
|
|
15
|
+
# the middleware detects fork via PID and restarts automatically.
|
|
16
16
|
Catpm.flusher&.start
|
|
17
17
|
|
|
18
|
-
if defined?(::PhusionPassenger)
|
|
19
|
-
register_passenger_hook
|
|
20
|
-
elsif defined?(::Pitchfork)
|
|
21
|
-
register_pitchfork_hook
|
|
22
|
-
end
|
|
23
|
-
|
|
24
18
|
register_shutdown_hooks
|
|
25
19
|
end
|
|
26
20
|
|
|
@@ -57,20 +51,6 @@ module Catpm
|
|
|
57
51
|
jitter: Catpm.config.flush_jitter
|
|
58
52
|
)
|
|
59
53
|
end
|
|
60
|
-
|
|
61
|
-
def register_passenger_hook
|
|
62
|
-
flusher = Catpm.flusher
|
|
63
|
-
::PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
|
64
|
-
flusher&.start if forked
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def register_pitchfork_hook
|
|
69
|
-
flusher = Catpm.flusher
|
|
70
|
-
::Pitchfork.configure do |server|
|
|
71
|
-
server.after_worker_fork { flusher&.start }
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
54
|
end
|
|
75
55
|
end
|
|
76
56
|
end
|
data/lib/catpm/middleware.rb
CHANGED
data/lib/catpm/version.rb
CHANGED