-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ru
85 lines (64 loc) · 1.71 KB
/
config.ru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "puma"
require "libhoney"
require "time"
require "shed"
LOAD_SHED = ENV["LOAD_SHED"] == "1"
class HoneyMiddleware
def initialize(app)
@app = app
at_exit { libhoney.close(true) }
end
def call(env)
start = current
request_start = env["HTTP_X_REQUEST_START"].to_i
params = Rack::Utils.parse_nested_query(env[Rack::QUERY_STRING])
label = params.fetch("label", "unknown")
response = @app.call(env)
record(label, request_start, start, current, response[0])
response
end
private
def record(label, request_start, app_start, app_finish, status)
event = libhoney.event
event.add(
"label" => label,
"load_shedding_enabled" => LOAD_SHED,
"response.queue_time" => app_start - request_start,
"response.serve_time" => app_finish - app_start,
"response.total_time" => app_finish - request_start,
"response.status" => status
)
event.timestamp = Time.at(request_start / 1000.0)
event.send
end
def libhoney
@libhoney ||= Libhoney::Client.new(
writekey: ENV["HONEYCOMB_WRITE_KEY"],
dataset: "cpuspin-ruby"
)
end
def current
(Time.now.to_f * 1_000).to_i
end
end
def spin(ms)
now = current
while (current - now) < ms
Shed.ensure_time_left!
end
end
def current
(Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1_000).to_i
end
def spin?(percent)
percent >= (rand(1..100))
end
use HoneyMiddleware
use Shed::RackMiddleware::Propagate, delta: Shed::HerokuDelta if LOAD_SHED
run ->(env) do
percent = Rack::Utils.parse_nested_query(env[Rack::QUERY_STRING]).fetch("percent", "0").to_i
spin_ms = 100
spin_ms = 1_000 if !percent.zero? && spin?(percent)
spin(spin_ms)
[200, {}, []]
end