Files
sqlpage--sqlpage/src/telemetry_metrics.rs
T
wehub-resource-sync d718c5a372
CI / compile_and_lint (push) Failing after 0s
CI / docker_build (linux/amd64, -linux-amd64-duckdb, duckdb) (push) Failing after 0s
CI / docker_build (linux/arm64, -linux-arm64, minimal) (push) Failing after 2s
CI / docker_build (linux/arm64, -linux-arm64-duckdb, duckdb) (push) Failing after 1s
CI / docker_build (linux/amd64, minimal) (push) Failing after 1s
CI / test (, sqlite, sqlite::memory:) (push) Has been skipped
CI / test (mssql, mssql, mssql://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / test (mysql, mysql, mysql://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / test (oracle, oracle, Driver=Oracle 21 ODBC driver;Dbq=//127.0.0.1:1521/FREEPDB1;Uid=root;Pwd=Password123!) (push) Has been skipped
CI / test (postgres, odbc, Driver=PostgreSQL Unicode;Server=127.0.0.1;Port=5432;Database=sqlpage;UID=root;PWD=Password123!, true) (push) Has been skipped
CI / test (postgres, postgres, postgres://root:Password123!@127.0.0.1/sqlpage) (push) Has been skipped
CI / playwright (push) Has been skipped
CI / docker_build (linux/arm/v7, -linux-arm-v7, minimal) (push) Failing after 0s
CI / hurl_examples (push) Failing after 8s
deploy website / deploy_official_site (push) Failing after 1s
CI / hurl (${{ matrix.example }}) (push) Has been skipped
CI / docker_push (duckdb) (push) Has been cancelled
CI / docker_push (minimal) (push) Has been cancelled
CI / windows_test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:57 +08:00

98 lines
3.8 KiB
Rust

use opentelemetry::global;
use opentelemetry::metrics::{Histogram, ObservableGauge};
use opentelemetry_semantic_conventions::attribute as otel;
use opentelemetry_semantic_conventions::metric as otel_metric;
use sqlx::AnyPool;
pub struct TelemetryMetrics {
pub http_request_duration: Histogram<f64>,
pub db_query_duration: Histogram<f64>,
_pool_connection_count: ObservableGauge<i64>,
}
impl Default for TelemetryMetrics {
fn default() -> Self {
let meter = global::meter("sqlpage");
let http_request_duration = meter
.f64_histogram(otel_metric::HTTP_SERVER_REQUEST_DURATION)
.with_unit("s")
.with_description("Duration of HTTP requests processed by the server.")
.build();
let db_query_duration = meter
.f64_histogram(otel_metric::DB_CLIENT_OPERATION_DURATION)
.with_unit("s")
.with_description("Duration of executing SQL queries.")
.build();
// This default is only used in tests that don't touch pool metrics.
let pool_connection_count = meter
.i64_observable_gauge(otel_metric::DB_CLIENT_CONNECTION_COUNT)
.with_unit("{connection}")
.with_description("Number of connections in the database pool.")
.with_callback(|_| {})
.build();
Self {
http_request_duration,
db_query_duration,
_pool_connection_count: pool_connection_count,
}
}
}
impl TelemetryMetrics {
#[must_use]
pub fn new(pool: &AnyPool, db_system_name: &'static str) -> Self {
let meter = global::meter("sqlpage");
let http_request_duration = meter
.f64_histogram(otel_metric::HTTP_SERVER_REQUEST_DURATION)
.with_unit("s")
.with_description("Duration of HTTP requests processed by the server.")
.build();
let db_query_duration = meter
.f64_histogram(otel_metric::DB_CLIENT_OPERATION_DURATION)
.with_unit("s")
.with_description("Duration of executing SQL queries.")
.build();
let pool_ref = pool.clone();
let pool_connection_count = meter
.i64_observable_gauge(otel_metric::DB_CLIENT_CONNECTION_COUNT)
.with_unit("{connection}")
.with_description("Number of connections in the database pool.")
.with_callback(move |observer| {
let size = pool_ref.size();
let idle_u32 = u32::try_from(pool_ref.num_idle()).unwrap_or(u32::MAX);
let used = i64::from(size.saturating_sub(idle_u32));
let idle = i64::from(idle_u32);
observer.observe(
used,
&[
opentelemetry::KeyValue::new(otel::DB_SYSTEM_NAME, db_system_name),
opentelemetry::KeyValue::new(
otel::DB_CLIENT_CONNECTION_POOL_NAME,
"sqlpage",
),
opentelemetry::KeyValue::new(otel::DB_CLIENT_CONNECTION_STATE, "used"),
],
);
observer.observe(
idle,
&[
opentelemetry::KeyValue::new(otel::DB_SYSTEM_NAME, db_system_name),
opentelemetry::KeyValue::new(
otel::DB_CLIENT_CONNECTION_POOL_NAME,
"sqlpage",
),
opentelemetry::KeyValue::new(otel::DB_CLIENT_CONNECTION_STATE, "idle"),
],
);
})
.build();
Self {
http_request_duration,
db_query_duration,
_pool_connection_count: pool_connection_count,
}
}
}