Module: KubernetesDeploy::StatsD::MeasureMethods

Included in:
DeployTask, ResourceWatcher
Defined in:
lib/kubernetes-deploy/statsd.rb

Instance Method Summary collapse

Instance Method Details

#measure_method(method_name, metric = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kubernetes-deploy/statsd.rb', line 41

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, &block|
    begin
      start_time = Time.now.utc
      super(*args, &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

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

  prepend(self::InstrumentationProxy) if should_prepend
end