gds_metrics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae59f4c6dcfbb57dc0ad51ee689ea926b8c58dc78684df7fc749fac81192e1d0
4
+ data.tar.gz: c3207917a94edd3fc16b59c7b8c0184f685fcf01e03de63ca6956f0210a5a27b
5
+ SHA512:
6
+ metadata.gz: aabeeedaaf9d71aef66b729d8088bfff7d1516fa542efb6d0c2a749ece6e6a2e4ce267d7124642ea0ffb6f0cfcaf1377b4b7359e9833fbb2938e7a74f04d97ec
7
+ data.tar.gz: 55bb3cd96daa713840223d2c109787b73a6f94e12a52b299eafeb132b2a773c14dda90e6222256b7d3b8d11eaa17fadf6c37c4ab7b24c8f6a9bf188ae3f08dfe
@@ -0,0 +1,5 @@
1
+ ## GDS Metrics
2
+
3
+ **This is under development.**
4
+
5
+ Instrument your web app to export Prometheus metrics.
@@ -0,0 +1,16 @@
1
+ require "rack"
2
+
3
+ require "prometheus/client"
4
+ require "prometheus/client/rack/collector"
5
+ require "prometheus/client/rack/exporter"
6
+
7
+ require "gds_metrics/version"
8
+ require "gds_metrics/mmap"
9
+ require "gds_metrics/middleware"
10
+ require "gds_metrics/config"
11
+ require "gds_metrics/auth"
12
+ require "gds_metrics/gzip"
13
+ require "gds_metrics/railtie" if defined?(Rails)
14
+
15
+ GDS::Metrics::Config.instance.populate_from_env
16
+ GDS::Metrics::Mmap.setup
@@ -0,0 +1,41 @@
1
+ module GDS
2
+ module Metrics
3
+ class Auth
4
+ attr_accessor :app
5
+
6
+ def initialize(app)
7
+ self.app = app
8
+ end
9
+
10
+ def call(env)
11
+ return app.call(env) unless metrics_path?(env)
12
+ return app.call(env) unless config.auth_enabled?
13
+ return app.call(env) if authorized?(env)
14
+
15
+ unauthorized
16
+ end
17
+
18
+ private
19
+
20
+ def metrics_path?(env)
21
+ path = env.fetch("PATH_INFO")
22
+ path == config.prometheus_metrics_path
23
+ end
24
+
25
+ def authorized?(env)
26
+ header = env.fetch("HTTP_AUTHORIZATION", "")
27
+ token = header[/Bearer (.*)/i, 1]
28
+
29
+ token == config.application_id
30
+ end
31
+
32
+ def config
33
+ Config.instance
34
+ end
35
+
36
+ def unauthorized
37
+ [401, { "Content-Type" => "text/plain" }, ["Unauthorized"]]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ module GDS
2
+ module Metrics
3
+ def self.configure(&block)
4
+ block.call(Config.instance)
5
+ end
6
+
7
+ class Config
8
+ attr_accessor :application_id
9
+ attr_accessor :prometheus_metrics_path
10
+ attr_accessor :auth_enabled
11
+ attr_accessor :mmap_directory
12
+
13
+ alias :auth_enabled? :auth_enabled
14
+
15
+ def self.instance
16
+ @singleton ||= Config.new
17
+ end
18
+
19
+ def populate_from_env
20
+ self.application_id = fetch_application_id
21
+ self.prometheus_metrics_path = fetch_prometheus_metrics_path
22
+ self.mmap_directory = fetch_mmap_directory
23
+ self.auth_enabled = !application_id.nil?
24
+ end
25
+
26
+ private
27
+
28
+ def fetch_application_id
29
+ vcap_application = JSON.parse(ENV.fetch("VCAP_APPLICATION", "{}"))
30
+ vcap_application["application_id"]
31
+ end
32
+
33
+ def fetch_prometheus_metrics_path
34
+ ENV.fetch("PROMETHEUS_METRICS_PATH", "/metrics")
35
+ end
36
+
37
+ def fetch_mmap_directory
38
+ ENV.fetch("MMAP_DIRECTORY", "/tmp")
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ module GDS
2
+ module Metrics
3
+ class Gzip
4
+ attr_accessor :app
5
+
6
+ def initialize(app)
7
+ self.app = app
8
+ end
9
+
10
+ def call(env)
11
+ if metrics_path?(env)
12
+ compress(env)
13
+ else
14
+ app.call(env)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def compress(env)
21
+ Rack::Deflater.new(app).call(env)
22
+ end
23
+
24
+ def metrics_path?(env)
25
+ path = env.fetch("PATH_INFO")
26
+ path == config.prometheus_metrics_path
27
+ end
28
+
29
+ def config
30
+ Config.instance
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ module GDS
2
+ module Metrics
3
+ class Middleware
4
+ attr_accessor :wrapped_app
5
+
6
+ def initialize(app)
7
+ self.wrapped_app = Rack::Builder.app do
8
+ use GDS::Metrics::Gzip
9
+ use GDS::Metrics::Auth
10
+
11
+ use Prometheus::Client::Rack::Collector
12
+ use Prometheus::Client::Rack::Exporter
13
+
14
+ run app
15
+ end
16
+ end
17
+
18
+ def call(env)
19
+ wrapped_app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ module GDS
2
+ module Metrics
3
+ module Mmap
4
+ def self.setup
5
+ directory = Config.instance.mmap_directory
6
+ Prometheus::Client.configuration.multiprocess_files_dir = directory
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module GDS
2
+ module Metrics
3
+ class Railtie < Rails::Railtie
4
+ initializer "gds_metrics.use_middleware" do |app|
5
+ app.middleware.use GDS::Metrics::Middleware
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module GDS
2
+ module Metrics
3
+ VERSION = "0.0.1".freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gds_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Government Digital Service
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-13 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.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: govuk-lint
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.11.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.11.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 5.1.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 5.1.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 12.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 12.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 3.7.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 3.7.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 3.7.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 3.7.2
111
+ description: Instrument your web app to export Prometheus metrics.
112
+ email: reliability-engineering-tools-team@digital.cabinet-office.gov.uk
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - README.md
118
+ - lib/gds_metrics.rb
119
+ - lib/gds_metrics/auth.rb
120
+ - lib/gds_metrics/config.rb
121
+ - lib/gds_metrics/gzip.rb
122
+ - lib/gds_metrics/middleware.rb
123
+ - lib/gds_metrics/mmap.rb
124
+ - lib/gds_metrics/railtie.rb
125
+ - lib/gds_metrics/version.rb
126
+ homepage: https://github.com/alphagov/gds_metrics_ruby
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.7.3
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: GDS Metrics
150
+ test_files: []