Skip to content

Commit

Permalink
feat(stdlib,store): use convetional 'op', force transaction, support …
Browse files Browse the repository at this point in the history
…queries as spans
  • Loading branch information
dirkdev98 committed Apr 15, 2024
1 parent cc9203e commit 432c519
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
6 changes: 5 additions & 1 deletion packages/stdlib/index.js
Expand Up @@ -93,4 +93,8 @@ export {
newEventFromEvent,
} from "./src/events.js";

export { _compasSentryExport, compasWithSentry } from "./src/sentry.js";
export {
_compasSentryExport,
compasWithSentry,
_compasSentryEnableQuerySpans,
} from "./src/sentry.js";
6 changes: 2 additions & 4 deletions packages/stdlib/src/events.js
Expand Up @@ -162,7 +162,7 @@ export function eventStart(event, name) {

if (typeof _compasSentryExport?.startInactiveSpan === "function") {
event._compasSentrySpan = _compasSentryExport.startInactiveSpan({
op: "event",
op: "function",
name: name,
description: name,
});
Expand Down Expand Up @@ -231,9 +231,7 @@ export function eventStop(event) {

if (event._compasSentrySpan) {
event._compasSentrySpan.end();
}

if (isNil(event.rootEvent)) {
} else if (isNil(event.rootEvent)) {
event.log.info({
type: "event_span",
aborted: !!event.signal?.aborted,
Expand Down
28 changes: 19 additions & 9 deletions packages/stdlib/src/sentry.js
Expand Up @@ -5,14 +5,21 @@
*/
export let _compasSentryExport = undefined;

/**
* Send queries executed via `query` from @compas/store as a sentry span.
*
* @type {boolean}
*/
export let _compasSentryEnableQuerySpans = false;

/**
* Enable Sentry support. This comes with the following changes:
*
* Stdlib:
* - Logger: both info and error logs are added as breadcrumbs to the current active span.
* - Event: Events are propagated to Sentry as (inactive) spans.
* Meaning that further logs are not necessarily correlated to the correct event.
* This can be inferred based on the timeline.
* The final event callstack is not logged.
*
* Server:
* - Starts a new root span for each incoming request.
Expand All @@ -22,19 +29,22 @@ export let _compasSentryExport = undefined;
* Note that if a custom list of `allowHeaders` is provided in the CORS options,
* 'sentry-trace' and 'baggage' should be allowed as well.
* - If the error handler retrieves an unknown or AppError.serverError, it is reported as an uncaught exception.
* It is advised to set 'maxDepth' to '0' in your Sentry config, and to enable the 'extraErrorDataIntegration' integration.
* It is advised to set 'normalizeDepth' to '0' in your Sentry config, and to enable the 'extraErrorDataIntegration' integration.
*
* Queue:
* - Starts a new root span for each handled Job
* - Names it based on the job name.
* - Reports unhandled errors as exceptions.
* Store:
* - Starts a new root span for each handled Job in the QueueWorker
* The span name is based on the job name. Unhandled errors are captured as exceptions.
* - Supports passing queries to Sentry as spans. Requires {@link opts.sendQueriesAsSpans} to be set.
*
* All:
* - Each package that has error logs, will report an exception as well.
* - Note that we still execute the logs for now. Which may be removed in a future release.
* - All error logs in Compas package code are captured as exceptions.
*
* @param {import("@sentry/node")} instance
* @param {{
* sendQueriesAsSpans?: boolean
* }} [opts]
*/
export function compasWithSentry(instance) {
export function compasWithSentry(instance, { sendQueriesAsSpans } = {}) {
_compasSentryExport = instance;
_compasSentryEnableQuerySpans = sendQueriesAsSpans ?? false;
}
25 changes: 19 additions & 6 deletions packages/store/src/query.js
@@ -1,6 +1,6 @@
// @ts-nocheck

import { isNil } from "@compas/stdlib";
import { _compasSentryExport, isNil } from "@compas/stdlib";

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

// Strip out undefined values
return await sql.unsafe(
str,
/** @type {NonNullable<QueryPartArg>} */
_values.filter((it) => it !== undefined),
);
/** @type {NonNullable<QueryPartArg>} */
const parameters = _values.filter((it) => it !== undefined);

if (
typeof _compasSentryExport?.startSpan === "function" &&
_compasSentryEnableQuerySpans
) {
return await _compasSentryExport.startSpan(
{
op: "db.query",
name: str,
data: { "db.system": "postgresql" },
},
() => sql.unsafe(str, parameters),
);
}

return await sql.unsafe(str, parameters);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/store/src/queue-worker.js
Expand Up @@ -511,9 +511,10 @@ async function queueWorkerExecuteJob(logger, sql, options, job) {
if (_compasSentryExport) {
await _compasSentryExport.startSpan(
{
op: "job",
op: "queue.task",
name: job.name,
description: job.name,
forceTransaction: true,
},
async () => {
return await exec();
Expand Down

0 comments on commit 432c519

Please sign in to comment.