class GraphQL::Flamegraph::Instrumentation

Public Class Methods

new(path: nil) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 7
def initialize(path: nil)
  validate_directory!(path) if path
  @path = path
end

Public Instance Methods

after_query(query) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 16
      def after_query(query)
        return unless enabled?(query)

        result = Result.new(cache(query))
        query.context[:flamegraph] = result
        return unless @path

        file_path = Dir::Tmpname.create(['graphql-flamegraph-', '.txt'], @path) {}
        File.write(file_path, result.serialize)
        puts <<~MESSAGE
          Check your flamegraph at #{file_path}
          Open it in https://www.speedscope.app/ or in local speedscope:

              speedscope #{file_path}

        MESSAGE
      end
before_query(query) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 12
def before_query(query)
  reset_cache!(query)
end

Private Instance Methods

cache(query) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 46
def cache(query)
  query.context.namespace(GraphQL::Flamegraph)[:field_runtime_cache]
end
enabled?(query) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 36
def enabled?(query)
  !!query.context[:flamegraph]
end
reset_cache!(query) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 50
def reset_cache!(query)
  query.context.namespace(GraphQL::Flamegraph)[:field_runtime_cache] =
    Hash.new { |h,k| h[k] = 0.0 }
end
validate_directory!(path) click to toggle source
# File lib/graphql/flamegraph/instrumentation.rb, line 40
def validate_directory!(path)
  return if Dir.exist?(path) && File.writable?(path)

  raise ArgumentError, "Path for graphql-flamegraph must be a writable directory!"
end