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



93
94
95
96
97
98
99
100
101
# File 'lib/honeybadger/instrumentation.rb', line 93

def decrement_counter(name, *args)
  attributes = extract_attributes(args)
  by = extract_callable(args)&.call || attributes.delete(:by) || 1
  by = yield if block_given?

  Honeybadger::Counter.register(registry, name, attributes).tap do |counter|
    counter.count(by * -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.



114
115
116
# File 'lib/honeybadger/instrumentation.rb', line 114

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.



119
120
121
# File 'lib/honeybadger/instrumentation.rb', line 119

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

#gauge(name, *args) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/honeybadger/instrumentation.rb', line 103

def gauge(name, *args)
  attributes = extract_attributes(args)
  value = extract_callable(args)&.call || attributes.delete(:value)
  value = yield if block_given?

  Honeybadger::Gauge.register(registry, name, attributes).tap do |gauge|
    gauge.record(value)
  end
end

#histogram(name, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/honeybadger/instrumentation.rb', line 65

def histogram(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  duration = attributes.delete(:duration)

  if callable
    duration = monotonic_timer{ callable.call }[0]
  elsif block_given?
    duration = monotonic_timer{ yield }[0]
  end

  raise 'No duration found' if duration.nil?

  Honeybadger::Histogram.register(registry, name, attributes).tap do |histogram|
    histogram.record(duration)
  end
end

#increment_counter(name, *args) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/honeybadger/instrumentation.rb', line 83

def increment_counter(name, *args)
  attributes = extract_attributes(args)
  by = extract_callable(args)&.call || attributes.delete(:by) || 1
  by = yield if block_given?

  Honeybadger::Counter.register(registry, name, attributes).tap do |counter|
    counter.count(by)
  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
# File 'lib/honeybadger/instrumentation.rb', line 47

def time(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  duration = attributes.delete(:duration)

  if callable
    duration = monotonic_timer{ callable.call }[0]
  elsif block_given?
    duration = monotonic_timer{ yield }[0]
  end

  raise 'No duration found' if duration.nil?

  Honeybadger::Timer.register(registry, name, attributes).tap do |timer|
    timer.record(duration)
  end
end