MiniProfiler / rack-mini-profiler

Profiler for your development and production Ruby rack apps.
MIT License
3.7k stars 402 forks source link

Broken label with profile_singleton_method #483

Open semaperepelitsa opened 3 years ago

semaperepelitsa commented 3 years ago

Example:

Rack::MiniProfiler.profile_singleton_method SomeModule, :lookup

It uses this label:

#<Class:SomeModule> lookup

But the label is not HTML-escaped, so it ends up looking like this:

# lookup

Besides escaping, it could also generate a better label, e.g. "SomeModule.lookup" for class/module methods, and "SomeClass#lookup" for instance methods.

semaperepelitsa commented 3 years ago

Patch:

module MiniProfilerExt
  # The default label is "Object#method" instead of "Object method"
  def profile_method(klass, method, type = :profile, &blk)
    label = "#{klass.name}##{method}" # "Class#method"
    blk ||= proc{ label }
    super(klass, method, type, &blk)
  end

  # The default label is "Object.method" instead of "#<Class:Object> method"
  # https://github.com/MiniProfiler/rack-mini-profiler/issues/483
  def profile_singleton_method(klass, method, type = :profile, &blk)
    label = "#{klass.name}.#{method}"
    blk ||= proc{ label }
    profile_method(klass.singleton_class, method, type, &blk)
  end
end

Rack::MiniProfiler.extend(MiniProfilerExt)
kbrock commented 3 years ago

@semaperepelitsa if you put this into a PR, it may be easier to review and get this great patch into the code.

(I am not part of the core of this project, so that is just a suggestion from a bystander)