mas_storage_pg/tracing.rs
1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use opentelemetry_semantic_conventions::attribute::DB_QUERY_TEXT;
8use tracing::Span;
9
10/// An extension trait for [`sqlx::Execute`] that records the SQL statement as
11/// `db.query.text` in a tracing span
12pub trait ExecuteExt<'q, DB>: Sized {
13 /// Records the statement as `db.query.text` in the current span
14 #[must_use]
15 fn traced(self) -> Self {
16 self.record(&Span::current())
17 }
18
19 /// Records the statement as `db.query.text` in the given span
20 #[must_use]
21 fn record(self, span: &Span) -> Self;
22}
23
24impl<'q, DB, T> ExecuteExt<'q, DB> for T
25where
26 T: sqlx::Execute<'q, DB>,
27 DB: sqlx::Database,
28{
29 fn record(self, span: &Span) -> Self {
30 span.record(DB_QUERY_TEXT, self.sql());
31 self
32 }
33}