Class: PrometheusExporter::Instrumentation::MethodProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/method_profiler.rb

Class Method Summary collapse

Class Method Details

.clearObject



42
43
44
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 42

def self.clear
  Thread.current[:_method_profiler] = nil
end

.patch(klass, methods, name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 5

def self.patch(klass, methods, name)
  patches = methods.map do |method_name|
    "    unless defined?(\#{method_name}__mp_unpatched)\n      alias_method :\#{method_name}__mp_unpatched, :\#{method_name}\n      def \#{method_name}(*args, &blk)\n        unless prof = Thread.current[:_method_profiler]\n          return \#{method_name}__mp_unpatched(*args, &blk)\n        end\n        begin\n          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)\n          \#{method_name}__mp_unpatched(*args, &blk)\n        ensure\n          data = (prof[:\#{name}] ||= {duration: 0.0, calls: 0})\n          data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start\n          data[:calls] += 1\n        end\n      end\n    end\n    RUBY\n  end.join(\"\\n\")\n\n  klass.class_eval patches\nend\n"

.start(transfer = nil) ⇒ Object



36
37
38
39
40
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 36

def self.start(transfer = nil)
  Thread.current[:_method_profiler] = transfer || {
    __start: Process.clock_gettime(Process::CLOCK_MONOTONIC)
  }
end

.stopObject



46
47
48
49
50
51
52
53
54
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 46

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

.transferObject



30
31
32
33
34
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 30

def self.transfer
  result = Thread.current[:_method_profiler]
  Thread.current[:_method_profiler] = nil
  result
end