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



29
30
31
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 29

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

.define_methods_on_module(klass, methods, name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 43

def self.define_methods_on_module(klass, methods, name)
  patch_source_line = __LINE__ + 3

  patches = methods.map { |method_name| <<~RUBY }.join("\n")
      def #{method_name}(...)
        unless prof = Thread.current[:_method_profiler]
          return super
        end
        begin
          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
          super
        ensure
          data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
          data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
          data[:calls] += 1
        end
      end
    RUBY

  klass.module_eval(patches, __FILE__, patch_source_line)
end

.patch(klass, methods, name, instrument:) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 8

def self.patch(klass, methods, name, instrument:)
  if instrument == :alias_method
    patch_using_alias_method(klass, methods, name)
  elsif instrument == :prepend
    patch_using_prepend(klass, methods, name)
  else
    raise ArgumentError, "instrument must be :alias_method or :prepend"
  end
end

.patch_using_alias_method(klass, methods, name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 71

def self.patch_using_alias_method(klass, methods, name)
  patch_source_line = __LINE__ + 3

  patches = methods.map { |method_name| <<~RUBY }.join("\n")
    unless defined?(#{method_name}__mp_unpatched)
      alias_method :#{method_name}__mp_unpatched, :#{method_name}

      def #{method_name}(...)
        unless prof = Thread.current[:_method_profiler]
          return #{method_name}__mp_unpatched(...)
        end

        begin
          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
          #{method_name}__mp_unpatched(...)
        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

  klass.class_eval(patches, __FILE__, patch_source_line)
end

.patch_using_prepend(klass, methods, name) ⇒ Object



65
66
67
68
69
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 65

def self.patch_using_prepend(klass, methods, name)
  prepend_instrument = Module.new
  define_methods_on_module(prepend_instrument, methods, name)
  klass.prepend(prepend_instrument)
end

.start(transfer = nil) ⇒ Object



24
25
26
27
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 24

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

.stopObject



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

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



18
19
20
21
22
# File 'lib/prometheus_exporter/instrumentation/method_profiler.rb', line 18

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