Sha256: 8daa7ffb351682ee3ddd1aed4dc158e1a499638ab6668728a02fa7de33291a94

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module RailsMiniProfiler
  class FlamegraphGuard
    def initialize(request_context, configuration: RailsMiniProfiler.configuration)
      @request_context = request_context
      @request = request_context.request
      @configuration = configuration
    end

    def record(&block)
      return block.call unless enabled?

      sample_rate = @configuration.flamegraph_sample_rate
      if StackProf.running?
        RailsMiniProfiler.logger.error('Stackprof is already running, cannot record Flamegraph')
        return block.call
      end

      result = nil
      flamegraph = StackProf.run(mode: :wall, raw: true, aggregate: false, interval: (sample_rate * 1000).to_i) do
        result = block.call
      end

      unless flamegraph
        RailsMiniProfiler.logger.error('Failed to record Flamegraph, possibly due to concurrent requests')
        return result
      end

      @request_context.flamegraph = flamegraph.to_json
      result
    end

    private

    def enabled?
      defined?(StackProf) && StackProf.respond_to?(:run) && config_enabled?
    end

    def config_enabled?
      params = CGI.parse(@request.query_string).transform_values(&:first).with_indifferent_access
      return params[:rmp_flamegraph] if params[:rmp_flamegraph]

      RailsMiniProfiler.configuration.flamegraph_enabled
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rails_mini_profiler-0.6.0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.5.0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.4.0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.3.0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.2.1 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.2.0 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.1.3 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.1.2 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.1.1 lib/rails_mini_profiler/flamegraph_guard.rb
rails_mini_profiler-0.1.0 lib/rails_mini_profiler/flamegraph_guard.rb