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
data/lib/sitegoose/queries.rb
CHANGED
|
@@ -12,6 +12,13 @@
|
|
|
12
12
|
# Sequel: SiteGoose.watch_database(DB), one line. A Sequel query
|
|
13
13
|
# run on Active Record's connection is counted once, as Sequel's.
|
|
14
14
|
# Mongo (the mongo gem, Mongoid): a global command subscriber, automatic.
|
|
15
|
+
# ClickHouse: the click_house gem's ClickHouse::Connection through
|
|
16
|
+
# SiteGoose.watch_database(connection), one line; an Active Record
|
|
17
|
+
# ClickHouse adapter (clickhouse-activerecord) through sql.active_record.
|
|
18
|
+
# Redis: redis-client's global middleware (RedisClient.register),
|
|
19
|
+
# automatic; the redis gem 5 and Sidekiq 7 run on it. Each command is
|
|
20
|
+
# "GET user:?:cart" (QueryText.redis_command): the command and its first
|
|
21
|
+
# key's shape, never an argument or value.
|
|
15
22
|
#
|
|
16
23
|
# A query is added to the request (or job) it ran in, and the request's
|
|
17
24
|
# queries go into the minute's rows when it finishes, under the method and
|
|
@@ -20,24 +27,176 @@
|
|
|
20
27
|
# off nothing is recorded; queries: false keeps Server speed without them.
|
|
21
28
|
|
|
22
29
|
module SiteGoose
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
# What Queries and Calls share (sdk/protocols/queries.md and calls.md,
|
|
31
|
+
# "Recording"): each timing is filed under the request (or job) it ran in,
|
|
32
|
+
# keyed by its two fields (db + statement, call method + host); when the
|
|
33
|
+
# request finishes they go into the minute's rows under the method and
|
|
34
|
+
# route Server speed recorded; outside any request or job they are
|
|
35
|
+
# "JOB" "(background)". Finished minutes are sent every 60 seconds, and
|
|
36
|
+
# everything on shutdown. The including class names FIELDS (its two key
|
|
37
|
+
# fields as sent), PATH, MOD (the name it logs under) and REQUEST (the
|
|
38
|
+
# thread-local the request's own map lives in, which Context.with clears).
|
|
39
|
+
module MinuteRows
|
|
40
|
+
FLUSH_EVERY = 60
|
|
25
41
|
MAX_ROWS = 500
|
|
26
|
-
# Distinct
|
|
42
|
+
# Distinct keys one request keeps; past it new ones are "(other)".
|
|
27
43
|
MAX_PER_REQUEST = 50
|
|
28
|
-
# Keys in a minute: past MAX_KEYS new
|
|
44
|
+
# Keys in a minute: past MAX_KEYS new ones are "(other)", past
|
|
29
45
|
# HARD_MAX_KEYS nothing new is kept.
|
|
30
46
|
MAX_KEYS = 200
|
|
31
47
|
HARD_MAX_KEYS = 300
|
|
32
48
|
# Minutes kept unsent (a platform that can't send flushes by hand).
|
|
33
49
|
MAX_MINUTES = 5
|
|
34
|
-
OTHER =
|
|
50
|
+
OTHER = "(other)"
|
|
35
51
|
BACKGROUND = ["JOB", "(background)"].freeze
|
|
52
|
+
|
|
53
|
+
def self.minute
|
|
54
|
+
(Time.now.to_i / 60) * 60
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# -> [count, errors, sum_ms, max_ms, buckets] as a hash.
|
|
58
|
+
def self.blank
|
|
59
|
+
{ count: 0, errors: 0, sum_ms: 0.0, max_ms: 0.0, buckets: Array.new(Performance::BUCKET_EDGES_MS.size + 1, 0) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.merge(into, from)
|
|
63
|
+
into[:count] += from[:count]
|
|
64
|
+
into[:errors] += from[:errors]
|
|
65
|
+
into[:sum_ms] += from[:sum_ms]
|
|
66
|
+
into[:max_ms] = from[:max_ms] if from[:max_ms] > into[:max_ms]
|
|
67
|
+
from[:buckets].each_with_index { |n, i| into[:buckets][i] += n }
|
|
68
|
+
into
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.add(into, ms, failed)
|
|
72
|
+
into[:count] += 1
|
|
73
|
+
into[:errors] += 1 if failed
|
|
74
|
+
into[:sum_ms] += ms
|
|
75
|
+
into[:max_ms] = ms if ms > into[:max_ms]
|
|
76
|
+
into[:buckets][Performance.bucket_of(ms)] += 1
|
|
77
|
+
into
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
attr_reader :rows
|
|
81
|
+
|
|
82
|
+
def start_rows(core, server_name)
|
|
83
|
+
@core = core
|
|
84
|
+
@server_name = SiteGoose.text(server_name || SiteGoose.hostname, 100)
|
|
85
|
+
@rows = {}
|
|
86
|
+
@lock = Mutex.new
|
|
87
|
+
@job = core.valid? ? Periodic.new(FLUSH_EVERY) { flush(everything: false); FLUSH_EVERY } : nil
|
|
88
|
+
Forking.register(self)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# In a forked worker the parent's rows are the parent's to send.
|
|
92
|
+
def after_fork_in_child
|
|
93
|
+
@lock = Mutex.new
|
|
94
|
+
@rows = {}
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def on?
|
|
98
|
+
@core.on?("performance")
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# One timing (first, second: its two key fields) into its request's map,
|
|
102
|
+
# or straight into the minute's rows as background.
|
|
103
|
+
def file(first, second, ms, failed)
|
|
104
|
+
ms = ms.to_f
|
|
105
|
+
ms = 0.0 if ms.negative? || ms.nan?
|
|
106
|
+
if Context.request
|
|
107
|
+
mine = (Thread.current[self.class::REQUEST] ||= {})
|
|
108
|
+
key = [first, second]
|
|
109
|
+
key = [first, OTHER] if !mine.key?(key) && mine.size >= MAX_PER_REQUEST
|
|
110
|
+
MinuteRows.add(mine[key] ||= MinuteRows.blank, ms, failed)
|
|
111
|
+
else
|
|
112
|
+
one = MinuteRows.add(MinuteRows.blank, ms, failed)
|
|
113
|
+
@lock.synchronize { add_row(MinuteRows.minute, *BACKGROUND, first, second, one) }
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The request (or job) finished, and Server speed recorded it under
|
|
118
|
+
# method and route: its timings go into this minute's rows.
|
|
119
|
+
def finish(method, route)
|
|
120
|
+
mine = Thread.current[self.class::REQUEST]
|
|
121
|
+
Thread.current[self.class::REQUEST] = nil
|
|
122
|
+
return if mine.nil? || mine.empty? || !on?
|
|
123
|
+
|
|
124
|
+
minute = MinuteRows.minute
|
|
125
|
+
@lock.synchronize do
|
|
126
|
+
mine.each { |(first, second), agg| add_row(minute, method, route, first, second, agg) }
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Under @lock.
|
|
131
|
+
def add_row(minute, method, route, first, second, agg)
|
|
132
|
+
keys = @rows[minute]
|
|
133
|
+
unless keys
|
|
134
|
+
@rows.delete(@rows.keys.min) while @rows.size >= MAX_MINUTES
|
|
135
|
+
keys = @rows[minute] = {}
|
|
136
|
+
end
|
|
137
|
+
key = [method, route, first, second]
|
|
138
|
+
key = [method, route, first, OTHER] if !keys.key?(key) && keys.size >= MAX_KEYS
|
|
139
|
+
return if !keys.key?(key) && keys.size >= HARD_MAX_KEYS
|
|
140
|
+
|
|
141
|
+
MinuteRows.merge(keys[key] ||= MinuteRows.blank, agg)
|
|
142
|
+
end
|
|
143
|
+
private :add_row
|
|
144
|
+
|
|
145
|
+
def flush(everything: true)
|
|
146
|
+
current = MinuteRows.minute
|
|
147
|
+
ready = @lock.synchronize do
|
|
148
|
+
taken = @rows.select { |minute, _| everything || minute < current }
|
|
149
|
+
taken.each_key { |minute| @rows.delete(minute) }
|
|
150
|
+
taken
|
|
151
|
+
end
|
|
152
|
+
first_name, second_name = self.class::FIELDS
|
|
153
|
+
rows = ready.flat_map do |minute, keys|
|
|
154
|
+
at = Time.at(minute).utc.iso8601(3)
|
|
155
|
+
keys.map do |(method, route, first, second), v|
|
|
156
|
+
{ "minute" => at, "method" => method, "route" => route, first_name => first, second_name => second,
|
|
157
|
+
"count" => v[:count], "errors" => v[:errors], "sum_ms" => v[:sum_ms].round(1),
|
|
158
|
+
"max_ms" => v[:max_ms].round(1), "buckets" => v[:buckets] }
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
rows.each_slice(MAX_ROWS) do |batch|
|
|
162
|
+
@core.request(self.class::MOD, self.class::PATH, auth: :bearer,
|
|
163
|
+
body: { "site_id" => @core.site_id, "server_name" => @server_name,
|
|
164
|
+
"runtime" => RUNTIME, "rows" => batch })
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def stop_rows
|
|
169
|
+
@job&.stop
|
|
170
|
+
Forking.unregister(self)
|
|
171
|
+
flush
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# The instances a class's hooks report to (Queries, Calls): the newest
|
|
176
|
+
# platform's that is still open. The class sets @open and @lock.
|
|
177
|
+
module OpenList
|
|
178
|
+
def active
|
|
179
|
+
@lock.synchronize { @open.last }
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def opened(item)
|
|
183
|
+
@lock.synchronize { @open << item }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def closed(item)
|
|
187
|
+
@lock.synchronize { @open.delete(item) }
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
class Queries
|
|
36
192
|
# The request's own queries, fiber-local beside Context's fields: Context.with
|
|
37
193
|
# starts each request or job with none.
|
|
38
194
|
REQUEST = Context::QUERIES
|
|
39
195
|
# Set while an outer layer times a query, so an inner one leaves it.
|
|
40
196
|
TIMING = :sitegoose_query_timing
|
|
197
|
+
# Set while the package makes Redis commands of its own (the queue
|
|
198
|
+
# readings), which aren't timed.
|
|
199
|
+
OWN = :sitegoose_own_redis
|
|
41
200
|
CACHE_MAX = 2000
|
|
42
201
|
CACHE_TEXT = 4000
|
|
43
202
|
|
|
@@ -45,21 +204,9 @@ module SiteGoose
|
|
|
45
204
|
@lock = Mutex.new
|
|
46
205
|
@cache = {}
|
|
47
206
|
@hooked = {}
|
|
207
|
+
extend OpenList
|
|
48
208
|
|
|
49
209
|
class << self
|
|
50
|
-
# The platform's Queries that hooks report to: the newest still open.
|
|
51
|
-
def active
|
|
52
|
-
@lock.synchronize { @open.last }
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def opened(queries)
|
|
56
|
-
@lock.synchronize { @open << queries }
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def closed(queries)
|
|
60
|
-
@lock.synchronize { @open.delete(queries) }
|
|
61
|
-
end
|
|
62
|
-
|
|
63
210
|
# The statement as stored ("" to leave it out); the same query text
|
|
64
211
|
# comes round again and again, so the answer is kept.
|
|
65
212
|
def statement(text, db)
|
|
@@ -90,6 +237,29 @@ module SiteGoose
|
|
|
90
237
|
Thread.current[TIMING] = saved
|
|
91
238
|
end
|
|
92
239
|
|
|
240
|
+
def own?
|
|
241
|
+
Thread.current[OWN] ? true : false
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Whether a Redis command now goes untimed: the package's own, no
|
|
245
|
+
# platform timing queries, or no answer to that (a signal handler
|
|
246
|
+
# can't take the lock), which never reaches the app's command.
|
|
247
|
+
def redis_untimed?
|
|
248
|
+
own? || active.nil?
|
|
249
|
+
rescue StandardError
|
|
250
|
+
true
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Runs the block with this thread's Redis commands left out: the
|
|
254
|
+
# package's own.
|
|
255
|
+
def own
|
|
256
|
+
saved = Thread.current[OWN]
|
|
257
|
+
Thread.current[OWN] = true
|
|
258
|
+
yield
|
|
259
|
+
ensure
|
|
260
|
+
Thread.current[OWN] = saved
|
|
261
|
+
end
|
|
262
|
+
|
|
93
263
|
# One finished query, from any hook. db: one of QueryText::DATABASES.
|
|
94
264
|
def record(db, text, ms, failed: false)
|
|
95
265
|
active&.record(db, text, ms, failed: failed)
|
|
@@ -97,33 +267,102 @@ module SiteGoose
|
|
|
97
267
|
nil
|
|
98
268
|
end
|
|
99
269
|
|
|
100
|
-
|
|
101
|
-
|
|
270
|
+
# ---- Hooks: installed once per process, for every platform ----
|
|
271
|
+
|
|
272
|
+
def install
|
|
273
|
+
hook_active_record
|
|
274
|
+
hook_mongo
|
|
275
|
+
hook_redis
|
|
102
276
|
end
|
|
103
277
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
278
|
+
# redis-client's global middleware, registered once per process. The
|
|
279
|
+
# redis gem 4 has its own connection code, which this doesn't see.
|
|
280
|
+
def hook_redis
|
|
281
|
+
return if @hooked[:redis] || !defined?(::RedisClient) || !::RedisClient.respond_to?(:register)
|
|
282
|
+
|
|
283
|
+
@hooked[:redis] = true
|
|
284
|
+
return if defined?(::RedisClient::Middlewares) && ::RedisClient::Middlewares.include?(RedisHook)
|
|
285
|
+
|
|
286
|
+
::RedisClient.register(RedisHook)
|
|
287
|
+
rescue StandardError
|
|
288
|
+
nil
|
|
111
289
|
end
|
|
112
290
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
291
|
+
# The connection's own set-up, which redis-client sends as a pipeline
|
|
292
|
+
# when it connects.
|
|
293
|
+
PRELUDE = %w[HELLO AUTH SELECT CLIENT ROLE READONLY].to_h { |name| [name, true] }.freeze
|
|
294
|
+
|
|
295
|
+
# A pipeline as one call: "PIPELINE <first command timed>", or "MULTI
|
|
296
|
+
# <first command>" for a transaction (MULTI ... EXEC); nil when none
|
|
297
|
+
# of its commands is timed, or it is the connection's set-up.
|
|
298
|
+
def redis_pipeline(commands)
|
|
299
|
+
return nil if !commands.is_a?(Array) || commands.empty?
|
|
300
|
+
|
|
301
|
+
if redis_name(commands[0]) == "MULTI"
|
|
302
|
+
prefix = "MULTI"
|
|
303
|
+
ends = redis_name(commands[-1])
|
|
304
|
+
last = ends == "EXEC" || ends == "DISCARD" ? -2 : -1
|
|
305
|
+
inside = commands[1..last]
|
|
306
|
+
else
|
|
307
|
+
# Stops at the first command that isn't set-up: an app's pipeline
|
|
308
|
+
# of thousands isn't read twice.
|
|
309
|
+
return nil if commands.all? { |command| PRELUDE.key?(redis_name(command)) }
|
|
310
|
+
|
|
311
|
+
prefix = "PIPELINE"
|
|
312
|
+
inside = commands
|
|
313
|
+
end
|
|
314
|
+
inside.each do |command|
|
|
315
|
+
first = QueryText.redis_command(command)
|
|
316
|
+
return "#{prefix} #{first}" if first
|
|
317
|
+
end
|
|
318
|
+
nil
|
|
120
319
|
end
|
|
121
320
|
|
|
122
|
-
|
|
321
|
+
def redis_name(command)
|
|
322
|
+
command.is_a?(Array) ? QueryText.redis_word(command[0]) : nil
|
|
323
|
+
end
|
|
123
324
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
325
|
+
# Runs a Redis command (or pipeline) and records it: failed when it
|
|
326
|
+
# raised (an error reply, no answer) or, for a pipeline, answered an
|
|
327
|
+
# error (sent with exception: false, or inside a transaction's EXEC,
|
|
328
|
+
# which redis-client raises only after the middleware). A single
|
|
329
|
+
# command's error reply always raises, so its answer isn't looked at.
|
|
330
|
+
# MOVED and ASK are redirections: the cluster client sends the command
|
|
331
|
+
# again (through this middleware too), so the redirected try isn't
|
|
332
|
+
# recorded, only the one that answers.
|
|
333
|
+
def time_redis(statement, pipelined: false)
|
|
334
|
+
started = SiteGoose.clock
|
|
335
|
+
failed = true
|
|
336
|
+
redirected = false
|
|
337
|
+
begin
|
|
338
|
+
result = yield
|
|
339
|
+
failed = pipelined ? redis_errors?(result, statement.start_with?("MULTI ")) : false
|
|
340
|
+
result
|
|
341
|
+
rescue StandardError => e
|
|
342
|
+
failed = redis_error?(e)
|
|
343
|
+
redirected = !failed
|
|
344
|
+
raise
|
|
345
|
+
ensure
|
|
346
|
+
record("redis", statement, (SiteGoose.clock - started) * 1000, failed: failed) unless redirected
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
# A pipeline's answers: an error among them, or in a transaction's
|
|
351
|
+
# EXEC answer (its last).
|
|
352
|
+
def redis_errors?(results, multi)
|
|
353
|
+
return false unless results.is_a?(Array)
|
|
354
|
+
return true if results.any? { |item| redis_error?(item) }
|
|
355
|
+
|
|
356
|
+
multi && results.last.is_a?(Array) && results.last.any? { |item| redis_error?(item) }
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def redis_error?(value)
|
|
360
|
+
return false unless value.is_a?(StandardError)
|
|
361
|
+
|
|
362
|
+
message = value.message.to_s
|
|
363
|
+
!(message.start_with?("MOVED ") || message.start_with?("ASK "))
|
|
364
|
+
rescue StandardError
|
|
365
|
+
true
|
|
127
366
|
end
|
|
128
367
|
|
|
129
368
|
# Active Record's sql.active_record notification (Rails, or Active
|
|
@@ -165,6 +404,7 @@ module SiteGoose
|
|
|
165
404
|
when /sqlite/ then "sqlite"
|
|
166
405
|
when /sqlserver|mssql/ then "mssql"
|
|
167
406
|
when /oracle/ then "oracle"
|
|
407
|
+
when /clickhouse/ then "clickhouse"
|
|
168
408
|
else "other"
|
|
169
409
|
end
|
|
170
410
|
end
|
|
@@ -186,6 +426,7 @@ module SiteGoose
|
|
|
186
426
|
when "sqlite" then "sqlite"
|
|
187
427
|
when "mssql" then "mssql"
|
|
188
428
|
when "oracle" then "oracle"
|
|
429
|
+
when "clickhouse" then "clickhouse"
|
|
189
430
|
else "other"
|
|
190
431
|
end
|
|
191
432
|
end
|
|
@@ -196,6 +437,95 @@ module SiteGoose
|
|
|
196
437
|
database.extend(SequelHook) unless database.singleton_class.include?(SequelHook)
|
|
197
438
|
database
|
|
198
439
|
end
|
|
440
|
+
|
|
441
|
+
# Times one click_house gem connection's public query methods. Only the
|
|
442
|
+
# methods the object has are wrapped, on that object alone.
|
|
443
|
+
def watch_clickhouse(connection)
|
|
444
|
+
return connection if connection.singleton_class.include?(ClickHouseHook)
|
|
445
|
+
|
|
446
|
+
wrapped = ClickHouseHook::METHODS.select { |name| connection.respond_to?(name) }
|
|
447
|
+
hook = Module.new do
|
|
448
|
+
include ClickHouseHook
|
|
449
|
+
|
|
450
|
+
wrapped.each do |name|
|
|
451
|
+
define_method(name) do |*args, **options, &block|
|
|
452
|
+
text = ClickHouseHook.text_of(name, args)
|
|
453
|
+
return super(*args, **options, &block) if text.nil? || Queries.timing? || Queries.active.nil?
|
|
454
|
+
|
|
455
|
+
Queries.time("clickhouse", text) { super(*args, **options, &block) }
|
|
456
|
+
end
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
connection.extend(hook)
|
|
460
|
+
connection
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
# Runs the block as one timed query, recorded when it ends (failed
|
|
464
|
+
# when it raised); queries the block runs underneath aren't timed again,
|
|
465
|
+
# and neither are its HTTP requests (Calls).
|
|
466
|
+
def time(db, text)
|
|
467
|
+
started = SiteGoose.clock
|
|
468
|
+
failed = false
|
|
469
|
+
begin
|
|
470
|
+
timing { yield }
|
|
471
|
+
rescue StandardError
|
|
472
|
+
failed = true
|
|
473
|
+
raise
|
|
474
|
+
ensure
|
|
475
|
+
record(db, text, (SiteGoose.clock - started) * 1000, failed: failed)
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
# The click_house gem (ClickHouse::Connection): select_all, select_one,
|
|
481
|
+
# select_value and execute carry the SQL; insert names its table, and
|
|
482
|
+
# its rows are never read. The gem answers once the whole result is
|
|
483
|
+
# read, so the time is the query's to the end.
|
|
484
|
+
module ClickHouseHook
|
|
485
|
+
METHODS = %i[select_all select_one select_value execute insert].freeze
|
|
486
|
+
|
|
487
|
+
def self.text_of(name, args)
|
|
488
|
+
first = args[0]
|
|
489
|
+
case name
|
|
490
|
+
when :insert then first.nil? ? nil : "INSERT INTO #{first}"
|
|
491
|
+
else first.is_a?(String) ? first : nil
|
|
492
|
+
end
|
|
493
|
+
rescue StandardError
|
|
494
|
+
# A table name that can't be written: the call runs untimed.
|
|
495
|
+
nil
|
|
496
|
+
end
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
# redis-client's middleware (RedisClient.register): every client's
|
|
500
|
+
# commands, the redis gem 5's and Sidekiq's too. Blocking commands
|
|
501
|
+
# (BLPOP, BRPOP: Sidekiq's fetch) aren't timed, nor the package's own
|
|
502
|
+
# (Queries.own). With no platform timing queries it only passes on.
|
|
503
|
+
module RedisHook
|
|
504
|
+
def call(command, redis_config)
|
|
505
|
+
return super if Queries.redis_untimed?
|
|
506
|
+
|
|
507
|
+
statement = begin
|
|
508
|
+
QueryText.redis_command(command)
|
|
509
|
+
rescue StandardError
|
|
510
|
+
nil
|
|
511
|
+
end
|
|
512
|
+
return super if statement.nil?
|
|
513
|
+
|
|
514
|
+
Queries.time_redis(statement) { super }
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
def call_pipelined(commands, redis_config)
|
|
518
|
+
return super if Queries.redis_untimed?
|
|
519
|
+
|
|
520
|
+
statement = begin
|
|
521
|
+
Queries.redis_pipeline(commands)
|
|
522
|
+
rescue StandardError
|
|
523
|
+
nil
|
|
524
|
+
end
|
|
525
|
+
return super if statement.nil?
|
|
526
|
+
|
|
527
|
+
Queries.time_redis(statement, pipelined: true) { super }
|
|
528
|
+
end
|
|
199
529
|
end
|
|
200
530
|
|
|
201
531
|
# Extends one Sequel::Database (Sequel's own extensions wrap the same
|
|
@@ -255,7 +585,7 @@ module SiteGoose
|
|
|
255
585
|
|
|
256
586
|
text = MongoSubscriber.text(name, event.command)
|
|
257
587
|
@lock.synchronize do
|
|
258
|
-
@waiting.
|
|
588
|
+
@waiting.shift while @waiting.size >= WAITING_MAX
|
|
259
589
|
@waiting[event.request_id] = text
|
|
260
590
|
end
|
|
261
591
|
rescue StandardError
|
|
@@ -278,29 +608,17 @@ module SiteGoose
|
|
|
278
608
|
end
|
|
279
609
|
end
|
|
280
610
|
|
|
281
|
-
|
|
611
|
+
include MinuteRows
|
|
612
|
+
FIELDS = %w[db statement].freeze
|
|
613
|
+
PATH = "/events/server-queries"
|
|
614
|
+
MOD = "queries"
|
|
282
615
|
|
|
283
616
|
def initialize(core, server_name: nil)
|
|
284
|
-
|
|
285
|
-
@server_name = SiteGoose.text(server_name || SiteGoose.hostname, 100)
|
|
286
|
-
@rows = {}
|
|
287
|
-
@lock = Mutex.new
|
|
288
|
-
@job = core.valid? ? Periodic.new(FLUSH_EVERY) { flush(everything: false); FLUSH_EVERY } : nil
|
|
289
|
-
Forking.register(self)
|
|
617
|
+
start_rows(core, server_name)
|
|
290
618
|
Queries.opened(self)
|
|
291
619
|
Queries.install
|
|
292
620
|
end
|
|
293
621
|
|
|
294
|
-
# In a forked worker the parent's rows are the parent's to send.
|
|
295
|
-
def after_fork_in_child
|
|
296
|
-
@lock = Mutex.new
|
|
297
|
-
@rows = {}
|
|
298
|
-
end
|
|
299
|
-
|
|
300
|
-
def on?
|
|
301
|
-
@core.on?("performance")
|
|
302
|
-
end
|
|
303
|
-
|
|
304
622
|
# One finished query: into its request's (or job's) queries, or, with
|
|
305
623
|
# none around it, straight into the minute's rows as background.
|
|
306
624
|
def record(db, text, ms, failed: false)
|
|
@@ -310,89 +628,27 @@ module SiteGoose
|
|
|
310
628
|
statement = Queries.statement(text, db)
|
|
311
629
|
return if statement.empty?
|
|
312
630
|
|
|
313
|
-
|
|
314
|
-
ms = 0.0 if ms.negative? || ms.nan?
|
|
315
|
-
if Context.request
|
|
316
|
-
mine = (Thread.current[REQUEST] ||= {})
|
|
317
|
-
key = [db, statement]
|
|
318
|
-
key = [db, OTHER] if !mine.key?(key) && mine.size >= MAX_PER_REQUEST
|
|
319
|
-
Queries.add(mine[key] ||= Queries.blank, ms, failed)
|
|
320
|
-
else
|
|
321
|
-
one = Queries.add(Queries.blank, ms, failed)
|
|
322
|
-
@lock.synchronize { add_row(Queries.minute, *BACKGROUND, db, statement, one) }
|
|
323
|
-
end
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
def self.minute
|
|
327
|
-
(Time.now.to_i / 60) * 60
|
|
328
|
-
end
|
|
329
|
-
|
|
330
|
-
# The request (or job) finished, and Server speed recorded it under
|
|
331
|
-
# method and route: its queries go into this minute's rows.
|
|
332
|
-
def finish(method, route)
|
|
333
|
-
mine = Thread.current[REQUEST]
|
|
334
|
-
Thread.current[REQUEST] = nil
|
|
335
|
-
return if mine.nil? || mine.empty? || !on?
|
|
336
|
-
|
|
337
|
-
minute = Queries.minute
|
|
338
|
-
@lock.synchronize do
|
|
339
|
-
mine.each { |(db, statement), agg| add_row(minute, method, route, db, statement, agg) }
|
|
340
|
-
end
|
|
341
|
-
end
|
|
342
|
-
|
|
343
|
-
# Under @lock.
|
|
344
|
-
def add_row(minute, method, route, db, statement, agg)
|
|
345
|
-
keys = @rows[minute]
|
|
346
|
-
unless keys
|
|
347
|
-
@rows.delete(@rows.keys.min) while @rows.size >= MAX_MINUTES
|
|
348
|
-
keys = @rows[minute] = {}
|
|
349
|
-
end
|
|
350
|
-
key = [method, route, db, statement]
|
|
351
|
-
key = [method, route, db, OTHER] if !keys.key?(key) && keys.size >= MAX_KEYS
|
|
352
|
-
return if !keys.key?(key) && keys.size >= HARD_MAX_KEYS
|
|
353
|
-
|
|
354
|
-
Queries.merge(keys[key] ||= Queries.blank, agg)
|
|
355
|
-
end
|
|
356
|
-
private :add_row
|
|
357
|
-
|
|
358
|
-
def flush(everything: true)
|
|
359
|
-
current = Queries.minute
|
|
360
|
-
ready = @lock.synchronize do
|
|
361
|
-
taken = @rows.select { |minute, _| everything || minute < current }
|
|
362
|
-
taken.each_key { |minute| @rows.delete(minute) }
|
|
363
|
-
taken
|
|
364
|
-
end
|
|
365
|
-
rows = ready.flat_map do |minute, keys|
|
|
366
|
-
at = Time.at(minute).utc.iso8601(3)
|
|
367
|
-
keys.map do |(method, route, db, statement), v|
|
|
368
|
-
{ "minute" => at, "method" => method, "route" => route, "db" => db, "statement" => statement,
|
|
369
|
-
"count" => v[:count], "errors" => v[:errors], "sum_ms" => v[:sum_ms].round(1),
|
|
370
|
-
"max_ms" => v[:max_ms].round(1), "buckets" => v[:buckets] }
|
|
371
|
-
end
|
|
372
|
-
end
|
|
373
|
-
rows.each_slice(MAX_ROWS) do |batch|
|
|
374
|
-
@core.request("queries", "/events/server-queries", auth: :bearer,
|
|
375
|
-
body: { "site_id" => @core.site_id, "server_name" => @server_name,
|
|
376
|
-
"runtime" => RUNTIME, "rows" => batch })
|
|
377
|
-
end
|
|
631
|
+
file(db, statement, ms, failed)
|
|
378
632
|
end
|
|
379
633
|
|
|
380
634
|
def close
|
|
381
|
-
@job&.stop
|
|
382
|
-
Forking.unregister(self)
|
|
383
635
|
Queries.closed(self)
|
|
384
|
-
|
|
636
|
+
stop_rows
|
|
385
637
|
end
|
|
386
638
|
end
|
|
387
639
|
|
|
388
|
-
# Times every query this
|
|
389
|
-
# Queries): SiteGoose.watch_database(DB)
|
|
390
|
-
#
|
|
640
|
+
# Times every query this database object runs (Health -> Server speed ->
|
|
641
|
+
# Queries): SiteGoose.watch_database(DB) for a Sequel::Database or a
|
|
642
|
+
# click_house gem ClickHouse::Connection. Rails' Active Record and the
|
|
643
|
+
# mongo gem are caught on their own.
|
|
391
644
|
def self.watch_database(database)
|
|
392
645
|
if defined?(::Sequel::Database) && database.is_a?(::Sequel::Database)
|
|
393
646
|
Queries.watch_sequel(database)
|
|
647
|
+
elsif defined?(::ClickHouse::Connection) && database.is_a?(::ClickHouse::Connection)
|
|
648
|
+
Queries.watch_clickhouse(database)
|
|
394
649
|
else
|
|
395
|
-
warn "[analytics-platform] watch_database: #{database.class} is not a Sequel::Database
|
|
650
|
+
warn "[analytics-platform] watch_database: #{database.class} is not a Sequel::Database or a ClickHouse::Connection; " \
|
|
651
|
+
"Active Record and Mongo are timed on their own"
|
|
396
652
|
end
|
|
397
653
|
database
|
|
398
654
|
end
|