pub trait QueryAnnotateExt: Sealed + Sized {
// Required method
fn with_annotations(
self,
annotations: QueryAnnotations,
) -> AnnotatedQuery<Self>;
// Provided method
fn with_operation(
self,
operation: impl Into<String>,
collection: impl Into<String>,
) -> AnnotatedQuery<Self> { ... }
}Expand description
Extension trait that attaches OpenTelemetry per-query annotations to the function-form
SQLx query builders (sqlx::query(), sqlx::query_as(),
sqlx::query_scalar()) and to the Map returned by
Query::map / Query::try_map.
The trait is sealed and cannot be implemented downstream.
§Example
use sqlx_otel::QueryAnnotateExt;
sqlx::query("INSERT INTO orders (user_id) VALUES (?)")
.bind(42_i64)
.with_operation("INSERT", "orders")
.execute(&pool)
.await?;Required Methods§
Sourcefn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self>
fn with_annotations(self, annotations: QueryAnnotations) -> AnnotatedQuery<Self>
Wrap the query with the given per-query annotations.
Returns an AnnotatedQuery that exposes the same execute/fetch* surface as the
inner query but threads the annotations through to OpenTelemetry span creation.
Provided Methods§
Sourcefn with_operation(
self,
operation: impl Into<String>,
collection: impl Into<String>,
) -> AnnotatedQuery<Self>
fn with_operation( self, operation: impl Into<String>, collection: impl Into<String>, ) -> AnnotatedQuery<Self>
Shorthand that sets db.operation.name and db.collection.name.
Equivalent to
self.with_annotations(QueryAnnotations::new().operation(op).collection(coll)).
The returned future is Send and may be used inside a Send-required async context
(e.g. tokio::spawn, axum handlers, tower::Service-bounded futures). The example
below intentionally swallows the inner sqlx::Error via .ok().flatten() – the
point is to exercise the Send contract on the spawned future at compile time, not
to demonstrate error handling. A non-Send future would fail to compile here, since
tokio::spawn requires Send.
use sqlx_otel::QueryAnnotateExt as _;
tokio::spawn(async move {
let _: Option<(i32,)> = sqlx::query_as("SELECT 1")
.with_operation("SELECT", "users")
.fetch_optional(&pool)
.await
.ok()
.flatten();
})
.await?;Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.