Crate sea_orm_tracing

Crate sea_orm_tracing 

Source
Expand description

§sea-orm-tracing

OpenTelemetry-compatible tracing instrumentation for SeaORM database operations.

This crate provides transparent tracing for all SeaORM database queries, automatically creating spans with proper parent-child relationships that integrate with your existing tracing infrastructure (like HTTP request spans from axum or actix-web).

§Features

  • Automatic Instrumentation: All queries executed through TracedConnection are traced
  • OpenTelemetry Compatible: Spans include semantic conventions for database operations
  • Proper Span Nesting: Database spans appear as children of HTTP request spans
  • SQL Visibility: Optionally include the actual SQL statement in spans
  • Performance Metrics: Query duration, row counts, and error tracking
  • Zero Config: Works out of the box with sensible defaults

§Quick Start

use sea_orm::Database;
use sea_orm_tracing::TracedConnection;

// Wrap your existing connection
let db = Database::connect("postgres://localhost/mydb").await?;
let traced_db = TracedConnection::from(db);

// Use it exactly like a normal DatabaseConnection
let users = Users::find().all(&traced_db).await?;

§Configuration

use sea_orm_tracing::{TracedConnection, TracingConfig};

let config = TracingConfig::default()
    .with_statement_logging(true)  // Include SQL in spans (default: false for security)
    .with_parameter_logging(false) // Include query parameters (default: false)
    .with_slow_query_threshold(Duration::from_millis(100));

let traced_db = TracedConnection::new(db, config);

§Span Attributes

The following OpenTelemetry semantic convention attributes are recorded:

AttributeDescription
db.systemAlways “postgresql”, “mysql”, or “sqlite”
db.operationSQL operation (SELECT, INSERT, UPDATE, DELETE)
db.sql.tableTarget table name (when detectable)
db.statementFull SQL query (when enabled)
db.rows_affectedNumber of rows returned/affected
otel.status_code“OK” or “ERROR”
error.messageError details (on failure)

Modules§

prelude
Prelude module for convenient imports

Structs§

TracedConnection
A traced wrapper around SeaORM’s DatabaseConnection.
TracingConfig
Configuration options for database tracing.

Traits§

TracingExt
Extension trait for easy wrapping of database connections.