gajus / slonik

A Node.js PostgreSQL client with runtime and build time type safety, and composable SQL.
Other
4.56k stars 138 forks source link

How to retrieve connection id from connection/transaction (CommonQueryMethods)? #647

Open joe-squint opened 2 weeks ago

joe-squint commented 2 weeks ago

In our project, we have the roarr logs of Slonik enabled alongside our own application logs. We'd like to correlate the roarr logs with our existing application logs by adding the connection id into our application log context, while leaving the roarr logs as is. I can see the connection id is present in the interceptor context, but I'm wondering if I there's a way I could retrieve this connection id from the connection itself?

Desired Behavior

After calling pool.connect the resulting connection object contains a connection id that can be accessed to correlate roarr logs with other logs and data.

Motivation

Today we have access to the roarr logs, but using them effectively can be difficult since we cannot easily correlate them with the rest of the contextual data that our services log. If our service is processing two identical requests at the same time, it can become impossible to attribute the roar logs to the individual request context within our application logs.

Implementation

A simple solution for this could be to add a getConnectionId or getContext method into CommonQueryMethods to allow connections and transactions the ability to expose the connection id