harness-haproxy 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9361e93fe139a37baa4690ae2ee179b2ae976bf8
4
+ data.tar.gz: 536b530cda3b3ddfea26476ec1eede8eb4bfbec4
5
+ SHA512:
6
+ metadata.gz: 7e0e4d23167a012a754ff983378572aad3efbc22a9bc2af9736801ea674ec820f70e9a11279338a5998eef07ad1e4cada12dd5b4135e6834e2f632dd64e79381
7
+ data.tar.gz: 0ba3ca3f4381e6502f35fbdbba01eccfdb65f4da01e7d696ad50c980ee714511a3121d1455c32ab6939230e47d113bbefd9250939ed63a77963ee13ea886072b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in harness-varnish.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ahawkins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # Harness::HAProxy
2
+
3
+ Log all HAProxy stats to Harness as guages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'harness-haproxy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install harness-haproxy
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'harness/haproxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "harness-haproxy"
8
+ spec.version = Harness::HAProxy::VERSION
9
+ spec.authors = ["ahawkins"]
10
+ spec.email = ["adam@hawkins.io"]
11
+ spec.description = %q{Harness gauge for HAProxy}
12
+ spec.summary = %q{}
13
+ spec.homepage = "http://github.com/ahawkins/harness-haproxy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "harness"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "webmock"
26
+ end
@@ -0,0 +1 @@
1
+ require 'harness/haproxy'
@@ -0,0 +1,74 @@
1
+ require "harness/haproxy/version"
2
+
3
+ require 'harness'
4
+ require 'uri'
5
+ require 'net/https'
6
+ require 'csv'
7
+
8
+ module Harness
9
+ class HAProxyGauge
10
+ include Instrumentation
11
+
12
+ BadResponseError = Class.new StandardError
13
+
14
+ def initialize(url)
15
+ @url = url
16
+ end
17
+
18
+ def log
19
+ uri = URI.parse @url
20
+
21
+ http = Net::HTTP.new uri.host, uri.port
22
+ http.use_ssl = uri.scheme == 'https'
23
+
24
+ request = Net::HTTP::Get.new uri.request_uri
25
+
26
+ if uri.user || uri.password
27
+ request.basic_auth uri.user, uri.password
28
+ end
29
+
30
+ response = http.request request
31
+
32
+ if response.code.to_i != 200
33
+ raise BadResponseError, "Server did not respond correctly! #{response.inspect}"
34
+ end
35
+
36
+ response = http.request request
37
+
38
+ csv = response.body.gsub /^#\s+/, ''
39
+
40
+ CSV.parse(csv, headers: true) do |row|
41
+ pxname = row.field 'pxname'
42
+ server = row.field('svname').downcase
43
+
44
+ gauge "haproxy.#{pxname}.queue.current.#{server}", row.field('qcur').to_i
45
+ gauge "haproxy.#{pxname}.queue.max.#{server}", row.field('qmax').to_i
46
+
47
+ gauge "haproxy.#{pxname}.session.current.#{server}", row.field('scur').to_i
48
+ gauge "haproxy.#{pxname}.session.max.#{server}", row.field('smax').to_i
49
+ gauge "haproxy.#{pxname}.session.limit.#{server}", row.field('slim').to_i
50
+ gauge "haproxy.#{pxname}.session.total.#{server}", row.field('stot').to_i
51
+
52
+ gauge "haproxy.#{pxname}.bytes.in.#{server}", row.field('bin').to_i
53
+ gauge "haproxy.#{pxname}.bytes.out.#{server}", row.field('bout').to_i
54
+
55
+ gauge "haproxy.#{pxname}.request.denied.#{server}", row.field('dreq').to_i
56
+ gauge "haproxy.#{pxname}.request.error.#{server}", row.field('ereq').to_i
57
+
58
+ gauge "haproxy.#{pxname}.response.1xx.#{server}", row.field('hrsp_1xx').to_i
59
+ gauge "haproxy.#{pxname}.response.2xx.#{server}", row.field('hrsp_2xx').to_i
60
+ gauge "haproxy.#{pxname}.response.3xx.#{server}", row.field('hrsp_3xx').to_i
61
+ gauge "haproxy.#{pxname}.response.4xx.#{server}", row.field('hrsp_4xx').to_i
62
+ gauge "haproxy.#{pxname}.response.5xx.#{server}", row.field('hrsp_5xx').to_i
63
+
64
+ gauge "haproxy.#{pxname}.request.rate.#{server}", row.field('req_rate').to_i
65
+ gauge "haproxy.#{pxname}.request.max_rate.#{server}", row.field('req_rate_max').to_i
66
+
67
+ gauge "haproxy.#{pxname}.request.total.#{server}", row.field('req_tot').to_i
68
+
69
+ gauge "haproxy.#{pxname}.downtime.#{server}", row.field('downtime').to_i
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,5 @@
1
+ module Harness
2
+ module HAProxy
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,151 @@
1
+ require_relative 'test_helper'
2
+
3
+ class HAProxyGaugeTest < MiniTest::Unit::TestCase
4
+ attr_accessor :csv, :gauge
5
+
6
+ def url ; 'http://example.com/stats' ; end
7
+
8
+ def setup
9
+ super
10
+ @gauge = Harness::HAProxyGauge.new url
11
+ @csv = <<-csv
12
+ #
13
+ pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
14
+ app,FRONTEND,,,19,41,2000,1389,166416061,554877036,0,0,0,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,13,,,,0,100069,6995,3082,147,28,,47,75,110338,,,
15
+ csv
16
+
17
+ stub_request(:get, url).to_return({
18
+ status: 200,
19
+ body: csv
20
+ })
21
+ end
22
+
23
+ def test_measures_current_queue_depth
24
+ log
25
+
26
+ assert_gauge 'haproxy.app.queue.current.frontend'
27
+ end
28
+
29
+ def test_measures_current_maximum_queue_size
30
+ log
31
+
32
+ assert_gauge 'haproxy.app.queue.max.frontend'
33
+ end
34
+
35
+ def test_measures_current_sessions
36
+ log
37
+
38
+ assert_gauge 'haproxy.app.session.current.frontend'
39
+ end
40
+
41
+ def test_measures_max_sessions
42
+ log
43
+
44
+ assert_gauge 'haproxy.app.session.max.frontend'
45
+ end
46
+
47
+ def test_measures_session_limit
48
+ log
49
+
50
+ assert_gauge 'haproxy.app.session.limit.frontend'
51
+ end
52
+
53
+ def test_measures_session_total
54
+ log
55
+
56
+ assert_gauge 'haproxy.app.session.total.frontend'
57
+ end
58
+
59
+ def test_measures_bytes_in
60
+ log
61
+
62
+ assert_gauge 'haproxy.app.bytes.in.frontend'
63
+ end
64
+
65
+ def test_measures_bytes_out
66
+ log
67
+
68
+ assert_gauge 'haproxy.app.bytes.out.frontend'
69
+ end
70
+
71
+ def test_measures_1xxs
72
+ log
73
+
74
+ assert_gauge 'haproxy.app.response.1xx.frontend'
75
+ end
76
+
77
+ def test_measures_2xxs
78
+ log
79
+
80
+ assert_gauge 'haproxy.app.response.2xx.frontend'
81
+ end
82
+
83
+ def test_measures_3xxs
84
+ log
85
+
86
+ assert_gauge 'haproxy.app.response.3xx.frontend'
87
+ end
88
+
89
+ def test_measures_4xxs
90
+ log
91
+
92
+ assert_gauge 'haproxy.app.response.4xx.frontend'
93
+ end
94
+
95
+ def test_measures_5xxs
96
+ log
97
+
98
+ assert_gauge 'haproxy.app.response.5xx.frontend'
99
+ end
100
+
101
+ def test_measures_requests_per_second
102
+ log
103
+
104
+ assert_gauge 'haproxy.app.request.rate.frontend'
105
+ end
106
+
107
+ def test_measures_maximum_requests_per_second
108
+ log
109
+
110
+ assert_gauge 'haproxy.app.request.max_rate.frontend'
111
+ end
112
+
113
+ def test_measures_total_number_http_requests
114
+ log
115
+
116
+ assert_gauge 'haproxy.app.request.total.frontend'
117
+ end
118
+
119
+ def test_measures_requests_denied
120
+ log
121
+
122
+ assert_gauge 'haproxy.app.request.denied.frontend'
123
+ end
124
+
125
+ def test_measures_request_errors
126
+ log
127
+
128
+ assert_gauge 'haproxy.app.request.error.frontend'
129
+ end
130
+
131
+ def test_measures_downtime
132
+ log
133
+
134
+ assert_gauge 'haproxy.app.downtime.frontend'
135
+ end
136
+
137
+ def test_raises_an_error_if_request_fails
138
+ assert_raises Harness::HAProxyGauge::BadResponseError do
139
+ stub_request(:get, 'http://bad.host').to_return({
140
+ status: 404
141
+ })
142
+
143
+ Harness::HAProxyGauge.new('http://bad.host').log
144
+ end
145
+ end
146
+
147
+ private
148
+ def log
149
+ gauge.log
150
+ end
151
+ end
@@ -0,0 +1,64 @@
1
+ require 'bundler/setup'
2
+ require 'harness-haproxy'
3
+ require 'minitest/unit'
4
+ require 'minitest/autorun'
5
+ require 'webmock/minitest'
6
+
7
+ class MiniTest::Unit::TestCase
8
+ def setup
9
+ Harness.config.collector = Harness::FakeCollector.new
10
+ Harness.config.queue = Harness::SyncQueue.new
11
+ end
12
+
13
+ def assert_timer(name)
14
+ refute_empty timers
15
+ timer = timers.find { |t| t.name == name }
16
+ assert timer, "Timer #{name} not logged!"
17
+ end
18
+
19
+ def assert_increment(name)
20
+ refute_empty increments
21
+ increment = increments.find { |i| i.name == name }
22
+ assert increment, "Increment #{name} not logged!"
23
+ end
24
+
25
+ def assert_decrement(name)
26
+ refute_empty decrements
27
+ decrement = decrements.find { |i| i.name == name }
28
+ assert decrement, "decrement #{name} not logged!"
29
+ end
30
+
31
+ def assert_gauge(name)
32
+ refute_empty gauges
33
+ gauge = gauges.find { |g| g.name == name }
34
+ assert gauge, "gauge #{name} not logged!"
35
+ end
36
+
37
+ def instrument(name, data = {}, &block)
38
+ ActiveSupport::Notifications.instrument name, data, &block
39
+ end
40
+
41
+ def collector
42
+ Harness.config.collector
43
+ end
44
+
45
+ def timers
46
+ collector.timers
47
+ end
48
+
49
+ def increments
50
+ collector.increments
51
+ end
52
+
53
+ def decrements
54
+ collector.decrements
55
+ end
56
+
57
+ def counters
58
+ collector.counters
59
+ end
60
+
61
+ def gauges
62
+ collector.gauges
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: harness-haproxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ahawkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: harness
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Harness gauge for HAProxy
70
+ email:
71
+ - adam@hawkins.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - harness-haproxy.gemspec
82
+ - lib/harness-haproxy.rb
83
+ - lib/harness/haproxy.rb
84
+ - lib/harness/haproxy/version.rb
85
+ - test/haproxy_test.rb
86
+ - test/test_helper.rb
87
+ homepage: http://github.com/ahawkins/harness-haproxy
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: ''
111
+ test_files:
112
+ - test/haproxy_test.rb
113
+ - test/test_helper.rb