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 +4 -4
- data/README.md +85 -45
- data/lib/gds_metrics.rb +1 -0
- data/lib/gds_metrics/middleware.rb +13 -1
- data/lib/gds_metrics/path_converter.rb +14 -0
- data/lib/gds_metrics/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ba99ea6af93f3dbd080c86097d2937a423b528599772066d43c58bd678d436b
|
4
|
+
data.tar.gz: 0cdf7fd8e318b815a91582683d9d566078d46f830539ac267d69a98e1c122995
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ea263c9680928bb3a22df92ca567ff13d8c641aa048428e26a04cde2c0092737e4459426c68123f35b0300deeecc576be86c58038b8cd57b32e8dde5e7ea3dc
|
7
|
+
data.tar.gz: fe71f518e8c9a15599321569b9a29af4dbe15efab8a0ff3d7b20d5029967d359093f483eeb3225a29d08511d309bd1f01a98fb0625213bda09c9119bcfaaba3f
|
data/README.md
CHANGED
@@ -1,65 +1,105 @@
|
|
1
|
-
|
1
|
+
# GDS metrics for Ruby apps
|
2
2
|
|
3
|
-
|
3
|
+
GDS Metrics are in Alpha and these instructions are subject to change.
|
4
4
|
|
5
|
-
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
17
|
-
gem 'gds_metrics', '~> x.x.x'
|
18
|
-
```
|
16
|
+
You can read more about the Reliability Engineering monitoring solution [here][].
|
19
17
|
|
20
|
-
|
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
|
-
|
20
|
+
Before using GDS metrics you should have:
|
27
21
|
|
28
|
-
|
22
|
+
* created a [Rails][] or [Rack][] app
|
23
|
+
* deployed it to [GOV.UK Platform as a Service (PaaS)][]
|
29
24
|
|
30
|
-
|
25
|
+
## How to install metrics for Rails apps
|
31
26
|
|
32
|
-
|
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
|
-
|
29
|
+
1. Add the [latest version of the gem](https://rubygems.org/gems/gds_metrics) to your Gemfile, for example:
|
40
30
|
|
41
|
-
|
42
|
-
need to set the environment variable with:
|
31
|
+
```gem 'gds_metrics'```
|
43
32
|
|
44
|
-
|
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
|
-
|
49
|
-
setup steps above only applied temporarily to the server on your local machine.
|
35
|
+
```bundle install```
|
50
36
|
|
51
|
-
|
52
|
-
Citizens will not be able to see your metrics.
|
37
|
+
3. Restart your Rails server by running:
|
53
38
|
|
54
|
-
|
39
|
+
```bundle exec rails server```
|
55
40
|
|
56
|
-
|
41
|
+
4. Visit any page of your app (for example [the index page][]) to generate some site traffic
|
57
42
|
|
58
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
data/lib/gds_metrics.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/gds_metrics/version.rb
CHANGED
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
|
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-
|
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.
|
148
|
+
rubygems_version: 2.7.6
|
148
149
|
signing_key:
|
149
150
|
specification_version: 4
|
150
151
|
summary: GDS Metrics
|