Skip to content

Commit 263f0d8

Browse files
authored
Add flamegraph path to response headers (#601)
* Add flamegraph path to response headers The motivation for this is to enable easy access to the flamegraph when using the async-flamegraph action. Currently, if have a CURL request that you are wanting to profile, you have to add the `pp=async-flamegraph` query parameter to the request, then copy the first ID from the `X-MiniProfiler-Ids` header, then add that ID to the `/mini-profiler-resources/flamegraph?id=` path in the browser. This assumes that you know that the current request profile id is the first one in the `X-MiniProfiler-Ids` header (personally I was not confident of this until I read the source code) and that you know the path to the flamegraph endpoint. I originally added the protocol, host, and port to the path, but I didn't know if that was guaranteed to be correct in all cases, so I removed it. * Add note of X-MiniProfiler-Flamegraph-Path header to README
1 parent 9081657 commit 263f0d8

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ To generate [flamegraphs](http://samsaffron.com/archive/2013/03/19/flame-graphs-
180180

181181
Then, to view the flamegraph as a direct HTML response from your request, just visit any page in your app with `?pp=flamegraph` appended to the URL, or add the header `X-Rack-Mini-Profiler` to the request with the value `flamegraph`.
182182

183-
Conversely, if you want your regular response instead (which is specially useful for JSON and/or XHR requests), just append the `?pp=async-flamegraph` parameter to your request/fetch URL; the request will then return as normal, and the flamegraph data will be stored for later *async* viewing, both for this request and for all subsequent requests made by this page (based on the `REFERER` header). For viewing these async flamegraphs, use the 'flamegraph' link that will appear inside the MiniProfiler UI for these requests.
183+
Conversely, if you want your regular response instead (which is specially useful for JSON and/or XHR requests), just append the `?pp=async-flamegraph` parameter to your request/fetch URL; the request will then return as normal, and the flamegraph data will be stored for later *async* viewing, both for this request and for all subsequent requests made by this page (based on the `REFERER` header). For viewing these async flamegraphs, use the 'flamegraph' link that will appear inside the MiniProfiler UI for these requests or path returned in the `X-MiniProfiler-Flamegraph-Path` header.
184184

185185
Note: Mini Profiler will not record SQL timings for a request if it asks for a flamegraph. The rationale behind this is to keep
186186
Mini Profiler's methods that are responsible for generating the timings data out of the flamegraph.
@@ -466,7 +466,7 @@ If you are using Heroku Redis, you may need to add the following to your `config
466466

467467
```ruby
468468
if Rails.env.production?
469-
Rack::MiniProfiler.config.storage_options = {
469+
Rack::MiniProfiler.config.storage_options = {
470470
url: ENV["REDIS_URL"],
471471
ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
472472
}

lib/mini_profiler.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def inject_profiler(env, status, headers, body)
436436
# inject header
437437
if headers.is_a? Hash
438438
headers['X-MiniProfiler-Ids'] = ids_comma_separated(env)
439+
headers['X-MiniProfiler-Flamegraph-Path'] = flamegraph_path(env) if current.page_struct[:has_flamegraph]
439440
end
440441

441442
if current.inject_js && content_type =~ /text\/html/
@@ -605,6 +606,10 @@ def ids_comma_separated(env)
605606
ids(env).join(",")
606607
end
607608

609+
def flamegraph_path(env)
610+
@config.base_url_path + 'flamegraph?id=' + current.page_struct[:id]
611+
end
612+
608613
# cancels automatic injection of profile script for the current page
609614
def cancel_auto_inject(env)
610615
current.inject_js = false

spec/integration/mini_profiler_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def app
177177
get '/html?pp=async-flamegraph'
178178
expect(last_response).to be_ok
179179
id = last_response.headers['X-MiniProfiler-Ids'].split(",")[0]
180-
get "/mini-profiler-resources/flamegraph?id=#{id}"
180+
flamegraph_path = last_response.headers['X-MiniProfiler-Flamegraph-Path']
181+
expect(flamegraph_path).to eq("/mini-profiler-resources/flamegraph?id=#{id}")
182+
get flamegraph_path
181183
expect(last_response).to be_ok
182184
expect(last_response.body).to include("var graph = {")
183185

0 commit comments

Comments
 (0)