Skip to content

Commit

Permalink
fix(driver-adapters): Ensure transaction metrics do not get negative (#…
Browse files Browse the repository at this point in the history
…4417)

* 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

* clippy
  • Loading branch information
SevInf authored Nov 6, 2023
1 parent a6053f3 commit 16679d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions query-engine/driver-adapters/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -555,6 +556,12 @@ impl DriverProxy {

pub async fn start_transaction(&self) -> quaint::Result<Box<JsTransaction>> {
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))
}
}
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,5 +1,5 @@
use async_trait::async_trait;
use metrics::{decrement_gauge, increment_gauge};
use metrics::decrement_gauge;
use napi::{bindgen_prelude::FromNapiValue, JsObject};
use quaint::{
connector::{IsolationLevel, Transaction as QuaintTransaction},
Expand All @@ -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 }
}

Expand All @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit 16679d0

Please sign in to comment.