-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add metrics tracking the V8 heap usage #5781
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
331c20e
add metrics tracking the V8 heap usage
Geal 1d7a026
changeset
Geal 09e5453
add a test
Geal 03368e3
fix
Geal 8f06e33
remove explicit panic
Geal 9e9447d
update router-bridge
Geal 04e354f
update federation version in tests
Geal 82a506f
add a changeset for the federation update
Geal 2008d8a
Update feat_update_federation.md
Geal ed41e1b
Update .changesets/feat_geal_v8_heap_statistics.md
Geal a499f15
Merge branch 'dev' into geal/v8-heap-statistics
Geal 9e710a5
add descriptiuon and unit
Geal ba0dea4
update unit
Geal b139d39
add unit
Geal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### Add V8 heap usage metrics ([PR #5781](https://github.com/apollographql/router/pull/5781)) | ||
|
||
The router supports new gauge metrics for tracking heap memory usage of the V8 Javascript engine: | ||
- `apollo.router.v8.heap.used`: heap memory used by V8, in bytes | ||
- `apollo.router.v8.heap.total`: total heap allocated by V8, in bytes | ||
|
||
By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/5781 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### Update federation to 2.8.3 ([PR #5781](https://github.com/apollographql/router/pull/5781)) | ||
|
||
> [!IMPORTANT] | ||
> If you have enabled [Distributed query plan caching](https://www.apollographql.com/docs/router/configuration/distributed-caching/#distributed-query-plan-caching), this release changes the hashing algorithm used for the cache keys. On account of this, you should anticipate additional cache regeneration cost when updating between these versions while the new hashing algorithm comes into service. | ||
|
||
This updates the router from federation version 2.8.1 to 2.8.3, with a [fix for fragment generation](https://github.com/apollographql/federation/pull/3043). | ||
|
||
By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/5781 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use std::collections::HashMap; | ||
use std::num::NonZeroUsize; | ||
use std::sync::atomic::AtomicU64; | ||
use std::sync::atomic::Ordering; | ||
use std::sync::Arc; | ||
use std::time::Instant; | ||
|
||
|
@@ -8,6 +10,9 @@ use async_channel::bounded; | |
use async_channel::Sender; | ||
use futures::future::BoxFuture; | ||
use opentelemetry::metrics::MeterProvider; | ||
use opentelemetry::metrics::ObservableGauge; | ||
use opentelemetry::metrics::Unit; | ||
use opentelemetry_api::metrics::Meter; | ||
use router_bridge::planner::Planner; | ||
use tokio::sync::oneshot; | ||
use tokio::task::JoinSet; | ||
|
@@ -37,6 +42,10 @@ pub(crate) struct BridgeQueryPlannerPool { | |
schema: Arc<Schema>, | ||
subgraph_schemas: Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>>, | ||
_pool_size_gauge: opentelemetry::metrics::ObservableGauge<u64>, | ||
v8_heap_used: Arc<AtomicU64>, | ||
_v8_heap_used_gauge: ObservableGauge<u64>, | ||
v8_heap_total: Arc<AtomicU64>, | ||
_v8_heap_total_gauge: ObservableGauge<u64>, | ||
} | ||
|
||
impl BridgeQueryPlannerPool { | ||
|
@@ -93,7 +102,7 @@ impl BridgeQueryPlannerPool { | |
})? | ||
.subgraph_schemas(); | ||
|
||
let planners = bridge_query_planners | ||
let planners: Vec<_> = bridge_query_planners | ||
.iter() | ||
.map(|p| p.planner().clone()) | ||
.collect(); | ||
|
@@ -119,21 +128,68 @@ impl BridgeQueryPlannerPool { | |
}); | ||
} | ||
let sender_for_gauge = sender.clone(); | ||
let pool_size_gauge = meter_provider() | ||
.meter("apollo/router") | ||
let meter = meter_provider().meter("apollo/router"); | ||
let pool_size_gauge = meter | ||
.u64_observable_gauge("apollo.router.query_planning.queued") | ||
.with_description("Number of queries waiting to be planned") | ||
.with_unit(Unit::new("query")) | ||
.with_callback(move |m| m.observe(sender_for_gauge.len() as u64, &[])) | ||
.init(); | ||
|
||
let (v8_heap_used, _v8_heap_used_gauge) = Self::create_heap_used_gauge(&meter); | ||
let (v8_heap_total, _v8_heap_total_gauge) = Self::create_heap_total_gauge(&meter); | ||
|
||
// initialize v8 metrics | ||
if let Some(bridge_query_planner) = planners.first().cloned() { | ||
Self::get_v8_metrics( | ||
bridge_query_planner, | ||
v8_heap_used.clone(), | ||
v8_heap_total.clone(), | ||
) | ||
.await; | ||
} | ||
|
||
Ok(Self { | ||
js_planners: planners, | ||
sender, | ||
schema, | ||
subgraph_schemas, | ||
_pool_size_gauge: pool_size_gauge, | ||
v8_heap_used, | ||
_v8_heap_used_gauge, | ||
v8_heap_total, | ||
_v8_heap_total_gauge, | ||
}) | ||
} | ||
|
||
fn create_heap_used_gauge(meter: &Meter) -> (Arc<AtomicU64>, ObservableGauge<u64>) { | ||
let current_heap_used = Arc::new(AtomicU64::new(0)); | ||
let current_heap_used_for_gauge = current_heap_used.clone(); | ||
let heap_used_gauge = meter | ||
.u64_observable_gauge("apollo.router.v8.heap.used") | ||
.with_description("V8 heap used, in bytes") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also set a unit. Sometimes in some apms it's required to display the metrics properly |
||
.with_unit(Unit::new("By")) | ||
.with_callback(move |i| { | ||
i.observe(current_heap_used_for_gauge.load(Ordering::SeqCst), &[]) | ||
}) | ||
.init(); | ||
(current_heap_used, heap_used_gauge) | ||
} | ||
|
||
fn create_heap_total_gauge(meter: &Meter) -> (Arc<AtomicU64>, ObservableGauge<u64>) { | ||
let current_heap_total = Arc::new(AtomicU64::new(0)); | ||
let current_heap_total_for_gauge = current_heap_total.clone(); | ||
let heap_total_gauge = meter | ||
.u64_observable_gauge("apollo.router.v8.heap.total") | ||
.with_description("V8 heap total, in bytes") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for the unit |
||
.with_unit(Unit::new("By")) | ||
.with_callback(move |i| { | ||
i.observe(current_heap_total_for_gauge.load(Ordering::SeqCst), &[]) | ||
}) | ||
.init(); | ||
(current_heap_total, heap_total_gauge) | ||
} | ||
|
||
pub(crate) fn planners(&self) -> Vec<Arc<Planner<QueryPlanResult>>> { | ||
self.js_planners.clone() | ||
} | ||
|
@@ -147,6 +203,18 @@ impl BridgeQueryPlannerPool { | |
) -> Arc<HashMap<String, Arc<Valid<apollo_compiler::Schema>>>> { | ||
self.subgraph_schemas.clone() | ||
} | ||
|
||
async fn get_v8_metrics( | ||
planner: Arc<Planner<QueryPlanResult>>, | ||
v8_heap_used: Arc<AtomicU64>, | ||
v8_heap_total: Arc<AtomicU64>, | ||
) { | ||
let metrics = planner.get_heap_statistics().await; | ||
if let Ok(metrics) = metrics { | ||
v8_heap_used.store(metrics.heap_used, Ordering::SeqCst); | ||
v8_heap_total.store(metrics.heap_total, Ordering::SeqCst); | ||
} | ||
} | ||
} | ||
|
||
impl tower::Service<QueryPlannerRequest> for BridgeQueryPlannerPool { | ||
|
@@ -173,6 +241,20 @@ impl tower::Service<QueryPlannerRequest> for BridgeQueryPlannerPool { | |
let (response_sender, response_receiver) = oneshot::channel(); | ||
let sender = self.sender.clone(); | ||
|
||
let get_metrics_future = | ||
if let Some(bridge_query_planner) = self.js_planners.first().cloned() { | ||
let v8_heap_used = self.v8_heap_used.clone(); | ||
let v8_heap_total = self.v8_heap_total.clone(); | ||
|
||
Some(Self::get_v8_metrics( | ||
bridge_query_planner, | ||
v8_heap_used, | ||
v8_heap_total, | ||
)) | ||
} else { | ||
None | ||
}; | ||
|
||
Box::pin(async move { | ||
let start = Instant::now(); | ||
let _ = sender.send((req, response_sender)).await; | ||
|
@@ -187,7 +269,73 @@ impl tower::Service<QueryPlannerRequest> for BridgeQueryPlannerPool { | |
start.elapsed().as_secs_f64() | ||
); | ||
|
||
if let Some(f) = get_metrics_future { | ||
// execute in a separate task to avoid blocking the request | ||
tokio::task::spawn(f); | ||
} | ||
|
||
res | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
||
mod tests { | ||
use opentelemetry_sdk::metrics::data::Gauge; | ||
|
||
use super::*; | ||
use crate::metrics::FutureMetricsExt; | ||
use crate::spec::Query; | ||
use crate::Context; | ||
|
||
#[tokio::test] | ||
async fn test_v8_metrics() { | ||
let sdl = include_str!("../testdata/supergraph.graphql"); | ||
let config = Arc::default(); | ||
let schema = Arc::new(Schema::parse(sdl, &config).unwrap()); | ||
|
||
async move { | ||
let mut pool = BridgeQueryPlannerPool::new( | ||
schema.clone(), | ||
config.clone(), | ||
NonZeroUsize::new(2).unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
let query = "query { me { name } }".to_string(); | ||
|
||
let doc = Query::parse_document(&query, None, &schema, &config).unwrap(); | ||
let context = Context::new(); | ||
context.extensions().with_lock(|mut lock| lock.insert(doc)); | ||
|
||
pool.call(QueryPlannerRequest::new(query, None, context)) | ||
.await | ||
.unwrap(); | ||
|
||
let metrics = crate::metrics::collect_metrics(); | ||
let heap_used = metrics.find("apollo.router.v8.heap.used").unwrap(); | ||
let heap_total = metrics.find("apollo.router.v8.heap.total").unwrap(); | ||
|
||
println!( | ||
"got heap_used: {:?}, heap_total: {:?}", | ||
heap_used | ||
.data | ||
.as_any() | ||
.downcast_ref::<Gauge<u64>>() | ||
.unwrap() | ||
.data_points[0] | ||
.value, | ||
heap_total | ||
.data | ||
.as_any() | ||
.downcast_ref::<Gauge<u64>>() | ||
.unwrap() | ||
.data_points[0] | ||
.value | ||
); | ||
} | ||
.with_metrics() | ||
.await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it existed before but could you add description and unit please ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what kind of unit do we set here? Is there a convention for the expected units in otel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(asking because I don't see many examples of setting units in our metrics)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes there are. At different places but for example for memory you can find an example here https://opentelemetry.io/docs/specs/semconv/system/hardware-metrics/#hwmemory---memory-module-metrics and more generally you can take examples from here there are some conventions too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this on I would suggest using
query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done