gds_metrics 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +62 -2
- data/lib/gds_metrics/middleware.rb +1 -1
- data/lib/gds_metrics/mmap.rb +22 -3
- data/lib/gds_metrics/proxy.rb +35 -0
- data/lib/gds_metrics/version.rb +1 -1
- data/lib/gds_metrics.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fb3bcd923d980e4161d41cb573069239b3e6fdd711f3378ff1b1547e02669a7
|
4
|
+
data.tar.gz: 5e4500ebed2c9e26214d9d0784e82b66a18a58b9ea479b3436b3f5f8bff250d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cebb8edb91fb588e5f8d4951087286699cc520818ff6ba49e5fd8da2bce4b968c56c872933a6027360695eab0d8a3fe6cb304aaf47f026dfa947ade1fd2384e
|
7
|
+
data.tar.gz: 4563e8671144d93a830115c0e16e4f35993793d8b5a9d30b1f71678767c3af6da3528d5a9a755e36ab8e209c561c651e34e6bafd1f552bd04f49c23a2f2c8c50
|
data/README.md
CHANGED
@@ -1,5 +1,65 @@
|
|
1
1
|
## GDS Metrics
|
2
2
|
|
3
|
-
|
3
|
+
Instrument your web app to export [Prometheus](https://prometheus.io/) metrics.
|
4
4
|
|
5
|
-
|
5
|
+
### Overview
|
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.
|
10
|
+
|
11
|
+
### Setup for Rails
|
12
|
+
|
13
|
+
1. Add the [latest version of the gem](https://rubygems.org/gems/gds_metrics) to
|
14
|
+
your Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'gds_metrics', '~> x.x.x'
|
18
|
+
```
|
19
|
+
|
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)
|
25
|
+
|
26
|
+
You should see a page containing metrics like `http_req_duration_seconds`.
|
27
|
+
|
28
|
+
The gem is now set up correctly.
|
29
|
+
|
30
|
+
### Non-Rails apps
|
31
|
+
|
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).
|
38
|
+
|
39
|
+
### Running on the PaaS
|
40
|
+
|
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:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
$ cf set-env your-app-name PROMETHEUS_METRICS_PATH /metrics
|
46
|
+
```
|
47
|
+
|
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.
|
50
|
+
|
51
|
+
In production, this endpoint is automatically protected with authentication.
|
52
|
+
Citizens will not be able to see your metrics.
|
53
|
+
|
54
|
+
### Adding custom metrics
|
55
|
+
|
56
|
+
This step is optional.
|
57
|
+
|
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.
|
61
|
+
|
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/).
|
data/lib/gds_metrics/mmap.rb
CHANGED
@@ -1,9 +1,28 @@
|
|
1
1
|
module GDS
|
2
2
|
module Metrics
|
3
3
|
module Mmap
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class << self
|
5
|
+
def setup
|
6
|
+
directory = Config.instance.mmap_directory
|
7
|
+
Prometheus::Client.configuration.multiprocess_files_dir = directory
|
8
|
+
end
|
9
|
+
|
10
|
+
def clean
|
11
|
+
directory = Config.instance.mmap_directory
|
12
|
+
|
13
|
+
Dir.glob("#{directory}/*").each do |path|
|
14
|
+
next unless database_file?(path)
|
15
|
+
FileUtils.rm(path)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def database_file?(path)
|
22
|
+
path.end_with?(".db") && METRIC_TYPES.any? { |t| path.include?(t) }
|
23
|
+
end
|
24
|
+
|
25
|
+
METRIC_TYPES = %w(counter histogram summary).freeze
|
7
26
|
end
|
8
27
|
end
|
9
28
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module GDS
|
2
|
+
module Metrics
|
3
|
+
class Proxy
|
4
|
+
def initialize(registry = Prometheus::Client.registry)
|
5
|
+
@registry = registry
|
6
|
+
end
|
7
|
+
|
8
|
+
def counter(metric_name, *args)
|
9
|
+
@registry.counter(rename(metric_name), *args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def histogram(metric_name, *args)
|
13
|
+
@registry.histogram(rename(metric_name), *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def summary(*)
|
17
|
+
NullSummary.new
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def rename(metric_name)
|
23
|
+
{
|
24
|
+
http_requests_total: :http_server_requests_total,
|
25
|
+
http_exceptions_total: :http_server_exceptions_total,
|
26
|
+
http_req_duration_seconds: :http_server_request_duration_seconds,
|
27
|
+
}.fetch(metric_name, metric_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
class NullSummary
|
31
|
+
def observe(*); end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/gds_metrics/version.rb
CHANGED
data/lib/gds_metrics.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.0.2
|
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-
|
11
|
+
date: 2018-02-19 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/proxy.rb
|
124
125
|
- lib/gds_metrics/railtie.rb
|
125
126
|
- lib/gds_metrics/version.rb
|
126
127
|
homepage: https://github.com/alphagov/gds_metrics_ruby
|