class Gruf::Profiler::Interceptor

Gruf hook that automatically loads rbtrace and memory profiler

Add to your gruf initializer:

require 'gruf/profiler'
Gruf.configure do |c|
  c.interceptors.use(Gruf::Profiler::Interceptor)
end

Public Instance Methods

call() { || ... } click to toggle source

Wraps the entire gruf call and provides memory reports

# File lib/gruf/profiler/interceptor.rb, line 36
def call
  result = nil
  report = MemoryProfiler.report(**memory_profiler_options) do
    result = yield
  end
  if report
    profile(report)
  else
    log('Memory profiler did not return a report')
  end
  result
end

Private Instance Methods

log(msg) click to toggle source

Log a message with a configurable level to the gruf logger @param [String]

# File lib/gruf/profiler/interceptor.rb, line 66
def log(msg)
  level = options.fetch(:log_level, :debug).to_sym
  Gruf.logger.send(level, msg)
end
memory_profiler_options() click to toggle source

Get sub-options for the memory profiler

@return [Hash]

# File lib/gruf/profiler/interceptor.rb, line 76
def memory_profiler_options
  options.fetch(:memory_profiler, top: 50)
end
pretty_print_options() click to toggle source

Get sub-options for pretty printing

@return [Hash]

# File lib/gruf/profiler/interceptor.rb, line 85
def pretty_print_options
  memory_profiler_options.fetch(:pretty_print_options, {}) || {}
end
profile(report) click to toggle source

Profile the given report to logs

@param [MemoryProfiler::Reporter] report

# File lib/gruf/profiler/interceptor.rb, line 56
def profile(report)
  io_obj = pretty_print_options.fetch(:io, nil)
  report_string = io_obj ? report.pretty_print(io_obj, **pretty_print_options) : report.pretty_print(**pretty_print_options)
  log("gruf profile for #{request.method_name}:\n#{report_string}")
end