Skip to content

Commit

Permalink
Update mobc and metrics crates (#5015)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
Serhii Tatarintsev and aqrln authored Oct 24, 2024
1 parent 8263a1f commit a87a639
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 296 deletions.
248 changes: 62 additions & 186 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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"] }
Expand Down
2 changes: 1 addition & 1 deletion libs/test-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.26"
quote = "1.0.2"
syn = "1.0.5"
syn = { version = "1.0.5", features = ["full"] }
4 changes: 2 additions & 2 deletions quaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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"
Expand All @@ -92,7 +92,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 = { version = "0.8.5", optional = true }
serde = { version = "1.0" }
sqlformat = { version = "0.2.3", optional = true }
uuid.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions quaint/src/connector/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,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
}
Expand All @@ -81,7 +81,7 @@ where
result,
);

histogram!("pool.check_out", start.elapsed_time());
histogram!("pool.check_out").record(start.elapsed_time());

res
}
Expand Down
8 changes: 4 additions & 4 deletions quaint/src/connector/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -71,15 +71,15 @@ 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(())
}

/// 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(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 })
}
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::sync::Arc;
Expand Down Expand Up @@ -85,8 +84,8 @@ where
let res = f().instrument(span).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 prisma/team-orm#136: fix log subscription.
// NOTE: `params` is a part of the interface for query logs.
Expand Down
21 changes: 6 additions & 15 deletions query-engine/core/src/executor/execute_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
}
Expand All @@ -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)),
Expand Down Expand Up @@ -115,7 +109,7 @@ pub async fn execute_many_self_contained<C: Connector + Send + Sync>(
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",
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -281,7 +272,7 @@ async fn execute_on<'a>(
trace_id: Option<String>,
) -> crate::Result<ResponseData> {
#[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)
Expand Down
2 changes: 1 addition & 1 deletion query-engine/driver-adapters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down
6 changes: 3 additions & 3 deletions query-engine/driver-adapters/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -114,7 +114,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";

Expand All @@ -130,7 +130,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";

Expand Down
6 changes: 3 additions & 3 deletions query-engine/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
metrics = "0.18"
metrics-util = "0.12.1"
metrics-exporter-prometheus = "0.10.0"
metrics.workspace = true
metrics-util = "0.17.0"
metrics-exporter-prometheus = { version = "0.15.3", default-features = false }
once_cell = "1.3"
serde.workspace = true
serde_json.workspace = true
Expand Down
Loading

0 comments on commit a87a639

Please sign in to comment.