sea-orm-tracing
OpenTelemetry-compatible tracing instrumentation for SeaORM database operations.
Features
- Automatic Instrumentation: All queries executed through
TracedConnectionare traced - OpenTelemetry Compatible: Spans include semantic conventions for database operations
- Proper Span Nesting: Database spans appear as children of HTTP request spans (works with tower-http, actix-web, etc.)
- SQL Visibility: Optionally include the actual SQL statement in spans
- Performance Metrics: Query duration, row counts, slow query detection, and error tracking
- Zero Config: Works out of the box with sensible defaults
Quick Start
use Database;
use *;
// Connect to your database
let db = connect.await?;
// Wrap with tracing - that's it!
let traced_db = db.with_tracing;
// Use it exactly like a normal DatabaseConnection
let users = find.all.await?;
Configuration
use ;
use Duration;
let db = connect.await?;
// Development: log everything
let traced_db = new;
// Production: minimal overhead
let traced_db = new;
// Custom configuration
let config = default
.with_statement_logging // Include SQL in spans
.with_parameter_logging // Don't log parameters (security)
.with_slow_query_threshold
.with_database_name; // Useful for multi-db setups
let traced_db = new;
Span Attributes
The following OpenTelemetry semantic convention attributes are recorded:
| Attribute | Description | Example |
|---|---|---|
db.system |
Database type | postgresql, mysql, sqlite |
db.operation |
SQL operation | SELECT, INSERT, UPDATE, DELETE |
db.sql.table |
Target table name | users |
db.statement |
Full SQL query (when enabled) | SELECT * FROM users WHERE id = $1 |
db.rows_affected |
Number of rows returned/affected | 42 |
db.duration_ms |
Query execution time in milliseconds | 12 |
otel.status_code |
Result status | OK or ERROR |
error.message |
Error details (on failure) | relation "users" does not exist |
slow_query |
Whether query exceeded threshold | true |
Integration with Web Frameworks
The magic of sea-orm-tracing is that database spans automatically become children of whatever span is currently active. This means if you're using tracing middleware in your web framework, you get perfect span hierarchies:
Axum
use ;
use TraceLayer;
use *;
use Arc;
async
let app = new
.route
.layer // Creates HTTP request spans
.with_state;
Resulting trace:
HTTP GET /users (200 OK) - 45ms
└── db.query SELECT users - 12ms
├── db.system: postgresql
├── db.operation: SELECT
├── db.sql.table: users
└── db.rows_affected: 42
Actix-web
use ;
use TracingLogger;
new
Security Considerations
By default, sea-orm-tracing does not log SQL statements or parameters, as these may contain sensitive data. Use the configuration options carefully:
// SAFE for production
production
// Only enable in development/debugging
development
// Custom: log SQL but not parameters
default
.with_statement_logging
.with_parameter_logging
Comparison with sqlx-tracing
This crate is inspired by sqlx-tracing but designed specifically for SeaORM:
| Feature | sea-orm-tracing | sqlx-tracing |
|---|---|---|
| ORM Support | SeaORM entities & relations | Raw SQL only |
| Table Detection | Automatic from SQL parsing | Manual |
| Configuration | Builder pattern | Similar |
| Span Nesting | Via tracing::Instrument |
Similar |
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.