semantic_logger 4.1.1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/lib/semantic_logger.rb +6 -13
  3. data/lib/semantic_logger/ansi_colors.rb +10 -10
  4. data/lib/semantic_logger/appender.rb +42 -26
  5. data/lib/semantic_logger/appender/async.rb +179 -0
  6. data/lib/semantic_logger/appender/async_batch.rb +95 -0
  7. data/lib/semantic_logger/appender/bugsnag.rb +2 -1
  8. data/lib/semantic_logger/appender/elasticsearch.rb +113 -81
  9. data/lib/semantic_logger/appender/elasticsearch_http.rb +1 -3
  10. data/lib/semantic_logger/appender/file.rb +1 -3
  11. data/lib/semantic_logger/appender/graylog.rb +6 -5
  12. data/lib/semantic_logger/appender/honeybadger.rb +0 -2
  13. data/lib/semantic_logger/appender/http.rb +25 -10
  14. data/lib/semantic_logger/appender/kafka.rb +1 -3
  15. data/lib/semantic_logger/appender/mongodb.rb +1 -3
  16. data/lib/semantic_logger/appender/new_relic.rb +7 -3
  17. data/lib/semantic_logger/appender/sentry.rb +6 -7
  18. data/lib/semantic_logger/appender/splunk.rb +1 -2
  19. data/lib/semantic_logger/appender/splunk_http.rb +3 -4
  20. data/lib/semantic_logger/appender/syslog.rb +1 -3
  21. data/lib/semantic_logger/appender/tcp.rb +7 -9
  22. data/lib/semantic_logger/appender/udp.rb +0 -2
  23. data/lib/semantic_logger/appender/wrapper.rb +0 -2
  24. data/lib/semantic_logger/base.rb +76 -19
  25. data/lib/semantic_logger/formatters.rb +37 -0
  26. data/lib/semantic_logger/formatters/base.rb +10 -3
  27. data/lib/semantic_logger/formatters/json.rb +2 -6
  28. data/lib/semantic_logger/formatters/one_line.rb +18 -0
  29. data/lib/semantic_logger/formatters/raw.rb +8 -2
  30. data/lib/semantic_logger/formatters/signalfx.rb +169 -0
  31. data/lib/semantic_logger/log.rb +23 -14
  32. data/lib/semantic_logger/loggable.rb +88 -15
  33. data/lib/semantic_logger/logger.rb +0 -20
  34. data/lib/semantic_logger/metric/new_relic.rb +75 -0
  35. data/lib/semantic_logger/metric/signalfx.rb +123 -0
  36. data/lib/semantic_logger/{metrics → metric}/statsd.rb +20 -8
  37. data/lib/semantic_logger/processor.rb +67 -169
  38. data/lib/semantic_logger/semantic_logger.rb +7 -31
  39. data/lib/semantic_logger/subscriber.rb +32 -36
  40. data/lib/semantic_logger/utils.rb +47 -0
  41. data/lib/semantic_logger/version.rb +1 -1
  42. data/test/appender/async_batch_test.rb +61 -0
  43. data/test/appender/async_test.rb +45 -0
  44. data/test/appender/elasticsearch_http_test.rb +3 -3
  45. data/test/appender/elasticsearch_test.rb +211 -49
  46. data/test/appender/file_test.rb +9 -8
  47. data/test/appender/mongodb_test.rb +3 -3
  48. data/test/appender/newrelic_rpm.rb +6 -0
  49. data/test/appender/sentry_test.rb +3 -1
  50. data/test/appender/wrapper_test.rb +29 -0
  51. data/test/concerns/compatibility_test.rb +64 -60
  52. data/test/debug_as_trace_logger_test.rb +62 -77
  53. data/test/formatters/one_line_test.rb +61 -0
  54. data/test/formatters/signalfx_test.rb +200 -0
  55. data/test/formatters_test.rb +36 -0
  56. data/test/in_memory_appender.rb +9 -0
  57. data/test/in_memory_appender_helper.rb +43 -0
  58. data/test/in_memory_batch_appender.rb +9 -0
  59. data/test/in_memory_metrics_appender.rb +14 -0
  60. data/test/loggable_test.rb +15 -30
  61. data/test/logger_test.rb +181 -135
  62. data/test/measure_test.rb +212 -113
  63. data/test/metric/new_relic_test.rb +36 -0
  64. data/test/metric/signalfx_test.rb +78 -0
  65. data/test/semantic_logger_test.rb +58 -65
  66. data/test/test_helper.rb +19 -2
  67. metadata +33 -7
  68. data/lib/semantic_logger/metrics/new_relic.rb +0 -30
  69. data/lib/semantic_logger/metrics/udp.rb +0 -80
  70. data/test/mock_logger.rb +0 -29
@@ -0,0 +1,36 @@
1
+ # So that the NewRelic appender will load the mock
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+ require_relative '../test_helper'
4
+
5
+ # Unit Test for SemanticLogger::Appender::NewRelic
6
+ module Metric
7
+ class NewRelicTest < Minitest::Test
8
+ describe SemanticLogger::Appender::NewRelic do
9
+
10
+ before do
11
+ @appender = SemanticLogger::Metric::NewRelic.new
12
+ @message = 'AppenderNewRelicTest log message'
13
+ end
14
+
15
+ it 'logs counter metric' do
16
+ name = amount = nil
17
+ NewRelic::Agent.stub(:increment_metric, -> name_, amount_ { name = name_, amount = amount_ }) do
18
+ @appender.info(message: @message, metric: 'User/authenticated')
19
+ end
20
+ assert_equal 'Custom/User/authenticated', name.first
21
+ assert_equal 1, amount
22
+ end
23
+
24
+ it 'logs duration metric' do
25
+ name = duration = nil
26
+ NewRelic::Agent.stub(:record_metric, -> name_, duration_ { name = name_, duration = duration_ }) do
27
+ @appender.measure_info(message: @message, metric: 'User/authenticate') do
28
+ sleep 0.001
29
+ end
30
+ end
31
+ assert_equal 'Custom/User/authenticate', name.first
32
+ assert duration
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,78 @@
1
+ require_relative '../test_helper'
2
+
3
+ module Appender
4
+ class SignalfxTest < Minitest::Test
5
+ describe SemanticLogger::Metric::Signalfx do
6
+ before do
7
+ @metric = '/user/login'
8
+ @log = SemanticLogger::Log.new('User', :debug)
9
+ @log.metric = @metric
10
+ end
11
+
12
+ let :appender do
13
+ if ENV['SIGNALFX_TOKEN']
14
+ SemanticLogger::Metric::Signalfx.new(token: ENV['SIGNALFX_TOKEN'])
15
+ else
16
+ Net::HTTP.stub_any_instance(:start, true) do
17
+ @appender = SemanticLogger::Metric::Signalfx.new(token: 'TEST')
18
+ end
19
+ end
20
+ end
21
+
22
+ describe 'log message' do
23
+ let :response do
24
+ # Do not stub if the token is available in the environment
25
+ if ENV['SIGNALFX_TOKEN']
26
+ appender.log(@log)
27
+ else
28
+ response_mock = Struct.new(:code, :body)
29
+ request = nil
30
+ appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do
31
+ appender.log(@log)
32
+ end
33
+ end
34
+ end
35
+
36
+ it 'send counter metric when there is no duration' do
37
+ assert response
38
+ end
39
+
40
+ it 'send custom counter metric when there is no duration' do
41
+ @log.metric = 'Filter/count'
42
+ @log.dimensions = {action: 'hit', user: 'jbloggs', state: 'FL'}
43
+ assert response
44
+ end
45
+
46
+ it 'send gauge metric when log includes duration' do
47
+ @log.duration = 1234
48
+ assert response
49
+ end
50
+
51
+ it 'whitelists dimensions' do
52
+ @log.named_tags = {user_id: 47, application: 'sample', tracking_number: 7474, session_id: 'hsdhngsd'}
53
+ appender.formatter.dimensions = [:user_id, :application]
54
+ assert response
55
+ end
56
+ end
57
+
58
+ describe 'should_log?' do
59
+ it 'logs metric only metric' do
60
+ assert appender.should_log?(@log)
61
+ end
62
+
63
+ it 'not logs when no metric' do
64
+ @log.message = 'blah'
65
+ @log.metric = nil
66
+ refute appender.should_log?(@log)
67
+ end
68
+
69
+ it 'logs metric only metric with dimensions' do
70
+ @log.metric = 'Filter/count'
71
+ @log.dimensions = {action: 'hit', user: 'jbloggs', state: 'FL'}
72
+ assert appender.should_log?(@log)
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -23,7 +23,7 @@ class SemanticLoggerTest < Minitest::Test
23
23
  @appender = SemanticLogger.add_appender(file_name: 'sample.log', formatter: :json)
24
24
  assert @appender.is_a?(SemanticLogger::Appender::File)
25
25
  assert SemanticLogger.appenders.include?(@appender)
26
- assert @appender.formatter.is_a?(SemanticLogger::Formatters::Json)
26
+ assert @appender.formatter.is_a?(SemanticLogger::Formatters::Json), @appender.formatter.inspect
27
27
  end
28
28
 
29
29
  it 'adds stream appender' do
@@ -116,40 +116,25 @@ class SemanticLoggerTest < Minitest::Test
116
116
  end
117
117
 
118
118
  describe 'mock_logger' do
119
- before do
120
- # Use a mock logger that just keeps the last logged entry in an instance
121
- # variable
122
- SemanticLogger.default_level = :trace
123
- SemanticLogger.backtrace_level = nil
124
- @mock_logger = MockLogger.new
125
- @appender = SemanticLogger.add_appender(logger: @mock_logger)
126
-
127
- # Use this test's class name as the application name in the log output
128
- @logger = SemanticLogger['LoggerTest']
129
- @hash = {session_id: 'HSSKLEU@JDK767', tracking_number: 12345}
130
- @hash_str = @hash.inspect.sub("{", "\\{").sub("}", "\\}")
131
- @thread_name = Thread.current.name
132
- end
133
-
134
- after do
135
- SemanticLogger.remove_appender(@appender)
136
- end
119
+ include InMemoryAppenderHelper
137
120
 
138
121
  describe '.tagged' do
139
122
  it 'add tags to log entries' do
140
123
  SemanticLogger.tagged('12345', 'DJHSFK') do
141
- @logger.info('Hello world')
142
- SemanticLogger.flush
143
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[12345\] \[DJHSFK\] LoggerTest -- Hello world/, @mock_logger.message)
124
+ logger.info('Hello world')
125
+
126
+ assert log = log_message
127
+ assert_equal %w(12345 DJHSFK), log.tags
144
128
  end
145
129
  end
146
130
 
147
131
  it 'add embedded tags to log entries' do
148
132
  SemanticLogger.tagged('First Level', 'tags') do
149
133
  SemanticLogger.tagged('Second Level') do
150
- @logger.info('Hello world')
151
- SemanticLogger.flush
152
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[First Level\] \[tags\] \[Second Level\] LoggerTest -- Hello world/, @mock_logger.message)
134
+ logger.info('Hello world')
135
+
136
+ assert log = log_message
137
+ assert_equal ['First Level', 'tags', 'Second Level'], log.tags
153
138
  end
154
139
  assert_equal 2, SemanticLogger.tags.count, SemanticLogger.tags
155
140
  assert_equal 'First Level', SemanticLogger.tags.first
@@ -189,9 +174,10 @@ class SemanticLoggerTest < Minitest::Test
189
174
  SemanticLogger.named_tagged(level1: 1) do
190
175
  SemanticLogger.named_tagged(level2: 2, more: 'data') do
191
176
  SemanticLogger.named_tagged(level3: 3) do
192
- @logger.info('Hello world')
193
- SemanticLogger.flush
194
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \{level1: 1, level2: 2, more: data, level3: 3\} LoggerTest -- Hello world/, @mock_logger.message)
177
+ logger.info('Hello world')
178
+
179
+ assert log = log_message
180
+ assert_equal({level1: 1, level2: 2, more: 'data', level3: 3}, log.named_tags)
195
181
  end
196
182
  end
197
183
  end
@@ -200,10 +186,11 @@ class SemanticLoggerTest < Minitest::Test
200
186
 
201
187
  describe '.fast_tag' do
202
188
  it 'add string tag to log entries' do
203
- @logger.fast_tag('12345') do
204
- @logger.info('Hello world')
205
- SemanticLogger.flush
206
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[12345\] LoggerTest -- Hello world/, @mock_logger.message)
189
+ logger.fast_tag('12345') do
190
+ logger.info('Hello world')
191
+
192
+ assert log = log_message
193
+ assert_equal %w(12345), log.tags
207
194
  end
208
195
  end
209
196
  end
@@ -215,28 +202,30 @@ class SemanticLoggerTest < Minitest::Test
215
202
 
216
203
  it 'not log at a level below the global default' do
217
204
  assert_equal :debug, SemanticLogger.default_level
218
- assert_equal :debug, @logger.level
219
- @logger.trace('hello world', @hash) { 'Calculations' }
220
- SemanticLogger.flush
221
- assert_nil @mock_logger.message
205
+ assert_equal :debug, logger.level
206
+ logger.trace('hello world')
207
+
208
+ refute log_message
222
209
  end
223
210
 
224
211
  it 'log at the instance level' do
225
212
  assert_equal :debug, SemanticLogger.default_level
226
- @logger.level = :trace
227
- assert_equal :trace, @logger.level
228
- @logger.trace('hello world', @hash) { 'Calculations' }
229
- SemanticLogger.flush
230
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ T \[\d+:#{@thread_name}\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message)
213
+ logger.level = :trace
214
+ assert_equal :trace, logger.level
215
+ logger.trace('hello world')
216
+
217
+ assert log = log_message
218
+ assert_equal :trace, log.level
219
+ assert_equal 'hello world', log.message
231
220
  end
232
221
 
233
222
  it 'not log at a level below the instance level' do
234
223
  assert_equal :debug, SemanticLogger.default_level
235
- @logger.level = :warn
236
- assert_equal :warn, @logger.level
237
- @logger.debug('hello world', @hash) { 'Calculations' }
238
- SemanticLogger.flush
239
- assert_nil @mock_logger.message
224
+ logger.level = :warn
225
+ assert_equal :warn, logger.level
226
+ logger.debug('hello world')
227
+
228
+ refute log_message
240
229
  end
241
230
  end
242
231
 
@@ -247,35 +236,39 @@ class SemanticLoggerTest < Minitest::Test
247
236
 
248
237
  it 'not log at a level below the silence level' do
249
238
  assert_equal :info, SemanticLogger.default_level
250
- assert_equal :info, @logger.level
251
- @logger.silence do
252
- @logger.warn('hello world', @hash) { 'Calculations' }
253
- @logger.info('hello world', @hash) { 'Calculations' }
254
- @logger.debug('hello world', @hash) { 'Calculations' }
255
- @logger.trace('hello world', @hash) { 'Calculations' }
239
+ assert_equal :info, logger.level
240
+ logger.silence do
241
+ logger.warn('hello world')
242
+ logger.info('hello world')
243
+ logger.debug('hello world')
244
+ logger.trace('hello world')
256
245
  end
257
- SemanticLogger.flush
258
- assert_nil @mock_logger.message
246
+
247
+ refute log_message
259
248
  end
260
249
 
261
250
  it 'log at the instance level even with the silencer at a higher level' do
262
- @logger.level = :trace
263
- assert_equal :trace, @logger.level
264
- @logger.silence do
265
- @logger.trace('hello world', @hash) { 'Calculations' }
251
+ logger.level = :trace
252
+ assert_equal :trace, logger.level
253
+ logger.silence do
254
+ logger.trace('hello world')
266
255
  end
267
- SemanticLogger.flush
268
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ T \[\d+:#{@thread_name}\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message)
256
+
257
+ assert log = log_message
258
+ assert_equal :trace, log.level
259
+ assert_equal 'hello world', log.message
269
260
  end
270
261
 
271
262
  it 'log at a silence level below the default level' do
272
263
  assert_equal :info, SemanticLogger.default_level
273
- assert_equal :info, @logger.level
274
- @logger.silence(:debug) do
275
- @logger.debug('hello world', @hash) { 'Calculations' }
264
+ assert_equal :info, logger.level
265
+ logger.silence(:debug) do
266
+ logger.debug('hello world')
276
267
  end
277
- SemanticLogger.flush
278
- assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message)
268
+
269
+ assert log = log_message
270
+ assert_equal :debug, log.level
271
+ assert_equal 'hello world', log.message
279
272
  end
280
273
  end
281
274
 
data/test/test_helper.rb CHANGED
@@ -8,8 +8,25 @@ require 'minitest/autorun'
8
8
  require 'minitest/reporters'
9
9
  require 'minitest/stub_any_instance'
10
10
  require 'semantic_logger'
11
- require 'logger'
12
- require_relative 'mock_logger'
11
+ #require 'logger'
12
+ require_relative 'in_memory_appender'
13
+ require_relative 'in_memory_batch_appender'
14
+ require_relative 'in_memory_metrics_appender'
15
+ require_relative 'in_memory_appender_helper'
13
16
  require 'awesome_print'
14
17
 
15
18
  #Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
19
+ class Minitest::Test
20
+ # Use AwesomePrint to display diffs
21
+ define_method :mu_pp, &:awesome_inspect
22
+
23
+ # Use AwesomePrint to display messages
24
+ def message msg = nil, ending = nil, &default
25
+ proc {
26
+ msg = msg.call.chomp(".") if Proc === msg
27
+ custom_message = "#{msg.ai}.\n" unless msg.nil? or msg.to_s.empty?
28
+ "#{custom_message}#{default.call}#{ending || "."}"
29
+ }
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.1
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-11 00:00:00.000000000 Z
11
+ date: 2017-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -38,6 +38,8 @@ files:
38
38
  - lib/semantic_logger.rb
39
39
  - lib/semantic_logger/ansi_colors.rb
40
40
  - lib/semantic_logger/appender.rb
41
+ - lib/semantic_logger/appender/async.rb
42
+ - lib/semantic_logger/appender/async_batch.rb
41
43
  - lib/semantic_logger/appender/bugsnag.rb
42
44
  - lib/semantic_logger/appender/elasticsearch.rb
43
45
  - lib/semantic_logger/appender/elasticsearch_http.rb
@@ -59,24 +61,30 @@ files:
59
61
  - lib/semantic_logger/concerns/compatibility.rb
60
62
  - lib/semantic_logger/core_ext/thread.rb
61
63
  - lib/semantic_logger/debug_as_trace_logger.rb
64
+ - lib/semantic_logger/formatters.rb
62
65
  - lib/semantic_logger/formatters/base.rb
63
66
  - lib/semantic_logger/formatters/color.rb
64
67
  - lib/semantic_logger/formatters/default.rb
65
68
  - lib/semantic_logger/formatters/json.rb
69
+ - lib/semantic_logger/formatters/one_line.rb
66
70
  - lib/semantic_logger/formatters/raw.rb
71
+ - lib/semantic_logger/formatters/signalfx.rb
67
72
  - lib/semantic_logger/formatters/syslog.rb
68
73
  - lib/semantic_logger/formatters/syslog_cee.rb
69
74
  - lib/semantic_logger/jruby/garbage_collection_logger.rb
70
75
  - lib/semantic_logger/log.rb
71
76
  - lib/semantic_logger/loggable.rb
72
77
  - lib/semantic_logger/logger.rb
73
- - lib/semantic_logger/metrics/new_relic.rb
74
- - lib/semantic_logger/metrics/statsd.rb
75
- - lib/semantic_logger/metrics/udp.rb
78
+ - lib/semantic_logger/metric/new_relic.rb
79
+ - lib/semantic_logger/metric/signalfx.rb
80
+ - lib/semantic_logger/metric/statsd.rb
76
81
  - lib/semantic_logger/processor.rb
77
82
  - lib/semantic_logger/semantic_logger.rb
78
83
  - lib/semantic_logger/subscriber.rb
84
+ - lib/semantic_logger/utils.rb
79
85
  - lib/semantic_logger/version.rb
86
+ - test/appender/async_batch_test.rb
87
+ - test/appender/async_test.rb
80
88
  - test/appender/bugsnag_test.rb
81
89
  - test/appender/elasticsearch_http_test.rb
82
90
  - test/appender/elasticsearch_test.rb
@@ -99,10 +107,18 @@ files:
99
107
  - test/debug_as_trace_logger_test.rb
100
108
  - test/formatters/color_test.rb
101
109
  - test/formatters/default_test.rb
110
+ - test/formatters/one_line_test.rb
111
+ - test/formatters/signalfx_test.rb
112
+ - test/formatters_test.rb
113
+ - test/in_memory_appender.rb
114
+ - test/in_memory_appender_helper.rb
115
+ - test/in_memory_batch_appender.rb
116
+ - test/in_memory_metrics_appender.rb
102
117
  - test/loggable_test.rb
103
118
  - test/logger_test.rb
104
119
  - test/measure_test.rb
105
- - test/mock_logger.rb
120
+ - test/metric/new_relic_test.rb
121
+ - test/metric/signalfx_test.rb
106
122
  - test/semantic_logger_test.rb
107
123
  - test/test_helper.rb
108
124
  homepage: https://github.com/rocketjob/semantic_logger
@@ -130,6 +146,8 @@ signing_key:
130
146
  specification_version: 4
131
147
  summary: Scalable, next generation enterprise logging for Ruby
132
148
  test_files:
149
+ - test/appender/async_batch_test.rb
150
+ - test/appender/async_test.rb
133
151
  - test/appender/bugsnag_test.rb
134
152
  - test/appender/elasticsearch_http_test.rb
135
153
  - test/appender/elasticsearch_test.rb
@@ -152,9 +170,17 @@ test_files:
152
170
  - test/debug_as_trace_logger_test.rb
153
171
  - test/formatters/color_test.rb
154
172
  - test/formatters/default_test.rb
173
+ - test/formatters/one_line_test.rb
174
+ - test/formatters/signalfx_test.rb
175
+ - test/formatters_test.rb
176
+ - test/in_memory_appender.rb
177
+ - test/in_memory_appender_helper.rb
178
+ - test/in_memory_batch_appender.rb
179
+ - test/in_memory_metrics_appender.rb
155
180
  - test/loggable_test.rb
156
181
  - test/logger_test.rb
157
182
  - test/measure_test.rb
158
- - test/mock_logger.rb
183
+ - test/metric/new_relic_test.rb
184
+ - test/metric/signalfx_test.rb
159
185
  - test/semantic_logger_test.rb
160
186
  - test/test_helper.rb
@@ -1,30 +0,0 @@
1
- module SemanticLogger
2
- module Metrics
3
- class NewRelic < Subscriber
4
- attr_accessor :prefix
5
-
6
- # Parameters:
7
- # :prefix [String]
8
- # Prefix to add to every metric before forwarding to NewRelic
9
- # Default: 'Custom'
10
- def initialize(options = {})
11
- options = options.dup
12
- @prefix = options.delete(:prefix) || 'Custom'
13
- raise(ArgumentError, "Unknown options: #{options.inspect}") if options.size > 0
14
- end
15
-
16
- def call(log)
17
- metric = log.metric
18
- # Add prefix for NewRelic
19
- metric = "#{prefix}/#{metric}" unless metric.start_with?(prefix)
20
-
21
- if duration = log.duration
22
- # Convert duration to seconds
23
- ::NewRelic::Agent.record_metric(metric, duration / 1000.0)
24
- else
25
- ::NewRelic::Agent.increment_metric(metric, log.metric_amount || 1)
26
- end
27
- end
28
- end
29
- end
30
- end