rs_zero/observability/
sql.rs1use std::{fmt::Display, future::Future, time::Instant};
2
3use tracing::Instrument;
4
5use crate::observability::{MetricsRegistry, SqlMetricLabels};
6
7pub async fn observe_sql_query<T, E, Fut>(
9 metrics: Option<&MetricsRegistry>,
10 db_kind: &str,
11 repository: &str,
12 method: &str,
13 operation: &str,
14 future: Fut,
15) -> Result<T, E>
16where
17 E: Display,
18 Fut: Future<Output = Result<T, E>>,
19{
20 let started = Instant::now();
21 let span = tracing::debug_span!(
22 "rs_zero.sql.query",
23 db_kind = db_kind,
24 repository = repository,
25 method = method,
26 operation = operation,
27 result = tracing::field::Empty
28 );
29 let result = future.instrument(span.clone()).await;
30 let result_label = if result.is_ok() { "success" } else { "error" };
31 span.record("result", tracing::field::display(result_label));
32 if let Some(metrics) = metrics {
33 metrics.record_sql_query(
34 SqlMetricLabels::new(db_kind, repository, method, operation, result_label),
35 started.elapsed(),
36 );
37 }
38 result
39}