diff --git a/packages/serverless/src/awslambda.ts b/packages/serverless/src/awslambda.ts index 92802bf808cd..88af7f66c3b5 100644 --- a/packages/serverless/src/awslambda.ts +++ b/packages/serverless/src/awslambda.ts @@ -11,7 +11,7 @@ import { } from '@sentry/node'; import { extractTraceparentData } from '@sentry/tracing'; import { Integration } from '@sentry/types'; -import { isString, logger } from '@sentry/utils'; +import { extensionRelayDSN, isString, logger } from '@sentry/utils'; // NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil // eslint-disable-next-line import/no-unresolved import { Context, Handler } from 'aws-lambda'; @@ -79,6 +79,8 @@ export function init(options: Sentry.NodeOptions = {}): void { version: Sentry.SDK_VERSION, }; + options.dsn = extensionRelayDSN(options.dsn) + Sentry.init(options); Sentry.addGlobalEventProcessor(serverlessEventProcessor); } diff --git a/packages/utils/src/dsn.ts b/packages/utils/src/dsn.ts index 56b864e2863b..b99c983aad71 100644 --- a/packages/utils/src/dsn.ts +++ b/packages/utils/src/dsn.ts @@ -27,6 +27,12 @@ export function dsnToString(dsn: DsnComponents, withPassword: boolean = false): ); } +/** + * Parses a Dsn from a given string. + * + * @param str A Dsn as string + * @returns Dsn as DsnComponents + */ function dsnFromString(str: string): DsnComponents { const match = DSN_REGEX.exec(str); @@ -101,3 +107,25 @@ export function makeDsn(from: DsnLike): DsnComponents { validateDsn(components); return components; } + + +/** + * Changes a Dsn to point to the `relay` server running in the Lambda Extension. + * + * This is only used by the serverless integration for AWS Lambda. + * + * @param originalDsn The original Dsn of the customer. + * @returns Dsn pointing to Lambda extension. + */ +export function extensionRelayDSN(originalDsn: string | undefined): string | undefined { + if (originalDsn === undefined) { + return undefined; + } + + const dsn = dsnFromString(originalDsn); + dsn.host = 'localhost'; + dsn.port = '3000'; + dsn.protocol = 'http'; + + return dsnToString(dsn); +}