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



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

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.



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

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.



118
119
120
# File 'lib/honeybadger/instrumentation_helper.rb', line 118

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

#gauge(name, *args) ⇒ Object



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

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



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

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



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

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



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

def metric_agent(agent)
  @metric_agent = agent
end

#metric_attributes(attributes) ⇒ Object



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

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

#metric_instrumentationObject



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

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

#metric_source(source) ⇒ Object



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

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



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

def monotonic_timer
  metric_instrumentation.monotonic_timer { yield }
end

#time(name, *args) ⇒ Object



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

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