Skip to content

fix(serverless): Explicitly export node package exports #7457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,47 @@ import * as AWSLambda from './awslambda';
import * as GCPFunction from './gcpfunction';
export { AWSLambda, GCPFunction };

export * from './awsservices';
export * from '@sentry/node';
export { AWSServices } from './awsservices';

// TODO(v8): We have to explicitly export these because of the namespace exports
// above. This is because just doing `export * from '@sentry/node'` will not
// work with Node native esm while we also have namespace exports in a package.
// What we should do is get rid of the namespace exports.
export {
Hub,
SDK_VERSION,
Scope,
addBreadcrumb,
addGlobalEventProcessor,
captureEvent,
captureException,
captureMessage,
configureScope,
createTransport,
getCurrentHub,
getHubFromCarrier,
makeMain,
setContext,
setExtra,
setExtras,
setTag,
setTags,
setUser,
startTransaction,
withScope,
NodeClient,
makeNodeTransport,
close,
defaultIntegrations,
defaultStackParser,
flush,
getSentryRelease,
init,
lastEventId,
DEFAULT_USER_INCLUDES,
addRequestDataToEvent,
extractRequestData,
deepReadDirSync,
Handlers,
Integrations,
} from '@sentry/node';
82 changes: 42 additions & 40 deletions packages/serverless/test/awslambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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 * as SentryNode from '@sentry/node';
// eslint-disable-next-line import/no-unresolved
import type { Callback, Handler } from 'aws-lambda';

import * as Sentry from '../src';
Expand Down Expand Up @@ -40,15 +42,15 @@ const fakeCallback: Callback = (err, result) => {

function expectScopeSettings(fakeTransactionContext: any) {
// @ts-ignore see "Why @ts-ignore" note
const fakeTransaction = { ...Sentry.fakeTransaction, ...fakeTransactionContext };
const fakeTransaction = { ...SentryNode.fakeTransaction, ...fakeTransactionContext };
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
expect(SentryNode.fakeScope.setSpan).toBeCalledWith(fakeTransaction);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
expect(SentryNode.fakeScope.setTag).toBeCalledWith('server_name', expect.anything());
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setTag).toBeCalledWith('url', 'awslambda:///functionName');
expect(SentryNode.fakeScope.setTag).toBeCalledWith('url', 'awslambda:///functionName');
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setContext).toBeCalledWith(
expect(SentryNode.fakeScope.setContext).toBeCalledWith(
'aws.lambda',
expect.objectContaining({
aws_request_id: 'awsRequestId',
Expand All @@ -59,7 +61,7 @@ function expectScopeSettings(fakeTransactionContext: any) {
}),
);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setContext).toBeCalledWith(
expect(SentryNode.fakeScope.setContext).toBeCalledWith(
'aws.cloudwatch.logs',
expect.objectContaining({
log_group: 'logGroupName',
Expand All @@ -77,7 +79,7 @@ describe('AWSLambda', () => {

afterEach(() => {
// @ts-ignore see "Why @ts-ignore" note
Sentry.resetMocks();
SentryNode.resetMocks();
});

describe('wrapHandler() options', () => {
Expand All @@ -88,7 +90,7 @@ describe('AWSLambda', () => {
const wrappedHandler = wrapHandler(handler, { flushTimeout: 1337 });

await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
expect(Sentry.flush).toBeCalledWith(1337);
expect(SentryNode.flush).toBeCalledWith(1337);
});

test('captureTimeoutWarning enabled (default)', async () => {
Expand All @@ -104,7 +106,7 @@ describe('AWSLambda', () => {

expect(Sentry.captureMessage).toBeCalled();
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setTag).toBeCalledWith('timeout', '1s');
expect(SentryNode.fakeScope.setTag).toBeCalledWith('timeout', '1s');
});

test('captureTimeoutWarning disabled', async () => {
Expand Down Expand Up @@ -152,14 +154,14 @@ describe('AWSLambda', () => {

expect(Sentry.captureMessage).toBeCalled();
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setTag).toBeCalledWith('timeout', '1m40s');
expect(SentryNode.fakeScope.setTag).toBeCalledWith('timeout', '1m40s');
});

test('captureAllSettledReasons disabled (default)', async () => {
const handler = () => Promise.resolve([{ status: 'rejected', reason: new Error() }]);
const wrappedHandler = wrapHandler(handler, { flushTimeout: 1337 });
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
expect(Sentry.captureException).toBeCalledTimes(0);
expect(SentryNode.captureException).toBeCalledTimes(0);
});

test('captureAllSettledReasons enable', async () => {
Expand All @@ -173,9 +175,9 @@ describe('AWSLambda', () => {
]);
const wrappedHandler = wrapHandler(handler, { flushTimeout: 1337, captureAllSettledReasons: true });
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);
expect(Sentry.captureException).toHaveBeenNthCalledWith(1, error);
expect(Sentry.captureException).toHaveBeenNthCalledWith(2, error2);
expect(Sentry.captureException).toBeCalledTimes(2);
expect(SentryNode.captureException).toHaveBeenNthCalledWith(1, error);
expect(SentryNode.captureException).toHaveBeenNthCalledWith(2, error2);
expect(SentryNode.captureException).toBeCalledTimes(2);
});
});

Expand All @@ -197,11 +199,11 @@ describe('AWSLambda', () => {

expect(rv).toStrictEqual(42);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalledWith(2000);
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalledWith(2000);
});

test('unsuccessful execution', async () => {
Expand All @@ -223,12 +225,12 @@ describe('AWSLambda', () => {
};

// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
expect(Sentry.captureException).toBeCalledWith(error);
expect(SentryNode.captureException).toBeCalledWith(error);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalledWith(2000);
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalledWith(2000);
}
});

Expand All @@ -254,7 +256,7 @@ describe('AWSLambda', () => {

const handler: Handler = (_event, _context, callback) => {
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(
expect.objectContaining({
parentSpanId: '1121201211212012',
parentSampled: false,
Expand Down Expand Up @@ -300,12 +302,12 @@ describe('AWSLambda', () => {
};

// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
expect(Sentry.captureException).toBeCalledWith(e);
expect(SentryNode.captureException).toBeCalledWith(e);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalled();
}
});
});
Expand All @@ -328,11 +330,11 @@ describe('AWSLambda', () => {

expect(rv).toStrictEqual(42);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalled();
});

test('event and context are correctly passed to the original handler', async () => {
Expand Down Expand Up @@ -365,12 +367,12 @@ describe('AWSLambda', () => {
};

// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
expect(Sentry.captureException).toBeCalledWith(error);
expect(SentryNode.captureException).toBeCalledWith(error);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalled();
}
});

Expand Down Expand Up @@ -408,11 +410,11 @@ describe('AWSLambda', () => {

expect(rv).toStrictEqual(42);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalled();
});

test('event and context are correctly passed to the original handler', async () => {
Expand Down Expand Up @@ -445,12 +447,12 @@ describe('AWSLambda', () => {
};

// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expect(SentryNode.fakeHub.startTransaction).toBeCalledWith(fakeTransactionContext);
expectScopeSettings(fakeTransactionContext);
expect(Sentry.captureException).toBeCalledWith(error);
expect(SentryNode.captureException).toBeCalledWith(error);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
expect(SentryNode.fakeTransaction.finish).toBeCalled();
expect(SentryNode.flush).toBeCalled();
}
});
});
Expand Down
12 changes: 6 additions & 6 deletions packages/serverless/test/awsservices.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as SentryNode from '@sentry/node';
import * as AWS from 'aws-sdk';
import * as nock from 'nock';

import * as Sentry from '../src';
import { AWSServices } from '../src/awsservices';

/**
Expand All @@ -17,7 +17,7 @@ describe('AWSServices', () => {
});
afterEach(() => {
// @ts-ignore see "Why @ts-ignore" note
Sentry.resetMocks();
SentryNode.resetMocks();
});
afterAll(() => {
nock.restore();
Expand All @@ -31,12 +31,12 @@ describe('AWSServices', () => {
const data = await s3.getObject({ Bucket: 'foo', Key: 'bar' }).promise();
expect(data.Body?.toString('utf-8')).toEqual('contents');
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
op: 'http.client',
description: 'aws.s3.getObject foo',
});
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeSpan.finish).toBeCalled();
expect(SentryNode.fakeSpan.finish).toBeCalled();
});

test('getObject with callback', done => {
Expand All @@ -48,7 +48,7 @@ describe('AWSServices', () => {
done();
});
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
op: 'http.client',
description: 'aws.s3.getObject foo',
});
Expand All @@ -63,7 +63,7 @@ describe('AWSServices', () => {
const data = await lambda.invoke({ FunctionName: 'foo' }).promise();
expect(data.Payload?.toString('utf-8')).toEqual('reply');
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.startChild).toBeCalledWith({
expect(SentryNode.fakeTransaction.startChild).toBeCalledWith({
op: 'http.client',
description: 'aws.lambda.invoke foo',
});
Expand Down
Loading