Skip to content

Commit 3ff39e4

Browse files
committed
feat(stdlib,queue): explicit wrap Sentry in new traces
Previously we had a hacky way to set `traceId`. This seems like a cleaner api.
1 parent 731fc72 commit 3ff39e4

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

packages/server/src/middleware/log.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export function logMiddleware(app, options) {
7979

8080
// Skip eventStop if we don't have events enabled.
8181
// Skip eventStop for CORS requests, this gives a bit cleaner logs.
82-
if (options.disableRootEvent !== true && ctx.method !== "OPTIONS") {
82+
if (
83+
options.disableRootEvent !== true &&
84+
ctx.method !== "OPTIONS" &&
85+
ctx.method !== "HEAD"
86+
) {
8387
if (_compasSentryExport) {
8488
const span = _compasSentryExport.getActiveSpan();
8589
if (span) {
@@ -91,12 +95,12 @@ export function logMiddleware(app, options) {
9195
}
9296

9397
if (_compasSentryExport) {
94-
if (_compasSentryExport.metrics?.increment) {
95-
let compasRouteName = ctx.event.name;
96-
if (!compasRouteName.startsWith("router.")) {
97-
compasRouteName = "<unmatched>";
98-
}
98+
const span = _compasSentryExport.getActiveSpan();
99+
const routeName = ctx.event.name;
100+
const isMatchedRoute = routeName.startsWith("router.");
99101

102+
if (_compasSentryExport.metrics?.increment) {
103+
const compasRouteName = isMatchedRoute ? routeName : "<unmatched>";
100104
_compasSentryExport.metrics.increment("compas.route.name", 1, {
101105
tags: {
102106
compasRouteName,
@@ -105,8 +109,14 @@ export function logMiddleware(app, options) {
105109
});
106110
}
107111

108-
const span = _compasSentryExport.getActiveSpan();
109112
if (span) {
113+
if (!isMatchedRoute) {
114+
// @ts-expect-error Private property?
115+
//
116+
// Discard sampled spans which don't match a route.
117+
span._sampled = false;
118+
}
119+
110120
span.setStatus(
111121
_compasSentryExport.getSpanStatusFromHttpCode(ctx.status),
112122
);

packages/server/src/middleware/sentry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function sentry() {
3333

3434
const _sentry = _compasSentryExport;
3535

36-
return _sentry.withIsolationScope(() => {
36+
return _sentry.startNewTrace(() => {
3737
return _sentry.startSpanManual(
3838
{
3939
op: "http.server",

packages/store/src/queue-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ async function queueWorkerExecuteJob(logger, sql, options, job) {
519519

520520
if (_compasSentryExport) {
521521
const _sentry = _compasSentryExport;
522-
await _sentry.withIsolationScope(() => {
522+
await _sentry.startNewTrace(() => {
523523
return _sentry.startSpan(
524524
{
525525
op: "queue.task",

0 commit comments

Comments
 (0)