1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

OpenTelemetry upgrade

This commit is contained in:
Quentin Gliech
2023-08-07 14:20:37 +02:00
parent 6e8222c765
commit 699dfba55f
13 changed files with 225 additions and 242 deletions

View File

@ -13,6 +13,7 @@ futures-util = "0.3.28"
# Logging and tracing
tracing = "0.1.37"
opentelemetry = "0.20.0"
opentelemetry-semantic-conventions = "0.12.0"
# Error management
thiserror = "1.0.44"

View File

@ -29,7 +29,7 @@ use mas_storage_pg::PgRepository;
use mas_templates::Templates;
use opentelemetry::{
metrics::{Histogram, MetricsError, Unit},
Context, KeyValue,
KeyValue,
};
use rand::SeedableRng;
use sqlx::PgPool;
@ -60,7 +60,12 @@ impl AppState {
/// Returns an error if the metrics could not be initialized.
pub fn init_metrics(&mut self) -> Result<(), MetricsError> {
// XXX: do we want to put that somewhere else?
let meter = opentelemetry::global::meter("mas-handlers");
let meter = opentelemetry::global::meter_with_version(
env!("CARGO_PKG_NAME"),
Some(env!("CARGO_PKG_VERSION")),
Some(opentelemetry_semantic_conventions::SCHEMA_URL),
None,
);
let pool = self.pool.clone();
let usage = meter
.i64_observable_up_down_counter("db.connections.usage")
@ -75,13 +80,13 @@ impl AppState {
.init();
// Observe the number of active and idle connections in the pool
meter.register_callback(move |cx| {
meter.register_callback(&[usage.as_any(), max.as_any()], move |observer| {
let idle = u32::try_from(pool.num_idle()).unwrap_or(u32::MAX);
let used = pool.size() - idle;
let max_conn = pool.options().get_max_connections();
usage.observe(cx, i64::from(idle), &[KeyValue::new("state", "idle")]);
usage.observe(cx, i64::from(used), &[KeyValue::new("state", "used")]);
max.observe(cx, i64::from(max_conn), &[]);
observer.observe_i64(&usage, i64::from(idle), &[KeyValue::new("state", "idle")]);
observer.observe_i64(&usage, i64::from(used), &[KeyValue::new("state", "used")]);
observer.observe_i64(&max, i64::from(max_conn), &[]);
})?;
// Track the connection acquisition time
@ -212,7 +217,7 @@ impl FromRequestParts<AppState> for BoxRepository {
let duration_ms = duration.as_millis().try_into().unwrap_or(u64::MAX);
if let Some(histogram) = &state.conn_acquisition_histogram {
histogram.record(&Context::new(), duration_ms, &[]);
histogram.record(duration_ms, &[]);
}
Ok(repo

View File

@ -42,5 +42,7 @@ pub async fn get(
let content = templates.render_index(&ctx).await?;
tracing::info!("rendered index page");
Ok((cookie_jar, Html(content)))
}