Skip to content

Commit dbc7b15

Browse files
committed
feat: instrument slonik.connection.create
1 parent 25a5cbd commit dbc7b15

File tree

2 files changed

+88
-66
lines changed

2 files changed

+88
-66
lines changed

.changeset/smart-crews-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"slonik": minor
3+
---
4+
5+
instrument slonik.connection.create

packages/slonik/src/factories/createConnection.ts

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -131,81 +131,98 @@ export const createConnection = async (
131131
}
132132
}
133133

134-
const connection = await establishConnection(
135-
parentLog,
136-
pool,
137-
clientConfiguration.connectionRetryLimit,
138-
);
139-
140-
return raceError(connection, async () => {
141-
const { connectionId } = getPoolClientState(connection);
142-
143-
const connectionLog = parentLog.child({
144-
connectionId,
145-
});
146-
147-
const connectionContext = {
148-
connectionId,
149-
connectionType,
150-
log: connectionLog,
151-
poolId,
152-
};
134+
return await tracer.startActiveSpan(
135+
'slonik.connection.create',
136+
async (span) => {
137+
try {
138+
const connection = await establishConnection(
139+
parentLog,
140+
pool,
141+
clientConfiguration.connectionRetryLimit,
142+
);
143+
144+
return raceError(connection, async () => {
145+
const { connectionId } = getPoolClientState(connection);
146+
147+
const connectionLog = parentLog.child({
148+
connectionId,
149+
});
150+
151+
const connectionContext = {
152+
connectionId,
153+
connectionType,
154+
log: connectionLog,
155+
poolId,
156+
};
157+
158+
const boundConnection = bindPoolConnection(
159+
connectionLog,
160+
connection,
161+
clientConfiguration,
162+
);
153163

154-
const boundConnection = bindPoolConnection(
155-
connectionLog,
156-
connection,
157-
clientConfiguration,
158-
);
164+
try {
165+
for (const interceptor of clientConfiguration.interceptors) {
166+
if (interceptor.afterPoolConnection) {
167+
await interceptor.afterPoolConnection(
168+
connectionContext,
169+
boundConnection,
170+
);
171+
}
172+
}
173+
} catch (error) {
174+
await connection.destroy();
159175

160-
try {
161-
for (const interceptor of clientConfiguration.interceptors) {
162-
if (interceptor.afterPoolConnection) {
163-
await interceptor.afterPoolConnection(
164-
connectionContext,
165-
boundConnection,
166-
);
167-
}
168-
}
169-
} catch (error) {
170-
await connection.destroy();
176+
throw error;
177+
}
171178

172-
throw error;
173-
}
179+
let result;
174180

175-
let result;
181+
try {
182+
result = await connectionHandler(
183+
connectionLog,
184+
connection,
185+
boundConnection,
186+
clientConfiguration,
187+
);
188+
} catch (error) {
189+
await connection.destroy();
176190

177-
try {
178-
result = await connectionHandler(
179-
connectionLog,
180-
connection,
181-
boundConnection,
182-
clientConfiguration,
183-
);
184-
} catch (error) {
185-
await connection.destroy();
191+
throw error;
192+
}
186193

187-
throw error;
188-
}
194+
try {
195+
for (const interceptor of clientConfiguration.interceptors) {
196+
if (interceptor.beforePoolConnectionRelease) {
197+
await interceptor.beforePoolConnectionRelease(
198+
connectionContext,
199+
boundConnection,
200+
);
201+
}
202+
}
203+
} catch (error) {
204+
await connection.destroy();
189205

190-
try {
191-
for (const interceptor of clientConfiguration.interceptors) {
192-
if (interceptor.beforePoolConnectionRelease) {
193-
await interceptor.beforePoolConnectionRelease(
194-
connectionContext,
195-
boundConnection,
196-
);
197-
}
198-
}
199-
} catch (error) {
200-
await connection.destroy();
206+
throw error;
207+
}
201208

202-
throw error;
203-
}
209+
destroyBoundConnection(boundConnection);
204210

205-
destroyBoundConnection(boundConnection);
211+
await connection.release();
206212

207-
await connection.release();
213+
return result;
214+
});
215+
} catch (error) {
216+
span.recordException(error);
217+
span.setStatus({
218+
code: SpanStatusCode.ERROR,
219+
message: String(error),
220+
});
208221

209-
return result;
210-
});
222+
throw error;
223+
} finally {
224+
span.end();
225+
}
226+
},
227+
);
211228
};

0 commit comments

Comments
 (0)