puma-log_stats-codeur 0.1.2 → 0.1.3
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/lib/puma/log_stats/version.rb +1 -1
- data/lib/puma/plugin/log_stats.rb +101 -99
- 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: 7dccacaaaed068382710999238bf1563335653ba81ac0afe70373eb53fc6c4ec
|
4
|
+
data.tar.gz: 3183d3640deb3b060873061e10087c06e429aa0be8d60ba0b8924adf3c379890
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 060ed7df03b49ccfb212d4d18f117bff2f4fc054fc6dcece408d11efbd8bbacf4f958c9e7e46251fb3da1703318afb60df7bad77723038677febf1197cc5d7bf
|
7
|
+
data.tar.gz: fbf036c802861ff041d4a3c777af0767b6c8e0d73cebb489c2049dea6183036f16bd6964cd8b8c54312a244502454271ead82259e2cb4abcee93f82811919a6c
|
@@ -2,138 +2,140 @@
|
|
2
2
|
|
3
3
|
require "puma/plugin"
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Puma
|
6
|
+
module LogStats
|
7
|
+
class << self
|
8
|
+
# Interval between logging attempts in seconds.
|
9
|
+
attr_accessor :interval
|
10
|
+
LogStats.interval = 10
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
attr_accessor :notify_change_with
|
13
|
+
LogStats.notify_change_with = :sentry
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
attr_accessor :warning_threshold
|
16
|
+
LogStats.warning_threshold = 0.7
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
attr_accessor :critical_threshold
|
19
|
+
LogStats.critical_threshold = 0.85
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def start(launcher)
|
23
|
+
@launcher = launcher
|
24
|
+
launcher.events.register(:state) do |state|
|
25
|
+
if %i[halt restart stop].include?(state)
|
26
|
+
@running = false
|
27
|
+
end
|
26
28
|
end
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
30
|
+
in_background do
|
31
|
+
@running = true
|
32
|
+
@load_level = :normal
|
33
|
+
while @running
|
34
|
+
sleep LogStats.interval
|
35
|
+
@previous_status_message = @status_message
|
36
|
+
@stats = Puma.stats_hash
|
37
|
+
@status_message = status_message
|
38
|
+
log(@status_message) if @previous_status_message != @status_message
|
39
|
+
check_alarms
|
40
|
+
end
|
39
41
|
end
|
40
42
|
end
|
41
|
-
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
def check_alarms
|
45
|
+
threshold_reached(:critical, LogStats.critical_threshold) ||
|
46
|
+
threshold_reached(:warning, LogStats.warning_threshold) ||
|
47
|
+
normal_load
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
50
|
+
def threshold_reached(level, threshold)
|
51
|
+
return false if threads_load < threshold
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
change_level(level, "Puma threads load is greater than #{threshold * 100}% (#{max_threads - pool_capacity}/#{max_threads})")
|
54
|
+
true
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
def normal_load
|
58
|
+
change_level(:normal, "Puma threads load is back to normal values")
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
61
|
+
def change_level(level, message)
|
62
|
+
return if @load_level == level
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
log("#{level.to_s.upcase}: #{message}")
|
65
|
+
notify(level, message)
|
66
|
+
@load_level = level
|
67
|
+
end
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
def notify(level, message)
|
70
|
+
if LogStats.notify_change_with == :sentry
|
71
|
+
Sentry.capture_message(message, level: (level == :critical) ? :error : level) if defined?(Sentry) && level != :normal
|
72
|
+
elsif LogStats.notify_change_with.respond_to?(:call)
|
73
|
+
LogStats.notify_change_with.call(level: level, message: message, threads_load: threads_load)
|
74
|
+
end
|
73
75
|
end
|
74
|
-
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
def status_message
|
78
|
+
if clustered?
|
79
|
+
"cluster: #{booted_workers}/#{workers} workers: #{running}/#{max_threads} threads, #{pool_capacity} available, #{backlog} backlog"
|
80
|
+
else
|
81
|
+
"single: #{running}/#{max_threads} threads, #{pool_capacity} available, #{backlog} backlog"
|
82
|
+
end
|
81
83
|
end
|
82
|
-
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
85
|
+
def log(str)
|
86
|
+
@launcher.log_writer.log("[#{Time.now}][puma #{Puma::Const::VERSION}] #{str}")
|
87
|
+
end
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
89
|
+
def threads_load
|
90
|
+
1.0 - pool_capacity.to_f / max_threads.to_f
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
94
|
-
|
93
|
+
def clustered?
|
94
|
+
@stats.key?(:workers)
|
95
|
+
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
def workers
|
98
|
+
@stats.fetch(:workers, 1)
|
99
|
+
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
101
|
+
def booted_workers
|
102
|
+
@stats.fetch(:booted_workers, 1)
|
103
|
+
end
|
103
104
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
105
|
+
def running
|
106
|
+
if clustered?
|
107
|
+
@stats[:worker_status].sum { |s| s[:last_status].fetch(:running, 0) }
|
108
|
+
else
|
109
|
+
@stats.fetch(:running, 0)
|
110
|
+
end
|
109
111
|
end
|
110
|
-
end
|
111
112
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
113
|
+
def backlog
|
114
|
+
if clustered?
|
115
|
+
@stats[:worker_status].sum { |s| s[:last_status].fetch(:backlog, 0) }
|
116
|
+
else
|
117
|
+
@stats.fetch(:backlog, 0)
|
118
|
+
end
|
117
119
|
end
|
118
|
-
end
|
119
120
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
121
|
+
def pool_capacity
|
122
|
+
if clustered?
|
123
|
+
@stats[:worker_status].sum { |s| s[:last_status].fetch(:pool_capacity, 0) }
|
124
|
+
else
|
125
|
+
@stats.fetch(:pool_capacity, 0)
|
126
|
+
end
|
125
127
|
end
|
126
|
-
end
|
127
128
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
def max_threads
|
130
|
+
if clustered?
|
131
|
+
@stats[:worker_status].sum { |s| s[:last_status].fetch(:max_threads, 0) }
|
132
|
+
else
|
133
|
+
@stats.fetch(:max_threads, 0)
|
134
|
+
end
|
133
135
|
end
|
134
136
|
end
|
135
137
|
end
|
136
138
|
|
137
139
|
Puma::Plugin.create do
|
138
|
-
include LogStats
|
140
|
+
include Puma::LogStats
|
139
141
|
end
|