statsd-rack-instrument 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/statsd_rack_instrument.rb +47 -0
  3. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: efaa90e27b52691e9d7f9cca3a3c2cc73a3d64c3ce215b434fc9c08f15a080d5
4
+ data.tar.gz: 33f3feca670e74cb73c2000db678798e711a9176a76e42d11b3401a9242292ec
5
+ SHA512:
6
+ metadata.gz: 9d7ebb5d97cfef20af5da2a13ef486de62343efed00549fd8c2da35bfa0d7c98aa5403c999ce5df3701b1a5a2e727eb724667c03951b7790daab2ef7a7914b8e
7
+ data.tar.gz: 1bb91c1c8041cafd0c8c9dd92a142c76ecfcd2f0037a91f399688d54b0837dc6194dfc776cba08e7a8affd6b3f275740a16b5ecfa9fb430b76d050fcd31b93e3
@@ -0,0 +1,47 @@
1
+ require 'statsd-instrument'
2
+
3
+ class StatsDRackInstrument
4
+ attr_reader :app
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env) # :nodoc:
11
+ observe(env) { @app.call(env) }
12
+ end
13
+
14
+ protected
15
+
16
+ def observe(env)
17
+ time_t0 = Time.now
18
+ response = yield
19
+ duration = Time.now - time_t0
20
+
21
+ tags = {
22
+ code: response.first.to_s,
23
+ method: env['REQUEST_METHOD'].downcase,
24
+ path: build_path(env)
25
+ }
26
+ StatsD.histogram('request_duration_seconds', duration, tags: tags)
27
+
28
+ response
29
+ rescue => e
30
+ StatsD.increment('exceptions_total', tags: { exception: e.class.name })
31
+ raise
32
+ end
33
+
34
+ def build_path(env)
35
+ strip_ids_from_path([env['SCRIPT_NAME'], env['PATH_INFO']].join)
36
+ end
37
+
38
+ # inspired by https://github.com/prometheus/client_ruby/blob/v2.1.0/lib/prometheus/middleware/collector.rb#L88
39
+ def strip_ids_from_path(path)
40
+ path.gsub(
41
+ %r{/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(/|$)},
42
+ '/:uuid\\1'
43
+ ).gsub(
44
+ %r{/\d+(/|$)}, '/:id\\1'
45
+ )
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsd-rack-instrument
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - michal-kazmierczak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: statsd-instrument
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: 'The statsd-rack-instrument gem enables a quick implementation of StatsD
28
+ metrics. It adds metrics such as: request_duration_seconds (histogram), requests_total
29
+ (counter), and exceptions_total (counter).'
30
+ email:
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/statsd_rack_instrument.rb
36
+ homepage: https://github.com/michal-kazmierczak/statsd-rack-instrument
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.22
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: StatsD instrumentation for Rack servers
60
+ test_files: []