Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pool checkout to Server-Timing #3442

Open
steve-chavez opened this issue Apr 23, 2024 · 1 comment
Open

Add pool checkout to Server-Timing #3442

steve-chavez opened this issue Apr 23, 2024 · 1 comment
Labels
idea Needs of discussion to become an enhancement, not ready for implementation observability

Comments

@steve-chavez
Copy link
Member

Problem

Server-Timing currently conflates the pool checkout time with the transaction time. Meaning we cannot differentiate if a query took a lot of time or if acquiring the connection from the pool did.

Solution

Add a pool-checkout duration:

HTTP/1.1 200 OK

Server-Timing: jwt;dur=14.9, parse;dur=71.1, plan;dur=109.0, pool-checkout;dur=10.3, transaction;dur=353.2, response;dur=4.4

Notes

Measuring check-in time (time to return the connection to the pool) doesn't make much sense in our case IMO, since we always return the connection right after finishing the transaction. This probably makes more sense in traditional three-tier apps where some business logic is done in the application layer while a pool connection is being used.

@steve-chavez steve-chavez added idea Needs of discussion to become an enhancement, not ready for implementation observability labels Apr 23, 2024
@steve-chavez
Copy link
Member Author

There's a problem doing this, so currently we get the timings with:

(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf req body sCache
(planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache
(queryTime, queryResult) <- withTiming $ Query.runQuery appState conf authResult apiReq plan sCache pgVer (Just authRole /= configDbAnonRole)
(respTime, resp) <- withTiming $ liftEither $ Response.actionResponse queryResult apiReq (T.decodeUtf8 prettyVersion, docsVersion) conf sCache iSchema iNegotiatedByProfile

But it's not possible to use the same pattern for the pool checkout time, we have to compute the time inside the pool.

According to Nikita, we have to measure the checkout time (or wait time) like:

executeObservedSession :: Pool -> Monitor -> Session -> IO ()
executeObservedSession pool monitor session = do
  Monitor.incGauge monitor "db.client.connections.pending_requests"
  startTime <- getCurrentTime
  Pool.use pool do
    liftIO $ do
      endTime <- getCurrentTime
      Monitor.observeHistogram monitor "db.client.connections.wait_time" (diffUTCTime endTime startTime)
      Monitor.decGauge monitor "db.client.connections.pending_requests"
    session

(nikita-volkov/hasql-pool#40 (comment))

So while we could have a Metric (o new Observation) inside the pool, is not straightforward to get it out. Maybe we could use an IORef there, but it's kind of messy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Needs of discussion to become an enhancement, not ready for implementation observability
Development

No branches or pull requests

1 participant