Repository navigation
Collecting metrics of a Pool #4500
Replies: 2 comments
|
For latency there's a way that needs no wrapper at all. Every statement sqlx runs already emits a use tracing::field::{Field, Visit};
use tracing_subscriber::{layer::Context, Layer};
struct SqlxMetricsLayer; // holds your counters / histogram
#[derive(Default)]
struct QueryFields {
elapsed_secs: Option<f64>,
}
impl Visit for QueryFields {
fn record_f64(&mut self, field: &Field, value: f64) {
if field.name() == "elapsed_secs" {
self.elapsed_secs = Some(value);
}
}
fn record_debug(&mut self, _: &Field, _: &dyn std::fmt::Debug) {}
}
impl<S: tracing::Subscriber> Layer<S> for SqlxMetricsLayer {
fn on_event(&self, event: &tracing::Event<'_>, _: Context<'_, S>) {
if event.metadata().target() != "sqlx::query" {
return;
}
let mut fields = QueryFields::default();
event.record(&mut fields);
if let Some(secs) = fields.elapsed_secs {
// count += 1; latency histogram.record(secs)
}
}
}Two things to know:
For pool-level gauges, |
|
I am really looking for something which allows me to track query passed vs failed. Ideally, the instrumentation logic also only lives in one place (centralized), and is not scattered throughout multiple places. I appreciate the idea of using a tracing subscriber, but unfortunately, I do not consider it flexible enough for my use case. |
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone,
I'm looking for advice on how to instrument SQLx database operations in a maintainable and idiomatic way.
Alongisde my applications, I typically run a monitoring server (currently Munin) to collect metrics such as:
My objective is to collect these metrics automatically, without spreading instrumentation logic throughout the application.
Approach 1: Instrument individual queries
A straightforward solution, is to maintain a collection of counters and update them whenever a query executes.
For example:
Each query could then update these counters based on its results.
However this becomes tedious. Instrumentation logic ends up scattered across the codebase, introducing boilerplate, and making it easy to forget to instrument an operation.
Approach 2: Wrap the connection pool and implement Executor
To centralize the instrumentation, I've experimented with wrapping the SQLx pool alongside the metrics.
For example:
The wrapper implements the
Executortrait, and delegates operations to the underlying pool, recording metrics along the way.Here is an excerpt from a similar implementation I use:
This works and keeps the instrumentation logic centralized, but I'm uncomfortable with two aspects:
Approach 3: Provide instrumented query functions
I could expose functions such as execute, fetch_optional, and fetch_many that accept my pool wrapper, delegate operations to the underlying sqlx pool and record the appropriate metrics.
This would remove the need to implement Executor, but it would also mean maintaining a parallel interface for executing queries.
Question
Is there a more idiomatic way to instrument sqlx operations centrally?
I'd appreciate any suggestions or examples of how others have approached this.
Thank you for your time in advance !
All reactions