leafy-rack 0.4.0-java

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.
@@ -0,0 +1,32 @@
1
+ require_relative 'setup'
2
+
3
+ require 'leafy/instrumented/collected_instrumented'
4
+
5
+ describe Leafy::Instrumented::CollectedInstrumented do
6
+
7
+ subject { Leafy::Instrumented::CollectedInstrumented.new( registry, "name" ) }
8
+
9
+ let( :registry ) { Leafy::Metrics::Registry.new }
10
+
11
+ [ 201, 304, 404, 503, 123 ].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.meters.keys.sort ).to eq [ "name.responseCodes.2xx", "name.responseCodes.3xx", "name.responseCodes.4xx", "name.responseCodes.5xx", "name.responseCodes.other" ]
21
+
22
+ stati = registry.metrics.meters.values.collect { |a| a.count }
23
+ expect( stati[ index ] ).to eq 1
24
+
25
+ expected = [0,0,0,0,0]
26
+ expected[ index ] = 1
27
+ expect( stati ).to eq expected
28
+
29
+ expect( registry.metrics.meters.values.to_a[ index ].mean_rate ).to be > 50
30
+ end
31
+ end
32
+ 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 503
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 503
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.gsub( /\s/m, '' ) ).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.gsub( /\s/m, '' ) ).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,56 @@
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
+ 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
+ end
28
+
29
+ [ 400, 201, 204, 404, 200, 123, 205, 500 ].each_with_index do |status, index|
30
+ it "collects metrics for a call for status #{status}" do
31
+ result, _, _ = subject.call do
32
+ sleep 0.01
33
+ [ status, nil, nil ]
34
+ end
35
+
36
+ expect( result ).to eq status
37
+
38
+ expect( registry.metrics.timers.keys ).to eq [ 'name.requests' ]
39
+ expect( registry.metrics.timers.values.first.mean_rate ).to be > 0.09
40
+
41
+ expect( registry.metrics.counters.keys ).to eq [ 'name.active_requests' ]
42
+ expect( registry.metrics.counters.values.collect { |a| a.count } ).to eq [ 0 ]
43
+
44
+ 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' ]
45
+
46
+ stati = registry.metrics.meters.values.collect { |a| a.count }
47
+ expect( stati[ index ] ).to eq 1
48
+
49
+ expected = [0,0,0,0,0,0,0,0]
50
+ expected[ index ] = 1
51
+ expect( stati ).to eq expected
52
+
53
+ expect( registry.metrics.meters.values.to_a[ index ].mean_rate ).to be > 50
54
+ end
55
+ end
56
+ 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