Skip to content

Adds the root sentry span to a resolver's context #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-rocks-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@envelop/sentry": patch
---

Adds the root sentry span to a resolver's context - so you can safely grab it from your own code without relying on singletons.
52 changes: 42 additions & 10 deletions packages/plugins/sentry/__tests__/sentry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ describe('sentry', () => {
}
`);

// run sentry flush
await new Promise(res => setTimeout(res, 10));
await Sentry.flush();

const reports = sentryTestkit.reports();
expect(reports).toHaveLength(1);
Expand Down Expand Up @@ -89,8 +88,7 @@ describe('sentry', () => {
}
`);

// run sentry flush
await new Promise(res => setTimeout(res, 10));
await Sentry.flush();

expect(sentryTestkit.reports()).toHaveLength(0);
});
Expand Down Expand Up @@ -130,8 +128,7 @@ describe('sentry', () => {
}
`);

// run sentry flush
await new Promise(res => setTimeout(res, 10));
await Sentry.flush();

const reports = sentryTestkit.reports();
expect(reports).toHaveLength(1);
Expand Down Expand Up @@ -176,8 +173,7 @@ describe('sentry', () => {
}
`);

// run sentry flush
await new Promise(res => setTimeout(res, 10));
await Sentry.flush();

expect(sentryTestkit.reports()).toHaveLength(0);
});
Expand Down Expand Up @@ -246,8 +242,7 @@ describe('sentry', () => {
}
`);

// run sentry flush
await new Promise(res => setTimeout(res, 10));
await Sentry.flush();

const reports = sentryTestkit.reports();
expect(reports).toHaveLength(1);
Expand All @@ -257,3 +252,40 @@ describe('sentry', () => {
});
});
});

test('sets the span in the query context', async () => {
const { testkit: sentryTestkit, sentryTransport } = createSentryTestkit();
Sentry.init({
dsn: 'https://[email protected]/2',
transport: sentryTransport,
});

// The test verifies that the span is set in the context, which would be
// available as the third argument
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
hasSentryInContext: String!
}
`,
resolvers: {
Query: {
hasSentryInContext: async (args, info, ctx) => {
return !!ctx.sentry;
},
},
},
});

const envelopTestkit = createTestkit([useSentry()], schema);
const result = await envelopTestkit.execute('{ hasSentryInContext }');
expect(result).toMatchInlineSnapshot(`
Object {
"data": Object {
"hasSentryInContext": "true",
},
}
`);

await Sentry.flush();
});
4 changes: 3 additions & 1 deletion packages/plugins/sentry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const useSentry = (options: SentryPluginOptions = {}): Plugin => {
}

return {
onExecute({ args }) {
onExecute({ args, extendContext }) {
if (skipOperation(args)) {
return;
}
Expand Down Expand Up @@ -179,6 +179,8 @@ export const useSentry = (options: SentryPluginOptions = {}): Plugin => {
Sentry.configureScope(scope => options.configureScope!(args, scope));
}

extendContext({ sentry: { scope } });

return {
onExecuteDone(payload) {
const handleResult: OnExecuteDoneHookResultOnNextHook<{}> = ({ result, setResult }) => {
Expand Down