gds_metrics 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8fb3bcd923d980e4161d41cb573069239b3e6fdd711f3378ff1b1547e02669a7
4
- data.tar.gz: 5e4500ebed2c9e26214d9d0784e82b66a18a58b9ea479b3436b3f5f8bff250d7
3
+ metadata.gz: 0ba99ea6af93f3dbd080c86097d2937a423b528599772066d43c58bd678d436b
4
+ data.tar.gz: 0cdf7fd8e318b815a91582683d9d566078d46f830539ac267d69a98e1c122995
5
5
  SHA512:
6
- metadata.gz: 7cebb8edb91fb588e5f8d4951087286699cc520818ff6ba49e5fd8da2bce4b968c56c872933a6027360695eab0d8a3fe6cb304aaf47f026dfa947ade1fd2384e
7
- data.tar.gz: 4563e8671144d93a830115c0e16e4f35993793d8b5a9d30b1f71678767c3af6da3528d5a9a755e36ab8e209c561c651e34e6bafd1f552bd04f49c23a2f2c8c50
6
+ metadata.gz: 8ea263c9680928bb3a22df92ca567ff13d8c641aa048428e26a04cde2c0092737e4459426c68123f35b0300deeecc576be86c58038b8cd57b32e8dde5e7ea3dc
7
+ data.tar.gz: fe71f518e8c9a15599321569b9a29af4dbe15efab8a0ff3d7b20d5029967d359093f483eeb3225a29d08511d309bd1f01a98fb0625213bda09c9119bcfaaba3f
data/README.md CHANGED
@@ -1,65 +1,105 @@
1
- ## GDS Metrics
1
+ # GDS metrics for Ruby apps
2
2
 
3
- Instrument your web app to export [Prometheus](https://prometheus.io/) metrics.
3
+ GDS Metrics are in Alpha and these instructions are subject to change.
4
4
 
5
- ### Overview
5
+ GDS Ruby metrics enable your [Ruby][] web app to export performance data to [Prometheus][], you can add metrics to your app using this [Ruby gem][].
6
6
 
7
- This gem can be added to your web app to capture metrics about how it's
8
- performing. These metrics are served from an endpoint of your app and can be
9
- scraped by Prometheus and turned into Grafana dashboards.
7
+ This gem is a thin wrapper around gitlab's [prometheus-client-mmap][] that:
10
8
 
11
- ### Setup for Rails
9
+ * adds a [railtie][] for easy configuration for [Rails][] apps
10
+ * fixes label naming so it's consistent with the official [Prometheus Ruby Client][]
11
+ * changes url path labels from unique paths to their Rails controller and action if the url path matches a route. For example - `blogs/8`, `blogs/9` => `blogs#view`
12
+ * protects your `/metrics` endpoint with basic HTTP authentication for apps deployed to GOV.UK PaaS
12
13
 
13
- 1. Add the [latest version of the gem](https://rubygems.org/gems/gds_metrics) to
14
- your Gemfile:
14
+ Once you’ve added the gem, metrics data is served from your app's metrics endpoint and is scraped by Prometheus. This data can be turned into performance dashboards using [Grafana][].
15
15
 
16
- ```ruby
17
- gem 'gds_metrics', '~> x.x.x'
18
- ```
16
+ You can read more about the Reliability Engineering monitoring solution [here][].
19
17
 
20
- 2. Install the gem: `bundle install`
21
- 3. Set an environment variable: `export PROMETHEUS_METRICS_PATH=/metrics`
22
- 4. Restart your rails server: `bundle exec rails server`
23
- 5. Visit any page of your app, e.g. [the index page](http://localhost:3000/)
24
- 6. Visit the metrics endpoint: [localhost:3000/metrics](http://localhost:3000/metrics)
18
+ ## Before using GDS metrics
25
19
 
26
- You should see a page containing metrics like `http_req_duration_seconds`.
20
+ Before using GDS metrics you should have:
27
21
 
28
- The gem is now set up correctly.
22
+ * created a [Rails][] or [Rack][] app
23
+ * deployed it to [GOV.UK Platform as a Service (PaaS)][]
29
24
 
30
- ### Non-Rails apps
25
+ ## How to install metrics for Rails apps
31
26
 
32
- If you're not using Rails, before running your server, you'll also need to add
33
- `GDS::Metrics::Middleware` as a
34
- [Rack middleware](https://www.amberbit.com/blog/2011/07/13/introduction-to-rack-middleware/).
35
- Refer to your framework's documentation for how to do this, e.g.
36
- [Sinatra](http://sinatrarb.com/intro#Rack%20Middleware),
37
- [Grape](https://github.com/ruby-grape/grape#using-custom-middleware).
27
+ To use GDS metrics you must:
38
28
 
39
- ### Running on the PaaS
29
+ 1. Add the [latest version of the gem](https://rubygems.org/gems/gds_metrics) to your Gemfile, for example:
40
30
 
41
- If your app runs on the [GOV.UK PaaS](https://www.cloud.service.gov.uk/), you'll
42
- need to set the environment variable with:
31
+ ```gem 'gds_metrics'```
43
32
 
44
- ```bash
45
- $ cf set-env your-app-name PROMETHEUS_METRICS_PATH /metrics
46
- ```
33
+ 2. Run the following command to install the gem:
47
34
 
48
- This command makes the metrics endpoint available in production, whereas the
49
- setup steps above only applied temporarily to the server on your local machine.
35
+ ```bundle install```
50
36
 
51
- In production, this endpoint is automatically protected with authentication.
52
- Citizens will not be able to see your metrics.
37
+ 3. Restart your Rails server by running:
53
38
 
54
- ### Adding custom metrics
39
+ ```bundle exec rails server```
55
40
 
56
- This step is optional.
41
+ 4. Visit any page of your app (for example [the index page][]) to generate some site traffic
57
42
 
58
- By default, common metrics will be recorded, but you can record your own
59
- metrics, too. You might want to capture how many users are signed up for your
60
- service or how many emails it's sent.
43
+ 5. Visit the metrics endpoint at `/metrics` to check if the gem was set up correctly. If it's set up correctly, you will see a page containing some metrics (for example `http_req_duration_seconds`).
61
44
 
62
- The gem is built on top of the `prometheus_ruby_client`, so you can use the
63
- [interface it provides](https://github.com/prometheus/client_ruby#metrics) for
64
- this. There's more documentation on types of metric
65
- [here](https://prometheus.io/docs/concepts/metric_types/).
45
+ If you're not using Rails, you'll also need to add GDS::Metrics::Middleware as [Rack middleware][] before running your Rails server. You’ll also need to refer to your framework's documentation, for example [Sinatra][] or [Grape][] middleware.
46
+
47
+ ## Running on GOV.UK Platform as a Service (PaaS)
48
+
49
+ When running on PaaS, citizens won’t see your metrics in production as this endpoint is automatically protected with authentication.
50
+
51
+ The PaaS documentation has information on how you can [deploy a basic Ruby on Rails app][]. You can also read the official Cloud Foundry guide which has detailed information on [deploying Ruby on Rails apps][].
52
+
53
+ ## Testing
54
+
55
+ The test suite can be run in full using `bundle exec rake`, this includes unit and integration tests as well as a code style check using the [Govuk Linter][].
56
+
57
+ Individual unit tests in `/spec/gds_metrics` can be run using `rspec path/to/test.rb`
58
+
59
+ Because of the need to test the apps integration with rails there is a dummy Rails app within `/spec`. This app is run from within the test suite and integration tests are then executed against it. At the moment these tests cannot be run individually but can be run separately from the unit tests by running `pushd spec/dummy && bundle exec rake && popd`
60
+
61
+ ## Optional configuration
62
+
63
+ You can change the path for serving metrics (by default `/metrics`) by setting the `PROMETHEUS_METRICS_PATH` [environment variable][].
64
+
65
+ ## How to setup extended metrics
66
+
67
+ While common metrics are recorded by default, you can also:
68
+
69
+ * record your own metrics such as how many users are signed up for your service, or how many emails it's sent
70
+ * use the Prometheus interface to set your own metrics as the metrics Ruby gem is built on top of [prometheus-client-mmap][]
71
+
72
+ You can read more about the different types of metrics available in the [Prometheus documentation][].
73
+
74
+ ## Contributing
75
+
76
+ GDS Reliability Engineering welcome contributions. We'd appreciate it if you write tests with your changes and document them where appropriate, this will help us review them quickly.
77
+
78
+ ## Licence
79
+
80
+ This project is licensed under the [MIT License][].
81
+
82
+
83
+
84
+ [Ruby]: https://www.ruby-lang.org/en/
85
+ [Rails]: http://rubyonrails.org/
86
+ [railtie]: http://api.rubyonrails.org/classes/Rails/Railtie.html
87
+ [Prometheus]: https://prometheus.io/
88
+ [Ruby gem]: https://rubygems.org/gems/gds_metrics
89
+ [Grafana]: https://grafana.com/
90
+ [here]: https://reliability-engineering.cloudapps.digital/#reliability-engineering
91
+ [Rack]: https://rack.github.io/
92
+ [GOV.UK Platform as a Service (PaaS)]: https://www.cloud.service.gov.uk/
93
+ [the index page]: http://localhost:3000/
94
+ [Rack middleware]: https://www.amberbit.com/blog/2011/07/13/introduction-to-rack-middleware/
95
+ [Sinatra]: http://sinatrarb.com/intro#Rack%20Middleware
96
+ [Grape]: https://github.com/ruby-grape/grape#using-custom-middleware
97
+ [PaaS]: https://www.cloud.service.gov.uk/
98
+ [environment variable]: https://docs.cloud.service.gov.uk/#environment-variables
99
+ [deploy a basic Ruby on Rails app]: https://docs.cloud.service.gov.uk/#deploy-a-ruby-on-rails-app
100
+ [deploying Ruby on Rails apps]: http://docs.cloudfoundry.org/buildpacks/ruby/gsg-ror.html
101
+ [Prometheus Ruby Client]: https://github.com/prometheus/client_ruby#metrics
102
+ [prometheus-client-mmap]: https://gitlab.com/gitlab-org/prometheus-client-mmap
103
+ [Prometheus documentation]: https://prometheus.io/docs/concepts/metric_types/
104
+ [MIT License]: https://github.com/alphagov/gds_metrics_ruby/blob/master/LICENSE
105
+ [Govuk Linter]: https://github.com/alphagov/govuk-lint
@@ -12,6 +12,7 @@ require "gds_metrics/config"
12
12
  require "gds_metrics/auth"
13
13
  require "gds_metrics/gzip"
14
14
  require "gds_metrics/railtie" if defined?(Rails)
15
+ require "gds_metrics/path_converter"
15
16
 
16
17
  GDS::Metrics::Config.instance.populate_from_env
17
18
  GDS::Metrics::Mmap.setup
@@ -8,7 +8,19 @@ module GDS
8
8
  use GDS::Metrics::Gzip
9
9
  use GDS::Metrics::Auth
10
10
 
11
- use Prometheus::Client::Rack::Collector, registry: Proxy.new
11
+ if defined?(Rails)
12
+ rails_label_builder = proc do |env|
13
+ {
14
+ method: env['REQUEST_METHOD'].downcase,
15
+ host: env['HTTP_HOST'].to_s,
16
+ controller: GDS::Metrics::PathConverter.convert_rails_path_to_route(env['PATH_INFO'].to_s),
17
+ }
18
+ end
19
+ use Prometheus::Client::Rack::Collector, registry: Proxy.new, &rails_label_builder
20
+ else
21
+ use Prometheus::Client::Rack::Collector, registry: Proxy.new
22
+ end
23
+
12
24
  use Prometheus::Client::Rack::Exporter
13
25
 
14
26
  run app
@@ -0,0 +1,14 @@
1
+ module GDS
2
+ module Metrics
3
+ class PathConverter
4
+ def self.convert_rails_path_to_route(path)
5
+ # Convert a path to it's controller and action so we minimise the number of unique timeseries
6
+ # in the case of IDs/variables in paths. Fallback to path if no route found
7
+ route = Rails.application.routes.recognize_path(path)
8
+ "#{route[:controller]}\##{route[:action]}"
9
+ rescue ActionController::RoutingError
10
+ nil
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module GDS
2
2
  module Metrics
3
- VERSION = "0.0.2".freeze
3
+ VERSION = "0.1.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Government Digital Service
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-19 00:00:00.000000000 Z
11
+ date: 2018-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prometheus-client-mmap
@@ -121,6 +121,7 @@ files:
121
121
  - lib/gds_metrics/gzip.rb
122
122
  - lib/gds_metrics/middleware.rb
123
123
  - lib/gds_metrics/mmap.rb
124
+ - lib/gds_metrics/path_converter.rb
124
125
  - lib/gds_metrics/proxy.rb
125
126
  - lib/gds_metrics/railtie.rb
126
127
  - lib/gds_metrics/version.rb
@@ -144,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
145
  version: '0'
145
146
  requirements: []
146
147
  rubyforge_project:
147
- rubygems_version: 2.7.3
148
+ rubygems_version: 2.7.6
148
149
  signing_key:
149
150
  specification_version: 4
150
151
  summary: GDS Metrics