litmus_paper 1.4.2 → 1.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 05ab0a2d2bdf985757966671d62f5394d050cf23
4
- data.tar.gz: 783dd54f751a15260583508c38d4cfb1cf67c8ec
2
+ SHA256:
3
+ metadata.gz: 9167b4c597e469bded0f8b14fad5fc3d1e13f1b75dc34026f2e864c6dc9d51f5
4
+ data.tar.gz: 577ae4f3475c9674549c8b4fa9c7a46566302e841b0596a331cd4990d25c13cb
5
5
  SHA512:
6
- metadata.gz: 3f0188ded625a2605a3da4bba95866517469f713400ff38372c1ffbd26924e2c356af26067207719383fa48562da7605004a452b2a1add0cb486afa96e7b03b7
7
- data.tar.gz: 62e49cdad532162054aa364acb84af933c5d4254f54eccb638f32283244e1973c42e2212ef6043ba6780f1c64167f597b8e518f552fed7550ffca9b4ef1187bc
6
+ metadata.gz: e24c3869e8c0b64809cd05f21f3039cc7427201dea6d5f8e9273b35aa9a3c2c9e75769ea2b91b6a178649fcf0fb17e29fd520f236aa314cf6e959e44f490d35e
7
+ data.tar.gz: abe0f3b37f24f939bdffe680515b69e870fe51f4fdadd17d3fc908b07260b302264a5c687551b626abdadff5dd1247605a94bcef7eb0a8993e23850fec589b52
@@ -97,6 +97,25 @@ module LitmusPaper
97
97
  _create_status_file(StatusFile.service_up_file(params[:service]))
98
98
  end
99
99
 
100
+ get "/:service/metrics" do
101
+ cache_key = "#{params[:service]}_metrics"
102
+ metrics = _cache.get(cache_key)
103
+
104
+ if metrics != nil
105
+ return _text(200, metrics)
106
+ end
107
+
108
+ metrics = LitmusPaper.services[params[:service]].checks.map do |check|
109
+ check.stats.map do |key, value|
110
+ %(#{key}{service="#{params[:service]}"} #{value} #{(Time.now.utc.to_f * 1000).to_i})
111
+ end.join("\n")
112
+ end.join("\n") + "\n"
113
+
114
+ _cache.set(cache_key, metrics)
115
+
116
+ _text(200, metrics)
117
+ end
118
+
100
119
  get "/test/error" do
101
120
  raise "an error"
102
121
  end
@@ -14,6 +14,10 @@ module LitmusPaper
14
14
  end
15
15
  end
16
16
 
17
+ def stats
18
+ {}
19
+ end
20
+
17
21
  def to_s
18
22
  "Metric::BigBrotherService(#{@service})"
19
23
  end
@@ -9,6 +9,10 @@ module LitmusPaper
9
9
  @weight
10
10
  end
11
11
 
12
+ def stats
13
+ {}
14
+ end
15
+
12
16
  def to_s
13
17
  "Metric::ConstantMetric(#{@weight})"
14
18
  end
@@ -24,6 +24,12 @@ module LitmusPaper
24
24
  File.read('/proc/loadavg').split(' ').first.to_f
25
25
  end
26
26
 
27
+ def stats
28
+ {
29
+ :cpu_load_average => load_average,
30
+ }
31
+ end
32
+
27
33
  def to_s
28
34
  "Metric::CPULoad(#{@weight})"
29
35
  end
@@ -25,6 +25,10 @@ module LitmusPaper
25
25
  ((up_weight / total_weight) * @weight).to_i
26
26
  end
27
27
 
28
+ def stats
29
+ {}
30
+ end
31
+
28
32
  def to_s
29
33
  "Metric::HaproxyBackendsHealth(#{@weight}, #{@cluster})"
30
34
  end
@@ -32,6 +32,10 @@ module LitmusPaper
32
32
  health.to_i
33
33
  end
34
34
 
35
+ def stats
36
+ {}
37
+ end
38
+
35
39
  def to_s
36
40
  "Metric::InternetHealth(#{@weight}, #{@hosts.inspect}, #{@options.inspect})"
37
41
  end
@@ -60,6 +60,10 @@ module LitmusPaper
60
60
  LitmusPaper.logger.info("Attempted to reap PID #{pid} but it has already been reaped (ECHILD)")
61
61
  end
62
62
 
63
+ def stats
64
+ {}
65
+ end
66
+
63
67
  def to_s
64
68
  "Metric::Script(#{@command}, #{@weight})"
65
69
  end
@@ -7,24 +7,32 @@ module LitmusPaper
7
7
  @weight = weight
8
8
  @socket_path = socket_path
9
9
  @maxconn = maxconn
10
- @active = 0
11
- @queued = 0
12
10
  end
13
11
 
14
12
  def current_health
15
13
  stats = _stats
16
- @active = stats.active
17
- @queued = stats.queued
18
- if @queued == 0
14
+
15
+ if stats.queued == 0
19
16
  return @weight
20
- else
21
- return [
22
- @weight - (
23
- (@weight * @active.to_f) / (3 * @maxconn.to_f) +
24
- (2 * @weight * @queued.to_f) / (3 * @maxconn.to_f)
25
- ), 1
26
- ].max
27
17
  end
18
+
19
+ [
20
+ @weight - (
21
+ (@weight * stats.active.to_f) / (3 * @maxconn.to_f) +
22
+ (2 * @weight * stats.queued.to_f) / (3 * @maxconn.to_f)
23
+ ),
24
+ 1
25
+ ].max
26
+ end
27
+
28
+ def stats
29
+ stats = _stats
30
+
31
+ {
32
+ :socket_active => stats.active,
33
+ :socket_queued => stats.queued,
34
+ :socket_utilization => ((stats.queued / @maxconn.to_f) * 100).round,
35
+ }
28
36
  end
29
37
 
30
38
  def _stats
@@ -1,5 +1,7 @@
1
1
  module LitmusPaper
2
2
  class Service
3
+ attr_reader :checks
4
+
3
5
  def initialize(name, dependencies = [], checks = [])
4
6
  @name = name
5
7
  @dependencies = dependencies
@@ -1,3 +1,3 @@
1
1
  module LitmusPaper
2
- VERSION = "1.4.2"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -459,6 +459,36 @@ describe LitmusPaper::App do
459
459
  end
460
460
  end
461
461
 
462
+ describe "GET /:service/metrics" do
463
+ it "returns the forced health value for a healthy service" do
464
+ LitmusPaper::Metric::UnixSocketUtilitization.any_instance.stub(
465
+ :_stats => OpenStruct.new({:queued => 7, :active => 10}),
466
+ )
467
+ LitmusPaper::Metric::CPULoad.any_instance.stub(
468
+ :load_average => 0.1
469
+ )
470
+ cpu_load = LitmusPaper::Metric::CPULoad.new(50)
471
+ socket_utilization = LitmusPaper::Metric::UnixSocketUtilitization.new(100, '/var/run/foo.sock', 10)
472
+ internet_health = LitmusPaper::Metric::InternetHealth.new(100, [
473
+ "127.0.0.1:3000",
474
+ "127.0.0.1:3001",
475
+ ])
476
+ test_service = LitmusPaper::Service.new('test', [AlwaysAvailableDependency.new], [
477
+ cpu_load,
478
+ socket_utilization,
479
+ internet_health,
480
+ ])
481
+ LitmusPaper.services['test'] = test_service
482
+
483
+ get "/test/metrics"
484
+ last_response.should be_ok
485
+ last_response.body.should match %r(cpu_load_average{service="test"} 0.1)
486
+ last_response.body.should match %r(active{service="test"} 10)
487
+ last_response.body.should match %r(queued{service="test"} 7)
488
+ last_response.body.should match %r(utilization{service="test"} 70)
489
+ end
490
+ end
491
+
462
492
  describe "server errors" do
463
493
  it "responds with a text/plain 500 response" do
464
494
  old_environment = :test
@@ -50,6 +50,16 @@ describe LitmusPaper::Metric::CPULoad do
50
50
  end
51
51
  end
52
52
 
53
+ describe "#stats" do
54
+ it "reports metrics" do
55
+ File.should_receive(:read).with('/proc/loadavg').and_return("0.08 0.12 0.15 2/1190 9152\n")
56
+ cpu_load = LitmusPaper::Metric::CPULoad.new(50)
57
+ cpu_load.stats.should == {
58
+ :cpu_load_average => 0.08
59
+ }
60
+ end
61
+ end
62
+
53
63
  describe "#to_s" do
54
64
  it "is the check name and the maximum weight" do
55
65
  cpu_load = LitmusPaper::Metric::CPULoad.new(50)
@@ -38,4 +38,22 @@ describe LitmusPaper::Metric::UnixSocketUtilitization do
38
38
  health.should == 1
39
39
  end
40
40
  end
41
+
42
+ describe "#stats" do
43
+ it "reports metrics" do
44
+ LitmusPaper::Metric::UnixSocketUtilitization.any_instance.stub(
45
+ :_stats => OpenStruct.new({:queued => 7, :active => 10}),
46
+ )
47
+ metric = LitmusPaper::Metric::UnixSocketUtilitization.new(
48
+ 100,
49
+ '/var/run/foo.sock',
50
+ 10
51
+ )
52
+ metric.stats.should == {
53
+ :socket_active => 10,
54
+ :socket_queued => 7,
55
+ :socket_utilization => 70,
56
+ }
57
+ end
58
+ end
41
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litmus_paper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintreeps
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-24 00:00:00.000000000 Z
11
+ date: 2019-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -262,8 +262,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
262
  - !ruby/object:Gem::Version
263
263
  version: '0'
264
264
  requirements: []
265
- rubyforge_project:
266
- rubygems_version: 2.5.2.1
265
+ rubygems_version: 3.0.3
267
266
  signing_key:
268
267
  specification_version: 4
269
268
  summary: Backend health tester for HA Services, partner project of big_brother