Class: PrometheusExporter::Instrumentation::MethodProfiler
- Inherits:
-
Object
- Object
- PrometheusExporter::Instrumentation::MethodProfiler
- Defined in:
- lib/prometheus_exporter/instrumentation/method_profiler.rb
Class Method Summary collapse
- .clear ⇒ Object
- .patch(klass, methods, name) ⇒ Object
- .start(transfer = nil) ⇒ Object
- .stop ⇒ Object
- .transfer ⇒ Object
Class Method Details
.clear ⇒ Object
44 45 46 |
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 44 def self.clear Thread.current[:_method_profiler] = nil end |
.patch(klass, methods, name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 7 def self.patch(klass, methods, name) patches = methods.map do |method_name| <<~RUBY unless defined?(#{method_name}__mp_unpatched) alias_method :#{method_name}__mp_unpatched, :#{method_name} def #{method_name}(*args, &blk) unless prof = Thread.current[:_method_profiler] return #{method_name}__mp_unpatched(*args, &blk) end begin start = Process.clock_gettime(Process::CLOCK_MONOTONIC) #{method_name}__mp_unpatched(*args, &blk) ensure data = (prof[:#{name}] ||= {duration: 0.0, calls: 0}) data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start data[:calls] += 1 end end end RUBY end.join("\n") klass.class_eval patches end |
.start(transfer = nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 38 def self.start(transfer = nil) Thread.current[:_method_profiler] = transfer || { __start: Process.clock_gettime(Process::CLOCK_MONOTONIC) } end |
.stop ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 48 def self.stop finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) if data = Thread.current[:_method_profiler] Thread.current[:_method_profiler] = nil start = data.delete(:__start) data[:total_duration] = finish - start end data end |
.transfer ⇒ Object
32 33 34 35 36 |
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 32 def self.transfer result = Thread.current[:_method_profiler] Thread.current[:_method_profiler] = nil result end |