11/**
22 * The Sentry version that all Compas packages use if set via {@link compasWithSentry}.
33 *
4- * @type {undefined|import("@sentry/node") }
4+ * @type {undefined|typeof import("@sentry/node") }
55 */
66export let _compasSentryExport = undefined ;
77
@@ -16,35 +16,61 @@ export let _compasSentryEnableQuerySpans = false;
1616 * Enable Sentry support. This comes with the following changes:
1717 *
1818 * Stdlib:
19- * - Logger: both info and error logs are added as breadcrumbs to the current active span.
19+ * - Logger: both info and error logs are added as breadcrumbs to the current active
20+ * span.
2021 * - Event: Events are propagated to Sentry as (inactive) spans.
2122 * Meaning that further logs are not necessarily correlated to the correct event.
2223 * The final event callstack is not logged.
2324 *
2425 * Server:
2526 * - Starts a new root span for each incoming request.
2627 * - Tries to name it based on the finalized name of `ctx.event`.
27- * This is most likely in the format `router.foo.bar` for matched routes by the generated router.
28+ * This is most likely in the format `router.foo.bar` for matched routes by the
29+ * generated router.
2830 * - Uses the sentry-trace header when provided.
2931 * Note that if a custom list of `allowHeaders` is provided in the CORS options,
3032 * 'sentry-trace' and 'baggage' should be allowed as well.
31- * - If the error handler retrieves an unknown or AppError.serverError, it is reported as an uncaught exception.
32- * It is advised to set 'normalizeDepth' to '0' in your Sentry config, and to enable the 'extraErrorDataIntegration' integration.
33+ * - If the error handler retrieves an unknown or AppError.serverError, it is reported as
34+ * an uncaught exception. It is advised to set 'normalizeDepth' to '0' in your Sentry
35+ * config, and to enable the 'extraErrorDataIntegration' integration.
3336 *
3437 * Store:
3538 * - 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.
39+ * The span name is based on the job name. Unhandled errors are captured as
40+ * exceptions.
41+ * - Supports passing queries to Sentry as spans. Requires {@link
42+ * opts.sendQueriesAsSpans} to be set.
3843 *
3944 * All:
4045 * - All error logs in Compas package code are captured as exceptions.
4146 *
42- * @param {import("@sentry/node") } instance
47+ * @param {typeof import("@sentry/node") } instance
4348 * @param {{
4449 * sendQueriesAsSpans?: boolean
4550 * }} [opts]
4651 */
4752export function compasWithSentry ( instance , { sendQueriesAsSpans } = { } ) {
4853 _compasSentryExport = instance ;
4954 _compasSentryEnableQuerySpans = sendQueriesAsSpans ?? false ;
55+
56+ _compasSentryExport . addEventProcessor ( ( event ) => {
57+ if ( event . spans ?. some ?. ( ( it ) => it . data ?. [ "_compas.skip-event" ] ) ) {
58+ return null ;
59+ }
60+
61+ return event ;
62+ } ) ;
63+ }
64+
65+ /**
66+ * @see https://github.com/getsentry/sentry-javascript/blob/8bec42e0285ee301e8fc9bcaf02046daf48e0495/packages/core/src/utils/spanUtils.ts#L103
67+ */
68+ export function sentrySpanIsSampled ( span ) {
69+ if ( ! span ) {
70+ return false ;
71+ }
72+
73+ const { traceFlags } = span . spanContext ( ) ;
74+
75+ return Boolean ( traceFlags & 0x1 ) ;
5076}
0 commit comments