11# frozen_string_literal: true
2+ if defined? ( ActiveRecord )
3+ require "graphql/tracing/detailed_trace/active_record_backend"
4+ end
25require "graphql/tracing/detailed_trace/memory_backend"
36require "graphql/tracing/detailed_trace/redis_backend"
47
58module GraphQL
69 module Tracing
7- # `DetailedTrace` can make detailed profiles for a subset of production traffic.
10+ # `DetailedTrace` can make detailed profiles for a subset of production traffic. Install it in Rails with `rails generate graphql:detailed_trace`.
811 #
912 # When `MySchema.detailed_trace?(query)` returns `true`, a profiler-specific `trace_mode: ...` will be used for the query,
1013 # overriding the one in `context[:trace_mode]`.
@@ -13,10 +16,19 @@ module Tracing
1316 # this behavior by extending {DetailedTrace} and overriding {#inspect_object}. You can opt out of debug annotations
1417 # entirely with `use ..., debug: false` or for a single query with `context: { detailed_trace_debug: false }`.
1518 #
16- # __Redis__: The sampler stores its results in a provided Redis database. Depending on your needs,
17- # You can configure this database to retain all data (persistent) or to expire data according to your rules.
19+ # You can store saved traces in two ways:
20+ #
21+ # - __ActiveRecord__: With `rails generate graphql:detailed_trace`, a new migration will be added to your app.
22+ # That table will be used to store trace data.
23+ #
24+ # - __Redis__: Pass `redis: ...` to save trace data to a Redis database. Depending on your needs,
25+ # you can configure this database to retain all data (persistent) or to expire data according to your rules.
26+ #
1827 # If you need to save traces indefinitely, you can download them from Perfetto after opening them there.
1928 #
29+ # @example Installing with Rails
30+ # rails generate graphql:detailed_trace # optional: --redis
31+ #
2032 # @example Adding the sampler to your schema
2133 # class MySchema < GraphQL::Schema
2234 # # Add the sampler:
@@ -53,13 +65,16 @@ class DetailedTrace
5365 # @param redis [Redis] If provided, profiles will be stored in Redis for later review
5466 # @param limit [Integer] A maximum number of profiles to store
5567 # @param debug [Boolean] if `false`, it won't create `debug` annotations in Perfetto traces (reduces overhead)
56- def self . use ( schema , trace_mode : :profile_sample , memory : false , debug : debug? , redis : nil , limit : nil )
68+ # @param model_class [Class<ActiveRecord::Base>] Overrides {ActiveRecordBackend::GraphqlDetailedTrace} if present
69+ def self . use ( schema , trace_mode : :profile_sample , memory : false , debug : debug? , redis : nil , limit : nil , model_class : nil )
5770 storage = if redis
5871 RedisBackend . new ( redis : redis , limit : limit )
5972 elsif memory
6073 MemoryBackend . new ( limit : limit )
74+ elsif defined? ( ActiveRecord )
75+ ActiveRecordBackend . new ( limit : limit , model_class : model_class )
6176 else
62- raise ArgumentError , "Pass `redis: ...` to store traces in Redis for later review "
77+ raise ArgumentError , "To store traces, install ActiveRecord or provide `redis: ...` "
6378 end
6479 detailed_trace = self . new ( storage : storage , trace_mode : trace_mode , debug : debug )
6580 schema . detailed_trace = detailed_trace
0 commit comments