Skip to content

Commit 432c519

Browse files
committed
feat(stdlib,store): use convetional 'op', force transaction, support queries as spans
1 parent cc9203e commit 432c519

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

packages/stdlib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,8 @@ export {
9393
newEventFromEvent,
9494
} from "./src/events.js";
9595

96-
export { _compasSentryExport, compasWithSentry } from "./src/sentry.js";
96+
export {
97+
_compasSentryExport,
98+
compasWithSentry,
99+
_compasSentryEnableQuerySpans,
100+
} from "./src/sentry.js";

packages/stdlib/src/events.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function eventStart(event, name) {
162162

163163
if (typeof _compasSentryExport?.startInactiveSpan === "function") {
164164
event._compasSentrySpan = _compasSentryExport.startInactiveSpan({
165-
op: "event",
165+
op: "function",
166166
name: name,
167167
description: name,
168168
});
@@ -231,9 +231,7 @@ export function eventStop(event) {
231231

232232
if (event._compasSentrySpan) {
233233
event._compasSentrySpan.end();
234-
}
235-
236-
if (isNil(event.rootEvent)) {
234+
} else if (isNil(event.rootEvent)) {
237235
event.log.info({
238236
type: "event_span",
239237
aborted: !!event.signal?.aborted,

packages/stdlib/src/sentry.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
*/
66
export let _compasSentryExport = undefined;
77

8+
/**
9+
* Send queries executed via `query` from @compas/store as a sentry span.
10+
*
11+
* @type {boolean}
12+
*/
13+
export let _compasSentryEnableQuerySpans = false;
14+
815
/**
916
* Enable Sentry support. This comes with the following changes:
1017
*
1118
* Stdlib:
1219
* - Logger: both info and error logs are added as breadcrumbs to the current active span.
1320
* - Event: Events are propagated to Sentry as (inactive) spans.
1421
* Meaning that further logs are not necessarily correlated to the correct event.
15-
* This can be inferred based on the timeline.
22+
* The final event callstack is not logged.
1623
*
1724
* Server:
1825
* - Starts a new root span for each incoming request.
@@ -22,19 +29,22 @@ export let _compasSentryExport = undefined;
2229
* Note that if a custom list of `allowHeaders` is provided in the CORS options,
2330
* 'sentry-trace' and 'baggage' should be allowed as well.
2431
* - If the error handler retrieves an unknown or AppError.serverError, it is reported as an uncaught exception.
25-
* It is advised to set 'maxDepth' to '0' in your Sentry config, and to enable the 'extraErrorDataIntegration' integration.
32+
* It is advised to set 'normalizeDepth' to '0' in your Sentry config, and to enable the 'extraErrorDataIntegration' integration.
2633
*
27-
* Queue:
28-
* - Starts a new root span for each handled Job
29-
* - Names it based on the job name.
30-
* - Reports unhandled errors as exceptions.
34+
* Store:
35+
* - Starts a new root span for each handled Job in the QueueWorker
36+
* The span name is based on the job name. Unhandled errors are captured as exceptions.
37+
* - Supports passing queries to Sentry as spans. Requires {@link opts.sendQueriesAsSpans} to be set.
3138
*
3239
* All:
33-
* - Each package that has error logs, will report an exception as well.
34-
* - Note that we still execute the logs for now. Which may be removed in a future release.
40+
* - All error logs in Compas package code are captured as exceptions.
3541
*
3642
* @param {import("@sentry/node")} instance
43+
* @param {{
44+
* sendQueriesAsSpans?: boolean
45+
* }} [opts]
3746
*/
38-
export function compasWithSentry(instance) {
47+
export function compasWithSentry(instance, { sendQueriesAsSpans } = {}) {
3948
_compasSentryExport = instance;
49+
_compasSentryEnableQuerySpans = sendQueriesAsSpans ?? false;
4050
}

packages/store/src/query.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-nocheck
22

3-
import { isNil } from "@compas/stdlib";
3+
import { _compasSentryExport, isNil } from "@compas/stdlib";
44

55
/**
66
* Format and append query parts, and execute the final result in a safe way.
@@ -84,11 +84,24 @@ export function query(strings, ...values) {
8484
}
8585

8686
// Strip out undefined values
87-
return await sql.unsafe(
88-
str,
89-
/** @type {NonNullable<QueryPartArg>} */
90-
_values.filter((it) => it !== undefined),
91-
);
87+
/** @type {NonNullable<QueryPartArg>} */
88+
const parameters = _values.filter((it) => it !== undefined);
89+
90+
if (
91+
typeof _compasSentryExport?.startSpan === "function" &&
92+
_compasSentryEnableQuerySpans
93+
) {
94+
return await _compasSentryExport.startSpan(
95+
{
96+
op: "db.query",
97+
name: str,
98+
data: { "db.system": "postgresql" },
99+
},
100+
() => sql.unsafe(str, parameters),
101+
);
102+
}
103+
104+
return await sql.unsafe(str, parameters);
92105
}
93106
}
94107

packages/store/src/queue-worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,10 @@ async function queueWorkerExecuteJob(logger, sql, options, job) {
511511
if (_compasSentryExport) {
512512
await _compasSentryExport.startSpan(
513513
{
514-
op: "job",
514+
op: "queue.task",
515515
name: job.name,
516516
description: job.name,
517+
forceTransaction: true,
517518
},
518519
async () => {
519520
return await exec();

0 commit comments

Comments
 (0)