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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/honeybadger/instrumentation.rb', line 106

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

  value = if callable
    callable.call
  elsif block_given?
    yield
  else
    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.



145
146
147
# File 'lib/honeybadger/instrumentation.rb', line 145

def extract_attributes(args)
  args.find { |a| a.is_a?(Hash) } || {}
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.



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

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

#gauge(name, *args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/honeybadger/instrumentation.rb', line 123

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

  value = if callable
    callable.call
  elsif block_given?
    yield
  else
    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



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

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

  value = if callable
    monotonic_timer { callable.call }[0]
  elsif block_given?
    monotonic_timer { yield }[0]
  else
    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



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

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

  value = if callable
    callable.call
  elsif block_given?
    yield
  else
    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
# File 'lib/honeybadger/instrumentation.rb', line 47

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

  value = if callable
    monotonic_timer { callable.call }[0]
  elsif block_given?
    monotonic_timer { yield }[0]
  else
    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