Skip to main content

QueryAnnotateExt

Trait QueryAnnotateExt 

Source
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§

Source

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§

Source

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.

Implementations on Foreign Types§

Source§

impl<DB: Database, A> QueryAnnotateExt for Query<'_, DB, A>

Source§

impl<DB: Database, F, A> QueryAnnotateExt for Map<'_, DB, F, A>

Source§

impl<DB: Database, O, A> QueryAnnotateExt for QueryAs<'_, DB, O, A>

Source§

impl<DB: Database, O, A> QueryAnnotateExt for QueryScalar<'_, DB, O, A>

Implementors§