Class: Honeybadger::Instrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/honeybadger/instrumentation.rb

Overview

Honeybadger::Instrumentation defines the API for collecting metric data from anywhere in an application. These class methods may be used directly, or from the Honeybadger singleton instance. There are three usage variations as show in the example below:

Examples:

class TicketsController < ApplicationController
  def create
    # pass a block
    Honeybadger.time('create.ticket') { Ticket.create(params[:ticket]) }

    # pass a lambda argument
    Honeybadger.time 'create.ticket', ->{ Ticket.create(params[:ticket]) }

    # pass the duration argument
    duration = timing_method { Ticket.create(params[:ticket]) }
    Honeybadger.time 'create.ticket', duration: duration
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ Instrumentation

Returns a new instance of Instrumentation.



30
31
32
# File 'lib/honeybadger/instrumentation.rb', line 30

def initialize(agent)
  @agent = agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



28
29
30
# File 'lib/honeybadger/instrumentation.rb', line 28

def agent
  @agent
end

Instance Method Details

#decrement_counter(name, *args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/honeybadger/instrumentation.rb', line 109

def decrement_counter(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  value = nil

  if callable
    value = callable.call
  elsif block_given?
    value = yield
  else
    value = attributes.delete(:by) || attributes.delete(:value) || 1
  end

  Honeybadger::Counter.register(registry, name, attributes).tap do |counter|
    counter.count(value * -1)
  end
end

#extract_attributes(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



150
151
152
# File 'lib/honeybadger/instrumentation.rb', line 150

def extract_attributes(args)
  args.select { |a| a.is_a?(Hash) }.first || {}
end

#extract_callable(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
# File 'lib/honeybadger/instrumentation.rb', line 155

def extract_callable(args)
  args.select { |a| a.respond_to?(:call) }.first
end

#gauge(name, *args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/honeybadger/instrumentation.rb', line 127

def gauge(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  value = nil

  if callable
    value = callable.call
  elsif block_given?
    value = yield
  else
    value = attributes.delete(:duration) || attributes.delete(:value)
  end

  Honeybadger::Gauge.register(registry, name, attributes).tap do |gauge|
    if value.nil?
      agent.config.logger.warn("No value found for gauge #{name}. Must specify value. Skipping.")
    else
      gauge.record(value)
    end
  end
end

#histogram(name, *args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/honeybadger/instrumentation.rb', line 69

def histogram(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  value = nil

  if callable
    value = monotonic_timer{ callable.call }[0]
  elsif block_given?
    value = monotonic_timer{ yield }[0]
  else
    value = attributes.delete(:duration) || attributes.delete(:value)
  end

  Honeybadger::Histogram.register(registry, name, attributes).tap do |histogram|
    if value.nil?
      agent.config.logger.warn("No value found for histogram #{name}. Must specify either duration or value. Skipping.")
    else
      histogram.record(value)
    end
  end
end

#increment_counter(name, *args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/honeybadger/instrumentation.rb', line 91

def increment_counter(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  value = nil

  if callable
    value = callable.call
  elsif block_given?
    value = yield
  else
    value = attributes.delete(:by) || attributes.delete(:value) || 1
  end

  Honeybadger::Counter.register(registry, name, attributes).tap do |counter|
    counter.count(value)
  end
end

#monotonic_timerObject

returns two parameters, the first is the duration of the execution, and the second is the return value of the passed block



40
41
42
43
44
45
# File 'lib/honeybadger/instrumentation.rb', line 40

def monotonic_timer
  start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  result = yield
  finish_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  [((finish_time - start_time) * 1000).round(2), result]
end

#registryObject



34
35
36
# File 'lib/honeybadger/instrumentation.rb', line 34

def registry
  agent.registry
end

#time(name, *args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/honeybadger/instrumentation.rb', line 47

def time(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  value = nil

  if callable
    value = monotonic_timer{ callable.call }[0]
  elsif block_given?
    value = monotonic_timer{ yield }[0]
  else
    value = attributes.delete(:duration) || attributes.delete(:value)
  end

  Honeybadger::Timer.register(registry, name, attributes).tap do |timer|
    if value.nil?
      agent.config.logger.warn("No value found for timer #{name}. Must specify either duration or value. Skipping.")
    else
      timer.record(value)
    end
  end
end