vault-tools 0.3.10 → 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,11 @@ module Vault
28
28
  end
29
29
 
30
30
  def app_name
31
- env("APP_NAME")
31
+ env!("APP_NAME")
32
+ end
33
+
34
+ def app_deploy
35
+ env!("APP_DEPLOY")
32
36
  end
33
37
 
34
38
  def port
@@ -3,9 +3,10 @@ module Vault
3
3
  # Log a count metric.
4
4
  #
5
5
  # @param name [String] The name of the metric.
6
- def self.count(name)
7
- name = "#{Config.app_name}.#{name}" if Config.app_name
8
- log("count##{name}" => 1)
6
+ # @param value [Integer] The number of items counted. Can be suffixed with a unit.
7
+ # @param extra_data [Hash] Optional extra data to log.
8
+ def self.count(name, value = 1, extra_data = {})
9
+ log(extra_data.merge("count##{Config.app_name}.#{name}" => value))
9
10
  end
10
11
 
11
12
  # Log an HTTP status code. Two log metrics are written each time this
@@ -20,24 +21,32 @@ module Vault
20
21
  #
21
22
  # @param status [Fixnum] The HTTP status code to record.
22
23
  def self.count_status(status)
23
- count("http_#{status}")
24
+ count("http.#{status}")
24
25
  if status_prefix = status.to_s.match(/\d/)[0]
25
- count("http_#{status_prefix}xx")
26
+ count("http.#{status_prefix}xx")
26
27
  end
27
28
  end
28
29
 
30
+ # Log a measurement.
31
+ #
32
+ # @param name [String] The name of the metric.
33
+ # @param value [Float] Value for the metric. A unit may be appended.
34
+ # @param extra_data [Hash] Optional extra data to log.
35
+ def self.measure(name, value, extra_data = {})
36
+ log(extra_data.merge("measure##{Config.app_name}.#{name}" => value))
37
+ end
38
+
29
39
  # Log a timing metric.
30
40
  #
31
41
  # @param name [String] A Sinatra-formatted route URL.
32
- # @param duration [Fixnum] The duration to record, in seconds or
33
- # milliseconds.
42
+ # @param duration [Fixnum] The duration to record, in milliseconds.
34
43
  def self.time(name, duration)
35
44
  if name
36
45
  name.gsub(/\/:\w+/, ''). # Remove param names from path.
37
- gsub("/", "_"). # Replace slash with underscore.
46
+ gsub("/", "-"). # Replace slash with dash.
38
47
  gsub(/[^A-Za-z0-9\-\_]/, ''). # Only keep subset of chars.
39
- slice(1..-1). # Strip the leading underscore.
40
- tap { |name| log("measure##{name}" => "#{duration}ms") }
48
+ sub(/^-/, ""). # Strip the leading dash.
49
+ tap { |name| measure(name, "#{duration}ms") }
41
50
  end
42
51
  end
43
52
 
@@ -48,6 +57,7 @@ module Vault
48
57
  # @param block [Proc] Optionally, a block of code to run before writing
49
58
  # the log message.
50
59
  def self.log(data, &block)
60
+ data['source'] ||= Config.app_deploy if Config.app_deploy
51
61
  Scrolls.log(data, &block)
52
62
  end
53
63
  end
@@ -1,5 +1,5 @@
1
1
  module Vault
2
2
  module Tools
3
- VERSION = '0.3.10'
3
+ VERSION = '0.3.11'
4
4
  end
5
5
  end
@@ -37,7 +37,7 @@ module Vault
37
37
  # Log details about the request including how long it took.
38
38
  after do
39
39
  Log.count_status(response.status)
40
- Log.time(@action, Time.now - @start_request)
40
+ Log.time("http.#{@action}", (Time.now - @start_request) * 1000)
41
41
  end
42
42
 
43
43
  # Make sure error handler blocks are invoked in tests.
data/test/config_test.rb CHANGED
@@ -39,11 +39,15 @@ class ConfigTest < Vault::TestCase
39
39
  # Vault::Config.app_name returns the value of the APP_NAME environment
40
40
  # variable.
41
41
  def test_app_name
42
- Vault::Config.app_name.must_equal nil
43
42
  set_env 'APP_NAME', "my-app"
44
43
  Vault::Config.app_name.must_equal 'my-app'
45
44
  end
46
45
 
46
+ def test_app_deploy
47
+ set_env 'APP_DEPLOY', "test"
48
+ Vault::Config.app_deploy.must_equal 'test'
49
+ end
50
+
47
51
  def test_port_raises
48
52
  assert_raises RuntimeError do
49
53
  Vault::Config.port
data/test/helper.rb CHANGED
@@ -5,4 +5,8 @@ Vault.setup
5
5
 
6
6
  class Vault::TestCase
7
7
  include Vault::Test::EnvironmentHelpers
8
+
9
+ def logged_data
10
+ Hash[Scrolls.stream.string.split(/\s+/).map {|p| p.split('=') }]
11
+ end
8
12
  end
data/test/log_test.rb CHANGED
@@ -1,59 +1,79 @@
1
1
  require 'helper'
2
2
 
3
3
  class LogTest < Vault::TestCase
4
+ def setup
5
+ super
6
+ set_env('APP_NAME', 'test-app')
7
+ set_env('APP_DEPLOY', 'test-deploy')
8
+ end
9
+
4
10
  # Vault::Log.count emits a metric, using the specified name, that represents
5
11
  # a countable event.
6
12
  def test_count
7
- set_env('APP_NAME', nil)
8
13
  Vault::Log.count('countable')
9
- assert_match(/count#countable=1/, Scrolls.stream.string)
14
+ assert_equal '1', logged_data['count#test-app.countable']
15
+ assert_equal 'test-deploy', logged_data['source']
10
16
  end
11
17
 
12
- # Vault::Log.count emits a metric that represents a countable event. If an
13
- # APP_NAME environment variable is available it will be prepended to the
14
- # measurement name.
15
- def test_count_with_app_name
16
- set_env('APP_NAME', 'vault_app')
17
- Vault::Log.count('countable')
18
- assert_match(/count#vault_app.countable=1/, Scrolls.stream.string)
18
+ def test_count_with_specified_value
19
+ Vault::Log.count('countable', 5)
20
+ assert_equal '5', logged_data['count#test-app.countable']
21
+ assert_equal 'test-deploy', logged_data['source']
19
22
  end
20
23
 
21
- # Vault::Log.count_status emits metrics to measure HTTP responses. The
22
- # exact HTTP status, and the status family, are recorded.
24
+ def test_count_with_extra_data
25
+ Vault::Log.count('countable', 1, 'request_id' => 'abc')
26
+ assert_equal '1', logged_data['count#test-app.countable']
27
+ assert_equal 'abc', logged_data['request_id']
28
+ end
29
+
30
+ # Vault::Log.count_status emits metrics to measure HTTP responses.
23
31
  def test_count_status
24
- set_env('APP_NAME', nil)
25
32
  Vault::Log.count_status(201)
26
- assert_match(/count#http_201=1/, Scrolls.stream.string)
27
- assert_match(/count#http_2xx=1/, Scrolls.stream.string)
33
+ assert_equal '1', logged_data['count#test-app.http.201']
34
+ assert_equal '1', logged_data['count#test-app.http.2xx']
35
+ assert_equal 'test-deploy', logged_data['source']
28
36
  end
29
37
 
30
- # Vault::Log.count_status emits metrics to measure HTTP responses. If an
31
- # APP_NAME environment variable is available it will be prepended to the
32
- # measurement name.
33
- def test_count_status_with_app_name
34
- set_env('APP_NAME', 'vault_app')
35
- Vault::Log.count_status(201)
36
- assert_match(/count#vault_app.http_201=1/, Scrolls.stream.string)
37
- assert_match(/count#vault_app.http_2xx=1/, Scrolls.stream.string)
38
+ def test_measure
39
+ Vault::Log.measure('thinking', 123.4)
40
+ assert_equal '123.400', logged_data['measure#test-app.thinking']
41
+ assert_equal 'test-deploy', logged_data['source']
42
+ end
43
+
44
+ def test_measure_with_units
45
+ Vault::Log.measure('thinking', '123.4ms')
46
+ assert_equal '123.4ms', logged_data['measure#test-app.thinking']
47
+ end
48
+
49
+ def test_measure_with_extra_data
50
+ Vault::Log.measure('thinking', '123.4', 'request_id' => 'abc')
51
+ assert_equal '123.4', logged_data['measure#test-app.thinking']
52
+ assert_equal 'abc', logged_data['request_id']
53
+ end
54
+
55
+ def test_time
56
+ Vault::Log.time('thinking', 123.4)
57
+ assert_equal '123.4ms', logged_data['measure#test-app.thinking']
38
58
  end
39
59
 
40
60
  # Vault::Log.time emits a metric to measure the duration of an HTTP request.
41
61
  # It converts slashes to underscores.
42
- def test_time_replaces_slash_with_underscore
62
+ def test_time_replaces_slash_with_dash
43
63
  Vault::Log.time('/some/web/path', 123.4)
44
- assert_match(/measure#some_web_path=123.4ms/, Scrolls.stream.string)
64
+ assert_equal '123.4ms', logged_data['measure#test-app.some-web-path']
45
65
  end
46
66
 
47
67
  # Vault::Log.time removes parameters.
48
68
  def test_time_removes_parameters
49
69
  Vault::Log.time('/some/:web/path', 123.4)
50
- assert_match(/measure#some_path=123.4ms/, Scrolls.stream.string)
70
+ assert_equal '123.4ms', logged_data['measure#test-app.some-path']
51
71
  end
52
72
 
53
73
  # Vault::Log.time removes non-alphanumeric characters.
54
74
  def test_time_removes_non_alphanumeric_characters
55
75
  Vault::Log.time('/some/web+path', 123.4)
56
- assert_match(/measure#some_webpath=123.4ms/, Scrolls.stream.string)
76
+ assert_equal '123.4ms', logged_data['measure#test-app.some-webpath']
57
77
  end
58
78
 
59
79
  # Vault::Log.time is a no-op if a nil name is provided.
@@ -66,8 +86,12 @@ class LogTest < Vault::TestCase
66
86
  # specified hash.
67
87
  def test_log
68
88
  Vault::Log.log(integer: 123, float: 123.4, string: 'string', bool: false)
69
- assert_match(/integer=123 float=123.400 string=string bool=false/,
70
- Scrolls.stream.string)
89
+ assert_equal 'test-deploy', logged_data['source']
90
+ assert_equal '123', logged_data['integer']
91
+ assert_equal '123.400', logged_data['float']
92
+ assert_equal 'string', logged_data['string']
93
+ assert_equal 'false', logged_data['bool']
94
+ assert_equal '123.400', logged_data['float']
71
95
  end
72
96
 
73
97
  # Vault::Log.log can be used to measure the time spent in a block.
@@ -75,7 +99,9 @@ class LogTest < Vault::TestCase
75
99
  Vault::Log.log(A: true) do
76
100
  Vault::Log.log(B: true)
77
101
  end
78
- assert_match(/A=true at=start\nB=true\nA=true at=finish elapsed=0/,
79
- Scrolls.stream.string)
102
+ assert_equal 'true', logged_data['A']
103
+ assert_equal 'true', logged_data['B']
104
+ assert_equal 'test-deploy', logged_data['source']
105
+ assert_match /\d/, logged_data['elapsed']
80
106
  end
81
107
  end
data/test/web_test.rb CHANGED
@@ -11,6 +11,8 @@ class WebTest < Vault::TestCase
11
11
  # Always reload the web class to eliminate test leakage
12
12
  def setup
13
13
  super
14
+ set_env('APP_NAME', 'test-app')
15
+ set_env('APP_DEPLOY', 'testing')
14
16
  reload_web!
15
17
  end
16
18
 
@@ -42,8 +44,8 @@ class WebTest < Vault::TestCase
42
44
  # requests.
43
45
  def test_head_status_check
44
46
  head '/'
45
- assert_match(/count#http_200=1/, Scrolls.stream.string)
46
- assert_match(/count#http_2xx=1/, Scrolls.stream.string)
47
+ assert_equal '1', logged_data['count#test-app.http.200']
48
+ assert_equal '1', logged_data['count#test-app.http.2xx']
47
49
  assert_equal(200, last_response.status)
48
50
  end
49
51
 
@@ -51,8 +53,8 @@ class WebTest < Vault::TestCase
51
53
  # response body.
52
54
  def test_get_health_check
53
55
  get '/health'
54
- assert_match(/count#http_200=1/, Scrolls.stream.string)
55
- assert_match(/count#http_2xx=1/, Scrolls.stream.string)
56
+ assert_equal '1', logged_data['count#test-app.http.200']
57
+ assert_equal '1', logged_data['count#test-app.http.2xx']
56
58
  assert_equal(200, last_response.status)
57
59
  assert_equal('OK', last_response.body)
58
60
  end
@@ -61,8 +63,8 @@ class WebTest < Vault::TestCase
61
63
  # match a known resource.
62
64
  def test_head_with_unknown_endpoint
63
65
  head '/unknown'
64
- assert_match(/count#http_404=1/, Scrolls.stream.string)
65
- assert_match(/count#http_4xx=1/, Scrolls.stream.string)
66
+ assert_equal '1', logged_data['count#test-app.http.404']
67
+ assert_equal '1', logged_data['count#test-app.http.4xx']
66
68
  assert_equal(404, last_response.status)
67
69
  end
68
70
 
@@ -70,8 +72,8 @@ class WebTest < Vault::TestCase
70
72
  # traceback is also written to the response body to ease debugging.
71
73
  def test_error_logs_500
72
74
  get '/boom'
73
- assert_match(/count#http_500=1/, Scrolls.stream.string)
74
- assert_match(/count#http_5xx=1/, Scrolls.stream.string)
75
+ assert_equal '1', logged_data['count#test-app.http.500']
76
+ assert_equal '1', logged_data['count#test-app.http.5xx']
75
77
  assert_match(/^RuntimeError: An expected error occurred.$/m,
76
78
  last_response.body)
77
79
  assert_equal(500, last_response.status)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vault-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.3.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-10-15 00:00:00.000000000 Z
13
+ date: 2013-10-24 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: scrolls
17
- requirement: !ruby/object:Gem::Requirement
17
+ requirement: &2156169280 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,15 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ! '>='
29
- - !ruby/object:Gem::Version
30
- version: '0'
25
+ version_requirements: *2156169280
31
26
  - !ruby/object:Gem::Dependency
32
27
  name: sinatra
33
- requirement: !ruby/object:Gem::Requirement
28
+ requirement: &2156168860 !ruby/object:Gem::Requirement
34
29
  none: false
35
30
  requirements:
36
31
  - - ! '>='
@@ -38,15 +33,10 @@ dependencies:
38
33
  version: '0'
39
34
  type: :runtime
40
35
  prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ! '>='
45
- - !ruby/object:Gem::Version
46
- version: '0'
36
+ version_requirements: *2156168860
47
37
  - !ruby/object:Gem::Dependency
48
38
  name: uuidtools
49
- requirement: !ruby/object:Gem::Requirement
39
+ requirement: &2156168440 !ruby/object:Gem::Requirement
50
40
  none: false
51
41
  requirements:
52
42
  - - ! '>='
@@ -54,15 +44,10 @@ dependencies:
54
44
  version: '0'
55
45
  type: :runtime
56
46
  prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: '0'
47
+ version_requirements: *2156168440
63
48
  - !ruby/object:Gem::Dependency
64
49
  name: rack-ssl-enforcer
65
- requirement: !ruby/object:Gem::Requirement
50
+ requirement: &2156168020 !ruby/object:Gem::Requirement
66
51
  none: false
67
52
  requirements:
68
53
  - - ! '>='
@@ -70,15 +55,10 @@ dependencies:
70
55
  version: '0'
71
56
  type: :runtime
72
57
  prerelease: false
73
- version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
- requirements:
76
- - - ! '>='
77
- - !ruby/object:Gem::Version
78
- version: '0'
58
+ version_requirements: *2156168020
79
59
  - !ruby/object:Gem::Dependency
80
60
  name: heroku-api
81
- requirement: !ruby/object:Gem::Requirement
61
+ requirement: &2156167600 !ruby/object:Gem::Requirement
82
62
  none: false
83
63
  requirements:
84
64
  - - ! '>='
@@ -86,28 +66,18 @@ dependencies:
86
66
  version: '0'
87
67
  type: :runtime
88
68
  prerelease: false
89
- version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
- requirements:
92
- - - ! '>='
93
- - !ruby/object:Gem::Version
94
- version: '0'
69
+ version_requirements: *2156167600
95
70
  - !ruby/object:Gem::Dependency
96
71
  name: fernet
97
- requirement: !ruby/object:Gem::Requirement
72
+ requirement: &2156167100 !ruby/object:Gem::Requirement
98
73
  none: false
99
74
  requirements:
100
- - - '='
75
+ - - =
101
76
  - !ruby/object:Gem::Version
102
77
  version: 2.0.rc2
103
78
  type: :runtime
104
79
  prerelease: false
105
- version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
- requirements:
108
- - - '='
109
- - !ruby/object:Gem::Version
110
- version: 2.0.rc2
80
+ version_requirements: *2156167100
111
81
  description: Basic tools for Heroku Vault's Ruby projects
112
82
  email:
113
83
  - christopher.continanza@gmail.com
@@ -164,21 +134,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
134
  - - ! '>='
165
135
  - !ruby/object:Gem::Version
166
136
  version: '0'
167
- segments:
168
- - 0
169
- hash: 3536867358711533404
170
137
  required_rubygems_version: !ruby/object:Gem::Requirement
171
138
  none: false
172
139
  requirements:
173
140
  - - ! '>='
174
141
  - !ruby/object:Gem::Version
175
142
  version: '0'
176
- segments:
177
- - 0
178
- hash: 3536867358711533404
179
143
  requirements: []
180
144
  rubyforge_project:
181
- rubygems_version: 1.8.23
145
+ rubygems_version: 1.8.9
182
146
  signing_key:
183
147
  specification_version: 3
184
148
  summary: Test classes, base web classes, and helpers - oh my!