Skip to content

Commit 22437de

Browse files
thejefflarsonclaude
andcommitted
feat: db.query child spans on the API handlers
Wrap each handler's query future in a db.query span, so a request trace breaks down into handler -> db.query and you can see how much of an endpoint's time is SQL (e.g. service_map's 710ms). Nests under the per-handler span, which nests under the (traefik-parented) request span. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent b9f4499 commit 22437de

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

server/src/api.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use axum::{
88
use chrono::{DateTime, Utc};
99
use serde::{Deserialize, Serialize};
1010
use sqlx::PgPool;
11+
use tracing::Instrument;
1112

1213
type ApiError = (StatusCode, String);
1314

@@ -66,6 +67,7 @@ pub async fn list_traces(
6667
.bind(q.to)
6768
.bind(limit)
6869
.fetch_all(&pool)
70+
.instrument(tracing::info_span!("db.query"))
6971
.await
7072
.map_err(internal)?;
7173
Ok(Json(rows))
@@ -101,6 +103,7 @@ pub async fn get_trace(
101103
)
102104
.bind(trace_id)
103105
.fetch_all(&pool)
106+
.instrument(tracing::info_span!("db.query"))
104107
.await
105108
.map_err(internal)?;
106109
Ok(Json(rows))
@@ -166,6 +169,7 @@ pub async fn list_logs(
166169
.bind(attr_json)
167170
.bind(limit)
168171
.fetch_all(&pool)
172+
.instrument(tracing::info_span!("db.query"))
169173
.await
170174
.map_err(internal)?;
171175
Ok(Json(rows))
@@ -263,6 +267,7 @@ pub async fn list_metrics(
263267
.bind(q.to)
264268
.bind(limit)
265269
.fetch_all(&pool)
270+
.instrument(tracing::info_span!("db.query"))
266271
.await
267272
.map_err(internal)?;
268273
Ok(Json(rows))
@@ -325,6 +330,7 @@ pub async fn metric_series(
325330
.bind(hours)
326331
.bind(width)
327332
.fetch_all(&pool)
333+
.instrument(tracing::info_span!("db.query"))
328334
.await
329335
.map_err(internal)?;
330336
Ok(Json(rows))
@@ -349,6 +355,7 @@ pub async fn metric_dims(
349355
)
350356
.bind(q.name)
351357
.fetch_all(&pool)
358+
.instrument(tracing::info_span!("db.query"))
352359
.await
353360
.map_err(internal)?;
354361
Ok(Json(keys))
@@ -392,6 +399,7 @@ pub async fn metric_series_grouped(
392399
.bind(hours)
393400
.bind(width)
394401
.fetch_all(&pool)
402+
.instrument(tracing::info_span!("db.query"))
395403
.await
396404
.map_err(internal)?;
397405
Ok(Json(rows))
@@ -461,6 +469,7 @@ pub async fn metric_facet(
461469
)
462470
.bind(&q.name)
463471
.fetch_optional(&pool)
472+
.instrument(tracing::info_span!("db.query"))
464473
.await
465474
.map_err(internal)?;
466475
let (kind, is_monotonic, unit) = match meta {
@@ -490,6 +499,7 @@ pub async fn metric_facet(
490499
.bind(&q.name)
491500
.bind(hours)
492501
.fetch_all(&pool)
502+
.instrument(tracing::info_span!("db.query"))
493503
.await
494504
.map_err(internal)?;
495505

@@ -610,6 +620,7 @@ pub async fn metric_histogram(
610620
.bind(&q.name)
611621
.bind(hours)
612622
.fetch_all(&pool)
623+
.instrument(tracing::info_span!("db.query"))
613624
.await
614625
.map_err(internal)?;
615626

@@ -744,6 +755,7 @@ pub async fn metric_hist_facet(
744755
.bind(&q.name)
745756
.bind(hours)
746757
.fetch_all(&pool)
758+
.instrument(tracing::info_span!("db.query"))
747759
.await
748760
.map_err(internal)?;
749761

@@ -843,6 +855,7 @@ pub async fn service_red(
843855
.bind(q.from)
844856
.bind(q.to)
845857
.fetch_all(&pool)
858+
.instrument(tracing::info_span!("db.query"))
846859
.await
847860
.map_err(internal)?;
848861
Ok(Json(rows))
@@ -872,6 +885,7 @@ pub async fn service_map(State(pool): State<PgPool>) -> Result<Json<ServiceMap>,
872885
ORDER BY 1",
873886
)
874887
.fetch_all(&pool)
888+
.instrument(tracing::info_span!("db.query"))
875889
.await
876890
.map_err(internal)?;
877891

@@ -889,6 +903,7 @@ pub async fn service_map(State(pool): State<PgPool>) -> Result<Json<ServiceMap>,
889903
ORDER BY calls DESC",
890904
)
891905
.fetch_all(&pool)
906+
.instrument(tracing::info_span!("db.query"))
892907
.await
893908
.map_err(internal)?;
894909

@@ -930,6 +945,7 @@ pub async fn list_alerts(State(pool): State<PgPool>) -> Result<Json<Vec<AlertRul
930945
ORDER BY r.created_at DESC",
931946
)
932947
.fetch_all(&pool)
948+
.instrument(tracing::info_span!("db.query"))
933949
.await
934950
.map_err(internal)?;
935951
Ok(Json(rows))
@@ -977,6 +993,7 @@ pub async fn create_alert(
977993
.bind(agg)
978994
.bind(window_secs)
979995
.fetch_one(&pool)
996+
.instrument(tracing::info_span!("db.query"))
980997
.await
981998
.map_err(internal)?;
982999
Ok(Json(id))
@@ -1029,6 +1046,7 @@ pub async fn list_alert_events(
10291046
)
10301047
.bind(limit)
10311048
.fetch_all(&pool)
1049+
.instrument(tracing::info_span!("db.query"))
10321050
.await
10331051
.map_err(internal)?;
10341052
Ok(Json(rows))

0 commit comments

Comments
 (0)