From 710a5ed52396190dc12dcfc6cc4c047091c82a45 Mon Sep 17 00:00:00 2001 From: Ian Hollander <39830572+ihollander@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:13:56 -0500 Subject: [PATCH] Add option to ignore garbage collection frames with flamegraphs (#599) * Add flamegraph_ignore_gc config * Add flamegraph_ignore_gc to query params --- README.md | 1 + lib/mini_profiler.rb | 11 ++++++++++- lib/mini_profiler/config.rb | 3 ++- lib/mini_profiler/views.rb | 1 + spec/integration/middleware_spec.rb | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf3f96c0..00322cad 100644 --- a/README.md +++ b/README.md @@ -431,6 +431,7 @@ start_hidden | `false` backtrace_threshold_ms | `0` | Minimum SQL query elapsed time before a backtrace is recorded. flamegraph_sample_rate | `0.5` | How often to capture stack traces for flamegraphs in milliseconds. flamegraph_mode | `:wall` | The [StackProf mode](https://github.com/tmm1/stackprof#all-options) to pass to `StackProf.run`. +flamegraph_ignore_gc | `false` | Whether to ignore garbage collection frames in flamegraphs. base_url_path | `'/mini-profiler-resources/'` | Path for assets; added as a prefix when naming assets and sought when responding to requests. cookie_path | `'/'` | Set-Cookie header path for profile cookie collapse_results | `true` | If multiple timing results exist in a single page, collapse them till clicked. diff --git a/lib/mini_profiler.rb b/lib/mini_profiler.rb index a86daaa3..46743b11 100644 --- a/lib/mini_profiler.rb +++ b/lib/mini_profiler.rb @@ -302,11 +302,20 @@ def call(env) mode = config.flamegraph_mode end + ignore_gc_match_data = action_parameters(env)['flamegraph_ignore_gc'] + + if ignore_gc_match_data + ignore_gc = ignore_gc_match_data == 'true' + else + ignore_gc = config.flamegraph_ignore_gc + end + flamegraph = StackProf.run( mode: mode, raw: true, aggregate: false, - interval: (sample_rate * 1000).to_i + interval: (sample_rate * 1000).to_i, + ignore_gc: ignore_gc ) do status, headers, body = @app.call(env) end diff --git a/lib/mini_profiler/config.rb b/lib/mini_profiler/config.rb index d74caecc..f7a4d281 100644 --- a/lib/mini_profiler/config.rb +++ b/lib/mini_profiler/config.rb @@ -30,6 +30,7 @@ def self.default @backtrace_threshold_ms = 0 @flamegraph_sample_rate = 0.5 @flamegraph_mode = :wall + @flamegraph_ignore_gc = false @storage_failure = Proc.new do |exception| if @logger @logger.warn("MiniProfiler storage failure: #{exception.message}") @@ -76,7 +77,7 @@ def self.default :storage_options, :user_provider, :enable_advanced_debugging_tools, :skip_sql_param_names, :suppress_encoding, :max_sql_param_length, :content_security_policy_nonce, :enable_hotwire_turbo_drive_support, - :flamegraph_mode, :profile_parameter + :flamegraph_mode, :flamegraph_ignore_gc, :profile_parameter # ui accessors attr_accessor :collapse_results, :max_traces_to_show, :position, diff --git a/lib/mini_profiler/views.rb b/lib/mini_profiler/views.rb index aa6edac2..bd27edb5 100644 --- a/lib/mini_profiler/views.rb +++ b/lib/mini_profiler/views.rb @@ -161,6 +161,7 @@ def help(client_settings, env) #{make_link "async-flamegraph", env} : store flamegraph data for this page and all its AJAX requests. Flamegraph links will be available in the mini-profiler UI (requires the stackprof gem). #{make_link "flamegraph&flamegraph_sample_rate=1", env}: creates a flamegraph with the specified sample rate (in ms). Overrides value set in config #{make_link "flamegraph&flamegraph_mode=cpu", env}: creates a flamegraph with the specified mode (one of cpu, wall, object, or custom). Overrides value set in config + #{make_link "flamegraph&flamegraph_ignore_gc=true", env}: ignore garbage collection frames in flamegraphs. Overrides value set in config #{make_link "flamegraph_embed", env} : a graph representing sampled activity (requires the stackprof gem), embedded resources for use on an intranet. #{make_link "trace-exceptions", env} : will return all the spots where your application raises exceptions #{make_link "analyze-memory", env} : will perform basic memory analysis of heap diff --git a/spec/integration/middleware_spec.rb b/spec/integration/middleware_spec.rb index f93122ad..063153f8 100644 --- a/spec/integration/middleware_spec.rb +++ b/spec/integration/middleware_spec.rb @@ -56,6 +56,7 @@ def app expect(last_response.body).to include("async-flamegraph") expect(last_response.body).to include("flamegraph&flamegraph_sample_rate=1") expect(last_response.body).to include("flamegraph&flamegraph_mode=cpu") + expect(last_response.body).to include("flamegraph&flamegraph_ignore_gc=true") expect(last_response.body).to include("flamegraph_embed") expect(last_response.body).to include("trace-exceptions") expect(last_response.body).to include("analyze-memory")