Module: Honeybadger::InstrumentationHelper

Overview

Honeybadger::InstrumentationHelper is a module that can be included into any class. This module provides a convenient DSL around the instrumentation methods to provide a cleaner interface. There are three usage variations as show in the example below:

Examples:

class TicketsController < ApplicationController
  include Honeybadger::InstrumentationHelper

  def create
    metric_source 'controller'
    metric_attributes(foo: 'bar') # These attributes get tagged to all metrics called after.

    # pass a block
    time('create.ticket') { Ticket.create(params[:ticket]) }

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

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

Instance Method Summary collapse

Instance Method Details

#decrement_counter(name, *args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/honeybadger/instrumentation_helper.rb', line 88

def decrement_counter(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  if callable
    metric_instrumentation.decrement_counter(name, attributes, ->{ callable.call })
  elsif block_given?
    metric_instrumentation.decrement_counter(name, attributes, ->{ yield })
  else
    metric_instrumentation.decrement_counter(name, attributes)
  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.



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

def extract_attributes(args)
  attributes = metric_instrumentation.extract_attributes(args)
  attributes.merge(metric_source: @metric_source).merge(@metric_attributes || {}).compact
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_helper.rb', line 119

def extract_callable(args)
  metric_instrumentation.extract_callable(args)
end

#gauge(name, *args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/honeybadger/instrumentation_helper.rb', line 100

def gauge(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  if callable
    metric_instrumentation.gauge(name, attributes, ->{ callable.call })
  elsif block_given?
    metric_instrumentation.gauge(name, attributes, ->{ yield })
  else
    metric_instrumentation.gauge(name, attributes)
  end
end

#histogram(name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/honeybadger/instrumentation_helper.rb', line 64

def histogram(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  if callable
    metric_instrumentation.histogram(name, attributes, ->{ callable.call })
  elsif block_given?
    metric_instrumentation.histogram(name, attributes, ->{ yield })
  else
    metric_instrumentation.histogram(name, attributes)
  end
end

#increment_counter(name, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/honeybadger/instrumentation_helper.rb', line 76

def increment_counter(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  if callable
    metric_instrumentation.increment_counter(name, attributes, ->{ callable.call })
  elsif block_given?
    metric_instrumentation.increment_counter(name, attributes, ->{ yield })
  else
    metric_instrumentation.increment_counter(name, attributes)
  end
end

#metric_agent(agent) ⇒ Object



39
40
41
# File 'lib/honeybadger/instrumentation_helper.rb', line 39

def metric_agent(agent)
  @metric_agent = agent
end

#metric_attributes(attributes) ⇒ Object



47
48
49
50
# File 'lib/honeybadger/instrumentation_helper.rb', line 47

def metric_attributes(attributes)
  raise "metric_attributes expects a hash" unless attributes.is_a?(Hash)
  @metric_attributes = attributes
end

#metric_instrumentationObject



43
44
45
# File 'lib/honeybadger/instrumentation_helper.rb', line 43

def metric_instrumentation
  @metric_instrumentation ||= @metric_agent ? Honeybadger::Instrumentation.new(@metric_agent) : Honeybadger.instrumentation
end

#metric_source(source) ⇒ Object



35
36
37
# File 'lib/honeybadger/instrumentation_helper.rb', line 35

def metric_source(source)
  @metric_source = source
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



31
32
33
# File 'lib/honeybadger/instrumentation_helper.rb', line 31

def monotonic_timer
  metric_instrumentation.monotonic_timer { yield }
end

#time(name, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/honeybadger/instrumentation_helper.rb', line 52

def time(name, *args)
  attributes = extract_attributes(args)
  callable = extract_callable(args)
  if callable
    metric_instrumentation.time(name, attributes, ->{ callable.call })
  elsif block_given?
    metric_instrumentation.time(name, attributes, ->{ yield })
  else
    metric_instrumentation.time(name, attributes)
  end
end