Class: Gruf::Profiler::Hook

Inherits:
Hooks::Base
  • Object
show all
Defined in:
lib/gruf/profiler/hook.rb

Overview

Gruf hook that automatically loads rbtrace and memory profiler

Add to your gruf initializer:

require 'gruf/profiler'
Gruf::Hooks::Registry.add(:profiler, Gruf::Profiler::Hook)

Instance Method Summary collapse

Instance Method Details

#outer_around(call_signature, _request, _active_call, &_block) ⇒ Object

Wraps the entire gruf call and provides memory reports

Parameters:

  • call_signature (Symbol)
  • _request (Object)
  • _active_call (GRPC::ActiveCall)

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gruf/profiler/hook.rb', line 42

def outer_around(call_signature, _request, _active_call, &_block)
  result = nil
  report = MemoryProfiler.report(memory_profiler_options) do
    result = yield
  end
  if report
    pp_options = memory_profiler_options.fetch(:pretty_print_options, {})
    io_obj = pp_options.fetch(:io, nil)
    report_string = io_obj ? report.pretty_print(io_obj, pp_options) : report.pretty_print(pp_options)
    log("gruf profile for #{service_key}.#{method_key(call_signature)}:\n#{report_string}")
  else
    log('Memory profiler did not return a report')
  end
  result
end

#setupObject

Initializes the hook


30
31
32
33
# File 'lib/gruf/profiler/hook.rb', line 30

def setup
  require 'rbtrace'
  require 'memory_profiler'
end