-
Notifications
You must be signed in to change notification settings - Fork 779
feat(metrics): add progress metrics via collector #17359
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
Merged
sundy-li
merged 15 commits into
databendlabs:main
from
flaneur2020:add-progress-metrics
Feb 15, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ff3f49b
add query scan rows metrics
flaneur2020 667230e
fix build
flaneur2020 16d4f36
register the metrics
flaneur2020 a8c55cf
track scan & write progress
flaneur2020 e78c55f
track spill progress
flaneur2020 b5b64ed
refactor
flaneur2020 6592f55
attach session manager to it
flaneur2020 97ecfdd
update the finished query
flaneur2020 b4306bf
finish the metrics
flaneur2020 fe88754
fix clippy
flaneur2020 b0a37b0
fix header
flaneur2020 a5c07b6
fix cargo fmt
flaneur2020 1833c30
fix tablo
flaneur2020 d62d405
Merge branch 'main' into add-progress-metrics
flaneur2020 75e5c9c
Merge branch 'main' into add-progress-metrics
flaneur2020 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use databend_common_base::base::ProgressValues; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_SCAN_PROGRESS_BYTES; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_SCAN_PROGRESS_ROWS; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_SPILL_PROGRESS_BYTES; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_SPILL_PROGRESS_ROWS; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_WRITE_PROGRESS_BYTES; | ||
use databend_common_metrics::interpreter::METRIC_QUERY_WRITE_PROGRESS_ROWS; | ||
use parking_lot::Mutex; | ||
use prometheus_client::collector::Collector; | ||
use prometheus_client::encoding::EncodeMetric; | ||
use prometheus_client::metrics::counter::ConstCounter; | ||
|
||
use crate::sessions::SessionManager; | ||
|
||
/// [`SessionManagerMetricsCollector`] dumps the progress metrics of scan/write/spills | ||
/// from the [`SessionManager`]'s running queries to the prometheus. To avoid the progress | ||
/// metrics being decreased, we also need to accumulate these progress values after the query | ||
/// is finished. | ||
#[derive(Clone)] | ||
pub struct SessionManagerMetricsCollector { | ||
inner: Arc<Mutex<SessionManagerMetricsCollectorInner>>, | ||
} | ||
|
||
pub(crate) struct SessionManagerMetricsCollectorInner { | ||
session_mgr: Option<Arc<SessionManager>>, | ||
finished_scan_total: ProgressValues, | ||
finished_write_total: ProgressValues, | ||
finished_spill_total: ProgressValues, | ||
} | ||
|
||
impl SessionManagerMetricsCollector { | ||
pub fn new() -> Self { | ||
Self { | ||
inner: Arc::new(Mutex::new(SessionManagerMetricsCollectorInner { | ||
session_mgr: None, | ||
finished_scan_total: ProgressValues::default(), | ||
finished_write_total: ProgressValues::default(), | ||
finished_spill_total: ProgressValues::default(), | ||
})), | ||
} | ||
} | ||
|
||
pub fn attach_session_manager(&self, session_mgr: Arc<SessionManager>) { | ||
let mut guard = self.inner.lock(); | ||
guard.session_mgr.replace(session_mgr); | ||
} | ||
|
||
pub fn track_finished_query( | ||
&self, | ||
scan: ProgressValues, | ||
write: ProgressValues, | ||
join_spill: ProgressValues, | ||
aggregate_spill: ProgressValues, | ||
group_by_spill: ProgressValues, | ||
window_partition_spill: ProgressValues, | ||
) { | ||
let mut guard = self.inner.lock(); | ||
guard.finished_scan_total = guard.finished_scan_total.add(&scan); | ||
guard.finished_write_total = guard.finished_write_total.add(&write); | ||
guard.finished_spill_total = guard | ||
.finished_spill_total | ||
.add(&join_spill) | ||
.add(&aggregate_spill) | ||
.add(&group_by_spill) | ||
.add(&window_partition_spill); | ||
} | ||
} | ||
|
||
impl Default for SessionManagerMetricsCollector { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for SessionManagerMetricsCollector { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "SessionMetricsCollector") | ||
} | ||
} | ||
|
||
impl Collector for SessionManagerMetricsCollector { | ||
fn encode( | ||
&self, | ||
mut encoder: prometheus_client::encoding::DescriptorEncoder, | ||
) -> Result<(), std::fmt::Error> { | ||
let processes = { | ||
match self.inner.lock().session_mgr.as_ref() { | ||
Some(mgr) => mgr.processes_info(), | ||
None => return Ok(()), | ||
} | ||
}; | ||
|
||
let (mut scan_progress, mut write_progress, mut spill_progress) = { | ||
let guard = self.inner.lock(); | ||
( | ||
guard.finished_scan_total.clone(), | ||
guard.finished_write_total.clone(), | ||
guard.finished_spill_total.clone(), | ||
) | ||
}; | ||
for process in processes { | ||
if let Some(scan) = &process.scan_progress_value { | ||
scan_progress = scan_progress.add(scan); | ||
} | ||
if let Some(write) = &process.write_progress_value { | ||
write_progress = write_progress.add(write); | ||
} | ||
if let Some(spill) = &process.spill_progress_value { | ||
spill_progress = spill_progress.add(spill); | ||
} | ||
} | ||
|
||
let metrics = vec![ | ||
( | ||
METRIC_QUERY_SCAN_PROGRESS_ROWS, | ||
scan_progress.rows as f64, | ||
"Total scan rows in progress.", | ||
), | ||
( | ||
METRIC_QUERY_SCAN_PROGRESS_BYTES, | ||
scan_progress.bytes as f64, | ||
"Total scan bytes in progress.", | ||
), | ||
( | ||
METRIC_QUERY_WRITE_PROGRESS_ROWS, | ||
write_progress.rows as f64, | ||
"Total write rows in progress.", | ||
), | ||
( | ||
METRIC_QUERY_WRITE_PROGRESS_BYTES, | ||
write_progress.bytes as f64, | ||
"Total write bytes in progress.", | ||
), | ||
( | ||
METRIC_QUERY_SPILL_PROGRESS_ROWS, | ||
spill_progress.rows as f64, | ||
"Total spill rows in progress.", | ||
), | ||
( | ||
METRIC_QUERY_SPILL_PROGRESS_BYTES, | ||
spill_progress.bytes as f64, | ||
"Total spill bytes in progress.", | ||
), | ||
]; | ||
|
||
for (name, value, help) in metrics { | ||
let counter = ConstCounter::new(value); | ||
let counter_encoder = | ||
encoder.encode_descriptor(name, help, None, counter.metric_type())?; | ||
counter.encode(counter_encoder)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.