diff --git a/Cargo.lock b/Cargo.lock index 62a21959bf0..287095941b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "ahash" -version = "0.8.7" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", "getrandom 0.2.11", @@ -41,15 +41,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - [[package]] name = "aho-corasick" version = "1.0.3" @@ -419,7 +410,7 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8a88e82b9106923b5c4d6edfca9e7db958d4e98a478ec115022e81b9b38e2c8" dependencies = [ - "ahash 0.8.7", + "ahash 0.8.11", "base64 0.13.1", "bitvec", "chrono", @@ -1195,7 +1186,7 @@ dependencies = [ "expect-test", "futures", "js-sys", - "metrics 0.18.1", + "metrics 0.23.0", "napi", "napi-derive", "once_cell", @@ -1774,7 +1765,7 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ - "ahash 0.8.7", + "ahash 0.8.11", "allocator-api2", ] @@ -2458,9 +2449,9 @@ dependencies = [ [[package]] name = "metrics" -version = "0.18.1" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e52eb6380b6d2a10eb3434aec0885374490f5b82c8aaf5cd487a183c98be834" +checksum = "142c53885123b68d94108295a09d4afe1a1388ed95b54d5dacd9a454753030f2" dependencies = [ "ahash 0.7.8", "metrics-macros", @@ -2468,12 +2459,12 @@ dependencies = [ [[package]] name = "metrics" -version = "0.19.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "142c53885123b68d94108295a09d4afe1a1388ed95b54d5dacd9a454753030f2" +checksum = "884adb57038347dfbaf2d5065887b6cf4312330dc8e94bc30a1a839bd79d3261" dependencies = [ - "ahash 0.7.8", - "metrics-macros", + "ahash 0.8.11", + "portable-atomic", ] [[package]] @@ -2488,7 +2479,7 @@ dependencies = [ "metrics 0.19.0", "metrics-util 0.13.0", "parking_lot 0.11.2", - "quanta", + "quanta 0.9.3", "thiserror", "tokio", "tracing", @@ -2507,40 +2498,38 @@ dependencies = [ [[package]] name = "metrics-util" -version = "0.12.1" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65a9e83b833e1d2e07010a386b197c13aa199bbd0fca5cf69bfa147972db890a" +checksum = "fd1f4b69bef1e2b392b2d4a12902f2af90bb438ba4a66aa222d1023fa6561b50" dependencies = [ - "aho-corasick 0.7.20", "atomic-shim", "crossbeam-epoch", "crossbeam-utils", "hashbrown 0.11.2", - "indexmap 1.9.3", - "metrics 0.18.1", + "metrics 0.19.0", "num_cpus", - "ordered-float", "parking_lot 0.11.2", - "quanta", - "radix_trie", - "sketches-ddsketch", + "quanta 0.9.3", + "sketches-ddsketch 0.1.3", ] [[package]] name = "metrics-util" -version = "0.13.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd1f4b69bef1e2b392b2d4a12902f2af90bb438ba4a66aa222d1023fa6561b50" +checksum = "4259040465c955f9f2f1a4a8a16dc46726169bca0f88e8fb2dbeced487c3e828" dependencies = [ - "atomic-shim", + "aho-corasick", "crossbeam-epoch", "crossbeam-utils", - "hashbrown 0.11.2", - "metrics 0.19.0", + "hashbrown 0.14.5", + "indexmap 2.2.2", + "metrics 0.23.0", "num_cpus", - "parking_lot 0.11.2", - "quanta", - "sketches-ddsketch", + "ordered-float", + "quanta 0.12.3", + "radix_trie", + "sketches-ddsketch 0.2.2", ] [[package]] @@ -2578,9 +2567,8 @@ dependencies = [ [[package]] name = "mobc" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90eb49dc5d193287ff80e72a86f34cfb27aae562299d22fea215e06ea1059dd3" +version = "0.8.4" +source = "git+https://github.com/SevInf/mobc.git?branch=metrics-guards#da80095bc4e6777991f5e20328008e91f1c03573" dependencies = [ "async-trait", "futures-channel", @@ -2588,7 +2576,7 @@ dependencies = [ "futures-timer", "futures-util", "log", - "metrics 0.18.1", + "metrics 0.23.0", "thiserror", "tokio", "tracing", @@ -3175,9 +3163,9 @@ dependencies = [ [[package]] name = "ordered-float" -version = "2.10.0" +version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7940cf2ca942593318d07fcf2596cdca60a85c9e7fab408a5e21a4f9dcd40d87" +checksum = "44d501f1a72f71d3c063a6bbc8f7271fa73aa09fe5d6283b6571e2ed176a2537" dependencies = [ "num-traits", ] @@ -3463,6 +3451,12 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "postgres-native-tls" version = "0.5.0" @@ -3788,7 +3782,7 @@ dependencies = [ "indoc 0.3.6", "itertools 0.12.0", "lru-cache", - "metrics 0.18.1", + "metrics 0.23.0", "mobc", "mysql_async", "names 0.11.0", @@ -3851,12 +3845,27 @@ dependencies = [ "libc", "mach", "once_cell", - "raw-cpuid", + "raw-cpuid 10.7.0", "wasi 0.10.2+wasi-snapshot-preview1", "web-sys", "winapi", ] +[[package]] +name = "quanta" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid 11.2.0", + "wasi 0.11.0+wasi-snapshot-preview1", + "web-sys", + "winapi", +] + [[package]] name = "query-connector" version = "0.1.0" @@ -4014,9 +4023,9 @@ name = "query-engine-metrics" version = "0.1.0" dependencies = [ "expect-test", - "metrics 0.18.1", + "metrics 0.23.0", "metrics-exporter-prometheus", - "metrics-util 0.12.1", + "metrics-util 0.17.0", "once_cell", "parking_lot 0.12.1", "serde", @@ -4340,6 +4349,15 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "raw-cpuid" +version = "11.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ab240315c661615f2ee9f0f2cd32d5a7343a84d5ebcccb99d46e6637565e7b0" +dependencies = [ + "bitflags 2.4.0", +] + [[package]] name = "rayon" version = "1.7.0" @@ -4395,7 +4413,7 @@ version = "1.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ - "aho-corasick 1.0.3", + "aho-corasick", "memchr", "regex-automata 0.4.5", "regex-syntax 0.8.2", @@ -4416,7 +4434,7 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ - "aho-corasick 1.0.3", + "aho-corasick", "memchr", "regex-syntax 0.8.2", ] @@ -5136,6 +5154,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04d2ecae5fcf33b122e2e6bd520a57ccf152d2dde3b38c71039df1a6867264ee" +[[package]] +name = "sketches-ddsketch" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85636c14b73d81f541e525f585c0a2109e6744e1565b5c1668e31c70c10ed65c" + [[package]] name = "slab" version = "0.4.8" diff --git a/Cargo.toml b/Cargo.toml index f2000ba619d..3fb2b32b7cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,6 +63,7 @@ napi = { version = "2.15.1", default-features = false, features = [ "serde-json", ] } napi-derive = "2.15.0" +metrics = "0.23.0" js-sys = { version = "0.3" } rand = { version = "0.8" } regex = { version = "1", features = ["std"] } diff --git a/quaint/Cargo.toml b/quaint/Cargo.toml index b63fe18b494..5ab8c90247e 100644 --- a/quaint/Cargo.toml +++ b/quaint/Cargo.toml @@ -74,7 +74,7 @@ tracing-core = "0.1" async-trait.workspace = true thiserror = "1.0" num_cpus = "1.12" -metrics = "0.18" +metrics.workspace = true futures = "0.3" url.workspace = true hex = "0.4" @@ -89,7 +89,7 @@ serde_json.workspace = true native-tls = { version = "0.2", optional = true } bit-vec = { version = "0.6.1", optional = true } bytes = { version = "1.0", optional = true } -mobc = { version = "0.8", optional = true } +mobc = { git="https://github.com/SevInf/mobc.git", branch = "metrics-guards", optional = true } serde = { version = "1.0" } sqlformat = { version = "0.2.3", optional = true } uuid.workspace = true diff --git a/quaint/src/connector/metrics.rs b/quaint/src/connector/metrics.rs index a0c4ef42698..2e2e4d18922 100644 --- a/quaint/src/connector/metrics.rs +++ b/quaint/src/connector/metrics.rs @@ -46,9 +46,9 @@ where trace_query(query, params, result, &start); } - histogram!(format!("{tag}.query.time"), start.elapsed_time()); - histogram!("prisma_datasource_queries_duration_histogram_ms", start.elapsed_time()); - increment_counter!("prisma_datasource_queries_total"); + histogram!(format!("{tag}.query.time")).record(start.elapsed_time()); + histogram!("prisma_datasource_queries_duration_histogram_ms").record(start.elapsed_time()); + counter!("prisma_datasource_queries_total").increment(1); res } @@ -74,7 +74,7 @@ where result, ); - histogram!("pool.check_out", start.elapsed_time()); + histogram!("pool.check_out").record(start.elapsed_time()); res } diff --git a/quaint/src/connector/transaction.rs b/quaint/src/connector/transaction.rs index df4084883e8..e0fd3cf6179 100644 --- a/quaint/src/connector/transaction.rs +++ b/quaint/src/connector/transaction.rs @@ -4,7 +4,7 @@ use crate::{ error::{Error, ErrorKind}, }; use async_trait::async_trait; -use metrics::{decrement_gauge, increment_gauge}; +use metrics::gauge; use std::{fmt, str::FromStr}; extern crate metrics as metrics; @@ -62,7 +62,7 @@ impl<'a> DefaultTransaction<'a> { inner.server_reset_query(&this).await?; - increment_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").increment(1.0); Ok(this) } } @@ -71,7 +71,7 @@ impl<'a> DefaultTransaction<'a> { impl<'a> Transaction for DefaultTransaction<'a> { /// Commit the changes to the database and consume the transaction. async fn commit(&self) -> crate::Result<()> { - decrement_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").decrement(1.0); self.inner.raw_cmd("COMMIT").await?; Ok(()) @@ -79,7 +79,7 @@ impl<'a> Transaction for DefaultTransaction<'a> { /// Rolls back the changes to the database. async fn rollback(&self) -> crate::Result<()> { - decrement_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").decrement(1.0); self.inner.raw_cmd("ROLLBACK").await?; Ok(()) diff --git a/query-engine/connectors/mongodb-query-connector/src/interface/transaction.rs b/query-engine/connectors/mongodb-query-connector/src/interface/transaction.rs index 2fe5d840fa1..41a5162dad7 100644 --- a/query-engine/connectors/mongodb-query-connector/src/interface/transaction.rs +++ b/query-engine/connectors/mongodb-query-connector/src/interface/transaction.rs @@ -5,7 +5,7 @@ use crate::{ }; use connector_interface::{ConnectionLike, ReadOperations, Transaction, UpdateType, WriteOperations}; use mongodb::options::{Acknowledgment, ReadConcern, TransactionOptions, WriteConcern}; -use query_engine_metrics::{decrement_gauge, increment_gauge, metrics, PRISMA_CLIENT_QUERIES_ACTIVE}; +use query_engine_metrics::{gauge, PRISMA_CLIENT_QUERIES_ACTIVE}; use query_structure::{RelationLoadStrategy, SelectionResult}; use std::collections::HashMap; @@ -31,7 +31,7 @@ impl<'conn> MongoDbTransaction<'conn> { .await .map_err(|err| MongoError::from(err).into_connector_error())?; - increment_gauge!(PRISMA_CLIENT_QUERIES_ACTIVE, 1.0); + gauge!(PRISMA_CLIENT_QUERIES_ACTIVE).increment(1.0); Ok(Self { connection }) } @@ -40,7 +40,7 @@ impl<'conn> MongoDbTransaction<'conn> { #[async_trait] impl<'conn> Transaction for MongoDbTransaction<'conn> { async fn commit(&mut self) -> connector_interface::Result<()> { - decrement_gauge!(PRISMA_CLIENT_QUERIES_ACTIVE, 1.0); + gauge!(PRISMA_CLIENT_QUERIES_ACTIVE).decrement(1.0); utils::commit_with_retry(&mut self.connection.session) .await @@ -50,7 +50,7 @@ impl<'conn> Transaction for MongoDbTransaction<'conn> { } async fn rollback(&mut self) -> connector_interface::Result<()> { - decrement_gauge!(PRISMA_CLIENT_QUERIES_ACTIVE, 1.0); + gauge!(PRISMA_CLIENT_QUERIES_ACTIVE).decrement(1.0); self.connection .session diff --git a/query-engine/connectors/mongodb-query-connector/src/root_queries/mod.rs b/query-engine/connectors/mongodb-query-connector/src/root_queries/mod.rs index 96b6d1fe73f..e6b29260006 100644 --- a/query-engine/connectors/mongodb-query-connector/src/root_queries/mod.rs +++ b/query-engine/connectors/mongodb-query-connector/src/root_queries/mod.rs @@ -14,8 +14,7 @@ use bson::Bson; use bson::Document; use futures::Future; use query_engine_metrics::{ - histogram, increment_counter, metrics, PRISMA_DATASOURCE_QUERIES_DURATION_HISTOGRAM_MS, - PRISMA_DATASOURCE_QUERIES_TOTAL, + counter, histogram, PRISMA_DATASOURCE_QUERIES_DURATION_HISTOGRAM_MS, PRISMA_DATASOURCE_QUERIES_TOTAL, }; use query_structure::*; use std::time::Instant; @@ -63,8 +62,8 @@ where let res = f().await; let elapsed = start.elapsed().as_millis() as f64; - histogram!(PRISMA_DATASOURCE_QUERIES_DURATION_HISTOGRAM_MS, elapsed); - increment_counter!(PRISMA_DATASOURCE_QUERIES_TOTAL); + histogram!(PRISMA_DATASOURCE_QUERIES_DURATION_HISTOGRAM_MS).record(elapsed); + counter!(PRISMA_DATASOURCE_QUERIES_TOTAL).increment(1); // TODO: emit tracing event only when "debug" level query logs are enabled. // TODO prisma/team-orm#136: fix log subscription. diff --git a/query-engine/core/src/executor/execute_operation.rs b/query-engine/core/src/executor/execute_operation.rs index 6ef445d8364..7621a17a1d6 100644 --- a/query-engine/core/src/executor/execute_operation.rs +++ b/query-engine/core/src/executor/execute_operation.rs @@ -12,7 +12,7 @@ use futures::future; #[cfg(feature = "metrics")] use query_engine_metrics::{ - histogram, increment_counter, metrics, PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS, PRISMA_CLIENT_QUERIES_TOTAL, + counter, histogram, PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS, PRISMA_CLIENT_QUERIES_TOTAL, }; use schema::{QuerySchema, QuerySchemaRef}; @@ -32,10 +32,7 @@ pub async fn execute_single_operation( let result = execute_on(conn, graph, serializer, query_schema.as_ref(), trace_id).await; #[cfg(feature = "metrics")] - histogram!( - PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS, - operation_timer.elapsed_time() - ); + histogram!(PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS,).record(operation_timer.elapsed_time()); result } @@ -58,10 +55,7 @@ pub async fn execute_many_operations( let result = execute_on(conn, graph, serializer, query_schema.as_ref(), trace_id.clone()).await; #[cfg(feature = "metrics")] - histogram!( - PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS, - operation_timer.elapsed_time() - ); + histogram!(PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS,).record(operation_timer.elapsed_time()); match result { Ok(result) => results.push(Ok(result)), @@ -115,7 +109,7 @@ pub async fn execute_many_self_contained( let dispatcher = crate::get_current_dispatcher(); for op in operations { #[cfg(feature = "metrics")] - increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL); + counter!(PRISMA_CLIENT_QUERIES_TOTAL).increment(1); let conn_span = info_span!( "prisma:engine:connection", @@ -176,10 +170,7 @@ async fn execute_self_contained( }; #[cfg(feature = "metrics")] - histogram!( - PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS, - operation_timer.elapsed_time() - ); + histogram!(PRISMA_CLIENT_QUERIES_DURATION_HISTOGRAM_MS,).record(operation_timer.elapsed_time()); result } @@ -281,7 +272,7 @@ async fn execute_on<'a>( trace_id: Option, ) -> crate::Result { #[cfg(feature = "metrics")] - increment_counter!(PRISMA_CLIENT_QUERIES_TOTAL); + counter!(PRISMA_CLIENT_QUERIES_TOTAL).increment(1); let interpreter = QueryInterpreter::new(conn); QueryPipeline::new(graph, interpreter, serializer) diff --git a/query-engine/driver-adapters/Cargo.toml b/query-engine/driver-adapters/Cargo.toml index 39a0314a476..767780ff65d 100644 --- a/query-engine/driver-adapters/Cargo.toml +++ b/query-engine/driver-adapters/Cargo.toml @@ -15,7 +15,7 @@ serde.workspace = true serde_json.workspace = true tracing.workspace = true tracing-core = "0.1" -metrics = "0.18" +metrics.workspace = true uuid.workspace = true pin-project = "1" serde_repr.workspace = true diff --git a/query-engine/driver-adapters/src/proxy.rs b/query-engine/driver-adapters/src/proxy.rs index 1c2a5a68240..65447c0b95e 100644 --- a/query-engine/driver-adapters/src/proxy.rs +++ b/query-engine/driver-adapters/src/proxy.rs @@ -7,7 +7,7 @@ use crate::{ use crate::{send_future::UnsafeFuture, transaction::JsTransactionContext}; use futures::Future; -use metrics::increment_gauge; +use metrics::gauge; use std::sync::atomic::{AtomicBool, Ordering}; /// Proxy is a struct wrapping a javascript object that exhibits basic primitives for @@ -121,7 +121,7 @@ impl TransactionContextProxy { // Previously, it was done in JsTransaction::new, similar to the native Transaction. // However, correct Dispatcher is lost there and increment does not register, so we moved // it here instead. - increment_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").increment(1.0); Ok(Box::new(tx)) } diff --git a/query-engine/driver-adapters/src/transaction.rs b/query-engine/driver-adapters/src/transaction.rs index 8d124bd4da0..1578eaf3220 100644 --- a/query-engine/driver-adapters/src/transaction.rs +++ b/query-engine/driver-adapters/src/transaction.rs @@ -1,7 +1,7 @@ use std::future::Future; use async_trait::async_trait; -use metrics::decrement_gauge; +use metrics::gauge; use quaint::{ connector::{DescribedQuery, IsolationLevel, Transaction as QuaintTransaction}, prelude::{Query as QuaintQuery, Queryable, ResultSet}, @@ -107,7 +107,7 @@ impl JsTransaction { impl QuaintTransaction for JsTransaction { async fn commit(&self) -> quaint::Result<()> { // increment of this gauge is done in DriverProxy::startTransaction - decrement_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").decrement(1.0); let commit_stmt = "COMMIT"; @@ -123,7 +123,7 @@ impl QuaintTransaction for JsTransaction { async fn rollback(&self) -> quaint::Result<()> { // increment of this gauge is done in DriverProxy::startTransaction - decrement_gauge!("prisma_client_queries_active", 1.0); + gauge!("prisma_client_queries_active").decrement(1.0); let rollback_stmt = "ROLLBACK"; diff --git a/query-engine/metrics/Cargo.toml b/query-engine/metrics/Cargo.toml index 5593b246c09..10e64c7bb9e 100644 --- a/query-engine/metrics/Cargo.toml +++ b/query-engine/metrics/Cargo.toml @@ -4,8 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -metrics = "0.18" -metrics-util = "0.12.1" +metrics.workspace = true +metrics-util = "0.17.0" metrics-exporter-prometheus = "0.10.0" once_cell = "1.3" serde.workspace = true diff --git a/query-engine/metrics/src/lib.rs b/query-engine/metrics/src/lib.rs index 1965b56cb07..8b6e5d41bfd 100644 --- a/query-engine/metrics/src/lib.rs +++ b/query-engine/metrics/src/lib.rs @@ -38,10 +38,7 @@ use std::collections::HashMap; use std::sync::Once; pub extern crate metrics; -pub use metrics::{ - absolute_counter, decrement_gauge, describe_counter, describe_gauge, describe_histogram, gauge, histogram, - increment_counter, increment_gauge, -}; +pub use metrics::{describe_counter, describe_gauge, describe_histogram, gauge, histogram, counter}; // Metrics that we emit from the engines, third party metrics emitted by libraries and that we rename are omitted. pub const PRISMA_CLIENT_QUERIES_TOTAL: &str = "prisma_client_queries_total"; // counter @@ -102,9 +99,7 @@ pub fn setup() { static METRIC_RECORDER: Once = Once::new(); fn set_recorder() { - METRIC_RECORDER.call_once(|| { - metrics::set_boxed_recorder(Box::new(MetricRecorder)).unwrap(); - }); + METRIC_RECORDER.call_once(|| metrics::set_global_recorder(MetricRecorder).unwrap()); } /// Initialize metrics descriptions and values @@ -145,15 +140,15 @@ fn initialize_metrics_descriptions() { /// Histograms are excluded, as their initialization will alter the histogram values. /// (i.e. histograms don't have a neutral value, like counters or gauges) fn initialize_metrics_values() { - absolute_counter!(PRISMA_CLIENT_QUERIES_TOTAL, 0); - absolute_counter!(PRISMA_DATASOURCE_QUERIES_TOTAL, 0); - gauge!(PRISMA_CLIENT_QUERIES_ACTIVE, 0.0); - absolute_counter!(MOBC_POOL_CONNECTIONS_OPENED_TOTAL, 0); - absolute_counter!(MOBC_POOL_CONNECTIONS_CLOSED_TOTAL, 0); - gauge!(MOBC_POOL_CONNECTIONS_OPEN, 0.0); - gauge!(MOBC_POOL_CONNECTIONS_BUSY, 0.0); - gauge!(MOBC_POOL_CONNECTIONS_IDLE, 0.0); - gauge!(MOBC_POOL_WAIT_COUNT, 0.0); + counter!(PRISMA_CLIENT_QUERIES_TOTAL).absolute(0); + counter!(PRISMA_DATASOURCE_QUERIES_TOTAL).absolute(0); + gauge!(PRISMA_CLIENT_QUERIES_ACTIVE).set(0.0); + counter!(MOBC_POOL_CONNECTIONS_OPENED_TOTAL).absolute(0); + counter!(MOBC_POOL_CONNECTIONS_CLOSED_TOTAL).absolute(0); + gauge!(MOBC_POOL_CONNECTIONS_OPEN).set(0.0); + gauge!(MOBC_POOL_CONNECTIONS_BUSY).set(0.0); + gauge!(MOBC_POOL_CONNECTIONS_IDLE).set(0.0); + gauge!(MOBC_POOL_WAIT_COUNT).set(0.0); } // At the moment the histogram is only used for timings. So the bounds are hard coded here @@ -171,10 +166,7 @@ pub enum MetricFormat { #[cfg(test)] mod tests { use super::*; - use metrics::{ - absolute_counter, decrement_gauge, describe_counter, describe_gauge, describe_histogram, gauge, histogram, - increment_counter, increment_gauge, register_counter, register_gauge, register_histogram, - }; + use metrics::{describe_counter, describe_gauge, describe_histogram, gauge, histogram}; use serde_json::json; use std::collections::HashMap; use std::time::Duration; @@ -211,12 +203,12 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - let counter1 = register_counter!("test_counter"); + let counter1 = counter!("test_counter"); counter1.increment(1); - increment_counter!("test_counter"); - increment_counter!("test_counter"); + counter!("test_counter").increment(1); + counter!("test_counter").increment(1); - increment_counter!("another_counter"); + counter!("another_counter").increment(1); let val = metrics.counter_value("test_counter").unwrap(); assert_eq!(val, 3); @@ -224,7 +216,7 @@ mod tests { let val2 = metrics.counter_value("another_counter").unwrap(); assert_eq!(val2, 1); - absolute_counter!("test_counter", 5); + counter!("test_counter").absolute(5); let val3 = metrics.counter_value("test_counter").unwrap(); assert_eq!(val3, 5); } @@ -239,11 +231,11 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - let gauge1 = register_gauge!("test_gauge"); + let gauge1 = gauge!("test_gauge"); gauge1.increment(1.0); - increment_gauge!("test_gauge", 1.0); - increment_gauge!("test_gauge", 1.0); - increment_gauge!("another_gauge", 1.0); + gauge!("test_gauge").increment(1.0); + gauge!("test_gauge").increment(1.0); + gauge!("another_gauge").increment(1.0); let val = metrics.gauge_value("test_gauge").unwrap(); assert_eq!(val, 3.0); @@ -253,11 +245,11 @@ mod tests { assert_eq!(None, metrics.counter_value("test_gauge")); - gauge!("test_gauge", 5.0); + gauge!("test_gauge").set(5.0); let val3 = metrics.gauge_value("test_gauge").unwrap(); assert_eq!(val3, 5.0); - decrement_gauge!("test_gauge", 2.0); + gauge!("test_gauge").decrement(2.0); let val4 = metrics.gauge_value("test_gauge").unwrap(); assert_eq!(val4, 3.0); } @@ -274,8 +266,8 @@ mod tests { async { trace!("a fake trace"); - increment_gauge!("test_gauge", 1.0); - increment_counter!("test_counter"); + gauge!("test_gauge").set(1.0); + counter!("test_counter").increment(1); trace!("another fake trace"); @@ -293,8 +285,8 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - increment_gauge!("not_accepted", 1.0); - increment_gauge!("test_gauge", 1.0); + gauge!("not_accepted").set(1.0); + gauge!("test_gauge").set(1.0); assert_eq!(1.0, metrics.gauge_value("test_gauge").unwrap()); assert_eq!(None, metrics.gauge_value("not_accepted")); @@ -310,15 +302,15 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - let hist = register_histogram!("test_histogram"); + let hist = histogram!("test_histogram"); hist.record(Duration::from_millis(9)); - histogram!("test_histogram", Duration::from_millis(100)); - histogram!("test_histogram", Duration::from_millis(1)); + histogram!("test_histogram").record(Duration::from_millis(100)); + histogram!("test_histogram").record(Duration::from_millis(1)); - histogram!("test_histogram", Duration::from_millis(1999)); - histogram!("test_histogram", Duration::from_millis(3999)); - histogram!("test_histogram", Duration::from_millis(610)); + histogram!("test_histogram").record(Duration::from_millis(1999)); + histogram!("test_histogram").record(Duration::from_millis(3999)); + histogram!("test_histogram").record(Duration::from_millis(610)); let hist = metrics.histogram_values("test_histogram").unwrap(); let expected: Vec<(f64, u64)> = Vec::from([ @@ -386,21 +378,21 @@ mod tests { assert_eq!(metrics.to_json(Default::default()), empty); - absolute_counter!("counter_1", 4, "label" => "one"); + counter!("counter_1", "label" => "one").absolute(4); describe_counter!("counter_2", "this is a description for counter 2"); - absolute_counter!("counter_2", 2, "label" => "one", "another_label" => "two"); + counter!("counter_2", "label" => "one", "another_label" => "two").absolute(2); describe_gauge!("gauge_1", "a description for gauge 1"); - gauge!("gauge_1", 7.0); - gauge!("gauge_2", 3.0, "label" => "three"); + gauge!("gauge_1").set(7.0); + gauge!("gauge_2", "label" => "three").set(3.0); describe_histogram!("histogram_1", "a description for histogram"); - let hist = register_histogram!("histogram_1", "label" => "one", "hist_two" => "two"); + let hist = histogram!("histogram_1", "label" => "one", "hist_two" => "two"); hist.record(Duration::from_millis(9)); - histogram!("histogram_2", Duration::from_millis(9)); - histogram!("histogram_2", Duration::from_millis(1000)); - histogram!("histogram_2", Duration::from_millis(40)); + histogram!("histogram_2").record(Duration::from_millis(9)); + histogram!("histogram_2").record(Duration::from_millis(1000)); + histogram!("histogram_2").record(Duration::from_millis(40)); let json = metrics.to_json(Default::default()); let expected = json!({ @@ -459,10 +451,10 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - let hist = register_histogram!("test_histogram", "label" => "one", "two" => "another"); + let hist = histogram!("test_histogram", "label" => "one", "two" => "another"); hist.record(Duration::from_millis(9)); - absolute_counter!("counter_1", 1); + counter!("counter_1").absolute(1); let mut global_labels: HashMap = HashMap::new(); global_labels.insert("global_one".to_string(), "one".to_string()); @@ -502,19 +494,19 @@ mod tests { let metrics = MetricRegistry::new_with_accept_list(TESTING_ACCEPT_LIST.to_vec()); let dispatch = Dispatch::new(tracing_subscriber::Registry::default().with(metrics.clone())); async { - absolute_counter!("counter_1", 4, "label" => "one"); + counter!("counter_1", "label" => "one").absolute(4); describe_counter!("counter_2", "this is a description for counter 2"); - absolute_counter!("counter_2", 2, "label" => "one", "another_label" => "two"); + counter!("counter_2", "label" => "one", "another_label" => "two").absolute(2); describe_gauge!("gauge_1", "a description for gauge 1"); - gauge!("gauge_1", 7.0); - gauge!("gauge_2", 3.0, "label" => "three"); + gauge!("gauge_1").set(7.0); + gauge!("gauge_2", "label" => "three").set(3.0); describe_histogram!("histogram_1", "a description for histogram"); - let hist = register_histogram!("histogram_1", "label" => "one", "hist_two" => "two"); + let hist = histogram!("histogram_1", "label" => "one", "hist_two" => "two"); hist.record(Duration::from_millis(9)); - histogram!("histogram_2", Duration::from_millis(1000)); + histogram!("histogram_2").record(Duration::from_millis(1000)); let mut global_labels: HashMap = HashMap::new(); global_labels.insert("global_two".to_string(), "two".to_string()); diff --git a/query-engine/metrics/src/recorder.rs b/query-engine/metrics/src/recorder.rs index 94d2c050f60..0d3ae8ecf22 100644 --- a/query-engine/metrics/src/recorder.rs +++ b/query-engine/metrics/src/recorder.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use metrics::KeyName; +use metrics::{KeyName, Metadata, SharedString}; use metrics::{Counter, CounterFn, Gauge, GaugeFn, Histogram, HistogramFn, Key, Recorder, Unit}; use tracing::trace; @@ -22,27 +22,27 @@ impl MetricRecorder { } impl Recorder for MetricRecorder { - fn describe_counter(&self, key_name: KeyName, _unit: Option, description: &'static str) { - self.register_description(key_name.as_str(), description); + fn describe_counter(&self, key_name: KeyName, _unit: Option, description: SharedString) { + self.register_description(key_name.as_str(), &description); } - fn describe_gauge(&self, key_name: KeyName, _unit: Option, description: &'static str) { - self.register_description(key_name.as_str(), description); + fn describe_gauge(&self, key_name: KeyName, _unit: Option, description: SharedString) { + self.register_description(key_name.as_str(), &description); } - fn describe_histogram(&self, key_name: KeyName, _unit: Option, description: &'static str) { - self.register_description(key_name.as_str(), description); + fn describe_histogram(&self, key_name: KeyName, _unit: Option, description: SharedString) { + self.register_description(key_name.as_str(), &description); } - fn register_counter(&self, key: &Key) -> Counter { + fn register_counter(&self, key: &Key, _metadata: &Metadata<'_>) -> Counter { Counter::from_arc(Arc::new(MetricHandle(key.clone()))) } - fn register_gauge(&self, key: &Key) -> Gauge { + fn register_gauge(&self, key: &Key, _metadata: &Metadata<'_>) -> Gauge { Gauge::from_arc(Arc::new(MetricHandle(key.clone()))) } - fn register_histogram(&self, key: &Key) -> Histogram { + fn register_histogram(&self, key: &Key, _metadata: &Metadata<'_>) -> Histogram { Histogram::from_arc(Arc::new(MetricHandle(key.clone()))) } }