Method: PdMetrics.time

Defined in:
lib/pd_metrics.rb

.time(namespace, key, tags = {}, additional_data = {}) ⇒ Object

Captures timing metrics for a block of Ruby code.

PdMetrics.time('api', 'receive_email', account: 'Netflix') do
  # process the email
end

Assuming the request took 2 seconds to process, the following log message will be written in SumoLogic.

api #account=Netflix|#receive_email=2.0|#failed=false|

Additionally, the following histogram metrics will be captured in DataDog

api.receive_email.count
api.receive_email.avg
api.receive_email.median
api.receive_email.max
api.receive_email.95percentile

In addition to capturing latency of the request, the success or failure of the block of code is captured as well. It is considered failed if an exception is thrown.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pd_metrics.rb', line 70

def self.time(namespace, key, tags = {}, additional_data = {})
  failed = false
  start = Time.now
  yield
rescue
  failed = true
  raise
ensure
  timing_data = tags || {}
  timing_data[key] = (Time.now - start).histogram
  timing_data['failed'] = failed
  send_event(namespace, timing_data)
end