redis_prometheus 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 795f9ada93c54358d409d12e704065f0ae71d37b
4
+ data.tar.gz: 4e0154742b837495bcab118e66fe21252cd1a570
5
+ SHA512:
6
+ metadata.gz: b3123cdcec1254735a8122aa9859daccb43711f3e775f996ad4deb5c5e80cf8e1c4d6ba525801fe0d8a1b2bacfab26c65caedd884e596770b1d1a8a1454e3d0c
7
+ data.tar.gz: 6117e961ddcebd2d6440075951e6883816d99be6a71b40cccfa139326bc7015ce0bd5de4fa0f76d861332354577e1c4543ef044975b1f65238915fcad253c5b6
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_prometheus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Andy Jeffries
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # RedisPrometheus
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/redis_prometheus`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'redis_prometheus'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install redis_prometheus
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andyjeffries/redis_prometheus.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_prometheus"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,124 @@
1
+ require 'benchmark'
2
+
3
+ module RedisPrometheus
4
+ class Middleware
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ if env["PATH_INFO"] == "/metrics/#{ENV["REDIS_PROMETHEUS_TOKEN"]}"
11
+ results(env)
12
+ else
13
+ trace(env) do
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def trace(env)
22
+ response = nil
23
+ duration = Benchmark.realtime { response = yield }
24
+ record(env, response.first.to_s, duration)
25
+ response
26
+ end
27
+
28
+ def results(env)
29
+ headers = {}
30
+ response = ""
31
+
32
+ keys = Redis.current.keys("http_request_duration_seconds_bucket:*")
33
+ values = Redis.current.mget(keys)
34
+ response << "# TYPE http_request_duration_seconds_bucket histogram\n"
35
+ response << "# HELP http_request_duration_seconds_bucket The HTTP response duration of the Rack application.\n"
36
+ keys.each_with_index do |key, i|
37
+ key_parts = key.split(":")
38
+ key_parts.shift
39
+
40
+ data = {}
41
+ key_parts.each do |p|
42
+ k,v = p.split("=")
43
+ data[k.to_sym] = v
44
+ end
45
+ data[:service] = ENV["REDIS_PROMETHEUS_SERVICE"]
46
+
47
+ next if Rails.application.config.redis_prometheus.ignored_urls.include?(data[:url])
48
+
49
+ response << "http_request_duration_seconds_bucket{"
50
+ response << data.map {|k,v| "#{k}=\"#{v}\""}.join(",")
51
+ response << "} #{values[i].to_f}\n"
52
+ end
53
+
54
+ response << "# TYPE http_request_requests_counter counter\n"
55
+ response << "# HELP http_request_requests_counter The total number of HTTP requests handled by the Rack application.\n"
56
+ requests = Redis.current.get("http_request_requests_counter") || 0
57
+ response << "http_request_requests_counter{service=\"#{ENV["REDIS_PROMETHEUS_SERVICE"]}\"} #{requests.to_f}\n"
58
+
59
+ response << "# TYPE http_request_client_errors_counter counter\n"
60
+ response << "# HELP http_request_client_errors_counter The total number of HTTP errors return by the Rack application.\n"
61
+ requests = Redis.current.get("http_request_client_errors_counter") || 0
62
+ response << "http_request_client_errors_counter{service=\"#{ENV["REDIS_PROMETHEUS_SERVICE"]}\"} #{requests.to_f}\n"
63
+
64
+ response << "# TYPE http_request_server_errors_counter counter\n"
65
+ response << "# HELP http_request_server_errors_counter The total number of HTTP errors return by the Rack application.\n"
66
+ requests = Redis.current.get("http_request_server_errors_counter") || 0
67
+ response << "http_request_server_errors_counter{service=\"#{ENV["REDIS_PROMETHEUS_SERVICE"]}\"} #{requests.to_f}\n"
68
+
69
+ headers['Content-Encoding'] = "gzip"
70
+ headers['Content-Type'] = "text/plain"
71
+ gzip = Zlib::GzipWriter.new(StringIO.new)
72
+ gzip << response
73
+ compressed_response = gzip.close.string
74
+
75
+ [200, headers, [compressed_response]]
76
+ end
77
+
78
+ def record(env, code, duration)
79
+ url = "#{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}"
80
+ return if Rails.application.config.redis_prometheus.ignored_urls.include?(url)
81
+
82
+ url.gsub!(%r{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}i, "{uuid}")
83
+
84
+ bucket = duration_to_bucket(duration)
85
+ Redis.current.incr("http_request_duration_seconds_bucket:url=#{url}:le=#{bucket}")
86
+ Redis.current.incr("http_request_requests_counter")
87
+ if code.to_i >= 400 && code.to_i <= 499
88
+ Redis.current.incr("http_request_client_errors_counter")
89
+ end
90
+ if code.to_i >= 500 && code.to_i <= 599
91
+ Redis.current.incr("http_request_server_errors_counter")
92
+ end
93
+ end
94
+
95
+ def duration_to_bucket(duration)
96
+ return case
97
+ when duration <= 0.005
98
+ "0.005"
99
+ when duration <= 0.01
100
+ "0.01"
101
+ when duration <= 0.025
102
+ "0.025"
103
+ when duration <= 0.05
104
+ "0.05"
105
+ when duration <= 0.1
106
+ "0.1"
107
+ when duration <= 0.25
108
+ "0.25"
109
+ when duration <= 0.5
110
+ "0.5"
111
+ when duration <= 1
112
+ "1"
113
+ when duration <= 2.5
114
+ "2.5"
115
+ when duration <= 5
116
+ "5"
117
+ when duration <= 10
118
+ "10"
119
+ else
120
+ "+Inf"
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,8 @@
1
+ class Railtie < Rails::Railtie
2
+ config.redis_prometheus = ActiveSupport::OrderedOptions.new
3
+ config.redis_prometheus.ignored_urls = ["/metrics/#{ENV["REDIS_PROMETHEUS_TOKEN"]}"]
4
+
5
+ initializer "redis_prometheus.configure_rails_initialization" do |app|
6
+ app.middleware.insert_before 0, RedisPrometheus::Middleware
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module RedisPrometheus
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "redis_prometheus/middleware"
2
+ require "redis_prometheus/railtie"
3
+ require "redis_prometheus/version"
4
+
5
+ module RedisPrometheus
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_prometheus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_prometheus"
8
+ spec.version = RedisPrometheus::VERSION
9
+ spec.authors = ["Andy Jeffries"]
10
+ spec.email = ["andy@andyjeffries.co.uk"]
11
+
12
+ spec.summary = %q{Capture and serve rack statistics for Prometheus in Redis}
13
+ spec.description = %q{In order to ensure all your statistics are served to Prometheus across multiple machines/pods/instances/containers, store the details in Redis}
14
+ spec.homepage = "https://github.com/civo/redis_prometheus"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_prometheus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andy Jeffries
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: In order to ensure all your statistics are served to Prometheus across
56
+ multiple machines/pods/instances/containers, store the details in Redis
57
+ email:
58
+ - andy@andyjeffries.co.uk
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/redis_prometheus.rb
73
+ - lib/redis_prometheus/middleware.rb
74
+ - lib/redis_prometheus/railtie.rb
75
+ - lib/redis_prometheus/version.rb
76
+ - redis_prometheus.gemspec
77
+ homepage: https://github.com/civo/redis_prometheus
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Capture and serve rack statistics for Prometheus in Redis
101
+ test_files: []