Skip to content

Commit

Permalink
Change DSN in Lambda Layer to point to relay running in Lambda Extens…
Browse files Browse the repository at this point in the history
…ion. (#5126)

If the serverless SDK is running in an AWS Lambda Function it should not send data directly to sentry.io but to the relay that is running in the AWS Lambda Extension (which is running on localhost:3000)

This PR parses the DSN and changes the host and port to point to localhost:3000.
  • Loading branch information
antonpirker authored May 18, 2022
1 parent d478c77 commit 8d08a8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/serverless/src/awslambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/utils/src/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

0 comments on commit 8d08a8b

Please sign in to comment.