Sha256: 8a92bf0c5c54db07195bd48dc1448f8ec23e2793c8c6d49b8171998fd8494945
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
module Rack::PerftoolsProfiler class ProfilerArgumentError < RuntimeError; end; class ProfilerMiddleware include Rack::Utils PRINTER_CONTENT_TYPE = { :text => 'text/plain', :gif => 'image/gif', :pdf => 'application/pdf', :callgrind => 'text/plain' } PRINTERS = PRINTER_CONTENT_TYPE.keys def initialize(app, options = {}) @app = app @profiler = Profiler.new(@app, options.clone) end def call(env) @env = env.clone action = Action.for_env(@env, @profiler, self) action.act action.response rescue ProfilerArgumentError => err @env['rack.errors'].write(err.message) [400, {'Content-Type' => 'text/plain'}, [err.message]] rescue ProfilingError => err @env['rack.errors'].write(err.message + "\n" + err.stderr) [500, {'Content-Type' => 'text/plain'}, [err.message+"\n\n", "Standard error:\n"+err.stderr+"\n"]] end def call_app(env) @app.call(env) end def force_stop @profiler.stop end def profiler_data_response(profiling_data) format, body = profiling_data body = Array(body) if format==:none message = 'No profiling data available. Visit /__stop__ and then visit /__data__' [404, {'Content-Type' => 'text/plain'}, [message]] else [200, headers(format, body), Array(body)] end end private def headers(printer, body) headers = { 'Content-Type' => PRINTER_CONTENT_TYPE[printer], 'Content-Length' => content_length(body) } if printer==:pdf filetype = printer filename='profile_data' headers['Content-Disposition'] = %(attachment; filename="#{filename}.#{filetype}") end headers end def content_length(body) body.inject(0) { |len, part| len + bytesize(part) }.to_s end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rack-perftools_profiler-0.4.1 | lib/rack/perftools_profiler/profiler_middleware.rb |