yabeda-prometheus-mmap 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: 5f0ebd6b2f081245c79f4d97b2e00e51897b26015bf96217f77236e3c0020279
4
+ data.tar.gz: 0f44b6533da9d8106ea693d932da4dccc38d9360a2a81af211bef93eb838e154
5
+ SHA512:
6
+ metadata.gz: c73cb144636f784778ccde67f7f9f6f48d47544ca145a63c2425714fb76efe40940e4a4b9f593054eb4723699244310fdb567e3423ecebc55744695952b5976c
7
+ data.tar.gz: e2f9adf3f4ce17dbf4552a8922378b06ddda1a22ec4b987d8c0bdb5f5d0632e9c88083fd7daf81229e26cab7cfa528909fc6817a13d0635e4fb873153ddebe1c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in yabeda-prometheus-mmap.gemspec
6
+ gemspec
7
+
8
+ gem 'byebug'
9
+ gem 'rack'
10
+ gem 'rake', '~> 12.0'
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 TODO: Write your name
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,40 @@
1
+ # Yabeda::Prometheus::Mmap
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/yabeda/prometheus/mmap`. 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 'yabeda-prometheus-mmap'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yabeda-prometheus-mmap
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/[USERNAME]/yabeda-prometheus-mmap.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'yabeda/prometheus/mmap'
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,18 @@
1
+ version: '3.4'
2
+
3
+ services:
4
+ app:
5
+ image: ruby:latest
6
+ environment:
7
+ - BUNDLE_PATH=/bundle
8
+ - BUNDLE_CONFIG=/app/.bundle/config
9
+ command: bash
10
+ working_dir: /app
11
+ volumes:
12
+ - .:/app:cached
13
+ - bundler_data:/bundle
14
+ tmpfs:
15
+ - /tmp
16
+
17
+ volumes:
18
+ bundler_data:
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yabeda/prometheus/mmap/exporter'
4
+
5
+ module Yabeda
6
+ module Prometheus
7
+ class Exporter < ::Yabeda::Prometheus::Mmap::Exporter
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yabeda'
4
+ require 'prometheus/client'
5
+ require 'yabeda/prometheus/mmap/version'
6
+ require 'yabeda/prometheus/mmap/adapter'
7
+ require 'yabeda/prometheus/exporter'
8
+
9
+ module Yabeda
10
+ module Prometheus
11
+ # Base module for mmap
12
+ module Mmap
13
+ class << self
14
+ def registry
15
+ ::Prometheus::Client.registry
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'prometheus/client'
4
+ require 'rack'
5
+ require 'prometheus/client/rack/collector'
6
+ require 'prometheus/client/rack/exporter'
7
+ require 'yabeda/base_adapter'
8
+
9
+ module Yabeda
10
+ module Prometheus
11
+ module Mmap
12
+ # Prometheus metrics collecion adapter
13
+ class Adapter < BaseAdapter
14
+ def registry
15
+ @registry ||= ::Yabeda::Prometheus::Mmap.registry
16
+ end
17
+
18
+ def register_counter!(metric)
19
+ validate_metric!(metric)
20
+
21
+ registry.counter(
22
+ build_name(metric),
23
+ metric.comment,
24
+ build_tags(metric.tags)
25
+ )
26
+ end
27
+
28
+ def perform_counter_increment!(metric, tags, value)
29
+ registry.get(build_name(metric)).increment(tags, value)
30
+ end
31
+
32
+ def register_gauge!(metric)
33
+ validate_metric!(metric)
34
+
35
+ registry.gauge(
36
+ build_name(metric),
37
+ metric.comment,
38
+ build_tags(metric.tags)
39
+ )
40
+ end
41
+
42
+ def perform_gauge_set!(metric, tags, value)
43
+ registry.get(build_name(metric)).set(tags, value)
44
+ end
45
+
46
+ def register_histogram!(metric)
47
+ buckets = metric.buckets ||
48
+ ::Prometheus::Client::Histogram::DEFAULT_BUCKETS
49
+
50
+ registry.histogram(
51
+ build_name(metric),
52
+ metric.comment,
53
+ build_tags(metric.tags),
54
+ buckets
55
+ )
56
+ end
57
+
58
+ def perform_histogram_measure!(metric, tags, value)
59
+ registry.get(build_name(metric)).observe(tags, value)
60
+ end
61
+
62
+ def build_name(metric)
63
+ [metric.group, metric.name, metric.unit].compact.join('_').to_sym
64
+ end
65
+
66
+ def build_tags(tags)
67
+ Array(tags).each_with_object({}) do |tag, hash|
68
+ hash[tag] = nil
69
+ end
70
+ end
71
+
72
+ def validate_metric!(metric)
73
+ return if metric.comment
74
+
75
+ raise ArgumentError, 'Prometheus require metrics to have comments'
76
+ end
77
+
78
+ Yabeda.register_adapter(:prometheus, new)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+ require 'prometheus/client/rack/collector'
5
+ require 'prometheus/client/rack/exporter'
6
+
7
+ module Yabeda
8
+ module Prometheus
9
+ module Mmap
10
+ # Rack application or middleware that provides metrics exposition endpoint
11
+ class Exporter < ::Prometheus::Client::Rack::Exporter
12
+ NOT_FOUND_HANDLER = lambda do |_env|
13
+ [404, { 'Content-Type' => 'text/plain' }, ["Not Found\n"]]
14
+ end.freeze
15
+
16
+ class << self
17
+ # Allows to use middleware as standalone rack application
18
+ def call(env)
19
+ @app ||= new(NOT_FOUND_HANDLER, path: '/metrics')
20
+ @app.call(env)
21
+ end
22
+
23
+ def start_metrics_server!
24
+ Thread.new do
25
+ default_port = ENV.fetch('PORT', 9394)
26
+ Rack::Handler::WEBrick.run(
27
+ rack_app,
28
+ Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0',
29
+ Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port),
30
+ AccessLog: []
31
+ )
32
+ end
33
+ end
34
+
35
+ def rack_app(exporter = self, path: '/metrics')
36
+ Rack::Builder.new do
37
+ use Rack::CommonLogger
38
+ use Rack::ShowExceptions
39
+ use exporter, path: path
40
+ run NOT_FOUND_HANDLER
41
+ end
42
+ end
43
+ end
44
+
45
+ def initialize(app, options = {})
46
+ super(app, options.merge(registry: Yabeda::Prometheus::Mmap.registry))
47
+ end
48
+
49
+ def call(env)
50
+ Yabeda.collectors.each(&:call) if env['PATH_INFO'] == path
51
+ super
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Prometheus
5
+ module Mmap
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/yabeda/prometheus/mmap/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'yabeda-prometheus-mmap'
7
+ spec.version = Yabeda::Prometheus::Mmap::VERSION
8
+ spec.authors = ['Dmitry Salahutdinov']
9
+ spec.email = ['dsalahutdinov@gmail.com']
10
+
11
+ spec.summary = 'Extensible Prometheus exporter for your application'
12
+ spec.description = 'Uses prometheus-client-mmap'
13
+ spec.homepage = 'https://github.com/yabeda-rb/yabeda-prometheus-mmap'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/yabeda-rb/yabeda-prometheus-mmap'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/yabeda-rb/yabeda-prometheus-mmap/blob/master/CHANGELOG.md'
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`
23
+ .split("\x0")
24
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'prometheus-client-mmap'
31
+ spec.add_dependency 'yabeda', '~> 0.5'
32
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-prometheus-mmap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Salahutdinov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prometheus-client-mmap
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: yabeda
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ description: Uses prometheus-client-mmap
42
+ email:
43
+ - dsalahutdinov@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - docker-compose.yml
58
+ - lib/yabeda/prometheus/exporter.rb
59
+ - lib/yabeda/prometheus/mmap.rb
60
+ - lib/yabeda/prometheus/mmap/adapter.rb
61
+ - lib/yabeda/prometheus/mmap/exporter.rb
62
+ - lib/yabeda/prometheus/mmap/version.rb
63
+ - yabeda-prometheus-mmap.gemspec
64
+ homepage: https://github.com/yabeda-rb/yabeda-prometheus-mmap
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://github.com/yabeda-rb/yabeda-prometheus-mmap
69
+ source_code_uri: https://github.com/yabeda-rb/yabeda-prometheus-mmap
70
+ changelog_uri: https://github.com/yabeda-rb/yabeda-prometheus-mmap/blob/master/CHANGELOG.md
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.3.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.1.2
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Extensible Prometheus exporter for your application
90
+ test_files: []