Skip to main content

Module sqlx

Module sqlx 

Source
Expand description

sqlx 0.8 and 0.9, by wrapping the pool: v0_8 with the sqlx-0.8 feature, v0_9 with sqlx-0.9.

let pool = oxtel_sql::sqlx::v0_9::Pool::new(pool);

let users = sqlx::query("SELECT name FROM users").fetch_all(&pool).await?;
let mut tx = pool.begin().await?;
sqlx::query("UPDATE users SET seen = now()").execute(&mut tx).await?;
tx.commit().await?;

Queries run on the wrapped Pool, on a Transaction from its begin, or on a Connection from its acquire are each recorded as a database call:

  • a client span, named by the query summary, with db.system.name (postgresql, mysql or sqlite), db.namespace and the server from the pool’s connection options, db.query.text (literals replaced by ?) and db.response.returned_rows;
  • db.client.operation.duration, from when the query starts on its connection;
  • for a failed query, the database’s error code (a SQLSTATE for PostgreSQL and MySQL) as db.response.status_code and error.type, or sqlx’s error kind, such as sqlx::Error::PoolTimedOut.

BEGIN, COMMIT and ROLLBACK are calls too. Preparing a statement isn’t. The pool also records the conventions’ connection pool metrics (db.client.connection.*): connections in use and idle, callers waiting, wait and use times, and timeouts. They’re still in development in the conventions.

The wrappers dereference to sqlx’s types, so the rest of their methods still work. A query run through sqlx’s own types, such as &*pool or &mut **tx, isn’t recorded by the wrapper.

§This, or oxtel’s sqlx mapping

oxtel::init() also records sqlx’s queries with no code change, from the event sqlx writes as each finishes. That event carries no error and doesn’t say which database it’s about, so its spans can’t be marked as failed and their db.system.name is other_sql unless configured. It needs sqlx’s statement logging, which is on by default. The wrapper records all of it, but only for queries run through it. The two never count a query twice: the mapping leaves alone the queries the wrapper records. See oxtel::mappings and the databases guide.

Modules§

v0_8
sqlx 0.8. See the sqlx module for what’s recorded.
v0_9
sqlx 0.9. See the sqlx module for what’s recorded.