Skip to content

Commit

Permalink
add global app labels
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Nov 7, 2023
1 parent 09d8148 commit 54e1e87
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions libsql-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jsonwebtoken = "8.2.0"
libsql = { path = "../libsql/", optional = true }
libsql-replication = { path = "../libsql-replication" }
metrics = "0.21.1"
metrics-util = "0.15"
metrics-exporter-prometheus = "0.12.1"
mimalloc = { version = "0.1.36", default-features = false }
nix = { version = "0.26.2", features = ["fs"] }
Expand Down
5 changes: 2 additions & 3 deletions libsql-server/src/connection/libsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,8 @@ impl<W: WalHook> Connection<W> {
));
}

// TODO: change how these metrics are gathered to limit cardinality
// self.stats
// .update_query_metrics(sql, rows_read, rows_written, mem_used, elapsed)
self.stats
.update_query_metrics(rows_read, rows_written, mem_used, elapsed)
}

fn describe(&self, sql: &str) -> crate::Result<DescribeResponse> {
Expand Down
18 changes: 17 additions & 1 deletion libsql-server/src/http/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::cell::OnceCell;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio_util::io::ReaderStream;
use url::Url;

Expand Down Expand Up @@ -65,9 +66,24 @@ where
M: MakeNamespace,
C: Connector,
{
let app_label = std::env::var("SQLD_APP_LABEL").ok();

let prom_handle = if !disable_metrics {
let lock = PROM_HANDLE.lock();
let prom_handle = lock.get_or_init(|| PrometheusBuilder::new().install_recorder().unwrap());
let prom_handle = lock.get_or_init(|| {
let b = PrometheusBuilder::new().idle_timeout(
metrics_util::MetricKindMask::ALL,
Some(Duration::from_secs(120)),
);

if let Some(app_label) = app_label {
b.add_global_label("app", app_label)
.install_recorder()
.unwrap()
} else {
b.install_recorder().unwrap()
}
});
Some(prom_handle.clone())
} else {
None
Expand Down
13 changes: 7 additions & 6 deletions libsql-server/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,20 @@ impl Stats {
self.slowest_query_threshold.store(0, Ordering::Relaxed);
}

// TOOD: Update these metrics with namespace labels in the future so we can localize
// issues to a specific namespace.
pub(crate) fn update_query_metrics(
&self,
sql: String,
rows_read: u64,
rows_written: u64,
mem_used: u64,
elapsed: u64,
) {
increment_counter!("libsql_server_query_count", "namespace" => self.namespace.to_string(), "query" => sql.clone());
counter!("libsql_server_query_latency", elapsed, "namespace" => self.namespace.to_string(), "query" => sql.clone());
counter!("libsql_server_query_rows_read", rows_read, "namespace" => self.namespace.to_string(), "query" => sql.clone());
counter!("libsql_server_query_rows_written", rows_written, "namespace" => self.namespace.to_string(), "query" => sql.clone());
counter!("libsql_server_query_mem_used", mem_used, "namespace" => self.namespace.to_string(), "query" => sql.clone());
increment_counter!("libsql_server_query_count");
counter!("libsql_server_query_latency", elapsed);
counter!("libsql_server_query_rows_read", rows_read);
counter!("libsql_server_query_rows_written", rows_written);
counter!("libsql_server_query_mem_used", mem_used);
}
}

Expand Down

0 comments on commit 54e1e87

Please sign in to comment.