Skip to content

Commit 1b8bfeb

Browse files
authored
feat(nextjs): Tag backend events when running on vercel (#4091)
Because nextjs apps behave somewhat differently on vercel than off, it's helpful to know when an event came from a vercel-deployed app vs. a non-vercel-deployed app. This adds a tag to events from vercel, based on an environment variable which vercel sets in the serverless environment in which backend lambdas run. Note: If the user has overridden the default and turned off `Automatically expose System Environment Variables` in their project settings, we won't know they're on vercel. That said, it's hard to know why one ever would, and likely most people don't, so presumably this data will be close enough to accurate to remain useful. See https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables.
1 parent 7c15a97 commit 1b8bfeb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/nextjs/src/index.server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export function init(options: NextjsOptions): void {
5353

5454
configureScope(scope => {
5555
scope.setTag('runtime', 'node');
56+
if (process.env.VERCEL) {
57+
scope.setTag('vercel', true);
58+
}
5659
});
5760

5861
if (activeDomain) {

packages/nextjs/test/index.server.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ describe('Server init()', () => {
6363
expect(currentScope._tags).toEqual({ runtime: 'node' });
6464
});
6565

66+
it('applies `vercel` tag when running on vercel', () => {
67+
const currentScope = getCurrentHub().getScope();
68+
69+
process.env.VERCEL = '1';
70+
71+
init({});
72+
73+
// @ts-ignore need access to protected _tags attribute
74+
expect(currentScope._tags.vercel).toEqual(true);
75+
76+
delete process.env.VERCEL;
77+
});
78+
79+
it('does not apply `vercel` tag when not running on vercel', () => {
80+
const currentScope = getCurrentHub().getScope();
81+
82+
expect(process.env.VERCEL).toBeUndefined();
83+
84+
init({});
85+
86+
// @ts-ignore need access to protected _tags attribute
87+
expect(currentScope._tags.vercel).toBeUndefined();
88+
});
89+
6690
it("initializes both global hub and domain hub when there's an active domain", () => {
6791
const globalHub = getCurrentHub();
6892
const local = domain.create();

0 commit comments

Comments
 (0)