Add task type label to metrics (#13240)

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>

Signed-off-by: Andrei Sandu <andrei-mihail@parity.io>
This commit is contained in:
Andrei Sandu
2023-01-26 08:22:51 +02:00
committed by GitHub
parent 96ca4bed0e
commit f552df7bbf
@@ -129,12 +129,20 @@ impl SpawnTaskHandle {
GroupName::Default => DEFAULT_GROUP_NAME, GroupName::Default => DEFAULT_GROUP_NAME,
}; };
let task_type_label = match task_type {
TaskType::Blocking => "blocking",
TaskType::Async => "async",
};
// Note that we increase the started counter here and not within the future. This way, // Note that we increase the started counter here and not within the future. This way,
// we could properly visualize on Prometheus situations where the spawning doesn't work. // we could properly visualize on Prometheus situations where the spawning doesn't work.
if let Some(metrics) = &self.metrics { if let Some(metrics) = &self.metrics {
metrics.tasks_spawned.with_label_values(&[name, group]).inc(); metrics.tasks_spawned.with_label_values(&[name, group, task_type_label]).inc();
// We do a dummy increase in order for the task to show up in metrics. // We do a dummy increase in order for the task to show up in metrics.
metrics.tasks_ended.with_label_values(&[name, "finished", group]).inc_by(0); metrics
.tasks_ended
.with_label_values(&[name, "finished", group, task_type_label])
.inc_by(0);
} }
let future = async move { let future = async move {
@@ -145,8 +153,10 @@ impl SpawnTaskHandle {
if let Some(metrics) = metrics { if let Some(metrics) = metrics {
// Add some wrappers around `task`. // Add some wrappers around `task`.
let task = { let task = {
let poll_duration = metrics.poll_duration.with_label_values(&[name, group]); let poll_duration =
let poll_start = metrics.poll_start.with_label_values(&[name, group]); metrics.poll_duration.with_label_values(&[name, group, task_type_label]);
let poll_start =
metrics.poll_start.with_label_values(&[name, group, task_type_label]);
let inner = let inner =
prometheus_future::with_poll_durations(poll_duration, poll_start, task); prometheus_future::with_poll_durations(poll_duration, poll_start, task);
// The logic of `AssertUnwindSafe` here is ok considering that we throw // The logic of `AssertUnwindSafe` here is ok considering that we throw
@@ -157,15 +167,24 @@ impl SpawnTaskHandle {
match select(on_exit, task).await { match select(on_exit, task).await {
Either::Right((Err(payload), _)) => { Either::Right((Err(payload), _)) => {
metrics.tasks_ended.with_label_values(&[name, "panic", group]).inc(); metrics
.tasks_ended
.with_label_values(&[name, "panic", group, task_type_label])
.inc();
panic::resume_unwind(payload) panic::resume_unwind(payload)
}, },
Either::Right((Ok(()), _)) => { Either::Right((Ok(()), _)) => {
metrics.tasks_ended.with_label_values(&[name, "finished", group]).inc(); metrics
.tasks_ended
.with_label_values(&[name, "finished", group, task_type_label])
.inc();
}, },
Either::Left(((), _)) => { Either::Left(((), _)) => {
// The `on_exit` has triggered. // The `on_exit` has triggered.
metrics.tasks_ended.with_label_values(&[name, "interrupted", group]).inc(); metrics
.tasks_ended
.with_label_values(&[name, "interrupted", group, task_type_label])
.inc();
}, },
} }
} else { } else {
@@ -433,28 +452,28 @@ impl Metrics {
buckets: exponential_buckets(0.001, 4.0, 9) buckets: exponential_buckets(0.001, 4.0, 9)
.expect("function parameters are constant and always valid; qed"), .expect("function parameters are constant and always valid; qed"),
}, },
&["task_name", "task_group"] &["task_name", "task_group", "kind"]
)?, registry)?, )?, registry)?,
poll_start: register(CounterVec::new( poll_start: register(CounterVec::new(
Opts::new( Opts::new(
"substrate_tasks_polling_started_total", "substrate_tasks_polling_started_total",
"Total number of times we started invoking Future::poll" "Total number of times we started invoking Future::poll"
), ),
&["task_name", "task_group"] &["task_name", "task_group", "kind"]
)?, registry)?, )?, registry)?,
tasks_spawned: register(CounterVec::new( tasks_spawned: register(CounterVec::new(
Opts::new( Opts::new(
"substrate_tasks_spawned_total", "substrate_tasks_spawned_total",
"Total number of tasks that have been spawned on the Service" "Total number of tasks that have been spawned on the Service"
), ),
&["task_name", "task_group"] &["task_name", "task_group", "kind"]
)?, registry)?, )?, registry)?,
tasks_ended: register(CounterVec::new( tasks_ended: register(CounterVec::new(
Opts::new( Opts::new(
"substrate_tasks_ended_total", "substrate_tasks_ended_total",
"Total number of tasks for which Future::poll has returned Ready(()) or panicked" "Total number of tasks for which Future::poll has returned Ready(()) or panicked"
), ),
&["task_name", "reason", "task_group"] &["task_name", "reason", "task_group", "kind"]
)?, registry)?, )?, registry)?,
}) })
} }