use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use id_effect::{
Boundaries, Counter, Effect, Histogram, Metric, MetricRegistry, Never, Stream, suspend,
};
use crate::client::SqlClient;
use crate::error::SqlError;
use crate::transaction::{SqlTransaction, TransactionOptions};
use crate::{SqlRow, SqlValue};
pub const DB_CLIENT_OPERATIONS_TOTAL: &str = "db_client_operations_total";
pub const DB_CLIENT_OPERATION_DURATION_SECONDS: &str = "db_client_operation_duration_seconds";
pub const DB_CLIENT_CONNECTION_COUNT: &str = "db_client_connection_count";
pub const DB_CLIENT_CONNECTION_MAX: &str = "db_client_connection_max";
pub fn duration_boundaries() -> Boundaries {
Boundaries::new([
0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
])
}
pub const DB_QUERY_SPAN: &str = "db.query";
const STATEMENT_MAX_CHARS: usize = 1024;
#[derive(Clone, Debug)]
pub struct InstrumentedSqlClient<C> {
inner: C,
operations: Counter,
duration: Histogram,
db_system: &'static str,
}
impl<C: SqlClient> InstrumentedSqlClient<C> {
pub fn new(inner: C) -> Self {
Self::with_registry(inner, MetricRegistry::global().clone())
}
pub fn with_registry(inner: C, registry: MetricRegistry) -> Self {
Self {
inner,
operations: registry.counter(DB_CLIENT_OPERATIONS_TOTAL),
duration: registry.histogram(DB_CLIENT_OPERATION_DURATION_SECONDS, duration_boundaries()),
db_system: "other_sql",
}
}
pub fn with_db_system(mut self, system: &'static str) -> Self {
self.db_system = system;
self
}
pub fn inner(&self) -> &C {
&self.inner
}
fn statement<A: Send + 'static>(
&self,
op: &'static str,
sql: &str,
eff: Effect<A, SqlError>,
) -> Effect<A, SqlError> {
let statement = statement_summary(sql);
let operation = statement
.split(|c: char| !c.is_ascii_alphabetic())
.find(|word| !word.is_empty())
.unwrap_or_default()
.to_ascii_uppercase();
self.instrument(op, eff).with_span_attrs(
DB_QUERY_SPAN,
[
("otel.kind", "client".to_owned()),
("db.system", self.db_system.to_owned()),
("db.operation", operation),
("db.statement", statement),
],
)
}
fn instrument<A: Send + 'static>(
&self,
op: &'static str,
eff: Effect<A, SqlError>,
) -> Effect<A, SqlError> {
let ops = self.operations.tagged("op", op);
let eff = ops
.tagged("outcome", "ok")
.track_exit_with(eff, |k| k.is_success().then_some(1));
let eff = ops
.tagged("outcome", "error")
.track_exit_with(eff, |k| (!k.is_success()).then_some(1));
self
.duration
.tagged("op", op)
.track_duration_with(eff, |d| d.as_secs_f64())
}
}
impl<C: SqlClient> SqlClient for InstrumentedSqlClient<C> {
fn connect(&self) -> Effect<(), SqlError> {
self.instrument("connect", self.inner.connect())
}
fn query(&self, sql: &str, params: &[SqlValue]) -> Effect<Vec<SqlRow>, SqlError> {
self.statement("query", sql, self.inner.query(sql, params))
}
fn execute(&self, sql: &str, params: &[SqlValue]) -> Effect<u64, SqlError> {
self.statement("execute", sql, self.inner.execute(sql, params))
}
fn begin(&self) -> Effect<Arc<dyn SqlTransaction>, SqlError> {
self.instrument("begin", self.inner.begin())
}
fn begin_with(&self, options: TransactionOptions) -> Effect<Arc<dyn SqlTransaction>, SqlError> {
self.instrument("begin", self.inner.begin_with(options))
}
fn query_stream(&self, sql: &str, params: &[SqlValue]) -> Stream<SqlRow, SqlError> {
let rest: Rest = Arc::new(Mutex::new(Some(self.inner.query_stream(sql, params))));
let size = Arc::new(AtomicUsize::new(0));
let (slot, want) = (Arc::clone(&rest), Arc::clone(&size));
let mut first = Some(self.statement(
"query_stream",
sql,
suspend(move || pull_rest(&slot, want.load(Ordering::Relaxed))),
));
Stream::from_pull(move |n| match first.take() {
Some(setup) => {
size.store(n, Ordering::Relaxed);
setup
}
None => pull_rest(&rest, n),
})
}
fn close(&self) -> Effect<(), Never> {
self.inner.close()
}
}
type Rest = Arc<Mutex<Option<Stream<SqlRow, SqlError>>>>;
fn pull_rest(rest: &Rest, n: usize) -> Effect<Option<Vec<SqlRow>>, SqlError> {
let stream = rest
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take()
.unwrap_or_else(Stream::empty);
let rest = Arc::clone(rest);
stream.next_chunk_with_size(n).map(move |step| {
step.map(|(chunk, more)| {
*rest.lock().unwrap_or_else(PoisonError::into_inner) = Some(more);
chunk.into_vec()
})
})
}
fn statement_summary(sql: &str) -> String {
let mut out = String::new();
let mut len = 0;
let mut prev = ' ';
let mut chars = sql.chars().peekable();
while let Some(c) = chars.next() {
if len >= STATEMENT_MAX_CHARS {
break;
}
let pushed = if c == '\'' {
while let Some(n) = chars.next() {
if n == '\'' && chars.next_if_eq(&'\'').is_none() {
break;
}
}
'?'
} else if c.is_whitespace() {
if prev == ' ' {
continue;
}
' '
} else if c.is_ascii_digit() && !(prev.is_alphanumeric() || matches!(prev, '_' | '$')) {
while chars
.next_if(|n| n.is_ascii_alphanumeric() || *n == '.')
.is_some()
{}
'?'
} else {
c
};
out.push(pushed);
len += 1;
prev = pushed;
}
out.truncate(out.trim_end().len());
out
}
#[cfg(feature = "pg")]
pub fn record_pg_pool(client: &crate::pg::PgSqlClient, registry: &MetricRegistry) {
let pool = client.pool();
let size = i64::from(pool.size());
let idle = i64::try_from(pool.num_idle()).unwrap_or(i64::MAX);
let count = registry.gauge(DB_CLIENT_CONNECTION_COUNT);
count.tagged("state", "idle").update_now(idle);
count
.tagged("state", "used")
.update_now(size.saturating_sub(idle).max(0));
registry
.gauge(DB_CLIENT_CONNECTION_MAX)
.update_now(i64::from(pool.options().get_max_connections()));
}
#[cfg(test)]
mod tests {
use id_effect::{MetricState, Runtime};
use super::*;
use crate::TestSqlClient;
use crate::client::tests::{Recording, run};
use crate::transaction::TransactionOptions;
fn count(registry: &MetricRegistry, op: &str, outcome: &str) -> u64 {
registry
.snapshot()
.into_iter()
.find(|p| {
p.key.name() == DB_CLIENT_OPERATIONS_TOTAL
&& p.key.tags()
== [
("op".to_owned(), op.to_owned()),
("outcome".to_owned(), outcome.to_owned()),
]
})
.map_or(0, |p| match p.state {
MetricState::Counter { count } => count,
other => panic!("not a counter: {other:?}"),
})
}
fn durations(registry: &MetricRegistry, op: &str) -> u64 {
registry
.snapshot()
.into_iter()
.find(|p| {
p.key.name() == DB_CLIENT_OPERATION_DURATION_SECONDS
&& p.key.tags() == [("op".to_owned(), op.to_owned())]
})
.map_or(0, |p| match p.state {
MetricState::Histogram(h) => h.count,
other => panic!("not a histogram: {other:?}"),
})
}
#[test]
fn records_operations_by_op_and_outcome_with_durations() {
let registry = MetricRegistry::new();
let test = TestSqlClient::new();
test.script_query("SELECT 1", vec![SqlRow::default()]);
let client = InstrumentedSqlClient::with_registry(test.clone(), registry.clone());
assert_eq!(
run(client.query("SELECT 1", &[])).unwrap_err(),
SqlError::NotConnected
);
run(client.connect()).unwrap();
assert_eq!(run(client.query("SELECT 1", &[])).unwrap().len(), 1);
assert_eq!(run(client.query("SELECT 1", &[])).unwrap().len(), 1);
run(client.execute("DELETE FROM t", &[])).unwrap();
let tx = run(client.begin()).unwrap();
run(tx.commit()).unwrap();
test.fail_queries("boom");
assert!(run(client.query("SELECT 1", &[])).is_err());
let closed = Runtime::global().block_on(client.close());
assert!(matches!(closed, id_effect::Exit::Success(())));
assert_eq!(count(®istry, "connect", "ok"), 1);
assert_eq!(count(®istry, "query", "ok"), 2);
assert_eq!(count(®istry, "query", "error"), 2);
assert_eq!(count(®istry, "execute", "ok"), 1);
assert_eq!(count(®istry, "begin", "ok"), 1);
assert_eq!(count(®istry, "connect", "error"), 0);
assert_eq!(durations(®istry, "query"), 4);
assert_eq!(durations(®istry, "connect"), 1);
assert_eq!(test.recorded_queries().len(), 2, "the inner client ran");
assert_eq!(test.closes(), 1, "close passes through");
assert!(
registry
.snapshot()
.iter()
.all(|p| p.key.tags().iter().all(|(_, v)| !v.contains("SELECT")))
);
assert!(registry.snapshot().iter().all(|p| p.key.name() != "op"));
}
type Started = (String, Vec<(String, String)>);
#[derive(Default)]
struct Recorder(std::sync::Mutex<Vec<Started>>);
impl id_effect::SpanHook for Recorder {
fn on_start(&self, span: &id_effect::Span) {
self
.0
.lock()
.unwrap()
.push((span.name().to_owned(), span.attributes().to_vec()));
}
}
#[test]
fn statements_run_in_a_db_query_span_without_values() {
let test = TestSqlClient::new();
run(test.connect()).unwrap();
let client = InstrumentedSqlClient::with_registry(test, MetricRegistry::new())
.with_db_system("postgresql");
let recorder = Arc::new(Recorder::default());
let hook: Arc<dyn id_effect::SpanHook> = recorder.clone();
let secret = SqlValue::Text("hunter2".into());
let sql = "select *\n from users where name = 'ada' and id = $1 and age > 42";
let _ = run(client.query(sql, &[secret]).with_span_hook(hook.clone()));
run(
client
.execute("DELETE FROM t", &[])
.with_span_hook(hook.clone()),
)
.unwrap();
run(client.begin().with_span_hook(hook)).unwrap();
let spans = recorder.0.lock().unwrap();
assert_eq!(spans.len(), 2, "begin opens no span: {spans:?}");
let (name, attrs) = &spans[0];
assert_eq!(name, DB_QUERY_SPAN);
let get = |k: &str| {
attrs
.iter()
.find(|(key, _)| key == k)
.map(|(_, v)| v.as_str())
};
assert_eq!(get("otel.kind"), Some("client"));
assert_eq!(get("db.system"), Some("postgresql"));
assert_eq!(get("db.operation"), Some("SELECT"));
assert_eq!(
get("db.statement"),
Some("select * from users where name = ? and id = $1 and age > ?")
);
assert!(
attrs
.iter()
.all(|(_, v)| !v.contains("hunter2") && !v.contains("ada"))
);
assert_eq!(spans[1].1[2], ("db.operation".into(), "DELETE".into()));
}
#[test]
fn begin_with_and_query_stream_reach_the_inner_client() {
let registry = MetricRegistry::new();
let inner = Recording::connected();
let client = InstrumentedSqlClient::with_registry(inner.clone(), registry.clone())
.with_db_system("postgresql");
let recorder = Arc::new(Recorder::default());
let hook: Arc<dyn id_effect::SpanHook> = recorder.clone();
let options = TransactionOptions::default();
let tx = run(client.begin_with(options).with_span_hook(hook.clone())).unwrap();
run(tx.commit()).unwrap();
let stream = client.query_stream("SELECT n", &[]);
let rows = run(stream.run_collect().with_span_hook(hook)).unwrap();
assert_eq!(rows.len(), 2);
assert_eq!(inner.calls(), ["begin_with", "query_stream"], "no fallback");
assert_eq!(count(®istry, "begin", "ok"), 1);
assert_eq!(
count(®istry, "query_stream", "ok"),
1,
"the first pull only"
);
assert_eq!(durations(®istry, "query_stream"), 1);
let spans = recorder.0.lock().unwrap();
assert_eq!(spans.len(), 1, "the stream's setup: {spans:?}");
assert_eq!(spans[0].0, DB_QUERY_SPAN);
assert!(
spans[0]
.1
.contains(&("db.statement".into(), "SELECT n".into()))
);
drop(spans);
inner.inner.fail_queries("boom");
assert!(run(client.query_stream("SELECT n", &[]).run_collect()).is_err());
assert_eq!(count(®istry, "query_stream", "error"), 1);
drop(client.query_stream("SELECT n", &[]));
assert_eq!(durations(®istry, "query_stream"), 2);
}
#[test]
fn the_wrapper_wraps_a_shared_client() {
let inner = Recording::connected();
let shared: Arc<dyn SqlClient> = Arc::new(inner.clone());
let client = InstrumentedSqlClient::with_registry(shared, MetricRegistry::new());
let rows = run(client.query_stream("SELECT n", &[]).run_collect()).unwrap();
assert_eq!(rows.len(), 2);
run(client.begin_with(TransactionOptions::default())).unwrap();
assert_eq!(inner.calls(), ["query_stream", "begin_with"]);
}
#[test]
fn statement_summary_masks_literals_and_keeps_shape() {
assert_eq!(
statement_summary("UPDATE t1 SET note = 'it''s', n = -3.5e2 WHERE k = ?"),
"UPDATE t1 SET note = ?, n = -? WHERE k = ?"
);
assert_eq!(statement_summary(" SELECT 'open"), "SELECT ?");
assert_eq!(
statement_summary("INSERT INTO col_2 VALUES (0x1F)"),
"INSERT INTO col_2 VALUES (?)"
);
let long = format!("SELECT {}", "abc,".repeat(2000));
assert_eq!(
statement_summary(&long).chars().count(),
STATEMENT_MAX_CHARS
);
let client = InstrumentedSqlClient::new(TestSqlClient::new());
assert_eq!(client.db_system, "other_sql");
}
#[test]
fn the_wrapper_is_a_client_capability() {
let test = TestSqlClient::new();
test.script_execute("DELETE FROM t", 2);
run(test.connect()).unwrap();
let client = InstrumentedSqlClient::new(test.clone());
assert!(client.inner().is_connected());
let n = run(crate::execute("DELETE FROM t", vec![]).provide_all(crate::sql_client_env(client)))
.unwrap();
assert_eq!(n, 2);
}
}