evil-metrics-prometheus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 509ebad673a4851d59c6bde88eb28d2860e428a6c30c813fd70a5dc66aa9b0bb
4
+ data.tar.gz: 45a02c6c00f18ec88111a81045a305a38f4a747db1157e6f6a689f21091ccf11
5
+ SHA512:
6
+ metadata.gz: 844dc54dc3d846964487612ce86d12d7df53fcc07b9c2ab812369df4a0e1c739861a3f0e793769edbd974fcc4f342e08695bdaabd569241ddc7cce6f2f7b6335
7
+ data.tar.gz: 34d17c7a862223e6bfc029535297ef3199042830d426c04ac8efb30ade055784f1e7f74b0a07be1e2e6cf801059a4c10e6aa007a17e6ac72ede397667a7862da
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ ---
2
+ require:
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - "Gemfile"
11
+ - "spec/**/*"
12
+
13
+ Style/BracesAroundHashParameters:
14
+ EnforcedStyle: context_dependent
15
+
16
+ Style/StringLiterals:
17
+ EnforcedStyle: double_quotes
18
+
19
+ # Allow to use let!
20
+ RSpec/LetSetup:
21
+ Enabled: false
22
+
23
+ RSpec/MultipleExpectations:
24
+ Enabled: false
25
+
26
+ Bundler/OrderedGems:
27
+ Enabled: false
28
+
29
+ Style/TrailingCommaInArguments:
30
+ Description: 'Checks for trailing comma in argument lists.'
31
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-params-comma'
32
+ Enabled: true
33
+ EnforcedStyleForMultiline: consistent_comma
34
+
35
+ Style/TrailingCommaInArrayLiteral:
36
+ Description: 'Checks for trailing comma in array literals.'
37
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
38
+ Enabled: true
39
+ EnforcedStyleForMultiline: consistent_comma
40
+
41
+ Style/TrailingCommaInHashLiteral:
42
+ Description: 'Checks for trailing comma in hash literals.'
43
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
44
+ Enabled: true
45
+ EnforcedStyleForMultiline: consistent_comma
46
+
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in evil-metrics.gemspec
8
+ gemspec
9
+
10
+ group :development, :test do
11
+ gem "pry"
12
+ gem "pry-byebug", platform: :mri
13
+
14
+ gem "rubocop"
15
+ gem "rubocop-rspec"
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Andrey Novikov
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,73 @@
1
+ # Evil::Metrics::[Prometheus]
2
+
3
+ Adapter for easy exporting your collected evil metrics from your application to the [Prometheus]!
4
+
5
+ ## One more? Why not X?
6
+
7
+ - https://github.com/discourse/prometheus_exporter – built on assumption that various processes (web, jobs, etc) are able to communicate between them on single machine. But in containerized environments all your processes on different “machines”!
8
+ - https://github.com/getqujing/prome – actually inspired this all these gems but seems abandoned and lacks extensibility.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'evil-metrics'
16
+ # Then add monitoring system adapter, e.g.:
17
+ # gem 'evil-metrics-prometheus'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ ## Usage
25
+
26
+ 1. Exporting from running web servers:
27
+
28
+ Place following in your `config.ru` _before_ running your application:
29
+
30
+ ```ruby
31
+ use Evil::Metrics::Prometheus::Exporter
32
+ ```
33
+
34
+ Metrics will be available on `/metrics` path.
35
+
36
+ 2. Run web-server from long-running processes (delayed jobs, …):
37
+
38
+ ```ruby
39
+ Evil::Metrics::Prometheus::Exporter.start_metrics_server!
40
+ ```
41
+
42
+ WEBrick will be launched in separate thread and will serve metrics on `/metrics` path.
43
+
44
+ See [evil-metrics-sidekiq] for example.
45
+
46
+ Listening address is configured via `PROMETHEUS_EXPORTER_BIND` env variable (default is `0.0.0.0`).
47
+
48
+ Port is configured by `PROMETHEUS_EXPORTER_PORT` or `PORT` variables (default is `9310`).
49
+
50
+ 3. Use push gateway for short living things (rake tasks, cron jobs, …):
51
+
52
+ ```ruby
53
+ Evil::Metrics::Prometheus.push_gateway.add(Evil::Metrics::Prometheus.registry)
54
+ ```
55
+
56
+ Address of push gateway is configured with `PROMETHEUS_PUSH_GATEWAY` env variable.
57
+
58
+ ## Development
59
+
60
+ 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.
61
+
62
+ 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).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/evil-metrics/evil-metrics-prometheus.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ [Prometheus]: https://prometheus.io/ "Open-source monitoring solution"
73
+ [evil-metrics-sidekiq]: https://github.com/evil-metrics/evil-metrics-sidekiq
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "evil/metrics"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "pry"
11
+ Pry.start
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,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "evil/metrics/prometheus/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "evil-metrics-prometheus"
9
+ spec.version = "0.1.0"
10
+ spec.authors = ["Andrey Novikov"]
11
+ spec.email = ["envek@envek.name"]
12
+
13
+ spec.summary = "Extensible Prometheus exporter for your application"
14
+ spec.homepage = "https://github.com/evil-metrics/evil-metrics-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_dependency "evil-metrics"
25
+ spec.add_dependency "prometheus-client"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 12.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "evil/metrics"
4
+ require "evil/metrics/prometheus/version"
5
+ require "evil/metrics/prometheus/adapter"
6
+ require "evil/metrics/prometheus/exporter"
7
+
8
+ module Evil
9
+ module Metrics
10
+ module Prometheus
11
+ class << self
12
+ def registry
13
+ ::Prometheus::Client.registry
14
+ end
15
+
16
+ def push_gateway
17
+ @push_gateway ||= begin
18
+ ::Prometheus::Client::Push.new(
19
+ ENV.fetch("PROMETHEUS_JOB_NAME", "evil-metrics"),
20
+ nil,
21
+ ENV.fetch("PROMETHEUS_PUSH_GATEWAY", "http://localhost:9091"),
22
+ ).tap do |gateway|
23
+ http = gateway.instance_variable_get(:@http)
24
+ http.open_timeout = 5
25
+ http.read_timeout = 5
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prometheus/client"
4
+ require "evil/metrics/base_adapter"
5
+
6
+ module Evil
7
+ module Metrics
8
+ class Prometheus::Adapter < BaseAdapter
9
+ def registry
10
+ @registry ||= ::Prometheus::Client.registry
11
+ end
12
+
13
+ def register_counter!(metric)
14
+ validate_metric!(metric)
15
+ registry.counter(build_name(metric), metric.comment)
16
+ end
17
+
18
+ def perform_counter_increment!(metric, tags, increment)
19
+ registry.get(build_name(metric)).increment(tags, increment)
20
+ end
21
+
22
+ def register_gauge!(metric)
23
+ validate_metric!(metric)
24
+ registry.gauge(build_name(metric), metric.comment)
25
+ end
26
+
27
+ def perform_gauge_set!(metric, tags, increment)
28
+ registry.get(build_name(metric)).set(tags, increment)
29
+ end
30
+
31
+ def register_histogram!(metric)
32
+ validate_metric!(metric)
33
+ buckets = metric.buckets || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
34
+ registry.histogram(build_name(metric), metric.comment, {}, buckets)
35
+ end
36
+
37
+ def perform_histogram_measure!(metric, tags, value)
38
+ registry.get(build_name(metric)).observe(tags, value)
39
+ end
40
+
41
+ def build_name(metric)
42
+ [metric.group, metric.name, metric.unit].compact.join("_").to_sym
43
+ end
44
+
45
+ def validate_metric!(metric)
46
+ return if metric.comment
47
+
48
+ raise ArgumentError, "Prometheus require metrics to have comments"
49
+ end
50
+
51
+ Evil::Metrics.register_adapter(:prometheus, new)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prometheus/middleware/exporter"
4
+
5
+ module Evil
6
+ module Metrics
7
+ module Prometheus
8
+ class Exporter < ::Prometheus::Middleware::Exporter
9
+ class << self
10
+ def start_metrics_server!
11
+ Thread.new do
12
+ default_port = ENV.fetch("PORT", 9310)
13
+ Rack::Handler::WEBrick.run(
14
+ rack_app,
15
+ Host: ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0",
16
+ Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port),
17
+ AccessLog: [],
18
+ )
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ def rack_app(exporter = self)
25
+ Rack::Builder.new do
26
+ use Rack::CommonLogger
27
+ use Rack::ShowExceptions
28
+ use exporter, registry: Evil::Metrics::Prometheus.registry
29
+ run ->(_env) do
30
+ [404, { "Content-Type" => "text/plain" }, ["Not Found\n"]]
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def call(env)
37
+ if env["REQUEST_PATH"].start_with?(path)
38
+ Evil::Metrics.collectors.each(&:call)
39
+ end
40
+ super
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ module Prometheus
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evil-metrics-prometheus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Novikov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: evil-metrics
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: prometheus-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - envek@envek.name
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - evil-metrics-prometheus.gemspec
101
+ - lib/evil/metrics/prometheus.rb
102
+ - lib/evil/metrics/prometheus/adapter.rb
103
+ - lib/evil/metrics/prometheus/exporter.rb
104
+ - lib/evil/metrics/prometheus/version.rb
105
+ homepage: https://github.com/evil-metrics/evil-metrics-prometheus
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.7.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Extensible Prometheus exporter for your application
129
+ test_files: []