Skip to content

Koa scopes are not isolated between requests #13992

@TheHolyWaffle

Description

@TheHolyWaffle

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.34.0

Framework Version

koa 2.15.3, @koa/router 13.1.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

instrument.ts

import * as Sentry from '@sentry/node';

Sentry.init({
  dsn: "mydns",
  tracesSampleRate: 1.0,
  integrations: [Sentry.koaIntegration()],
  debug: true,
});

server.ts

import './instrument';
import Koa from 'koa';
import Router from '@koa/router';
import * as Sentry from '@sentry/node';

const app = new Koa();
const router = new Router();
Sentry.setupKoaErrorHandler(app);

function log(msg: string) {
  const globalScope = Sentry.getGlobalScope().getScopeData();
  const isolationScope = Sentry.getIsolationScope().getScopeData();
  const currentScope = Sentry.getCurrentScope().getScopeData();

  const data = {
    ...globalScope.tags,
    ...globalScope.extra,
    ...isolationScope.tags,
    ...isolationScope.extra,
    ...currentScope.tags,
    ...currentScope.extra,
  };

  console.info(msg, JSON.stringify(data));
}

router.get('/', (ctx) => {
  log('BEFORE');
  Sentry.setTag('query', ctx.request.query.test as string);
  log('AFTER');
});

app.use(router.routes());

export const server = app.listen(8088, () => log('Server started on 8088'));

Steps to Reproduce

  1. Run npx tsx server.ts
  2. Trigger the / endpoint with a GET request and a URL query parameter test set to foo
  3. Console outputs:
BEFORE {}
AFTER {"query":"foo"}
  1. Trigger the / endpoint again, but change query parameter value to foo2
  2. Console outputs:
BEFORE {"query":"foo"}
AFTER {"query":"foo2"}

Expected Result

The expected result is that the Sentry tag query is not persisted across consecutive incoming HTTP requests. Wrapping the / handler with Sentry.withScope does not fix this

In practice this means the second trigger should output:

BEFORE {}
AFTER {"query":"foo2"}

Actual Result

The second trigger outputs:

BEFORE {"query":"foo"}
AFTER {"query":"foo2"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPackage: nodeIssues related to the Sentry Node SDK

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions