Skip to content

Commit 8d08a8b

Browse files
authored
Change DSN in Lambda Layer to point to relay running in Lambda Extension. (#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.
1 parent d478c77 commit 8d08a8b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/serverless/src/awslambda.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@sentry/node';
1212
import { extractTraceparentData } from '@sentry/tracing';
1313
import { Integration } from '@sentry/types';
14-
import { isString, logger } from '@sentry/utils';
14+
import { extensionRelayDSN, isString, logger } from '@sentry/utils';
1515
// 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
1616
// eslint-disable-next-line import/no-unresolved
1717
import { Context, Handler } from 'aws-lambda';
@@ -79,6 +79,8 @@ export function init(options: Sentry.NodeOptions = {}): void {
7979
version: Sentry.SDK_VERSION,
8080
};
8181

82+
options.dsn = extensionRelayDSN(options.dsn)
83+
8284
Sentry.init(options);
8385
Sentry.addGlobalEventProcessor(serverlessEventProcessor);
8486
}

packages/utils/src/dsn.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export function dsnToString(dsn: DsnComponents, withPassword: boolean = false):
2727
);
2828
}
2929

30+
/**
31+
* Parses a Dsn from a given string.
32+
*
33+
* @param str A Dsn as string
34+
* @returns Dsn as DsnComponents
35+
*/
3036
function dsnFromString(str: string): DsnComponents {
3137
const match = DSN_REGEX.exec(str);
3238

@@ -101,3 +107,25 @@ export function makeDsn(from: DsnLike): DsnComponents {
101107
validateDsn(components);
102108
return components;
103109
}
110+
111+
112+
/**
113+
* Changes a Dsn to point to the `relay` server running in the Lambda Extension.
114+
*
115+
* This is only used by the serverless integration for AWS Lambda.
116+
*
117+
* @param originalDsn The original Dsn of the customer.
118+
* @returns Dsn pointing to Lambda extension.
119+
*/
120+
export function extensionRelayDSN(originalDsn: string | undefined): string | undefined {
121+
if (originalDsn === undefined) {
122+
return undefined;
123+
}
124+
125+
const dsn = dsnFromString(originalDsn);
126+
dsn.host = 'localhost';
127+
dsn.port = '3000';
128+
dsn.protocol = 'http';
129+
130+
return dsnToString(dsn);
131+
}

0 commit comments

Comments
 (0)