Skip to content

Instantly share code, notes, and snippets.

@gmrdad82
Created August 29, 2019 11:04
Show Gist options
  • Save gmrdad82/227f9af4ef33f3be2843d7034b5eb1ae to your computer and use it in GitHub Desktop.
Save gmrdad82/227f9af4ef33f3be2843d7034b5eb1ae to your computer and use it in GitHub Desktop.
# query_timer_instrumentation.rb
module QueryTimerInstrumentation
module_function
def before_query(_query)
@start_time = Time.now.to_i
end
def after_query(query)
end_time = Time.now.to_i
elapsed_time = end_time - @start_time
operation = begin
query.selected_operation.selections.first.name
rescue StandardError
nil
end
Appsignal.add_distribution_value('graphql', elapsed_time, operation: operation) if operation
end
end
# fepra_api_schema.rb
class FepraApiSchema < GraphQL::Schema
use(GraphQL::Tracing::AppsignalTracing)
instrument(:query, QueryTimerInstrumentation)
MAX_PAGE_SIZE = 50
MAX_DEPTH = 50
MAX_COMPLEXITY = 50
max_depth MAX_DEPTH
mutation(Types::MutationType)
query(Types::QueryType)
# disables encoding - there's no advantages of using base64 on cursors
cursor_encoder(CustomEncoder)
if Rails.env.development? || Rails.env.staging?
log_query_complexity = GraphQL::Analysis::QueryComplexity.new do |query, complexity|
operation = begin
query.selected_operation.selections.first.name
rescue StandardError
nil
end
Appsignal.add_distribution_value('complexity', complexity, operation: operation) if operation
end
query_analyzer(log_query_complexity)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment