Skip to content

Commit

Permalink
ref(nextjs): Use @sentry/vercel-edge in nextjs package (#9053)
Browse files Browse the repository at this point in the history
Switch to new vercel-edge package 

Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
  • Loading branch information
AbhiPrasad and Lms24 authored Sep 20, 2023
1 parent 5582d80 commit b372f8e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 439 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('Should create a transaction for edge routes', async ({ request }) => {

expect(edgerouteTransaction.contexts?.trace?.status).toBe('ok');
expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server');
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge');
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('vercel-edge');
});

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

expect(edgerouteTransaction.contexts?.trace?.status).toBe('internal_error');
expect(edgerouteTransaction.contexts?.trace?.op).toBe('http.server');
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('edge');
expect(edgerouteTransaction.contexts?.runtime?.name).toBe('vercel-edge');
});

test('Should record exceptions for faulty edge routes', async ({ request }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('Should create a transaction for middleware', async ({ request }) => {

expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
});

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

expect(middlewareTransaction.contexts?.trace?.status).toBe('internal_error');
expect(middlewareTransaction.contexts?.trace?.op).toBe('middleware.nextjs');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('edge');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
});

test('Records exceptions happening in middleware', async ({ request }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ test.describe('Edge runtime', () => {

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

expect(routehandlerError.exception?.values?.[0].value).toBe('route-handler-edge-error');
expect(routehandlerError.contexts?.runtime?.name).toBe('edge');
expect(routehandlerError.contexts?.runtime?.name).toBe('vercel-edge');
});
});
1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@sentry/react": "7.69.0",
"@sentry/types": "7.69.0",
"@sentry/utils": "7.69.0",
"@sentry/vercel-edge": "7.69.0",
"@sentry/webpack-plugin": "1.20.0",
"chalk": "3.0.0",
"rollup": "2.78.0",
Expand Down
59 changes: 0 additions & 59 deletions packages/nextjs/src/edge/asyncLocalStorageAsyncContextStrategy.ts

This file was deleted.

117 changes: 10 additions & 107 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
import type { ServerRuntimeClientOptions } from '@sentry/core';
import {
getIntegrationsToSetup,
initAndBind,
Integrations as CoreIntegrations,
SDK_VERSION,
ServerRuntimeClient,
} from '@sentry/core';
import type { Options } from '@sentry/types';
import { createStackParser, GLOBAL_OBJ, nodeStackLineParser, stackParserFromStackParserOptions } from '@sentry/utils';
import { SDK_VERSION } from '@sentry/core';
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
import { init as vercelEdgeInit } from '@sentry/vercel-edge';

import { getVercelEnv } from '../common/getVercelEnv';
import { setAsyncLocalStorageAsyncContextStrategy } from './asyncLocalStorageAsyncContextStrategy';
import { makeEdgeTransport } from './transport';

const nodeStackParser = createStackParser(nodeStackLineParser());

export const defaultIntegrations = [new CoreIntegrations.InboundFilters(), new CoreIntegrations.FunctionToString()];

export type EdgeOptions = Options;
export type EdgeOptions = VercelEdgeOptions;

/** Inits the Sentry NextJS SDK on the Edge Runtime. */
export function init(options: EdgeOptions = {}): void {
setAsyncLocalStorageAsyncContextStrategy();

if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = defaultIntegrations;
}

if (options.dsn === undefined && process.env.SENTRY_DSN) {
options.dsn = process.env.SENTRY_DSN;
}

if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) {
const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE);
if (isFinite(tracesSampleRate)) {
options.tracesSampleRate = tracesSampleRate;
}
}

if (options.release === undefined) {
const detectedRelease = getSentryRelease();
if (detectedRelease !== undefined) {
options.release = detectedRelease;
} else {
// If release is not provided, then we should disable autoSessionTracking
options.autoSessionTracking = false;
}
}

options.environment =
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;

if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
options.autoSessionTracking = true;
}

if (options.instrumenter === undefined) {
options.instrumenter = 'sentry';
}

const clientOptions: ServerRuntimeClientOptions = {
...options,
stackParser: stackParserFromStackParserOptions(options.stackParser || nodeStackParser),
integrations: getIntegrationsToSetup(options),
transport: options.transport || makeEdgeTransport,
};

clientOptions._metadata = clientOptions._metadata || {};
clientOptions._metadata.sdk = clientOptions._metadata.sdk || {
export function init(options: VercelEdgeOptions = {}): void {
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.nextjs',
packages: [
{
Expand All @@ -78,45 +18,7 @@ export function init(options: EdgeOptions = {}): void {
version: SDK_VERSION,
};

clientOptions.platform = 'edge';
clientOptions.runtime = { name: 'edge' };
clientOptions.serverName = process.env.SENTRY_NAME;

initAndBind(ServerRuntimeClient, clientOptions);

// TODO?: Sessiontracking
}

/**
* Returns a release dynamically from environment variables.
*/
export function getSentryRelease(fallback?: string): string | undefined {
// Always read first as Sentry takes this as precedence
if (process.env.SENTRY_RELEASE) {
return process.env.SENTRY_RELEASE;
}

// This supports the variable that sentry-webpack-plugin injects
if (GLOBAL_OBJ.SENTRY_RELEASE && GLOBAL_OBJ.SENTRY_RELEASE.id) {
return GLOBAL_OBJ.SENTRY_RELEASE.id;
}

return (
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
process.env.GITHUB_SHA ||
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
process.env.COMMIT_REF ||
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
process.env.VERCEL_GIT_COMMIT_SHA ||
process.env.VERCEL_GITHUB_COMMIT_SHA ||
process.env.VERCEL_GITLAB_COMMIT_SHA ||
process.env.VERCEL_BITBUCKET_COMMIT_SHA ||
// Zeit (now known as Vercel)
process.env.ZEIT_GITHUB_COMMIT_SHA ||
process.env.ZEIT_GITLAB_COMMIT_SHA ||
process.env.ZEIT_BITBUCKET_COMMIT_SHA ||
fallback
);
vercelEdgeInit(options);
}

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

export * from '@sentry/core';
export * from '@sentry/vercel-edge';
export { Span, Transaction } from '@sentry/core';

// eslint-disable-next-line import/export
export * from '../common';
Expand Down
103 changes: 0 additions & 103 deletions packages/nextjs/src/edge/transport.ts

This file was deleted.

Loading

0 comments on commit b372f8e

Please sign in to comment.