From dc2eb8aacbefbeff2d9f02957156b103c5cdcada Mon Sep 17 00:00:00 2001 From: Sergey Tatarintsev Date: Mon, 6 Nov 2023 10:15:42 +0100 Subject: [PATCH] fix(driver-adapters): Ensure transaction metrics do not get negative Current dispatcher for a thread gets lost somewhere within napi-rs/napi `ThreadsafeFunction` nad thearfore, gauge increment in `Transaction::new` is not registered. That in turn means that when the same gauge is decremented in `commit`/`rollback`, it's value will get negative. Moving increment to proxy fixes the problem. Unblocks prisma/prisma-orm#21746 --- query-engine/driver-adapters/src/proxy.rs | 7 +++++++ query-engine/driver-adapters/src/transaction.rs | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/query-engine/driver-adapters/src/proxy.rs b/query-engine/driver-adapters/src/proxy.rs index da03336bdf53..62086a245199 100644 --- a/query-engine/driver-adapters/src/proxy.rs +++ b/query-engine/driver-adapters/src/proxy.rs @@ -4,6 +4,7 @@ use std::str::FromStr; use crate::async_js_function::AsyncJsFunction; use crate::conversion::JSArg; use crate::transaction::JsTransaction; +use metrics::increment_gauge; use napi::bindgen_prelude::{FromNapiValue, ToNapiValue}; use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction}; use napi::{JsObject, JsString}; @@ -555,6 +556,12 @@ impl DriverProxy { pub async fn start_transaction(&self) -> quaint::Result> { let tx = self.start_transaction.call(()).await?; + + // Decrement for this gauge is done in JsTransaction::commit/JsTransaction::rollback + // 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); Ok(Box::new(tx)) } } diff --git a/query-engine/driver-adapters/src/transaction.rs b/query-engine/driver-adapters/src/transaction.rs index 0d26c7f863aa..6c75ff4ad6b1 100644 --- a/query-engine/driver-adapters/src/transaction.rs +++ b/query-engine/driver-adapters/src/transaction.rs @@ -22,8 +22,6 @@ pub(crate) struct JsTransaction { impl JsTransaction { pub(crate) fn new(inner: JsBaseQueryable, tx_proxy: TransactionProxy) -> Self { - increment_gauge!("prisma_client_queries_active", 1.0); - Self { inner, tx_proxy } } @@ -40,6 +38,7 @@ impl JsTransaction { #[async_trait] 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); let commit_stmt = "COMMIT"; @@ -55,6 +54,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); let rollback_stmt = "ROLLBACK";