Module: Krane::StatsD::MeasureMethods

Included in:
DeployTask, GlobalDeployTask, ResourceDeployer, ResourceWatcher
Defined in:
lib/krane/statsd.rb

Instance Method Summary collapse

Instance Method Details

#measure_method(method_name, metric = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/krane/statsd.rb', line 27

def measure_method(method_name, metric = nil)
  unless method_defined?(method_name) || private_method_defined?(method_name)
    raise NotImplementedError, "Cannot instrument undefined method #{method_name}"
  end

  unless const_defined?("InstrumentationProxy")
    const_set("InstrumentationProxy", Module.new)
    should_prepend = true
  end

  metric ||= "#{method_name}.duration"
  self::InstrumentationProxy.send(:define_method, method_name) do |*args, **kwargs, &block|
    begin
      start_time = Time.now.utc
      super(*args, **kwargs, &block)
    rescue
      error = true
      raise
    ensure
      dynamic_tags = send(:statsd_tags) if respond_to?(:statsd_tags, true)
      dynamic_tags ||= {}
      if error
        dynamic_tags[:error] = error if dynamic_tags.is_a?(Hash)
        dynamic_tags << "error:#{error}" if dynamic_tags.is_a?(Array)
      end

      Krane::StatsD.client.distribution(
        metric,
        Krane::StatsD.duration(start_time),
        tags: dynamic_tags
      )
    end
  end

  prepend(self::InstrumentationProxy) if should_prepend
end