pliny 0.20.1 → 0.20.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pliny.rb +1 -0
- data/lib/pliny/middleware/metrics.rb +28 -0
- data/lib/pliny/version.rb +1 -1
- data/lib/template/lib/routes.rb +1 -0
- data/spec/metrics_spec.rb +4 -0
- data/spec/middleware/metrics_spec.rb +65 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11d6e6303689db3c75d06599309d8044f8e24058
|
4
|
+
data.tar.gz: ad4702309f3488e7bf99654a732c1e03f4685989
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5f26f1cdb8f55a373917e750fcae6866e4dbaf091fcd98f3876cfb6f7249359bde685aa523ca1df156ec0b57b78686d7ce6de053b8116e7209d889ddddf4cb6
|
7
|
+
data.tar.gz: d6e5c047b08ff89ed6c1f6e3fe1246877886ddd2ea491a3effc24e157ee23ca011d83f9eaea67d53e94711c8a7e21ab331bc182f032f1650ae5997e79dcd431d
|
data/lib/pliny.rb
CHANGED
@@ -16,6 +16,7 @@ require_relative "pliny/router"
|
|
16
16
|
require_relative "pliny/utils"
|
17
17
|
require_relative "pliny/middleware/cors"
|
18
18
|
require_relative "pliny/middleware/instruments"
|
19
|
+
require_relative "pliny/middleware/metrics"
|
19
20
|
require_relative "pliny/middleware/request_id"
|
20
21
|
require_relative "pliny/middleware/request_store/clear"
|
21
22
|
require_relative "pliny/middleware/request_store/seed"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Pliny::Middleware
|
2
|
+
class Metrics
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
start = Time.now
|
9
|
+
|
10
|
+
Pliny::Metrics.count("requests")
|
11
|
+
|
12
|
+
begin
|
13
|
+
status, headers, body = @app.call(env)
|
14
|
+
rescue
|
15
|
+
status = 500
|
16
|
+
raise
|
17
|
+
ensure
|
18
|
+
elapsed = (Time.now - start).to_f
|
19
|
+
Pliny::Metrics.measure("requests.latency", value: elapsed)
|
20
|
+
|
21
|
+
status_level = "#{status/100}xx"
|
22
|
+
Pliny::Metrics.count("requests.status.#{status_level}")
|
23
|
+
end
|
24
|
+
|
25
|
+
[status, headers, body]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/pliny/version.rb
CHANGED
data/lib/template/lib/routes.rb
CHANGED
@@ -3,6 +3,7 @@ Routes = Rack::Builder.new do
|
|
3
3
|
use Pliny::Middleware::CORS
|
4
4
|
use Pliny::Middleware::RequestID
|
5
5
|
use Pliny::Middleware::RequestStore::Seed, store: Pliny::RequestStore
|
6
|
+
use Pliny::Middleware::Metrics
|
6
7
|
use Pliny::Middleware::Instruments
|
7
8
|
use Pliny::Middleware::RescueErrors, raise: Config.raise_errors?
|
8
9
|
use Rack::Timeout,
|
data/spec/metrics_spec.rb
CHANGED
@@ -52,6 +52,10 @@ describe Pliny::Metrics do
|
|
52
52
|
Pliny::Metrics.backends = [test_backend]
|
53
53
|
end
|
54
54
|
|
55
|
+
after do
|
56
|
+
Timecop.return
|
57
|
+
end
|
58
|
+
|
55
59
|
it "measures a block's execution time with a single key" do
|
56
60
|
metrics.measure(:foo) { }
|
57
61
|
expect(test_backend).to have_received(:report_measures).once.with(
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pliny::Middleware::Instruments do
|
4
|
+
def app
|
5
|
+
Rack::Builder.new do
|
6
|
+
run Sinatra.new {
|
7
|
+
use Pliny::Middleware::Metrics
|
8
|
+
|
9
|
+
get "/hello" do
|
10
|
+
Timecop.travel(5)
|
11
|
+
status 200
|
12
|
+
"hi"
|
13
|
+
end
|
14
|
+
|
15
|
+
get "/error" do
|
16
|
+
status 503
|
17
|
+
"boom"
|
18
|
+
end
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
before do
|
24
|
+
Timecop.freeze(Time.now)
|
25
|
+
allow(Pliny::Metrics).to receive(:count)
|
26
|
+
allow(Pliny::Metrics).to receive(:measure)
|
27
|
+
end
|
28
|
+
|
29
|
+
after do
|
30
|
+
Timecop.return
|
31
|
+
end
|
32
|
+
|
33
|
+
it "counts total requests" do
|
34
|
+
expect(Pliny::Metrics).to receive(:count).with("requests").once
|
35
|
+
|
36
|
+
get "/hello"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "counts 2xx statuses" do
|
40
|
+
expect(Pliny::Metrics).to receive(:count).with("requests.status.2xx").once
|
41
|
+
|
42
|
+
get "/hello"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "counts 4xx statuses" do
|
46
|
+
expect(Pliny::Metrics).to receive(:count).with("requests.status.4xx").once
|
47
|
+
|
48
|
+
get "/not-found"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "counts 5xx statuses" do
|
52
|
+
expect(Pliny::Metrics).to receive(:count).with("requests.status.5xx").once
|
53
|
+
|
54
|
+
get "/error"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "measures the request latency" do
|
58
|
+
expect(Pliny::Metrics).to receive(:measure) do |key, opts|
|
59
|
+
assert_equal(key, "requests.latency")
|
60
|
+
assert(4 < opts[:value] && opts[:value] < 6)
|
61
|
+
end
|
62
|
+
|
63
|
+
get "/hello"
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pliny
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.20.
|
4
|
+
version: 0.20.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur Leach
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-12-
|
12
|
+
date: 2016-12-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -365,6 +365,7 @@ files:
|
|
365
365
|
- lib/pliny/metrics/backends/logger.rb
|
366
366
|
- lib/pliny/middleware/cors.rb
|
367
367
|
- lib/pliny/middleware/instruments.rb
|
368
|
+
- lib/pliny/middleware/metrics.rb
|
368
369
|
- lib/pliny/middleware/request_id.rb
|
369
370
|
- lib/pliny/middleware/request_store/clear.rb
|
370
371
|
- lib/pliny/middleware/request_store/seed.rb
|
@@ -454,6 +455,7 @@ files:
|
|
454
455
|
- spec/metrics_spec.rb
|
455
456
|
- spec/middleware/cors_spec.rb
|
456
457
|
- spec/middleware/instruments_spec.rb
|
458
|
+
- spec/middleware/metrics_spec.rb
|
457
459
|
- spec/middleware/request_id_spec.rb
|
458
460
|
- spec/middleware/request_store/clear_spec.rb
|
459
461
|
- spec/middleware/request_store/seed_spec.rb
|