prometheus-client-forty_two 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f209f70a2727a37c304186be755185c9d64074f
4
+ data.tar.gz: ea07ad385e403a1c0da0d6c400a7e2b12f01926f
5
+ SHA512:
6
+ metadata.gz: f03437e61ce83717f56b92decea86df7ee359f9d94f934eb0e8880ea6d8e86b9a0ebcde021d27dab06d718746393e6a2b8a9cb4118e6a4550dbfc57f6a1b9785
7
+ data.tar.gz: 79ed96f43dae5d70d7c45a6c29690818de15c1bc80da56d5c3a64a981f95afbd40ff7a3e0b17891926716e3c0536e1174a724d20ebc4a031067bd9b350d09d48
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,15 @@
1
+ FROM ruby:2.2.3
2
+
3
+ ENV SRC_HOME /src
4
+ RUN mkdir $SRC_HOME
5
+ WORKDIR $SRC_HOME
6
+
7
+ RUN gem install bundler -v 1.13.6
8
+
9
+ COPY Gemfile* ./
10
+ COPY prometheus-client-forty_two.gemspec ./
11
+ RUN mkdir -p lib/prometheus/client/forty_two
12
+ COPY lib/prometheus/client/forty_two/version.rb lib/prometheus/client/forty_two/
13
+ RUN bundle
14
+
15
+ CMD rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prometheus-client-forty_two.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 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.
@@ -0,0 +1,127 @@
1
+ # Prometheus::Client::FortyTwo
2
+
3
+ A configurable Collector middleware for prometheus-client at 42.
4
+
5
+ ## Installation
6
+
7
+ If you're using [Bundler](https://bundler.io/) add this line to your `Gemfile`.
8
+
9
+ ```ruby
10
+ gem 'prometheus-client-forty_two'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install prometheus-client-forty_two
20
+
21
+ ## Overview
22
+
23
+ The Prometheus Ruby Client gem comes with nice standard middlewares to collect
24
+ simple metrics on routes visited on the app, but it fails in some specific cases:
25
+
26
+ 1. Non-numeric, non-uuid identifiers are not cleaned up.
27
+ 2. When static files are served by the server, those queries are collected.
28
+
29
+ This gem offers another Collector that can be customized to:
30
+
31
+ 1. Clean custom identifiers.
32
+
33
+ use Prometheus::FortyTwo::Collector,
34
+ specific_id_stripper: lambda { |path|
35
+ path
36
+ .gsub(%r{/users/[^/]*}, '/users/:name')
37
+ .gsub(%r{/[en|es|fr]/}, '/:locale/')
38
+ }
39
+
40
+ # '/en/users/albert/posts/10/articles'
41
+ # '/fr/users/julie/posts/223/articles'
42
+ # '/es/users/zoe/posts/68/articles'
43
+ # would be stripped as one route:
44
+ # '/:locale/users/:name/posts/:id/articles'
45
+
46
+ 2. Not collect metrics on static files.
47
+
48
+ use Prometheus::FortyTwo::Collector,
49
+ static_files_path: File.join(File.dirname(__FILE__), 'public')
50
+
51
+ # this will recursively list all files in the `/public` directory
52
+ # and not collect any metrics on those
53
+
54
+ See [Prometheus Client](https://github.com/prometheus/client_ruby) to build your
55
+ own metrics.
56
+
57
+ ## Usage
58
+
59
+ ```ruby
60
+ # config.ru
61
+
62
+ require 'rack'
63
+ require 'prometheus/forty_two/collector'
64
+ require 'prometheus/middleware/exporter'
65
+
66
+ use Rack::Deflater
67
+ use Prometheus::FortyTwo::Collector,
68
+ # TODO: add your configuration for the collector
69
+ use Prometheus::Middleware::Exporter
70
+
71
+ run ->(_) { [200, {'Content-Type' => 'text/html'}, ['OK']] }
72
+ ```
73
+
74
+ ## Development
75
+
76
+ ### Using docker
77
+
78
+ This gem has been built for ruby 2.2.3 compatibility. Since this version is now
79
+ deprecated, and therefore difficult to install on a recent system, this
80
+ repository has been provided a `Dockerfile` and a `docher-compose.yml` to work
81
+ inside a container.
82
+
83
+ To build the container:
84
+
85
+ ```sh
86
+ $ docker-compose build gem
87
+ ```
88
+
89
+ To run commands in the container:
90
+
91
+ ```sh
92
+ $ docker-compose run gem {COMMAND}
93
+ ```
94
+
95
+ To run as session in the container:
96
+
97
+ ```sh
98
+ $ docker-compose run gem bash
99
+ ```
100
+
101
+ See next section for development commands
102
+
103
+ ### Directly on your system
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
106
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
107
+ prompt that will allow you to experiment.
108
+
109
+ To install this gem onto your local machine, run `bundle exec rake install`.
110
+ To release a new version, update the version number in `version.rb`, and then
111
+ run `bundle exec rake release`, which will create a git tag for the version,
112
+ push git commits and tags, and push the `.gem` file to
113
+ [rubygems.org](https://rubygems.org).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at [42School/prometheus-client-forty_two](https://github.com/42School/prometheus-client-forty_two).
118
+
119
+ This project is intended to be a safe, welcoming space for collaboration, and
120
+ contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
121
+ code of conduct.
122
+
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
127
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prometheus/client/forty_two"
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
@@ -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,11 @@
1
+ version: '3.2'
2
+
3
+ services:
4
+ gem:
5
+ build:
6
+ context: .
7
+ dockerfile: Dockerfile
8
+ volumes:
9
+ - ./:/src
10
+ entrypoint:
11
+ - "rspec"
@@ -0,0 +1,94 @@
1
+ require 'prometheus/forty_two/version'
2
+ require 'prometheus/middleware/collector'
3
+ require 'find'
4
+
5
+ module Prometheus
6
+ module FortyTwo
7
+ # Collector is a Rack middleware that improves on the basic
8
+ # collector provided by the prometheus-client gem.
9
+ #
10
+ # By default, the original collector will strip routes of their
11
+ # ids assuming they are either numeric or uuids. Set the
12
+ # `:specific_id_stripper` option to provide a specific method
13
+ # to strip ids from urls and replace them by a generic label.
14
+ # The lambda will receive the route as a string before the
15
+ # default stripper strips the ids from it, and should return
16
+ # the cleaned up route.
17
+ #
18
+ # use(
19
+ # Prometheus::Client::FortyTwo::Middleware::Collector,
20
+ # specific_id_stripper: lambda { |path|
21
+ # path
22
+ # .gsub(%r{/users/[^/]*}, '/users/:name')
23
+ # .gsub(%r{/[en|es|fr]/}, '/:locale/')
24
+ # }
25
+ # )
26
+ #
27
+ # # '/en/users/albert/posts/10/articles'
28
+ # # '/fr/users/julie/posts/223/articles'
29
+ # # '/es/users/zoe/posts/68/articles'
30
+ # # would be stripped as one route:
31
+ # # '/:locale/users/:name/posts/:id/articles'
32
+ #
33
+ # If the cleaner fails, the collector will not and only use the
34
+ # original strip function.
35
+ #
36
+ #
37
+ # When your rails server serves static files, those requests
38
+ # are not necessarily very relevant to your stats. Set the
39
+ # `:static_files_path` option to make the middleware list those
40
+ # files on startup and ignore them.
41
+ # If the directory does not exist or an exception is raised
42
+ # when discovering it, the Collector will just ignore it and
43
+ # start anyways.
44
+ #
45
+ # use(
46
+ # Prometheus::Client::FortyTwo::Middleware::Collector,
47
+ # static_files_path: File.join(File.dirname(__FILE__), 'public')
48
+ # )
49
+ #
50
+ # # all routes pointing to /public will be ignored
51
+ class Collector < Prometheus::Middleware::Collector
52
+ def initialize(app, options = {})
53
+ super
54
+
55
+ @static_files = self.class.find_static_files(options[:static_files_path])
56
+ @specific_id_stripper = options[:specific_id_stripper] || ->(path) { path }
57
+ end
58
+
59
+ def call(env)
60
+ return @app.call(env) if @static_files.include?(env['PATH_INFO'])
61
+
62
+ super
63
+ end
64
+
65
+ protected
66
+
67
+ def strip_ids_from_path(path)
68
+ stripped_path = super
69
+ begin
70
+ @specific_id_stripper.call(stripped_path)
71
+ rescue StandardError
72
+ stripped_path
73
+ end
74
+ end
75
+
76
+ class << self
77
+ def find_static_files(path)
78
+ find_static_files!(path)
79
+ rescue StandardError
80
+ []
81
+ end
82
+
83
+ def find_static_files!(path)
84
+ return [] unless path
85
+
86
+ Find
87
+ .find(path)
88
+ .select { |f| File.file?(f) }
89
+ .map { |f| f.gsub(%r{\A\./}, '/') }
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,5 @@
1
+ module Prometheus
2
+ module FortyTwo
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'prometheus/forty_two/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'prometheus-client-forty_two'
7
+ spec.version = Prometheus::FortyTwo::VERSION
8
+ spec.authors = ['Michel Belleville']
9
+ spec.email = ['michel.belleville@gmail.com']
10
+
11
+ spec.summary = 'A configurable Collector middleware for prometheus-client at 42'
12
+ spec.description = <<-DESC.strip
13
+ Using prometheus-client with non-standard routes (i.e. using non-hexadecimal ids) yields problematic results.
14
+ This gem solves the problem offering a configurable Collector middleware.
15
+ DESC
16
+ spec.homepage = 'https://github.com/42School/prometheus-client-forty_two'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_dependency 'prometheus-client', '~> 2.1.0'
36
+
37
+ spec.add_development_dependency 'bundler', '~> 1.13'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prometheus-client-forty_two
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michel Belleville
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prometheus-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.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.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: |-
70
+ Using prometheus-client with non-standard routes (i.e. using non-hexadecimal ids) yields problematic results.
71
+ This gem solves the problem offering a configurable Collector middleware.
72
+ email:
73
+ - michel.belleville@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Dockerfile
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - docker-compose.yml
90
+ - lib/prometheus/forty_two/collector.rb
91
+ - lib/prometheus/forty_two/version.rb
92
+ - prometheus-client-forty_two.gemspec
93
+ homepage: https://github.com/42School/prometheus-client-forty_two
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: https://rubygems.org
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.11
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A configurable Collector middleware for prometheus-client at 42
118
+ test_files: []