Skip to content

Commit b372f8e

Browse files
AbhiPrasadLms24
andauthored
ref(nextjs): Use @sentry/vercel-edge in nextjs package (#9053)
Switch to new vercel-edge package Co-authored-by: Lukas Stracke <[email protected]>
1 parent 5582d80 commit b372f8e

File tree

9 files changed

+19
-439
lines changed

9 files changed

+19
-439
lines changed

packages/e2e-tests/test-applications/nextjs-app-dir/tests/edge-route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('Should create a transaction for edge routes', async ({ request }) => {
1515

1616
expect(edgerouteTransaction.contexts?.trace?.status).toBe('ok');
1717
expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server');
18-
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge');
18+
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('vercel-edge');
1919
});
2020

2121
test('Should create a transaction with error status for faulty edge routes', async ({ request }) => {
@@ -34,7 +34,7 @@ test('Should create a transaction with error status for faulty edge routes', asy
3434

3535
expect(edgerouteTransaction.contexts?.trace?.status).toBe('internal_error');
3636
expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server');
37-
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge');
37+
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('vercel-edge');
3838
});
3939

4040
test('Should record exceptions for faulty edge routes', async ({ request }) => {

packages/e2e-tests/test-applications/nextjs-app-dir/tests/middleware.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('Should create a transaction for middleware', async ({ request }) => {
1313

1414
expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
1515
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
16-
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
16+
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
1717
});
1818

1919
test('Should create a transaction with error status for faulty middleware', async ({ request }) => {
@@ -31,7 +31,7 @@ test('Should create a transaction with error status for faulty middleware', asyn
3131

3232
expect(middlewareTransaction.contexts?.trace?.status).toBe('internal_error');
3333
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
34-
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
34+
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
3535
});
3636

3737
test('Records exceptions happening in middleware', async ({ request }) => {

packages/e2e-tests/test-applications/nextjs-app-dir/tests/route-handlers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ test.describe('Edge runtime', () => {
8787

8888
expect(routehandlerTransaction.contexts?.trace?.status).toBe('internal_error');
8989
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');
90-
expect(routehandlerTransaction.contexts?.runtime?.name).toBe('edge');
90+
expect(routehandlerTransaction.contexts?.runtime?.name).toBe('vercel-edge');
9191

9292
expect(routehandlerError.exception?.values?.[0].value).toBe('route-handler-edge-error');
93-
expect(routehandlerError.contexts?.runtime?.name).toBe('edge');
93+
expect(routehandlerError.contexts?.runtime?.name).toBe('vercel-edge');
9494
});
9595
});

packages/nextjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@sentry/react": "7.69.0",
3232
"@sentry/types": "7.69.0",
3333
"@sentry/utils": "7.69.0",
34+
"@sentry/vercel-edge": "7.69.0",
3435
"@sentry/webpack-plugin": "1.20.0",
3536
"chalk": "3.0.0",
3637
"rollup": "2.78.0",

packages/nextjs/src/edge/asyncLocalStorageAsyncContextStrategy.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/nextjs/src/edge/index.ts

Lines changed: 10 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,13 @@
1-
import type { ServerRuntimeClientOptions } from '@sentry/core';
2-
import {
3-
getIntegrationsToSetup,
4-
initAndBind,
5-
Integrations as CoreIntegrations,
6-
SDK_VERSION,
7-
ServerRuntimeClient,
8-
} from '@sentry/core';
9-
import type { Options } from '@sentry/types';
10-
import { createStackParser, GLOBAL_OBJ, nodeStackLineParser, stackParserFromStackParserOptions } from '@sentry/utils';
1+
import { SDK_VERSION } from '@sentry/core';
2+
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
3+
import { init as vercelEdgeInit } from '@sentry/vercel-edge';
114

12-
import { getVercelEnv } from '../common/getVercelEnv';
13-
import { setAsyncLocalStorageAsyncContextStrategy } from './asyncLocalStorageAsyncContextStrategy';
14-
import { makeEdgeTransport } from './transport';
15-
16-
const nodeStackParser = createStackParser(nodeStackLineParser());
17-
18-
export const defaultIntegrations = [new CoreIntegrations.InboundFilters(), new CoreIntegrations.FunctionToString()];
19-
20-
export type EdgeOptions = Options;
5+
export type EdgeOptions = VercelEdgeOptions;
216

227
/** Inits the Sentry NextJS SDK on the Edge Runtime. */
23-
export function init(options: EdgeOptions = {}): void {
24-
setAsyncLocalStorageAsyncContextStrategy();
25-
26-
if (options.defaultIntegrations === undefined) {
27-
options.defaultIntegrations = defaultIntegrations;
28-
}
29-
30-
if (options.dsn === undefined && process.env.SENTRY_DSN) {
31-
options.dsn = process.env.SENTRY_DSN;
32-
}
33-
34-
if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) {
35-
const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE);
36-
if (isFinite(tracesSampleRate)) {
37-
options.tracesSampleRate = tracesSampleRate;
38-
}
39-
}
40-
41-
if (options.release === undefined) {
42-
const detectedRelease = getSentryRelease();
43-
if (detectedRelease !== undefined) {
44-
options.release = detectedRelease;
45-
} else {
46-
// If release is not provided, then we should disable autoSessionTracking
47-
options.autoSessionTracking = false;
48-
}
49-
}
50-
51-
options.environment =
52-
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
53-
54-
if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
55-
options.autoSessionTracking = true;
56-
}
57-
58-
if (options.instrumenter === undefined) {
59-
options.instrumenter = 'sentry';
60-
}
61-
62-
const clientOptions: ServerRuntimeClientOptions = {
63-
...options,
64-
stackParser: stackParserFromStackParserOptions(options.stackParser || nodeStackParser),
65-
integrations: getIntegrationsToSetup(options),
66-
transport: options.transport || makeEdgeTransport,
67-
};
68-
69-
clientOptions._metadata = clientOptions._metadata || {};
70-
clientOptions._metadata.sdk = clientOptions._metadata.sdk || {
8+
export function init(options: VercelEdgeOptions = {}): void {
9+
options._metadata = options._metadata || {};
10+
options._metadata.sdk = options._metadata.sdk || {
7111
name: 'sentry.javascript.nextjs',
7212
packages: [
7313
{
@@ -78,45 +18,7 @@ export function init(options: EdgeOptions = {}): void {
7818
version: SDK_VERSION,
7919
};
8020

81-
clientOptions.platform = 'edge';
82-
clientOptions.runtime = { name: 'edge' };
83-
clientOptions.serverName = process.env.SENTRY_NAME;
84-
85-
initAndBind(ServerRuntimeClient, clientOptions);
86-
87-
// TODO?: Sessiontracking
88-
}
89-
90-
/**
91-
* Returns a release dynamically from environment variables.
92-
*/
93-
export function getSentryRelease(fallback?: string): string | undefined {
94-
// Always read first as Sentry takes this as precedence
95-
if (process.env.SENTRY_RELEASE) {
96-
return process.env.SENTRY_RELEASE;
97-
}
98-
99-
// This supports the variable that sentry-webpack-plugin injects
100-
if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) {
101-
return GLOBAL_OBJ.SENTRY_RELEASE.id;
102-
}
103-
104-
return (
105-
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
106-
process.env.GITHUB_SHA ||
107-
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
108-
process.env.COMMIT_REF ||
109-
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
110-
process.env.VERCEL_GIT_COMMIT_SHA ||
111-
process.env.VERCEL_GITHUB_COMMIT_SHA ||
112-
process.env.VERCEL_GITLAB_COMMIT_SHA ||
113-
process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
114-
// Zeit (now known as Vercel)
115-
process.env.ZEIT_GITHUB_COMMIT_SHA ||
116-
process.env.ZEIT_GITLAB_COMMIT_SHA ||
117-
process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
118-
fallback
119-
);
21+
vercelEdgeInit(options);
12022
}
12123

12224
/**
@@ -126,7 +28,8 @@ export function withSentryConfig<T>(exportedUserNextConfig: T): T {
12628
return exportedUserNextConfig;
12729
}
12830

129-
export * from '@sentry/core';
31+
export * from '@sentry/vercel-edge';
32+
export { Span, Transaction } from '@sentry/core';
13033

13134
// eslint-disable-next-line import/export
13235
export * from '../common';

packages/nextjs/src/edge/transport.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)