#![cfg(feature = "otel")]
use diesel::prelude::*;
use diesel_libsql::{LibSqlConnection, OtelInstrumentation};
#[test]
fn test_otel_instrumentation_no_panic() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::new());
diesel::sql_query("CREATE TABLE otel_test (id INTEGER PRIMARY KEY, val TEXT)")
.execute(&mut conn)
.expect("Failed to create table");
diesel::sql_query("INSERT INTO otel_test (id, val) VALUES (1, 'hello')")
.execute(&mut conn)
.expect("Failed to insert");
diesel::sql_query("INSERT INTO otel_test (id, val) VALUES (99, 'check')")
.execute(&mut conn)
.expect("Failed to insert second row");
conn.transaction::<_, diesel::result::Error, _>(|conn| {
diesel::sql_query("INSERT INTO otel_test (id, val) VALUES (2, 'world')").execute(conn)?;
Ok(())
})
.expect("Transaction failed");
}
#[test]
fn test_otel_transaction_commit() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::new());
diesel::sql_query("CREATE TABLE otel_txn (id INTEGER PRIMARY KEY, val TEXT)")
.execute(&mut conn)
.expect("Failed to create table");
conn.transaction::<_, diesel::result::Error, _>(|conn| {
diesel::sql_query("INSERT INTO otel_txn (id, val) VALUES (1, 'committed')")
.execute(conn)?;
Ok(())
})
.expect("Transaction failed");
diesel::table! {
otel_txn (id) {
id -> Integer,
val -> Text,
}
}
let results: Vec<(i32, String)> = otel_txn::table
.select((otel_txn::id, otel_txn::val))
.load(&mut conn)
.expect("Failed to load");
assert_eq!(results.len(), 1);
assert_eq!(results[0].1, "committed");
}
#[test]
fn test_otel_transaction_rollback() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::new());
diesel::sql_query("CREATE TABLE otel_rollback (id INTEGER PRIMARY KEY, val TEXT)")
.execute(&mut conn)
.expect("Failed to create table");
let result: Result<(), diesel::result::Error> = conn
.transaction::<_, diesel::result::Error, _>(|conn| {
diesel::sql_query("INSERT INTO otel_rollback (id, val) VALUES (1, 'will_rollback')")
.execute(conn)?;
Err(diesel::result::Error::RollbackTransaction)
});
assert!(result.is_err());
}
#[test]
fn test_otel_query_error() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::new());
let result = diesel::sql_query("SELECT * FROM nonexistent_table_otel").execute(&mut conn);
assert!(result.is_err());
}
#[test]
fn test_otel_no_query_text() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::new().with_query_text(false));
diesel::sql_query("CREATE TABLE otel_notext (id INTEGER PRIMARY KEY)")
.execute(&mut conn)
.expect("Failed to create table");
diesel::sql_query("INSERT INTO otel_notext (id) VALUES (1)")
.execute(&mut conn)
.expect("Failed to insert");
}
#[test]
fn test_otel_establish_connection_events() {
use diesel::connection::SimpleConnection;
let _ = diesel::connection::set_default_instrumentation(|| {
Some(Box::new(OtelInstrumentation::new()))
});
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.batch_execute("SELECT 1")
.expect("Connection should work");
let _ = diesel::connection::set_default_instrumentation(|| None);
}
#[test]
fn test_otel_default_impl() {
let mut conn = LibSqlConnection::establish(":memory:").expect("Failed to connect");
conn.set_instrumentation(OtelInstrumentation::default());
diesel::sql_query("CREATE TABLE otel_default_test (id INTEGER PRIMARY KEY)")
.execute(&mut conn)
.expect("Failed to create table");
}
#[test]
fn test_otel_auth_token_redaction() {
use diesel::connection::SimpleConnection;
let _ = diesel::connection::set_default_instrumentation(|| {
Some(Box::new(OtelInstrumentation::new()))
});
let result = LibSqlConnection::establish("libsql://fake.example.com?authToken=secret123");
if let Ok(mut conn) = result {
let _ = conn.batch_execute("SELECT 1");
}
let _ = diesel::connection::set_default_instrumentation(|| None);
}
#[test]
fn test_otel_establish_error() {
let _ = diesel::connection::set_default_instrumentation(|| {
Some(Box::new(OtelInstrumentation::new()))
});
std::env::remove_var("LIBSQL_AUTH_TOKEN");
let result = LibSqlConnection::establish("libsql://fake.example.com");
assert!(result.is_err());
let _ = diesel::connection::set_default_instrumentation(|| None);
}