leafy-rack 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +155 -0
- data/Rakefile +14 -0
- data/leafy-rack.gemspec +29 -0
- data/lib/leafy/instrumented/basic_instrumented.rb +38 -0
- data/lib/leafy/instrumented/instrumented.rb +27 -0
- data/lib/leafy/json/health_writer.rb +15 -0
- data/lib/leafy/json/json_writer.rb +33 -0
- data/lib/leafy/json/metrics_writer.rb +22 -0
- data/lib/leafy/rack/admin.rb +72 -0
- data/lib/leafy/rack/health.rb +36 -0
- data/lib/leafy/rack/instrumented.rb +20 -0
- data/lib/leafy/rack/metrics.rb +35 -0
- data/lib/leafy/rack/ping.rb +29 -0
- data/lib/leafy/rack/thread_dump.rb +56 -0
- data/lib/leafy/rack/version.rb +6 -0
- data/lib/leafy/rack.rb +1 -0
- data/lib/leafy-rack.rb +1 -0
- data/lib/leafy-rack_jars.rb +10 -0
- data/spec/admin_rack_spec.rb +141 -0
- data/spec/basic_instrumented_spec.rb +31 -0
- data/spec/health_rack_spec.rb +136 -0
- data/spec/health_writer_spec.rb +47 -0
- data/spec/instrumented_rack_spec.rb +37 -0
- data/spec/instrumented_spec.rb +36 -0
- data/spec/metrics_rack_spec.rb +110 -0
- data/spec/metrics_writer_spec.rb +55 -0
- data/spec/ping_rack_spec.rb +63 -0
- data/spec/setup.rb +3 -0
- data/spec/thread_dump_spec.rb +59 -0
- metadata +163 -0
@@ -0,0 +1,141 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/rack/admin'
|
4
|
+
require 'yaml'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe Leafy::Rack::Admin do
|
8
|
+
|
9
|
+
subject { Leafy::Rack::Admin }
|
10
|
+
|
11
|
+
let( :expected_headers ) do
|
12
|
+
{ 'Content-Type' => 'text/html' }
|
13
|
+
end
|
14
|
+
|
15
|
+
let( :metrics ) { Leafy::Metrics::Registry.new }
|
16
|
+
|
17
|
+
let( :health ) { Leafy::Health::Registry.new }
|
18
|
+
|
19
|
+
let( :health_report ) do
|
20
|
+
{ 'version' => '3.0.0',
|
21
|
+
'gauges' => {},
|
22
|
+
'counters' => {},
|
23
|
+
'histograms' => {},
|
24
|
+
'meters' => {},
|
25
|
+
'timers' => {}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
let( :result ){ [ 200, nil, [] ] }
|
30
|
+
let( :app ) do
|
31
|
+
Proc.new() { result }
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has response' do
|
35
|
+
status, headers, body = subject.response( "/path" )
|
36
|
+
expect( status ).to eq 200
|
37
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
38
|
+
expect( body.join.count("\n" ) ).to eq 15
|
39
|
+
expect( body.join.gsub( '/path/' ).collect { |f| f }.size ).to eq 6
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'default path' do
|
43
|
+
subject { Leafy::Rack::Admin.new( app, metrics, health ) }
|
44
|
+
|
45
|
+
it 'passes request if not matches the given path' do
|
46
|
+
env = { 'PATH_INFO'=> '/something' }
|
47
|
+
expect( subject.call( env ) ).to eq result
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'shows menu page on admin path' do
|
51
|
+
env = { 'PATH_INFO'=> '/admin' }
|
52
|
+
status, headers, body = subject.call( env )
|
53
|
+
expect( status ).to eq 200
|
54
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
55
|
+
expect( body.join.count("\n" ) ).to eq 15
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'pongs on ping path' do
|
59
|
+
env = { 'PATH_INFO'=> '/admin/ping' }
|
60
|
+
status, _, body = subject.call( env )
|
61
|
+
expect( status ).to eq 200
|
62
|
+
expect( body.join ).to eq 'pong'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'thread dump on threads path' do
|
66
|
+
env = { 'PATH_INFO'=> '/admin/threads' }
|
67
|
+
status, _, body = subject.call( env )
|
68
|
+
expect( status ).to eq 200
|
69
|
+
expect( body.join.count( "\n" ) ).to be > 100
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'reports metrics on metrics path' do
|
73
|
+
env = { 'PATH_INFO'=> '/admin/metrics' }
|
74
|
+
status, _, body = subject.call( env )
|
75
|
+
expect( status ).to eq 200
|
76
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
77
|
+
body = JSON.parse(body.join)
|
78
|
+
expect( body.to_yaml ).to eq health_report.to_yaml
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'reports health-checks on health path' do
|
82
|
+
env = { 'PATH_INFO'=> '/admin/health' }
|
83
|
+
status, _, body = subject.call( env )
|
84
|
+
expect( status ).to eq 200
|
85
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
86
|
+
body = JSON.parse(body.join)
|
87
|
+
expect( body.to_yaml ).to eq "--- {}\n"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'custom path' do
|
92
|
+
subject { Leafy::Rack::Admin.new( app, metrics, health, '/custom' ) }
|
93
|
+
|
94
|
+
it 'passes request if not matches the given path' do
|
95
|
+
env = { 'PATH_INFO'=> '/something' }
|
96
|
+
expect( subject.call( env ) ).to eq result
|
97
|
+
env = { 'PATH_INFO'=> '/ping' }
|
98
|
+
expect( subject.call( env ) ).to eq result
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'shows menu page on admin path' do
|
102
|
+
env = { 'PATH_INFO'=> '/custom' }
|
103
|
+
status, headers, body = subject.call( env )
|
104
|
+
expect( status ).to eq 200
|
105
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
106
|
+
expect( body.join.count("\n" ) ).to eq 15
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'pongs on ping path' do
|
110
|
+
env = { 'PATH_INFO'=> '/custom/ping' }
|
111
|
+
status, _, body = subject.call( env )
|
112
|
+
expect( status ).to eq 200
|
113
|
+
expect( body.join ).to eq 'pong'
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'thread dump on threads path' do
|
117
|
+
env = { 'PATH_INFO'=> '/custom/threads' }
|
118
|
+
status, _, body = subject.call( env )
|
119
|
+
expect( status ).to eq 200
|
120
|
+
expect( body.join.count( "\n" ) ).to be > 100
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'reports metrics on metrics path' do
|
124
|
+
env = { 'PATH_INFO'=> '/custom/metrics' }
|
125
|
+
status, _, body = subject.call( env )
|
126
|
+
expect( status ).to eq 200
|
127
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
128
|
+
body = JSON.parse(body.join)
|
129
|
+
expect( body.to_yaml ).to eq health_report.to_yaml
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'reports health-checks on health path' do
|
133
|
+
env = { 'PATH_INFO'=> '/custom/health' }
|
134
|
+
status, _, body = subject.call( env )
|
135
|
+
expect( status ).to eq 200
|
136
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
137
|
+
body = JSON.parse(body.join)
|
138
|
+
expect( body.to_yaml ).to eq "--- {}\n"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/instrumented/basic_instrumented'
|
4
|
+
|
5
|
+
describe Leafy::Instrumented::BasicInstrumented do
|
6
|
+
|
7
|
+
subject { Leafy::Instrumented::BasicInstrumented.new( registry, "name" ) }
|
8
|
+
|
9
|
+
let( :registry ) { Leafy::Metrics::Registry.new }
|
10
|
+
|
11
|
+
let( :app ) do
|
12
|
+
Proc.new() do
|
13
|
+
sleep 0.1
|
14
|
+
[ 200, nil, registry.metrics.counters.values.first.count ]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'collects metrics for a call' do
|
19
|
+
_, _, count = subject.call do
|
20
|
+
app.call
|
21
|
+
end
|
22
|
+
expect( count ).to eq 1
|
23
|
+
expect( registry.metrics.timers.keys ).to eq [ 'name.requests' ]
|
24
|
+
expect( registry.metrics.timers.values.first.mean_rate ).to be > 0.9
|
25
|
+
expect( registry.metrics.counters.keys ).to eq [ 'name.active_requests' ]
|
26
|
+
expect( registry.metrics.counters.values.collect { |a| a.count } ).to eq [ 0 ]
|
27
|
+
expect( registry.metrics.meters.keys ).to eq [ 'name.responseCodes.other' ]
|
28
|
+
expect( registry.metrics.meters.values.collect { |a| a.count } ).to eq [ 1 ]
|
29
|
+
expect( registry.metrics.meters.values.first.mean_rate ).to be > 5.0
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/rack/health'
|
4
|
+
require 'yaml'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe Leafy::Rack::Health do
|
8
|
+
|
9
|
+
subject { Leafy::Rack::Health }
|
10
|
+
|
11
|
+
let( :registry ) { Leafy::Health::Registry.new }
|
12
|
+
|
13
|
+
let( :expected_headers ) do
|
14
|
+
{ 'Content-Type' => 'application/json',
|
15
|
+
'Cache-Control' => 'must-revalidate,no-cache,no-store' }
|
16
|
+
end
|
17
|
+
|
18
|
+
let( :result ){ [ 200, nil, [] ] }
|
19
|
+
let( :app ) do
|
20
|
+
Proc.new() { result }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'healthy' do
|
24
|
+
|
25
|
+
let( :health ) do
|
26
|
+
m = registry
|
27
|
+
m.register( 'two' ) do |ctx|
|
28
|
+
ctx.healthy 'ok'
|
29
|
+
end
|
30
|
+
m.health
|
31
|
+
end
|
32
|
+
let( :report ) do
|
33
|
+
{ 'two' => { 'healthy' => true, 'message' => 'ok' } }
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'has response' do
|
37
|
+
status, headers, body = subject.response( health, {} )
|
38
|
+
expect( status ).to eq 200
|
39
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
40
|
+
body = JSON.parse( body.join )
|
41
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'has pretty response' do
|
45
|
+
status, headers, body = subject.response( health, { 'QUERY_STRING' => 'pretty' } )
|
46
|
+
expect( status ).to eq 200
|
47
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
48
|
+
expect( body.join.count( "\n" ) ).to eq 5
|
49
|
+
body = JSON.parse( body.join )
|
50
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'unhealthy' do
|
55
|
+
let( :health ) do
|
56
|
+
m = registry
|
57
|
+
m.register( 'one' ) do |ctx|
|
58
|
+
'error'
|
59
|
+
end
|
60
|
+
m.health
|
61
|
+
end
|
62
|
+
|
63
|
+
let( :report ) do
|
64
|
+
{ 'one' => { 'healthy' => false, 'message' => 'error' } }
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'has response' do
|
68
|
+
status, headers, body = subject.response( health, {} )
|
69
|
+
expect( status ).to eq 500
|
70
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
71
|
+
body = JSON.parse( body.join )
|
72
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'has pretty response' do
|
76
|
+
status, headers, body = subject.response( health, { 'QUERY_STRING' => 'pretty' } )
|
77
|
+
expect( status ).to eq 500
|
78
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
79
|
+
expect( body.join.count( "\n" ) ).to eq 5
|
80
|
+
body = JSON.parse( body.join )
|
81
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'default path' do
|
86
|
+
subject { Leafy::Rack::Health.new( app, registry ) }
|
87
|
+
|
88
|
+
it 'passes request if not matches the given path' do
|
89
|
+
env = { 'PATH_INFO'=> '/something' }
|
90
|
+
expect( subject.call( env ) ).to eq result
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'reports health-checks on health path' do
|
94
|
+
env = { 'PATH_INFO'=> '/health' }
|
95
|
+
status, headers, body = subject.call( env )
|
96
|
+
expect( status ).to eq 200
|
97
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
98
|
+
expect( body.join ).to eq '{}'
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'reports "pretty" health-checks on health path' do
|
102
|
+
env = { 'PATH_INFO'=> '/health', 'QUERY_STRING' => 'pretty' }
|
103
|
+
status, headers, body = subject.call( env )
|
104
|
+
expect( status ).to eq 200
|
105
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
106
|
+
expect( body.join ).to eq '{ }'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'custom path' do
|
111
|
+
subject { Leafy::Rack::Health.new( app, registry, '/custom' ) }
|
112
|
+
|
113
|
+
it 'passes request if not matches the given path' do
|
114
|
+
env = { 'PATH_INFO'=> '/something' }
|
115
|
+
expect( subject.call( env ) ).to eq result
|
116
|
+
env = { 'PATH_INFO'=> '/health' }
|
117
|
+
expect( subject.call( env ) ).to eq result
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'reports health-checks on health path' do
|
121
|
+
env = { 'PATH_INFO'=> '/custom' }
|
122
|
+
status, headers, body = subject.call( env )
|
123
|
+
expect( status ).to eq 200
|
124
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
125
|
+
expect( body.join ).to eq '{}'
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'reports "pretty" health-checks on health path' do
|
129
|
+
env = { 'PATH_INFO'=> '/custom', 'QUERY_STRING' => 'pretty' }
|
130
|
+
status, headers, body = subject.call( env )
|
131
|
+
expect( status ).to eq 200
|
132
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
133
|
+
expect( body.join ).to eq '{ }'
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/health'
|
4
|
+
require 'leafy/json/health_writer'
|
5
|
+
require 'yaml'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
describe Leafy::Json::HealthWriter do
|
9
|
+
|
10
|
+
subject { Leafy::Json::HealthWriter.new }
|
11
|
+
|
12
|
+
let( :health ) do
|
13
|
+
m = Leafy::Health::Registry.new
|
14
|
+
m.register( 'one' ) do
|
15
|
+
'error'
|
16
|
+
end
|
17
|
+
m.register( 'two' ) do |ctx|
|
18
|
+
ctx.healthy 'ok'
|
19
|
+
end
|
20
|
+
m.register( 'three' ) do |ctx|
|
21
|
+
ctx.unhealthy 'no ok'
|
22
|
+
end
|
23
|
+
m.health
|
24
|
+
end
|
25
|
+
|
26
|
+
let( :expected ) do
|
27
|
+
{
|
28
|
+
"one"=> {"healthy"=>false, "message"=>"error"},
|
29
|
+
"three"=>{"healthy"=>false, "message"=>"no ok"},
|
30
|
+
"two"=>{"healthy"=>true, "message"=>"ok"}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'serializes health-check data to json' do
|
35
|
+
data = subject.to_json( health.run_health_checks )
|
36
|
+
expect( data.count( "\n" ) ).to eq 0
|
37
|
+
data = JSON.parse( data )
|
38
|
+
expect( data.to_yaml ).to eq expected.to_yaml
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'serializes health-check data to json (pretty print)' do
|
42
|
+
data = subject.to_json( health.run_health_checks, true )
|
43
|
+
expect( data.count( "\n" ) ).to eq 13
|
44
|
+
data = JSON.parse( data )
|
45
|
+
expect( data.to_yaml ).to eq expected.to_yaml
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/rack/instrumented'
|
4
|
+
|
5
|
+
describe Leafy::Rack::Instrumented do
|
6
|
+
|
7
|
+
subject { Leafy::Rack::Instrumented.new( app, instrumented ) }
|
8
|
+
|
9
|
+
let( :instrumented ) { Leafy::Instrumented::Instrumented.new( registry, "name" ) }
|
10
|
+
|
11
|
+
let( :registry ) { Leafy::Metrics::Registry.new }
|
12
|
+
|
13
|
+
let( :status ) { (Random.rand * 500 + 100).to_i }
|
14
|
+
|
15
|
+
let( :random ) { Random.rand.to_s }
|
16
|
+
|
17
|
+
let( :app ) do
|
18
|
+
Proc.new do
|
19
|
+
[ status, {}, [ random ] ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "collects metrics for a call and pass result on" do
|
24
|
+
result, _, body = subject.call( {} )
|
25
|
+
expect( result ).to eq status
|
26
|
+
expect( body.join ).to eq random
|
27
|
+
|
28
|
+
expect( registry.metrics.timers.keys ).to eq [ 'name.requests' ]
|
29
|
+
expect( registry.metrics.timers.values.first.mean_rate ).to be > 0.09
|
30
|
+
|
31
|
+
expect( registry.metrics.counters.keys ).to eq [ 'name.active_requests' ]
|
32
|
+
expect( registry.metrics.counters.values.collect { |a| a.count } ).to eq [ 0 ]
|
33
|
+
|
34
|
+
expect( registry.metrics.meters.values.select{ |a| a.count == 0 }.size ).to eq 7
|
35
|
+
expect( registry.metrics.meters.values.select{ |a| a.count == 1 }.size ).to eq 1
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/instrumented/instrumented'
|
4
|
+
|
5
|
+
describe Leafy::Instrumented::Instrumented do
|
6
|
+
|
7
|
+
subject { Leafy::Instrumented::Instrumented.new( registry, "name" ) }
|
8
|
+
|
9
|
+
let( :registry ) { Leafy::Metrics::Registry.new }
|
10
|
+
|
11
|
+
[ 400, 201, 204, 404, 200, 123, 205, 500 ].each_with_index do |status, index|
|
12
|
+
it "collects metrics for a call for status #{status}" do
|
13
|
+
result, _, _ = subject.call do
|
14
|
+
sleep 0.01
|
15
|
+
[ status, nil, nil ]
|
16
|
+
end
|
17
|
+
|
18
|
+
expect( result ).to eq status
|
19
|
+
|
20
|
+
expect( registry.metrics.timers.keys ).to eq [ 'name.requests' ]
|
21
|
+
expect( registry.metrics.timers.values.first.mean_rate ).to be > 0.09
|
22
|
+
|
23
|
+
expect( registry.metrics.counters.keys ).to eq [ 'name.active_requests' ]
|
24
|
+
expect( registry.metrics.counters.values.collect { |a| a.count } ).to eq [ 0 ]
|
25
|
+
|
26
|
+
expect( registry.metrics.meters.keys.sort ).to eq [ 'name.responseCodes.badRequest', 'name.responseCodes.created', 'name.responseCodes.noContent', 'name.responseCodes.notFound', 'name.responseCodes.ok', 'name.responseCodes.other', 'name.responseCodes.resetContent', 'name.responseCodes.serverError' ]
|
27
|
+
|
28
|
+
stati = registry.metrics.meters.values.collect { |a| a.count }
|
29
|
+
expect( stati[ index ] ).to eq 1
|
30
|
+
stati.delete 1
|
31
|
+
expect( stati ).to eq [0,0,0,0,0,0,0]
|
32
|
+
|
33
|
+
expect( registry.metrics.meters.values.to_a[ index ].mean_rate ).to be > 50
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/rack/metrics'
|
4
|
+
require 'yaml'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe Leafy::Rack::Metrics do
|
8
|
+
|
9
|
+
subject { Leafy::Rack::Metrics }
|
10
|
+
|
11
|
+
let( :registry ) { Leafy::Metrics::Registry.new }
|
12
|
+
|
13
|
+
let( :expected_headers ) do
|
14
|
+
{ 'Content-Type' => 'application/json',
|
15
|
+
'Cache-Control' => 'must-revalidate,no-cache,no-store' }
|
16
|
+
end
|
17
|
+
let( :report ) do
|
18
|
+
{ 'version' => '3.0.0',
|
19
|
+
'gauges' => {},
|
20
|
+
'counters' => {},
|
21
|
+
'histograms' => {},
|
22
|
+
'meters' => {},
|
23
|
+
'timers' => {}
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
let( :result ){ [ 200, nil, [] ] }
|
28
|
+
let( :app ) do
|
29
|
+
Proc.new() { result }
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'has response' do
|
33
|
+
status, headers, body = subject.response( registry.metrics, {} )
|
34
|
+
expect( status ).to eq 200
|
35
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
36
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
37
|
+
body = JSON.parse( body.join )
|
38
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'has pretty response' do
|
42
|
+
status, headers, body = subject.response( registry.metrics,
|
43
|
+
{ 'QUERY_STRING' => 'pretty' } )
|
44
|
+
expect( status ).to eq 200
|
45
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
46
|
+
expect( body.join.count( "\n" ) ).to eq 7
|
47
|
+
body = JSON.parse( body.join )
|
48
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'default path' do
|
52
|
+
subject { Leafy::Rack::Metrics.new( app, registry ) }
|
53
|
+
|
54
|
+
it 'passes request if not matches the given path' do
|
55
|
+
env = { 'PATH_INFO'=> '/something' }
|
56
|
+
expect( subject.call( env ) ).to eq result
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'reports metricss on metrics path' do
|
60
|
+
env = { 'PATH_INFO'=> '/metrics' }
|
61
|
+
status, headers, body = subject.call( env )
|
62
|
+
expect( status ).to eq 200
|
63
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
64
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
65
|
+
body = JSON.parse(body.join)
|
66
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'reports "pretty" health-checks on health path' do
|
70
|
+
env = { 'PATH_INFO'=> '/metrics', 'QUERY_STRING' => 'pretty' }
|
71
|
+
status, headers, body = subject.call( env )
|
72
|
+
expect( status ).to eq 200
|
73
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
74
|
+
expect( body.join.count( "\n" ) ).to eq 7
|
75
|
+
body = JSON.parse(body.join)
|
76
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'custom path' do
|
81
|
+
subject { Leafy::Rack::Metrics.new( app, registry, '/custom' ) }
|
82
|
+
|
83
|
+
it 'passes request if not matches the given path' do
|
84
|
+
env = { 'PATH_INFO'=> '/something' }
|
85
|
+
expect( subject.call( env ) ).to eq result
|
86
|
+
env = { 'PATH_INFO'=> '/metrics' }
|
87
|
+
expect( subject.call( env ) ).to eq result
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'reports metricss on metrics path' do
|
91
|
+
env = { 'PATH_INFO'=> '/custom' }
|
92
|
+
status, headers, body = subject.call( env )
|
93
|
+
expect( status ).to eq 200
|
94
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
95
|
+
expect( body.join.count( "\n" ) ).to eq 0
|
96
|
+
body = JSON.parse(body.join)
|
97
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'reports "pretty" health-checks on health path' do
|
101
|
+
env = { 'PATH_INFO'=> '/custom', 'QUERY_STRING' => 'pretty' }
|
102
|
+
status, headers, body = subject.call( env )
|
103
|
+
expect( status ).to eq 200
|
104
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
105
|
+
expect( body.join.count( "\n" ) ).to eq 7
|
106
|
+
body = JSON.parse(body.join)
|
107
|
+
expect( body.to_yaml ).to eq report.to_yaml
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/metrics'
|
4
|
+
require 'leafy/json/metrics_writer'
|
5
|
+
require 'yaml'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
describe Leafy::Json::MetricsWriter do
|
9
|
+
|
10
|
+
subject { Leafy::Json::MetricsWriter.new }
|
11
|
+
|
12
|
+
let( :metrics ) do
|
13
|
+
m = Leafy::Metrics::Registry.new
|
14
|
+
m.register_meter( 'one' ).mark
|
15
|
+
m.register_meter( 'two' ).mark
|
16
|
+
c = m.register_counter( 'three' )
|
17
|
+
c.inc
|
18
|
+
c.inc
|
19
|
+
m.metrics
|
20
|
+
end
|
21
|
+
|
22
|
+
let( :expected ) do
|
23
|
+
{
|
24
|
+
"two"=> {"count"=>1,
|
25
|
+
"m15_rate"=>0.0,
|
26
|
+
"m1_rate"=>0.0,
|
27
|
+
"m5_rate"=>0.0,
|
28
|
+
"units"=>"events/second"},
|
29
|
+
"one"=> {"count"=>1,
|
30
|
+
"m15_rate"=>0.0,
|
31
|
+
"m1_rate"=>0.0,
|
32
|
+
"m5_rate"=>0.0,
|
33
|
+
"units"=>"events/second"},
|
34
|
+
"three"=>{"count"=>2}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'serializes metrics data to json' do
|
39
|
+
data = subject.to_json( metrics.metrics )
|
40
|
+
expect( data.count( "\n" ) ).to eq 0
|
41
|
+
data = JSON.parse( data )
|
42
|
+
expect( data['one'].delete('mean_rate') ).to be >0
|
43
|
+
expect( data['two'].delete('mean_rate') ).to be >0
|
44
|
+
expect( data.to_yaml ).to eq expected.to_yaml
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'serializes metrics data to json (pretty print)' do
|
48
|
+
data = subject.to_json( metrics.metrics, true )
|
49
|
+
expect( data.count( "\n" ) ).to eq 20
|
50
|
+
data = JSON.parse( data )
|
51
|
+
expect( data['one'].delete('mean_rate') ).to be >0
|
52
|
+
expect( data['two'].delete('mean_rate') ).to be >0
|
53
|
+
expect( data.to_yaml ).to eq expected.to_yaml
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
|
3
|
+
require 'leafy/rack/ping'
|
4
|
+
require 'yaml'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe Leafy::Rack::Ping do
|
8
|
+
|
9
|
+
subject { Leafy::Rack::Ping }
|
10
|
+
|
11
|
+
let( :expected_headers ) do
|
12
|
+
{ 'Content-Type' => 'text/plain',
|
13
|
+
'Cache-Control' => 'must-revalidate,no-cache,no-store' }
|
14
|
+
end
|
15
|
+
|
16
|
+
let( :result ){ [ 200, nil, [] ] }
|
17
|
+
let( :app ) do
|
18
|
+
Proc.new() { result }
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has response' do
|
22
|
+
status, headers, body = subject.response
|
23
|
+
expect( status ).to eq 200
|
24
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
25
|
+
expect( body ).to eq ['pong']
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'default path' do
|
29
|
+
subject { Leafy::Rack::Ping.new( app ) }
|
30
|
+
|
31
|
+
it 'passes request if not matches the given path' do
|
32
|
+
env = { 'PATH_INFO'=> '/something' }
|
33
|
+
expect( subject.call( env ) ).to eq result
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'pongs on ping path' do
|
37
|
+
env = { 'PATH_INFO'=> '/ping' }
|
38
|
+
status, headers, body = subject.call( env )
|
39
|
+
expect( status ).to eq 200
|
40
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
41
|
+
expect( body ).to eq ['pong']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'custom path' do
|
46
|
+
subject { Leafy::Rack::Ping.new( app, '/custom' ) }
|
47
|
+
|
48
|
+
it 'passes request if not matches the given path' do
|
49
|
+
env = { 'PATH_INFO'=> '/something' }
|
50
|
+
expect( subject.call( env ) ).to eq result
|
51
|
+
env = { 'PATH_INFO'=> '/ping' }
|
52
|
+
expect( subject.call( env ) ).to eq result
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'pongs on ping path' do
|
56
|
+
env = { 'PATH_INFO'=> '/custom' }
|
57
|
+
status, headers, body = subject.call( env )
|
58
|
+
expect( status ).to eq 200
|
59
|
+
expect( headers.to_yaml).to eq expected_headers.to_yaml
|
60
|
+
expect( body ).to eq ['pong']
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/setup.rb
ADDED