rack-instruments 0.1.10 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rack/instruments.rb +39 -34
  2. metadata +5 -5
@@ -1,59 +1,64 @@
1
+ require "securerandom"
1
2
  require "slides"
2
3
 
3
4
  module Rack
4
5
  class Instruments
5
- def initialize(app)
6
+ UUID_PATTERN =
7
+ /\A[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}\Z/
8
+
9
+ def initialize(app, options={})
6
10
  @app = app
11
+
12
+ @context = (options[:context] || {}).
13
+ map { |k, v| [k, v] }
14
+ @header_request_ids = options.fetch(:header_request_ids, true)
15
+ @ignore_extensions = options.fetch(:ignore_extensions,
16
+ %w{css gif ico jpg js jpeg pdf png})
17
+ @request_id_generator = options.fetch(:request_id_generator,
18
+ lambda { SecureRandom.uuid })
19
+ @request_id_pattern = options.fetch(:request_id_pattern, UUID_PATTERN)
7
20
  end
8
21
 
9
22
  def call(env)
10
23
  return @app.call(env) \
11
- if self.class.ignore_extensions.any? { |ext|
24
+ if @ignore_extensions && @ignore_extensions.any? { |ext|
12
25
  env["REQUEST_PATH"] =~ /\.#{ext}$/
13
26
  }
14
27
 
15
- request_id = self.class.id_generator.call
28
+ request_ids = [@request_id_generator.call] + extract_request_ids(env)
16
29
  status, headers, response = nil, nil, nil
17
30
 
18
31
  # make ID of the request accessible to consumers down the stack
19
- env["REQUEST_ID"] = request_id
20
-
21
- data = {
22
- :method => env["REQUEST_METHOD"],
23
- :path => env["REQUEST_PATH"],
24
- :ip => env["X-FORWARDED-FOR"] || env["HTTP_X_FORWARDED_FOR"] ||
25
- env["REMOTE_ADDR"],
26
- :id => request_id,
27
- :status => lambda { status }
28
- }
29
- data.merge!(self.class.context) if self.class.context
30
-
31
- Slides.log(:instrumentation, data) do
32
+ env["REQUEST_ID"] = request_ids
33
+
34
+ data = [
35
+ [:method, env["REQUEST_METHOD"]],
36
+ [:path, env["REQUEST_PATH"]],
37
+ [:ip, env["X-FORWARDED-FOR"] || env["HTTP_X_FORWARDED_FOR"] ||
38
+ env["REMOTE_ADDR"]],
39
+ [:status, lambda { status }],
40
+ ]
41
+ data += request_ids.map { |id| [:id, id] }
42
+ data += @context if @context
43
+
44
+ Slides.log_array(:instrumentation, data) do
32
45
  status, headers, response = @app.call(env)
33
46
  end
34
47
 
35
48
  [status, headers, response]
36
49
  end
37
- end
38
-
39
- module InstrumentsConfig
40
- def self.extended(base)
41
- base.context = nil
42
- base.id_generator = lambda { rand(36**8).to_s(36) }
43
- base.ignore_extensions = %w{css gif ico jpg js jpeg pdf png}
44
- end
45
50
 
46
- attr_accessor :context
47
- attr_accessor :id_generator
48
- attr_accessor :ignore_extensions
51
+ private
49
52
 
50
- def configure
51
- yield self
52
- end
53
-
54
- def ignore_extensions
55
- @ignore_extensions || []
53
+ def extract_request_ids(env)
54
+ return [] unless @header_request_ids
55
+ request_ids = []
56
+ if env["HTTP_REQUEST_ID"]
57
+ request_ids = env["HTTP_REQUEST_ID"].split(",")
58
+ request_ids.map! { |id| id.strip }
59
+ request_ids.select! { |id| id =~ @request_id_pattern }
60
+ end
61
+ request_ids
56
62
  end
57
63
  end
58
- Instruments.extend(InstrumentsConfig)
59
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-instruments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-17 00:00:00.000000000 Z
12
+ date: 2013-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slides
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.9
21
+ version: 0.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.9
29
+ version: 0.2.0
30
30
  description:
31
31
  email: brandur@mutelight.org
32
32
  executables: []
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  version: '0'
56
56
  requirements: []
57
57
  rubyforge_project:
58
- rubygems_version: 1.8.23
58
+ rubygems_version: 1.8.24
59
59
  signing_key:
60
60
  specification_version: 3
61
61
  summary: Rack middleware providing extremely basic instrumentation.