sitegoose 0.3.3 → 0.3.5
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 +125 -6
- data/lib/sitegoose/calls.rb +175 -0
- data/lib/sitegoose/errors.rb +6 -4
- data/lib/sitegoose/faraday.rb +54 -0
- data/lib/sitegoose/firewall.rb +252 -18
- data/lib/sitegoose/performance.rb +38 -0
- data/lib/sitegoose/platform.rb +53 -21
- data/lib/sitegoose/queries.rb +384 -128
- data/lib/sitegoose/query_text.rb +302 -90
- data/lib/sitegoose/queues.rb +193 -0
- data/lib/sitegoose/request_rules.rb +37 -23
- data/lib/sitegoose/resources.rb +359 -1
- data/lib/sitegoose/sidekiq.rb +289 -12
- data/lib/sitegoose/version.rb +1 -1
- data/lib/sitegoose.rb +5 -0
- metadata +9 -5
|
@@ -356,35 +356,49 @@ module SiteGoose
|
|
|
356
356
|
end
|
|
357
357
|
end
|
|
358
358
|
|
|
359
|
-
# The
|
|
360
|
-
#
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
359
|
+
# The request's parts, prepared once for every rule and page limit:
|
|
360
|
+
# method upper-cased, path raw/decoded/routed, query raw/decoded, ua.
|
|
361
|
+
# target is the request target as the server received it, query string and all.
|
|
362
|
+
def prepare(method = "", target = "", user_agent = "")
|
|
364
363
|
target = SiteGoose.text(target)
|
|
365
364
|
mark = target.index("?")
|
|
366
365
|
path_part = mark ? target[0, mark] : target
|
|
367
366
|
query = mark ? target[(mark + 1)..] : ""
|
|
368
|
-
raw_path = normalize_path(path_part[0, MAX_TEXT])
|
|
369
367
|
query = query[0, MAX_TEXT]
|
|
370
|
-
raw_query = query.downcase
|
|
371
368
|
ua = SiteGoose.text(user_agent)[0, MAX_TEXT].downcase
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
369
|
+
{
|
|
370
|
+
method: method.to_s.upcase,
|
|
371
|
+
raw_path: normalize_path(path_part[0, MAX_TEXT]),
|
|
372
|
+
decoded_path: normalize_path(decode_once(path_part[0, MAX_TEXT])[0, MAX_TEXT]),
|
|
373
|
+
# And as the router reads it: "/x;y/../admin" is "/admin" there, so a
|
|
374
|
+
# rule on "/admin" catches it; a hit on any of the three counts.
|
|
375
|
+
routed_path: router_path(path_part[0, MAX_TEXT])[0, MAX_TEXT],
|
|
376
|
+
raw_query: query.downcase,
|
|
377
|
+
# Lower-cased after decoding: %55NION decodes to "UNION", which a
|
|
378
|
+
# lower-cased "union select" rule would otherwise never see.
|
|
379
|
+
decoded_query: decode_once(query)[0, MAX_TEXT].downcase,
|
|
380
|
+
ua: ua
|
|
381
|
+
}
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# True when every section the rule names matches the prepared request.
|
|
385
|
+
def matches?(rule, req)
|
|
386
|
+
return false if !rule["methods"].empty? && !rule["methods"].include?(req[:method])
|
|
387
|
+
return false if rule["path"] && !hits?(rule["path"], req[:raw_path], req[:decoded_path], req[:routed_path])
|
|
388
|
+
return false if rule["query"] && !hits?(rule["query"], req[:raw_query], req[:decoded_query])
|
|
389
|
+
return false if rule["ua"] && !hits?(rule["ua"], req[:ua], req[:ua])
|
|
390
|
+
|
|
391
|
+
true
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# The first rule that matches, or nil. target is the request target as
|
|
395
|
+
# the server received it, query string and all; prepared, when given, is
|
|
396
|
+
# prepare() of the same request.
|
|
397
|
+
def match(rules, method = "", target = "", user_agent = "", prepared = nil)
|
|
398
|
+
return nil if rules.nil? || rules.empty?
|
|
399
|
+
|
|
400
|
+
req = prepared || prepare(method, target, user_agent)
|
|
401
|
+
rules.find { |rule| matches?(rule, req) }
|
|
388
402
|
end
|
|
389
403
|
end
|
|
390
404
|
end
|
data/lib/sitegoose/resources.rb
CHANGED
|
@@ -17,11 +17,272 @@
|
|
|
17
17
|
# same statvfs figures: used is total minus free, and total is used plus what
|
|
18
18
|
# is available to us, which is the percentage df prints); without df, the
|
|
19
19
|
# disks are left out.
|
|
20
|
+
#
|
|
21
|
+
# Beside them, the limits pass (Limits, below): the container's memory and
|
|
22
|
+
# CPU caps, throttling and kills, the machine's waiting time (PSI), refused
|
|
23
|
+
# connections, the connection-tracking table, open files, garbage collection,
|
|
24
|
+
# Puma's thread pool and the most requests in progress at once. Ruby has no
|
|
25
|
+
# heap cap, so heap_used_mb / heap_max_mb are never sent.
|
|
20
26
|
|
|
21
27
|
require "etc"
|
|
22
28
|
require "io/wait"
|
|
29
|
+
require "securerandom"
|
|
23
30
|
|
|
24
31
|
module SiteGoose
|
|
32
|
+
# How close the app is to a wall (resources.md, "Limits"): its
|
|
33
|
+
# container's own caps, the machine's waiting time, connections refused
|
|
34
|
+
# before the app saw them, the process's open files and garbage
|
|
35
|
+
# collection, and Puma's thread pool. A port of the Node package's
|
|
36
|
+
# lib/limits.js. Same rule as the rest of the module: a figure this
|
|
37
|
+
# machine won't give is left out, never sent as 0; every reader here gets
|
|
38
|
+
# nil from a file that isn't there, which off Linux is all of them.
|
|
39
|
+
module Limits
|
|
40
|
+
MB = 1024 * 1024
|
|
41
|
+
# cgroup v1 says "no limit" with the largest page-aligned 64-bit number;
|
|
42
|
+
# v2 says "max". Anything this size is not a cap a machine has.
|
|
43
|
+
NO_LIMIT_BYTES = 2**62
|
|
44
|
+
NUMBER = /\A-?\d+(?:\.\d+)?\z/.freeze
|
|
45
|
+
READ = ->(path) { Resources.read_text(path) }
|
|
46
|
+
|
|
47
|
+
module_function
|
|
48
|
+
|
|
49
|
+
# A number from a file holding one, or nil for a missing or unreadable one.
|
|
50
|
+
def num(text)
|
|
51
|
+
trimmed = text.to_s.strip
|
|
52
|
+
return nil unless trimmed.match?(NUMBER)
|
|
53
|
+
|
|
54
|
+
trimmed.include?(".") ? trimmed.to_f : trimmed.to_i
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# "name value" lines (memory.stat, cpu.stat, memory.events) as numbers.
|
|
58
|
+
def key_values(text)
|
|
59
|
+
text.to_s.each_line.with_object({}) do |line, out|
|
|
60
|
+
name, value = line.strip.split(/\s+/)
|
|
61
|
+
n = num(value)
|
|
62
|
+
out[name] = n if name && n
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# /proc/self/cgroup: the v2 path ("0::<path>") and the v1 memory and cpu
|
|
67
|
+
# controllers' paths ("4:memory:<path>", "3:cpu,cpuacct:<path>").
|
|
68
|
+
def cgroup_paths(text)
|
|
69
|
+
out = { v2: nil, memory: nil, cpu: nil }
|
|
70
|
+
text.to_s.each_line do |line|
|
|
71
|
+
id, controllers, at = line.chomp.split(":", 3)
|
|
72
|
+
next if at.nil?
|
|
73
|
+
|
|
74
|
+
at = at.strip
|
|
75
|
+
next if at.empty?
|
|
76
|
+
|
|
77
|
+
names = controllers.split(",")
|
|
78
|
+
out[:v2] = at if id == "0" && names.empty?
|
|
79
|
+
out[:memory] = at if names.include?("memory")
|
|
80
|
+
out[:cpu] = at if names.include?("cpu")
|
|
81
|
+
end
|
|
82
|
+
out
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Where this process's cgroup files are, or nil. Under the mount at the
|
|
86
|
+
# path /proc/self/cgroup names first, then the mount itself: in a
|
|
87
|
+
# container with its own cgroup namespace the path is "/" and the mount
|
|
88
|
+
# is already the container's; without one the path names the host's
|
|
89
|
+
# hierarchy. v2 needs the memory or cpu controller's own files: a hybrid
|
|
90
|
+
# machine lists a v2 path with no controllers while the limits live in v1.
|
|
91
|
+
def find_cgroup(read = READ)
|
|
92
|
+
paths = cgroup_paths(read.call("/proc/self/cgroup"))
|
|
93
|
+
under = lambda do |base, at, probes|
|
|
94
|
+
dirs = at && at != "/" ? [base + at, base] : [base]
|
|
95
|
+
dirs.find { |dir| probes.any? { |probe| !read.call("#{dir}/#{probe}").empty? } }
|
|
96
|
+
end
|
|
97
|
+
if paths[:v2]
|
|
98
|
+
dir = under.call("/sys/fs/cgroup", paths[:v2], %w[memory.current cpu.max])
|
|
99
|
+
return { version: 2, memory: dir, cpu: dir } if dir
|
|
100
|
+
end
|
|
101
|
+
memory = paths[:memory] && under.call("/sys/fs/cgroup/memory", paths[:memory], %w[memory.usage_in_bytes])
|
|
102
|
+
cpu = paths[:cpu] && under.call("/sys/fs/cgroup/cpu", paths[:cpu], %w[cpu.cfs_quota_us])
|
|
103
|
+
memory || cpu ? { version: 1, memory: memory, cpu: cpu } : nil
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# One reading of the cgroup's caps and counters, in bytes and raw counts.
|
|
107
|
+
def cgroup_reading(cgroup, read = READ)
|
|
108
|
+
return nil unless cgroup
|
|
109
|
+
|
|
110
|
+
out = { mem_cap: nil, mem_used: nil, cpu_cores: nil, periods: nil, throttled: nil, oom: nil }
|
|
111
|
+
if cgroup[:version] == 2
|
|
112
|
+
max = read.call("#{cgroup[:memory]}/memory.max").strip
|
|
113
|
+
out[:mem_cap] = max == "max" ? nil : num(max)
|
|
114
|
+
current = num(read.call("#{cgroup[:memory]}/memory.current"))
|
|
115
|
+
# The working set: page cache counts against the cgroup too, so
|
|
116
|
+
# memory.current sits near the cap on a healthy container that reads
|
|
117
|
+
# files. Less inactive_file is what Docker and the kubelet report.
|
|
118
|
+
stat = key_values(read.call("#{cgroup[:memory]}/memory.stat"))
|
|
119
|
+
out[:mem_used] = [0, current - (stat["inactive_file"] || 0)].max if current
|
|
120
|
+
out[:oom] = key_values(read.call("#{cgroup[:memory]}/memory.events"))["oom_kill"]
|
|
121
|
+
quota, period = read.call("#{cgroup[:cpu]}/cpu.max").strip.split(/\s+/)
|
|
122
|
+
quota_n = num(quota)
|
|
123
|
+
period_n = num(period)
|
|
124
|
+
out[:cpu_cores] = quota_n.to_f / period_n if quota != "max" && quota_n&.positive? && period_n&.positive?
|
|
125
|
+
cpu = key_values(read.call("#{cgroup[:cpu]}/cpu.stat"))
|
|
126
|
+
out[:periods] = cpu["nr_periods"]
|
|
127
|
+
out[:throttled] = cpu["nr_throttled"]
|
|
128
|
+
return out
|
|
129
|
+
end
|
|
130
|
+
if cgroup[:memory]
|
|
131
|
+
out[:mem_cap] = num(read.call("#{cgroup[:memory]}/memory.limit_in_bytes"))
|
|
132
|
+
usage = num(read.call("#{cgroup[:memory]}/memory.usage_in_bytes"))
|
|
133
|
+
stat = key_values(read.call("#{cgroup[:memory]}/memory.stat"))
|
|
134
|
+
out[:mem_used] = [0, usage - (stat["total_inactive_file"] || 0)].max if usage
|
|
135
|
+
# Kernels from 4.13 carry the count here.
|
|
136
|
+
out[:oom] = key_values(read.call("#{cgroup[:memory]}/memory.oom_control"))["oom_kill"]
|
|
137
|
+
end
|
|
138
|
+
if cgroup[:cpu]
|
|
139
|
+
quota = num(read.call("#{cgroup[:cpu]}/cpu.cfs_quota_us"))
|
|
140
|
+
period = num(read.call("#{cgroup[:cpu]}/cpu.cfs_period_us"))
|
|
141
|
+
out[:cpu_cores] = quota.to_f / period if quota&.positive? && period&.positive?
|
|
142
|
+
cpu = key_values(read.call("#{cgroup[:cpu]}/cpu.stat"))
|
|
143
|
+
out[:periods] = cpu["nr_periods"]
|
|
144
|
+
out[:throttled] = cpu["nr_throttled"]
|
|
145
|
+
end
|
|
146
|
+
out
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# How much a counter moved between two readings, or nil: no pair, or it
|
|
150
|
+
# went down (a reset, not a negative count). Unlike a rate, a count is
|
|
151
|
+
# never dropped for the pair being far apart: a kill is a kill.
|
|
152
|
+
def count_since(before, after)
|
|
153
|
+
return nil if before.nil? || after.nil?
|
|
154
|
+
|
|
155
|
+
after >= before ? after - before : nil
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The row's cgroup fields from two readings. A cap at or above what the
|
|
159
|
+
# machine itself has isn't one, so neither half is sent.
|
|
160
|
+
def cgroup_fields(before, after, mem_total_mb: nil, cores: nil)
|
|
161
|
+
out = {}
|
|
162
|
+
return out unless after
|
|
163
|
+
|
|
164
|
+
cap = after[:mem_cap]
|
|
165
|
+
cap_mb = cap&.positive? && cap < NO_LIMIT_BYTES ? cap / MB : nil
|
|
166
|
+
if cap_mb&.positive? && !(mem_total_mb && cap_mb >= mem_total_mb) && after[:mem_used]
|
|
167
|
+
out["limit_mem_mb"] = cap_mb
|
|
168
|
+
out["limit_mem_used_mb"] = [cap_mb, after[:mem_used] / MB].min
|
|
169
|
+
end
|
|
170
|
+
cores_cap = after[:cpu_cores]
|
|
171
|
+
out["limit_cpu_cores"] = cores_cap.round(2) if cores_cap && !(cores && cores_cap >= cores)
|
|
172
|
+
# Periods only pass while a quota is set, so no cap means nothing sent.
|
|
173
|
+
periods = count_since(before && before[:periods], after[:periods])
|
|
174
|
+
throttled = count_since(before && before[:throttled], after[:throttled])
|
|
175
|
+
out["cpu_throttled_pct"] = [100.0, 100.0 * throttled / periods].min.round(1) if periods&.positive? && throttled
|
|
176
|
+
# Kills of other processes in this cgroup: this process's own kill
|
|
177
|
+
# can't be reported by it (see the protocol).
|
|
178
|
+
oom = count_since(before && before[:oom], after[:oom])
|
|
179
|
+
out["oom_kills"] = oom if oom
|
|
180
|
+
out
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# /proc/pressure/<resource>'s "some" line, avg60: the share of the last
|
|
184
|
+
# minute in which at least one task was waiting. One reading is enough.
|
|
185
|
+
def pressure_some(text)
|
|
186
|
+
match = /^some\b[^\n]*\bavg60=([\d.]+)/.match(text.to_s)
|
|
187
|
+
match ? [100.0, match[1].to_f].min.round(1) : nil
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def pressure_fields(read = READ)
|
|
191
|
+
{ "psi_cpu_pct" => "cpu", "psi_mem_pct" => "memory", "psi_io_pct" => "io" }.each_with_object({}) do |(name, file), out|
|
|
192
|
+
value = pressure_some(read.call("/proc/pressure/#{file}"))
|
|
193
|
+
out[name] = value if value
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# TcpExt ListenOverflows from /proc/net/netstat: connections refused
|
|
198
|
+
# since boot because the accept queue was full. A header and a value line.
|
|
199
|
+
def listen_overflows(text)
|
|
200
|
+
lines = text.to_s.split("\n")
|
|
201
|
+
lines.each_cons(2) do |names, values|
|
|
202
|
+
next unless names.start_with?("TcpExt:") && values.start_with?("TcpExt:")
|
|
203
|
+
|
|
204
|
+
at = names.strip.split(/\s+/).index("ListenOverflows")
|
|
205
|
+
return at&.positive? ? num(values.strip.split(/\s+/)[at]) : nil
|
|
206
|
+
end
|
|
207
|
+
nil
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# How full the connection-tracking table is. Only where the firewall
|
|
211
|
+
# loaded it, and usually unreadable inside a container.
|
|
212
|
+
def conntrack_pct(read = READ)
|
|
213
|
+
count = num(read.call("/proc/sys/net/netfilter/nf_conntrack_count"))
|
|
214
|
+
max = num(read.call("/proc/sys/net/netfilter/nf_conntrack_max"))
|
|
215
|
+
count && max&.positive? ? [100.0, 100.0 * count / max].min.round(1) : nil
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# The soft "Max open files" from /proc/self/limits, or nil when unlimited.
|
|
219
|
+
def fd_limit(text)
|
|
220
|
+
match = /^Max open files\s+(\S+)/.match(text.to_s)
|
|
221
|
+
match ? num(match[1]) : nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Open descriptors of this process, less the one listing them takes.
|
|
225
|
+
def open_files(list = -> { Dir.children("/proc/self/fd") })
|
|
226
|
+
[0, list.call.size - 1].max
|
|
227
|
+
rescue StandardError
|
|
228
|
+
nil
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def file_fields(read = READ, list = nil)
|
|
232
|
+
out = {}
|
|
233
|
+
used = list ? open_files(list) : open_files
|
|
234
|
+
max = fd_limit(read.call("/proc/self/limits"))
|
|
235
|
+
out["fds_used"] = max&.positive? ? [used, max].min : used if used
|
|
236
|
+
out["fds_max"] = max if max&.positive?
|
|
237
|
+
out
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Milliseconds spent in garbage collection since the process started
|
|
241
|
+
# (GC.stat(:time), Ruby 3.1+), or nil where Ruby doesn't keep it.
|
|
242
|
+
def gc_total_ms
|
|
243
|
+
value = GC.stat(:time)
|
|
244
|
+
value.is_a?(Numeric) ? value : nil
|
|
245
|
+
rescue StandardError
|
|
246
|
+
nil
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# How long this process has run, from /proc/self/stat's start time
|
|
250
|
+
# (clock ticks after boot) and the machine's uptime, or nil.
|
|
251
|
+
def proc_uptime(stat_text, uptime_text, ticks)
|
|
252
|
+
rest = stat_text.to_s.rpartition(")")[2].split
|
|
253
|
+
start = num(rest[19])
|
|
254
|
+
up = num(uptime_text.to_s.split.first)
|
|
255
|
+
return nil unless start && up && ticks.to_i.positive?
|
|
256
|
+
|
|
257
|
+
seconds = (up - start.to_f / ticks.to_i).floor
|
|
258
|
+
seconds.negative? ? nil : seconds
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def clock_ticks
|
|
262
|
+
Etc.sysconf(Etc::SC_CLK_TCK)
|
|
263
|
+
rescue StandardError
|
|
264
|
+
nil
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Puma's thread pool: Puma::Server#stats (or Puma.stats_hash in single
|
|
268
|
+
# mode). Busy is the pool's threads less what it could still take on;
|
|
269
|
+
# max_threads is a real cap, so it is sent.
|
|
270
|
+
def puma_fields(stats)
|
|
271
|
+
return {} unless stats.is_a?(Hash)
|
|
272
|
+
|
|
273
|
+
get = ->(name) { stats[name] || stats[name.to_s] }
|
|
274
|
+
max = get.call(:max_threads)
|
|
275
|
+
return {} unless max.is_a?(Integer) && max.positive?
|
|
276
|
+
|
|
277
|
+
out = { "workers_max" => max }
|
|
278
|
+
capacity = get.call(:pool_capacity)
|
|
279
|
+
out["workers_busy"] = (max - capacity).clamp(0, max) if capacity.is_a?(Integer)
|
|
280
|
+
backlog = get.call(:backlog)
|
|
281
|
+
out["queue_waiting"] = backlog if backlog.is_a?(Integer) && !backlog.negative?
|
|
282
|
+
out
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
25
286
|
class Resources
|
|
26
287
|
# Keep in step with the collector (expresscollect/Analytics/resources.js) and
|
|
27
288
|
# the agent (sdk/vps-agent).
|
|
@@ -329,6 +590,16 @@ module SiteGoose
|
|
|
329
590
|
@last_disk = Resources.disk_counters(Resources.read_text("/proc/diskstats"))
|
|
330
591
|
@last_net = Resources.net_counters(Resources.read_text("/proc/net/dev"))
|
|
331
592
|
@last_at = Time.now.to_f
|
|
593
|
+
# The limits pass: the cgroup is found once, its counters, the listen
|
|
594
|
+
# queue's and garbage collection's are read every sample and diffed.
|
|
595
|
+
@cgroup = Limits.find_cgroup
|
|
596
|
+
limits_baseline
|
|
597
|
+
# Requests in progress at once, from the performance module (the
|
|
598
|
+
# platform wires it); nil until then.
|
|
599
|
+
@inflight = nil
|
|
600
|
+
@puma = nil
|
|
601
|
+
@server_seen = false
|
|
602
|
+
new_process
|
|
332
603
|
@sampler = core.valid? ? Periodic.new(SAMPLE_EVERY) { sample; SAMPLE_EVERY } : nil
|
|
333
604
|
@job = core.valid? ? Periodic.new(EVERY) { report } : nil
|
|
334
605
|
Forking.register(self)
|
|
@@ -337,6 +608,88 @@ module SiteGoose
|
|
|
337
608
|
def after_fork_in_child
|
|
338
609
|
@lock = Mutex.new
|
|
339
610
|
@rows = {}
|
|
611
|
+
@puma = nil
|
|
612
|
+
@server_seen = false
|
|
613
|
+
new_process
|
|
614
|
+
# The parent counts what happened before the fork: the child's first
|
|
615
|
+
# differences start here, not at the parent's last sample.
|
|
616
|
+
limits_baseline
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# The first readings the limits pass's counts are differences from.
|
|
620
|
+
def limits_baseline
|
|
621
|
+
@last_cgroup = Limits.cgroup_reading(@cgroup)
|
|
622
|
+
@last_overflows = Limits.listen_overflows(Resources.read_text("/proc/net/netstat"))
|
|
623
|
+
@last_gc = Limits.gc_total_ms
|
|
624
|
+
end
|
|
625
|
+
private :limits_baseline
|
|
626
|
+
|
|
627
|
+
# This process among others of the same name (Puma's workers each send
|
|
628
|
+
# their own), so a read can tell siblings from resent copies.
|
|
629
|
+
def new_process
|
|
630
|
+
@proc_key = SecureRandom.hex(6)
|
|
631
|
+
@started = SiteGoose.clock
|
|
632
|
+
end
|
|
633
|
+
private :new_process
|
|
634
|
+
|
|
635
|
+
attr_reader :proc_key
|
|
636
|
+
|
|
637
|
+
# callable -> the most requests in progress at once since it was last
|
|
638
|
+
# asked (Performance#take_inflight_peak), or nil.
|
|
639
|
+
def use_inflight(callable)
|
|
640
|
+
@inflight = callable.respond_to?(:call) ? callable : nil
|
|
641
|
+
end
|
|
642
|
+
|
|
643
|
+
def server_seen?
|
|
644
|
+
@server_seen
|
|
645
|
+
end
|
|
646
|
+
|
|
647
|
+
# From a request's thread, once: the Puma server running it, whose
|
|
648
|
+
# thread pool the samples read (Puma::Server.current, public).
|
|
649
|
+
def see_server
|
|
650
|
+
@server_seen = true
|
|
651
|
+
@puma = ::Puma::Server.current if defined?(::Puma::Server) && ::Puma::Server.respond_to?(:current)
|
|
652
|
+
rescue StandardError
|
|
653
|
+
@puma = nil
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
# Puma's pool now: this process's own server, else Puma's stats in
|
|
657
|
+
# single mode (a cluster's master stats are its workers', not ours).
|
|
658
|
+
def puma_stats
|
|
659
|
+
return @puma.stats if @puma.respond_to?(:stats)
|
|
660
|
+
return nil unless defined?(::Puma) && ::Puma.respond_to?(:stats_hash)
|
|
661
|
+
|
|
662
|
+
stats = ::Puma.stats_hash
|
|
663
|
+
stats.is_a?(Hash) && (stats.key?(:max_threads) || stats.key?("max_threads")) ? stats : nil
|
|
664
|
+
rescue StandardError
|
|
665
|
+
nil
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
# The limits pass for one sample: caps and counters diffed against the
|
|
669
|
+
# previous sample's, the rest read once.
|
|
670
|
+
def limits_row(row)
|
|
671
|
+
out = {}
|
|
672
|
+
uptime = Limits.proc_uptime(Resources.read_text("/proc/self/stat"), Resources.read_text("/proc/uptime"), Limits.clock_ticks)
|
|
673
|
+
out["proc_uptime_s"] = uptime || (SiteGoose.clock - @started).floor
|
|
674
|
+
reading = Limits.cgroup_reading(@cgroup)
|
|
675
|
+
out.merge!(Limits.cgroup_fields(@last_cgroup, reading, mem_total_mb: row["mem_total_mb"], cores: row["cpu_cores"]))
|
|
676
|
+
@last_cgroup = reading
|
|
677
|
+
out.merge!(Limits.pressure_fields)
|
|
678
|
+
overflows = Limits.listen_overflows(Resources.read_text("/proc/net/netstat"))
|
|
679
|
+
drops = Limits.count_since(@last_overflows, overflows)
|
|
680
|
+
out["listen_drops"] = drops if drops
|
|
681
|
+
@last_overflows = overflows
|
|
682
|
+
conntrack = Limits.conntrack_pct
|
|
683
|
+
out["conntrack_pct"] = conntrack if conntrack
|
|
684
|
+
out.merge!(Limits.file_fields)
|
|
685
|
+
gc = Limits.gc_total_ms
|
|
686
|
+
spent = Limits.count_since(@last_gc, gc)
|
|
687
|
+
out["gc_ms"] = spent.to_f.round(1) if spent
|
|
688
|
+
@last_gc = gc
|
|
689
|
+
out.merge!(Limits.puma_fields(puma_stats))
|
|
690
|
+
peak = @inflight&.call
|
|
691
|
+
out["inflight_peak"] = peak if peak.is_a?(Integer) && !peak.negative?
|
|
692
|
+
out
|
|
340
693
|
end
|
|
341
694
|
|
|
342
695
|
# Takes one sample, covering the time since the last one.
|
|
@@ -348,7 +701,7 @@ module SiteGoose
|
|
|
348
701
|
percent = Resources.cpu_percent(@last_cpu && Resources.cpu_times_of(@last_cpu), cpu && Resources.cpu_times_of(cpu))
|
|
349
702
|
elapsed = at - @last_at
|
|
350
703
|
# The minute the interval started in, not the one it ended in.
|
|
351
|
-
row = { "minute" => Time.at((@last_at.to_i / 60) * 60).utc.iso8601(3) }
|
|
704
|
+
row = { "minute" => Time.at((@last_at.to_i / 60) * 60).utc.iso8601(3), "proc_key" => @proc_key }
|
|
352
705
|
shares = Resources.cpu_shares(@last_cpu, cpu)
|
|
353
706
|
rates = {}
|
|
354
707
|
if @last_disk && disk
|
|
@@ -380,6 +733,11 @@ module SiteGoose
|
|
|
380
733
|
row["proc_rss_mb"] = rss if rss
|
|
381
734
|
uptime = Resources.host_uptime(Resources.read_text("/proc/uptime"))
|
|
382
735
|
row["host_uptime_s"] = uptime if uptime
|
|
736
|
+
begin
|
|
737
|
+
row.merge!(limits_row(row))
|
|
738
|
+
rescue StandardError
|
|
739
|
+
nil
|
|
740
|
+
end
|
|
383
741
|
@lock.synchronize do
|
|
384
742
|
# Off in the dashboard: only the newest minute is kept, as the one row
|
|
385
743
|
# that asks the collector, now and then, whether it is back on.
|