Devtools
gpui-query exposes runtime diagnostics through the QueryClient. You get a point-in-time snapshot of every active query and mutation, which you can log or render in a debug panel.
ClientDiagnostic
Section titled “ClientDiagnostic”Calling client.diagnostics(cx) returns a ClientDiagnostic struct. It is a point-in-time snapshot of the client internals.
pub struct ClientDiagnostic { pub query_count: usize, pub mutation_count: usize, pub queries: Vec<QueryDiagnostic>, pub mutations: Vec<MutationDiagnostic>,}- query_count: number of query cache entries across all type buckets. An upper bound on
queries.len(): entries whose entities were collected linger in the maps until the next GC sweep. - mutation_count: number of mutation cache entries, an upper bound on
mutations.len()for the same reason. - queries: per-query diagnostics, one entry per resource.
- mutations: per-mutation diagnostics (
MutationDiagnostic: optional key, status, retry count), one entry per resource.
The struct derives Clone, Debug, and Default. You can render it in a GPUI debug panel or log it with Debug formatting.
QueryDiagnostic
Section titled “QueryDiagnostic”Each entry in queries is a QueryDiagnostic:
pub struct QueryDiagnostic { pub key: String, pub status: QueryStatus, pub cache_policy: String, pub cache_age_ms: Option<u64>, pub cache_hits: u64, pub retry_count: u32,}The fields:
| Field | What it tells you |
|---|---|
key |
The stringified QueryKey for this resource. |
status |
The QueryStatus enum value: Idle, LoadingEmpty, LoadingWithData, Success, Failure, or Cancelled. Call q.status.label() for the human-readable string. |
cache_policy |
Label from the configured CachePolicy: "No cache", "Cache TTL 60s", or "Stale-while-revalidate TTL 60s stale 60s". |
cache_age_ms |
Age in milliseconds of the last completed request, success or failure. None when no completed request is on record: never fetched, seeded via set_query_data, or invalidated. That does not mean the resource holds no data. |
cache_hits |
How many times this resource returned cached data instead of fetching. |
retry_count |
How many retries the current or most recent fetch has attempted. |
Basic usage
Section titled “Basic usage”let client = cx.global::<QueryClient>();let diag = client.diagnostics(cx);
println!("Active queries: {}", diag.query_count);println!("Mutations: {}", diag.mutation_count);
for q in &diag.queries { println!( " {} - {} (age: {:?}, hits: {}, retries: {})", q.key, q.status.label(), q.cache_age_ms, q.cache_hits, q.retry_count );}Filtering
Section titled “Filtering”client.diagnostics(cx) returns a single snapshot covering every type bucket. There is no per-type accessor, so filter client-side over diag.queries (or diag.mutations) by key prefix:
let diag = client.diagnostics(cx);let user_diags: Vec<_> = diag .queries .iter() .filter(|q| q.key.starts_with("users")) .collect();
for q in &user_diags { println!("user query: {} - {}", q.key, q.status.label());}Diagnostics are type-erased (key is a plain string), so a key prefix is the closest thing to per-type scoping when you are debugging a specific domain type.
Practical debugging patterns
Section titled “Practical debugging patterns”Check whether your TTL is right
Section titled “Check whether your TTL is right”Use cache_age_ms to figure out how old cached data is. It is already an age in milliseconds, so there is no clock to read. If data is consistently stale, your CachePolicy::Ttl { ttl_ms } might be too long. If cache_hits is zero across the board, your TTL might be too short and every read triggers a refetch.
for q in &diag.queries { if let Some(age_ms) = q.cache_age_ms { println!("{} is {}ms old", q.key, age_ms); }}Verify deduplication is working
Section titled “Verify deduplication is working”A high cache_hits count means the cache is doing its job. A count of zero on a resource that should be shared means you might be creating duplicate resources with slightly different keys. Check that your key construction is consistent.
use gpui_query::QueryStatus;
for q in &diag.queries { if q.cache_hits == 0 && q.status == QueryStatus::Success { println!("warning: {} has data but zero cache hits", q.key); }}Spot flaky endpoints
Section titled “Spot flaky endpoints”Look at retry_count. A count at or near your RetryPolicy cap (3 retries by default) means the fetch keeps failing and the policy keeps re-attempting it, which points at a flaky or down endpoint.
for q in &diag.queries { if q.retry_count > 2 { println!("{}: {} retries", q.key, q.retry_count); }}Verify invalidation hit the right keys
Section titled “Verify invalidation hit the right keys”After calling invalidate_queries on the QueryClient, run diagnostics again to see which keys the filter matched. Invalidation never changes status and never drops data, so a matched resource still reads Success. The signal is cache_age_ms: a matched key now reports None, because invalidation clears the last-updated timestamp that the age is computed from.
client.invalidate_queries(&filter, cx);
let diag = client.diagnostics(cx);for q in &diag.queries { if q.cache_age_ms.is_none() { println!("{} was invalidated", q.key); }}Serialization
Section titled “Serialization”QueryDiagnostic derives only Clone and Debug, and ClientDiagnostic adds Default; neither implements Serialize. If you need JSON for a log aggregator, snapshot tests, or a debug panel that communicates over IPC, map the snapshot into your own serde types first: the snapshot itself exposes key and cache_policy as plain strings. The crate types behind it, QueryStatus and MutationStatus (the enums the diagnostics actually carry) plus the related CachePolicy, RequestPolicy, QueryKey, and QueryError, all implement Serialize/Deserialize.
Observers
Section titled “Observers”For real-time state tracking rather than point-in-time snapshots, see Observers. Observers subscribe to individual resource state changes, while diagnostics give you the full client picture in a single call. Use both together: diagnostics for the overview, observers for reactive updates on specific keys.