Skip to content

Commit

Permalink
Add a histogram metric tracking evaluated query plans (#5875)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Rosenberger <git@jro.cc>
  • Loading branch information
Geal and abernix authored Sep 4, 2024
1 parent f331c89 commit 48c03cf
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changesets/feat_geal_evaluate_plan_count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Add a histogram metric tracking evaluated query plans ([PR #5875](https://github.com/apollographql/router/pull/5875))

The `supergraph.query_planning.experimental_plans_limit` option can be used to limit the number of query plans evaluated for a query, to reduce the time spent planning. When reaching that limit, the planner would still return a valid query plan, but maybe the most optimized one.
This adds the `apollo.router.query_planning.plan.evaluated_plans` histogram metric to track the number of evaluated query plans, giving more context to configure this option.

By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/5875
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5630,9 +5630,9 @@ dependencies = [

[[package]]
name = "router-bridge"
version = "0.6.0+v2.9.0"
version = "0.6.1+v2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96ef4910ade6753863c8437a76e88e236ab91688dcfe739d73417ae7848f3b92"
checksum = "b3be2d3ebbcbceb19dc813f5cee507295c673bb4e3f7a7cd532c8c27c95f92fc"
dependencies = [
"anyhow",
"async-channel 1.9.0",
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ regex = "1.10.5"
reqwest.workspace = true

# note: this dependency should _always_ be pinned, prefix the version with an `=`
router-bridge = "=0.6.0+v2.9.0"
router-bridge = "=0.6.1+v2.9.0"

rust-embed = { version = "8.4.0", features = ["include-exclude"] }
rustls = "0.21.12"
Expand Down
33 changes: 33 additions & 0 deletions apollo-router/src/query_planner/bridge_query_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,19 @@ impl PlannerMode {
if let Some(node) = &mut root_node {
init_query_plan_root_node(node)?;
}

Ok(PlanSuccess {
usage_reporting,
data: QueryPlanResult {
formatted_query_plan: Some(Arc::new(plan.to_string())),
query_plan: QueryPlan {
node: root_node.map(Arc::new),
},
evaluated_plan_count: plan
.statistics
.evaluated_plan_count
.clone()
.into_inner() as u64,
},
})
}
Expand Down Expand Up @@ -532,6 +538,7 @@ impl BridgeQueryPlanner {
QueryPlanResult {
query_plan: QueryPlan { node: Some(node) },
formatted_query_plan,
evaluated_plan_count,
},
mut usage_reporting,
} => {
Expand All @@ -549,6 +556,12 @@ impl BridgeQueryPlanner {
doc.clone()
};

u64_histogram!(
"apollo.router.query_planning.plan.evaluated_plans",
"Number of query plans evaluated for a query before choosing the best one",
evaluated_plan_count
);

let generated_usage_reporting = generate_usage_reporting(
&signature_doc.executable,
&doc.executable,
Expand Down Expand Up @@ -844,6 +857,7 @@ impl BridgeQueryPlanner {
pub struct QueryPlanResult {
pub(super) formatted_query_plan: Option<Arc<String>>,
pub(super) query_plan: QueryPlan,
pub(super) evaluated_plan_count: u64,
}

impl QueryPlanResult {
Expand Down Expand Up @@ -1595,4 +1609,23 @@ mod tests {
"init.is_success" = false
);
}

#[test(tokio::test)]
async fn test_evaluated_plans_histogram() {
async {
let _ = plan(
EXAMPLE_SCHEMA,
include_str!("testdata/query.graphql"),
include_str!("testdata/query.graphql"),
None,
PlanOptions::default(),
)
.await
.unwrap();

assert_histogram_exists!("apollo.router.query_planning.plan.evaluated_plans", u64);
}
.with_metrics()
.await;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The coprocessor operations metric has the following attributes:
- `apollo.router.query_planning.plan.duration` - Histogram of plan durations isolated to query planning time only.
- `apollo.router.query_planning.total.duration` - Histogram of plan durations including queue time.
- `apollo.router.query_planning.queued` - A gauge of the number of queued plans requests.
- `apollo.router.query_planning.plan.evaluated_plans` - Histogram of the number of evaluated query plans.
- `apollo.router.v8.heap.used` - heap memory used by V8, in bytes.
- `apollo.router.v8.heap.total` - total heap allocated by V8, in bytes.

Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ reqwest = { workspace = true, features = ["json", "blocking"] }
serde_json.workspace = true
tokio.workspace = true
# note: this dependency should _always_ be pinned, prefix the version with an `=`
router-bridge = "=0.6.0+v2.9.0"
router-bridge = "=0.6.1+v2.9.0"

[dev-dependencies]
anyhow = "1"
Expand Down

0 comments on commit 48c03cf

Please sign in to comment.