inst_statsd 3.0.2 → 3.0.4

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.
@@ -1,20 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
5
  describe InstStatsd do
6
- before(:each) do
6
+ before do
7
7
  InstStatsd.settings = nil
8
8
  end
9
9
 
10
- after(:each) do
11
- [
12
- 'INST_STATSD_HOST',
13
- 'INST_STATSD_NAMESPACE',
14
- 'INST_STATSD_PORT',
15
- 'INST_STATSD_APPEND_HOST_NAME',
16
- 'INST_DOG_TAGS'
17
- ].each {|k| ENV.delete k}
10
+ after do
11
+ %w[
12
+ INST_STATSD_HOST
13
+ INST_STATSD_NAMESPACE
14
+ INST_STATSD_PORT
15
+ INST_STATSD_APPEND_HOST_NAME
16
+ INST_DOG_TAGS
17
+ ].each { |k| ENV.delete k }
18
18
  end
19
19
 
20
20
  describe ".settings" do
@@ -23,185 +23,189 @@ describe InstStatsd do
23
23
  end
24
24
 
25
25
  it "can be assigned a new value" do
26
- settings = { host: 'bar', port: 1234 }
26
+ settings = { host: "bar", port: 1234 }
27
27
  InstStatsd.settings = settings
28
28
 
29
29
  expect(InstStatsd.settings).to eq settings
30
30
  end
31
31
 
32
- it 'pulls from ENV if not already set' do
33
- ENV['INST_STATSD_HOST'] = 'statsd.example.org'
34
- ENV['INST_STATSD_NAMESPACE'] = 'canvas'
32
+ it "pulls from ENV if not already set" do
33
+ ENV["INST_STATSD_HOST"] = "statsd.example.org"
34
+ ENV["INST_STATSD_NAMESPACE"] = "canvas"
35
35
 
36
36
  expected = {
37
- host: 'statsd.example.org',
38
- namespace: 'canvas',
37
+ host: "statsd.example.org",
38
+ namespace: "canvas"
39
39
  }
40
40
  expect(InstStatsd.settings).to eq(expected)
41
41
  end
42
42
 
43
- it 'configured settings are merged into and take precedence over any existing ENV settings' do
44
- ENV['INST_STATSD_HOST'] = 'statsd.example.org'
45
- ENV['INST_STATSD_NAMESPACE'] = 'canvas'
43
+ it "configured settings are merged into and take precedence over any existing ENV settings" do
44
+ ENV["INST_STATSD_HOST"] = "statsd.example.org"
45
+ ENV["INST_STATSD_NAMESPACE"] = "canvas"
46
46
 
47
- settings = { host: 'statsd.example-override.org' }
47
+ settings = { host: "statsd.example-override.org" }
48
48
  InstStatsd.settings = settings
49
49
 
50
50
  expect(InstStatsd.settings).to eq(InstStatsd.env_settings.merge(settings))
51
51
  expect(InstStatsd.settings[:host]).to eq(settings[:host])
52
52
  end
53
53
 
54
- it 'validates settings' do
55
- settings = { foo: 'blah' }
54
+ it "validates settings" do
55
+ settings = { foo: "blah" }
56
56
  expect { InstStatsd.settings = settings }.to raise_error(InstStatsd::ConfigurationError)
57
57
  end
58
58
 
59
- it 'converts string keys to symbols' do
60
- settings = { 'host' => 'bar', 'port' => 1234 }
59
+ it "converts string keys to symbols" do
60
+ settings = { "host" => "bar", "port" => 1234 }
61
61
  InstStatsd.settings = settings
62
- expect(InstStatsd.settings).to eq({ host: 'bar', port: 1234 })
62
+ expect(InstStatsd.settings).to eq({ host: "bar", port: 1234 })
63
63
  end
64
64
  end
65
65
 
66
66
  describe ".convert_bool" do
67
- it 'sets string true values to boolean true' do
68
- config = {potential_string_bool: 'true'}
67
+ it "sets string true values to boolean true" do
68
+ config = { potential_string_bool: "true" }
69
69
  InstStatsd.convert_bool(config, :potential_string_bool)
70
70
  expect(config[:potential_string_bool]).to be(true)
71
71
  end
72
- it 'sets string True values to boolean true' do
73
- config = {potential_string_bool: 'True'}
72
+
73
+ it "sets string True values to boolean true" do
74
+ config = { potential_string_bool: "True" }
74
75
  InstStatsd.convert_bool(config, :potential_string_bool)
75
76
  expect(config[:potential_string_bool]).to be(true)
76
77
  end
77
- it 'sets boolean true values to boolean true' do
78
- config = {potential_string_bool: true}
78
+
79
+ it "sets boolean true values to boolean true" do
80
+ config = { potential_string_bool: true }
79
81
  InstStatsd.convert_bool(config, :potential_string_bool)
80
82
  expect(config[:potential_string_bool]).to be(true)
81
83
  end
82
- it 'sets false strings to boolean false' do
83
- config = {potential_string_bool: 'false'}
84
+
85
+ it "sets false strings to boolean false" do
86
+ config = { potential_string_bool: "false" }
84
87
  InstStatsd.convert_bool(config, :potential_string_bool)
85
88
  expect(config[:potential_string_bool]).to be(false)
86
89
  end
87
- it 'sets False strings to boolean false' do
88
- config = {potential_string_bool: 'False'}
90
+
91
+ it "sets False strings to boolean false" do
92
+ config = { potential_string_bool: "False" }
89
93
  InstStatsd.convert_bool(config, :potential_string_bool)
90
94
  expect(config[:potential_string_bool]).to be(false)
91
95
  end
92
- it 'sets false booleans to boolean false' do
93
- config = {potential_string_bool: false}
96
+
97
+ it "sets false booleans to boolean false" do
98
+ config = { potential_string_bool: false }
94
99
  InstStatsd.convert_bool(config, :potential_string_bool)
95
100
  expect(config[:potential_string_bool]).to be(false)
96
101
  end
97
- it 'makes no change for nil values' do
98
- config = {foo: 'bar'}
102
+
103
+ it "makes no change for nil values" do
104
+ config = { foo: "bar" }
99
105
  InstStatsd.convert_bool(config, :potential_string_bool)
100
- expect(config).to eq({foo: 'bar'})
101
- end
102
- it 'raises error for non true or false strings or booleans' do
103
- config = {potential_string_bool: 'trrruuue'}
104
- expect{InstStatsd.convert_bool(config, :potential_string_bool)}.to raise_error(InstStatsd::ConfigurationError)
106
+ expect(config).to eq({ foo: "bar" })
105
107
  end
106
108
 
109
+ it "raises error for non true or false strings or booleans" do
110
+ config = { potential_string_bool: "trrruuue" }
111
+ expect { InstStatsd.convert_bool(config, :potential_string_bool) }.to raise_error(InstStatsd::ConfigurationError)
112
+ end
107
113
  end
108
114
 
109
115
  describe ".env_settings" do
110
- it 'returns empty hash when no INST_STATSD_HOST found' do
116
+ it "returns empty hash when no INST_STATSD_HOST found" do
111
117
  env = {
112
- 'INST_STATSD_NAMESPACE' => 'canvas'
118
+ "INST_STATSD_NAMESPACE" => "canvas"
113
119
  }
114
120
  expect(InstStatsd.env_settings(env)).to eq({})
115
121
  end
116
122
 
117
- it 'returns empty hash when missing dog tags' do
123
+ it "returns empty hash when missing dog tags" do
118
124
  env = {
119
- 'INST_DOG_API_KEY' => 'SEKRET KEY'
125
+ "INST_DOG_API_KEY" => "SEKRET KEY"
120
126
  }
121
127
  expect(InstStatsd.env_settings(env)).to eq({})
122
128
  end
123
129
 
124
- it 'builds settings hash with dog environment vars' do
130
+ it "builds settings hash with dog environment vars" do
125
131
  env = {
126
- 'INST_DOG_TAGS' => '{"app": "canvas", "env": "prod"}',
132
+ "INST_DOG_TAGS" => '{"app": "canvas", "env": "prod"}'
127
133
  }
128
134
  expected = {
129
- dog_tags: {"app" => "canvas", "env" => "prod"},
135
+ dog_tags: { "app" => "canvas", "env" => "prod" }
130
136
  }
131
137
  expect(InstStatsd.env_settings(env)).to eq(expected)
132
138
  end
133
139
 
134
- it 'builds settings hash from environment vars' do
140
+ it "builds settings hash from environment vars" do
135
141
  env = {
136
- 'INST_STATSD_HOST' => 'statsd.example.org',
137
- 'INST_STATSD_NAMESPACE' => 'canvas',
142
+ "INST_STATSD_HOST" => "statsd.example.org",
143
+ "INST_STATSD_NAMESPACE" => "canvas"
138
144
  }
139
145
  expected = {
140
- host: 'statsd.example.org',
141
- namespace: 'canvas',
146
+ host: "statsd.example.org",
147
+ namespace: "canvas"
142
148
  }
143
149
  expect(InstStatsd.env_settings(env)).to eq(expected)
144
150
  end
145
151
 
146
- it 'uses ENV if env argument hash not passed' do
147
- ENV['INST_STATSD_HOST'] = 'statsd.example.org'
148
- ENV['INST_STATSD_NAMESPACE'] = 'canvas'
152
+ it "uses ENV if env argument hash not passed" do
153
+ ENV["INST_STATSD_HOST"] = "statsd.example.org"
154
+ ENV["INST_STATSD_NAMESPACE"] = "canvas"
149
155
 
150
156
  expected = {
151
- host: 'statsd.example.org',
152
- namespace: 'canvas',
157
+ host: "statsd.example.org",
158
+ namespace: "canvas"
153
159
  }
154
160
  expect(InstStatsd.env_settings).to eq(expected)
155
161
  end
156
162
 
157
163
  it 'converts env append_hostname "false" to boolean' do
158
164
  env = {
159
- 'INST_STATSD_HOST' => 'statsd.example.org',
160
- 'INST_STATSD_APPEND_HOSTNAME' => 'false',
165
+ "INST_STATSD_HOST" => "statsd.example.org",
166
+ "INST_STATSD_APPEND_HOSTNAME" => "false"
161
167
  }
162
168
  expected = {
163
- host: 'statsd.example.org',
164
- append_hostname: false,
169
+ host: "statsd.example.org",
170
+ append_hostname: false
165
171
  }
166
172
  expect(InstStatsd.env_settings(env)).to eq(expected)
167
173
  end
168
174
 
169
175
  it 'converts env append_hostname "true" to boolean' do
170
176
  env = {
171
- 'INST_STATSD_HOST' => 'statsd.example.org',
172
- 'INST_STATSD_APPEND_HOSTNAME' => 'true',
177
+ "INST_STATSD_HOST" => "statsd.example.org",
178
+ "INST_STATSD_APPEND_HOSTNAME" => "true"
173
179
  }
174
180
  expected = {
175
- host: 'statsd.example.org',
176
- append_hostname: true,
181
+ host: "statsd.example.org",
182
+ append_hostname: true
177
183
  }
178
184
  expect(InstStatsd.env_settings(env)).to eq(expected)
179
185
  end
180
186
 
181
- it 'keeps boolean false values for append_hostname' do
187
+ it "keeps boolean false values for append_hostname" do
182
188
  env = {
183
- 'INST_STATSD_HOST' => 'statsd.example.org',
184
- 'INST_STATSD_APPEND_HOSTNAME' => false,
189
+ "INST_STATSD_HOST" => "statsd.example.org",
190
+ "INST_STATSD_APPEND_HOSTNAME" => false
185
191
  }
186
192
  expected = {
187
- host: 'statsd.example.org',
188
- append_hostname: false,
193
+ host: "statsd.example.org",
194
+ append_hostname: false
189
195
  }
190
196
  expect(InstStatsd.env_settings(env)).to eq(expected)
191
197
  end
192
198
 
193
- it 'keeps boolean true values for append_hostname' do
199
+ it "keeps boolean true values for append_hostname" do
194
200
  env = {
195
- 'INST_STATSD_HOST' => 'statsd.example.org',
196
- 'INST_STATSD_APPEND_HOSTNAME' => true,
201
+ "INST_STATSD_HOST" => "statsd.example.org",
202
+ "INST_STATSD_APPEND_HOSTNAME" => true
197
203
  }
198
204
  expected = {
199
- host: 'statsd.example.org',
200
- append_hostname: true,
205
+ host: "statsd.example.org",
206
+ append_hostname: true
201
207
  }
202
208
  expect(InstStatsd.env_settings(env)).to eq(expected)
203
209
  end
204
-
205
210
  end
206
-
207
211
  end
@@ -1,25 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
5
  describe InstStatsd::NullLogger do
6
-
7
- describe 'initialize' do
8
- it 'takes any arguments' do
9
- expect{InstStatsd::NullLogger.new}.to_not raise_error
10
- expect{InstStatsd::NullLogger.new(1, 2, 3)}.to_not raise_error
6
+ describe "initialize" do
7
+ it "takes any arguments" do
8
+ expect { InstStatsd::NullLogger.new }.not_to raise_error
9
+ expect { InstStatsd::NullLogger.new(1, 2, 3) }.not_to raise_error
11
10
  end
12
11
  end
13
12
 
14
- describe 'debug, info, warn, fatal, and unknown' do
15
- it 'should all no-op instead of logging' do
16
- log_path = 'spec/support/test.log'
17
- File.open(log_path, 'w') { |f| f.write('') } # empty log file
13
+ describe "debug, info, warn, fatal, and unknown" do
14
+ it "should all no-op instead of logging" do
15
+ log_path = "spec/support/test.log"
16
+ File.write(log_path, "") # empty log file
18
17
  logger = InstStatsd::NullLogger.new(log_path)
19
- %w[debug info warn error fatal unknown].each { |m| logger.send(m, 'foo') }
18
+ %w[debug info warn error fatal unknown].each { |m| logger.send(m, "foo") }
20
19
  log_contents = File.read(log_path)
21
- expect(log_contents).to eq ''
20
+ expect(log_contents).to eq ""
22
21
  end
23
22
  end
24
-
25
23
  end
@@ -1,60 +1,56 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
4
- require 'logger'
3
+ require "spec_helper"
4
+ require "logger"
5
5
 
6
6
  describe InstStatsd::RequestLogger do
7
-
8
- describe '#build_log_message' do
9
- before :all do
10
- @logger = InstStatsd::RequestLogger.new(Logger.new(STDOUT))
7
+ describe "#build_log_message" do
8
+ before do
9
+ @logger = InstStatsd::RequestLogger.new(Logger.new($stdout))
11
10
  end
12
- it 'includes the supplied header' do
13
- request_stat = double('request_stat')
14
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
15
- allow(request_stat).to receive(:stats).and_return({})
16
- results = @logger.build_log_message(request_stat, 'FOO_STATS')
11
+
12
+ it "includes the supplied header" do
13
+ request_stat = instance_double(InstStatsd::RequestStat)
14
+ allow(request_stat).to receive_messages(exclusive_stats: nil, stats: {})
15
+ results = @logger.build_log_message(request_stat, "FOO_STATS")
17
16
  expect(results).to eq("[FOO_STATS]")
18
17
  end
19
- it 'falls back to the default header' do
20
- request_stat = double('request_stat')
21
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
22
- allow(request_stat).to receive(:stats).and_return({})
18
+
19
+ it "falls back to the default header" do
20
+ request_stat = instance_double(InstStatsd::RequestStat)
21
+ allow(request_stat).to receive_messages(exclusive_stats: nil, stats: {})
23
22
  results = @logger.build_log_message(request_stat)
24
23
  expect(results).to eq("[STATSD]")
25
24
  end
26
- it 'includes stats that are available' do
27
- request_stat = double('request_stat')
28
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
29
- allow(request_stat).to receive(:stats).and_return(
30
- "total" => 100.21,
31
- "active.record" => 24)
25
+
26
+ it "includes stats that are available" do
27
+ request_stat = instance_double(InstStatsd::RequestStat)
28
+ allow(request_stat).to receive_messages(exclusive_stats: nil, stats: { "total" => 100.21,
29
+ "active.record" => 24 })
32
30
  results = @logger.build_log_message(request_stat)
33
31
  expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00)")
34
32
  end
35
33
 
36
- it 'includes exclusive_stats if there are any' do
37
- request_stat = double('request_stat')
38
- allow(request_stat).to receive(:stats).and_return(
39
- "total" => 100.21,
40
- "active.record" => 24)
41
- allow(request_stat).to receive(:exclusive_stats).and_return(
42
- "total" => 54.32,
43
- "active.record" => 1)
34
+ it "includes exclusive_stats if there are any" do
35
+ request_stat = instance_double(InstStatsd::RequestStat)
36
+ allow(request_stat).to receive_messages(stats: { "total" => 100.21,
37
+ "active.record" => 24 },
38
+ exclusive_stats: { "total" => 54.32,
39
+ "active.record" => 1 })
44
40
  results = @logger.build_log_message(request_stat)
45
- expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00) (exclusive_total: 54.32) (exclusive_active_record: 1.00)")
41
+ expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00) (exclusive_total: 54.32) (exclusive_active_record: 1.00)") # rubocop:disable Layout/LineLength
46
42
  end
47
43
 
48
- describe 'decimal precision' do
49
- it 'forces 2 decimal precision' do
50
- request_stat = double('request_stat')
51
- allow(request_stat).to receive(:stats).and_return(total: 72.1)
52
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
44
+ describe "decimal precision" do
45
+ it "forces 2 decimal precision" do
46
+ request_stat = instance_double(InstStatsd::RequestStat)
47
+ allow(request_stat).to receive_messages(stats: { total: 72.1 }, exclusive_stats: nil)
53
48
  results = @logger.build_log_message(request_stat)
54
49
  expect(results).to eq("[STATSD] (total: 72.10)")
55
50
  end
56
- it 'rounds values to 2 decimals' do
57
- request_stat = double('request_stat')
51
+
52
+ it "rounds values to 2 decimals" do
53
+ request_stat = instance_double(InstStatsd::RequestStat)
58
54
  allow(request_stat).to receive(:stats).and_return(total: 72.1382928)
59
55
  allow(request_stat).to receive(:exclusive_stats).and_return(nil)
60
56
  results = @logger.build_log_message(request_stat)
@@ -64,28 +60,25 @@ describe InstStatsd::RequestLogger do
64
60
  expect(results).to eq("[STATSD] (total: 72.13)")
65
61
  end
66
62
  end
67
-
68
63
  end
69
64
 
70
- describe '#log' do
71
- it 'sends info method to logger if logger exists' do
72
- std_out_logger = Logger.new(STDOUT)
65
+ describe "#log" do
66
+ it "sends info method to logger if logger exists" do
67
+ std_out_logger = Logger.new($stdout)
73
68
  logger = InstStatsd::RequestLogger.new(std_out_logger)
74
69
  expect(std_out_logger).to receive(:info)
75
- request_stat = double('request_stat')
76
- allow(request_stat).to receive(:stats).and_return({})
77
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
70
+ request_stat = instance_double(InstStatsd::RequestStat)
71
+ allow(request_stat).to receive_messages(stats: {}, exclusive_stats: nil)
78
72
  logger.log(request_stat)
79
73
  end
80
- it 'sends info method with build_log_message output if logger exists' do
81
- std_out_logger = Logger.new(STDOUT)
74
+
75
+ it "sends info method with build_log_message output if logger exists" do
76
+ std_out_logger = Logger.new($stdout)
82
77
  logger = InstStatsd::RequestLogger.new(std_out_logger)
83
78
  expect(std_out_logger).to receive(:info).with("[DEFAULT_METRICS] (total: 100.20)")
84
- request_stat = double('request_stat')
85
- allow(request_stat).to receive(:stats).and_return(total: 100.2)
86
- allow(request_stat).to receive(:exclusive_stats).and_return(nil)
79
+ request_stat = instance_double(InstStatsd::RequestStat)
80
+ allow(request_stat).to receive_messages(stats: { total: 100.2 }, exclusive_stats: nil)
87
81
  logger.log(request_stat, "DEFAULT_METRICS")
88
82
  end
89
83
  end
90
-
91
84
  end