yabeda-prometheus 0.1.2

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: 3853e35a99555c0cba4020b448c677d49209b0dfde07d7852a075ef032c844bb
4
+ data.tar.gz: 7fd7ba53e5d5c3d5c59ffaa371819867bbe09bd363cfc5adcc747584bc21a721
5
+ SHA512:
6
+ metadata.gz: e50aeccdde88032fa4347fe79dc94f276b4d1681288d02ed90ac2246dceab796ae24c9a3201b287eba8fed157c7a11d379aac79c30dd268b8df78f57a18a9b44
7
+ data.tar.gz: b20bc4cd6cee5fc905e2bd8597cb046e778064b4cbc2f0c0802a7a8777a5d003bdbd5cfe2ff7622f6ef6d6d91c2289c5c5ed26cd6ab4e3aef38024bedfcc9763
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 yabeda.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
+ # Yabeda::[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 'yabeda'
16
+ # Then add monitoring system adapter, e.g.:
17
+ # gem 'yabeda-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 Yabeda::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
+ Yabeda::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 [yabeda-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
+ Yabeda::Prometheus.push_gateway.add(Yabeda::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/yabeda-rb/yabeda-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
+ [yabeda-sidekiq]: https://github.com/yabeda-rb/yabeda-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 "yabeda"
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
+ require "yabeda"
4
+ require "yabeda/prometheus/version"
5
+ require "yabeda/prometheus/adapter"
6
+ require "yabeda/prometheus/exporter"
7
+
8
+ module Yabeda
9
+ module Prometheus
10
+ class << self
11
+ def registry
12
+ ::Prometheus::Client.registry
13
+ end
14
+
15
+ def push_gateway
16
+ @push_gateway ||= begin
17
+ ::Prometheus::Client::Push.new(
18
+ ENV.fetch("PROMETHEUS_JOB_NAME", "yabeda"),
19
+ nil,
20
+ ENV.fetch("PROMETHEUS_PUSH_GATEWAY", "http://localhost:9091"),
21
+ ).tap do |gateway|
22
+ http = gateway.instance_variable_get(:@http)
23
+ http.open_timeout = 5
24
+ http.read_timeout = 5
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prometheus/client"
4
+ require "yabeda/base_adapter"
5
+
6
+ require_relative "./counter_wrapper"
7
+ require_relative "./gauge_wrapper"
8
+
9
+ module Yabeda
10
+ class Prometheus::Adapter < BaseAdapter
11
+ def registry
12
+ @registry ||= ::Prometheus::Client.registry
13
+ end
14
+
15
+ def register_counter!(metric)
16
+ validate_metric!(metric)
17
+ registry.register(Prometheus::CounterWrapper.new(metric))
18
+ end
19
+
20
+ def perform_counter_increment!(*)
21
+ # Do nothing. Prometheus will read current value from evil metric
22
+ end
23
+
24
+ def register_gauge!(metric)
25
+ validate_metric!(metric)
26
+ registry.register(Prometheus::GaugeWrapper.new(metric))
27
+ end
28
+
29
+ def perform_gauge_set!(*)
30
+ # Do nothing. Prometheus will read current value from evil metric
31
+ end
32
+
33
+ def register_histogram!(metric)
34
+ validate_metric!(metric)
35
+ buckets = metric.buckets || ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
36
+ registry.histogram(build_name(metric), metric.comment, {}, buckets)
37
+ end
38
+
39
+ def perform_histogram_measure!(metric, tags, value)
40
+ registry.get(build_name(metric)).observe(tags, value)
41
+ end
42
+
43
+ def build_name(metric)
44
+ [metric.group, metric.name, metric.unit].compact.join("_").to_sym
45
+ end
46
+
47
+ def validate_metric!(metric)
48
+ return if metric.comment
49
+
50
+ raise ArgumentError, "Prometheus require metrics to have comments"
51
+ end
52
+
53
+ Yabeda.register_adapter(:prometheus, new)
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./metric_wrapper"
4
+
5
+ module Yabeda
6
+ class Prometheus::CounterWrapper < Prometheus::MetricWrapper
7
+ def type
8
+ :counter
9
+ end
10
+
11
+ def increment(labels = {}, by = 1)
12
+ metric.increment(labels, by: by)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prometheus/middleware/exporter"
4
+
5
+ module Yabeda
6
+ module Prometheus
7
+ class Exporter < ::Prometheus::Middleware::Exporter
8
+ class << self
9
+ def start_metrics_server!
10
+ Thread.new do
11
+ default_port = ENV.fetch("PORT", 9310)
12
+ Rack::Handler::WEBrick.run(
13
+ rack_app,
14
+ Host: ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0",
15
+ Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port),
16
+ AccessLog: [],
17
+ )
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def rack_app(exporter = self)
24
+ Rack::Builder.new do
25
+ use Rack::CommonLogger
26
+ use Rack::ShowExceptions
27
+ use exporter, registry: Yabeda::Prometheus.registry
28
+ run ->(_env) do
29
+ [404, { "Content-Type" => "text/plain" }, ["Not Found\n"]]
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def call(env)
36
+ if env["REQUEST_PATH"].start_with?(path)
37
+ Yabeda.collectors.each(&:call)
38
+ end
39
+ super
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./metric_wrapper"
4
+
5
+ module Yabeda
6
+ class Prometheus::GaugeWrapper < Prometheus::MetricWrapper
7
+ def type
8
+ :gauge
9
+ end
10
+
11
+ def set(labels, value)
12
+ unless value.is_a?(Numeric)
13
+ raise ArgumentError, 'value must be a number'
14
+ end
15
+
16
+ metric.set(labels, value)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prometheus/client/metric"
4
+
5
+ module Yabeda
6
+ class Prometheus::MetricWrapper < ::Prometheus::Client::Metric
7
+ attr_reader :metric
8
+
9
+ def initialize(metric, base_labels = {})
10
+ @metric = metric
11
+
12
+ @validator = ::Prometheus::Client::LabelSetValidator.new
13
+ @base_labels = base_labels
14
+
15
+ validate_name(self.name)
16
+ validate_docstring(self.docstring)
17
+ @validator.valid?(base_labels)
18
+ end
19
+
20
+ def name
21
+ @name ||=
22
+ [metric.group, metric.name, metric.unit].compact.join("_").to_sym
23
+ end
24
+
25
+ def docstring
26
+ metric.comment
27
+ end
28
+
29
+ def get(labels = {})
30
+ @validator.valid?(labels)
31
+
32
+ metric.get(labels)
33
+ end
34
+
35
+ def values
36
+ metric.values
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Prometheus
5
+ VERSION = "0.1.2"
6
+ end
7
+ end
@@ -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 "yabeda/prometheus/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "yabeda-prometheus"
9
+ spec.version = Yabeda::Prometheus::VERSION
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/yabeda-rb/yabeda-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 "yabeda"
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
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-prometheus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Novikov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yabeda
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
+ - lib/yabeda/prometheus.rb
101
+ - lib/yabeda/prometheus/adapter.rb
102
+ - lib/yabeda/prometheus/counter_wrapper.rb
103
+ - lib/yabeda/prometheus/exporter.rb
104
+ - lib/yabeda/prometheus/gauge_wrapper.rb
105
+ - lib/yabeda/prometheus/metric_wrapper.rb
106
+ - lib/yabeda/prometheus/version.rb
107
+ - yabeda-prometheus.gemspec
108
+ homepage: https://github.com/yabeda-rb/yabeda-prometheus
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Extensible Prometheus exporter for your application
132
+ test_files: []