Skip to content

ref(node): Avoid double wrapping http module for vercel on Node #16178

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

process.env.VERCEL = 'true';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
transport: loggingTransport,
// We look at debug logs in this test
debug: true,
integrations: [
Sentry.httpIntegration({
spans: false,
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Sentry from '@sentry/node';
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
import express from 'express';

const app = express();

app.get('/test/error', () => {
throw new Error('test error');
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ app.get('/test/express', (_req, res) => {
res.send({ response: 'response 1' });
});

app.get('/test/error', () => {
throw new Error('test error');
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
133 changes: 130 additions & 3 deletions dev-packages/node-integration-tests/suites/vercel/test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../utils/runner';

describe('vercel xxx', () => {
describe('vercel', () => {
afterAll(() => {
cleanupChildProcesses();
});

createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
test('should flush events correctly on Vercel', async () => {
test('should flush spans correctly on Vercel', async () => {
const runner = createRunner()
.expect({
transaction: {
Expand Down Expand Up @@ -47,7 +47,134 @@ describe('vercel xxx', () => {
}
}

expect(expectedLogs).toEqual([]);
if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});

test('should flush errors correctly on Vercel', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: 'GET /test/error',
},
})
.expect({
event: {
transaction: 'GET /test/error',
exception: {
values: [
{
value: 'test error',
},
],
},
},
})
.start();
runner.makeRequest('get', '/test/error', { expectError: true });
await runner.completed();

const actualLogs = runner.getLogs();

// We want to test that the following logs are present in this order
// other logs may be in between
const expectedLogs = [
'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit',
'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request',
'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on',
'Sentry Logger [debug]: @opentelemetry_sentry-patched/instrumentation-http http instrumentation incomingRequest',
'Sentry Logger [log]: [Tracing] Starting sampled root span',
// later...
'Sentry Logger [log]: Patching response to flush on Vercel',
'Sentry Logger [log]: Patching res.end()',
// later...
'Sentry Logger [log]: Captured error event `test error`',
// later...
'Sentry Logger [log]: Flushing events before Vercel Lambda freeze',
'Sentry Logger [log]: SpanExporter exported 4 spans, 0 spans are waiting for their parent spans to finish',
];

// Test that the order of logs is correct
for (const log of actualLogs) {
if (expectedLogs.length === 0) {
break;
}

if (log === expectedLogs[0]) {
expectedLogs.shift();
}
}

if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});
});

describe('without http.server spans', () => {
createEsmAndCjsTests(
__dirname,
'scenario-withoutSpans.mjs',
'instrument-withoutSpans.mjs',
(createRunner, test) => {
test('should flush errors correctly on Vercel even without HTTP span instrumentation', async () => {
const runner = createRunner()
.expect({
event: {
transaction: 'GET /test/error',
exception: {
values: [
{
value: 'test error',
},
],
},
},
})
.start();
runner.makeRequest('get', '/test/error', { expectError: true });
await runner.completed();

const actualLogs = runner.getLogs();

// We want to test that the following logs are present in this order
// other logs may be in between
const expectedLogs = [
'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit',
'Sentry Logger [log]: Patching response to flush on Vercel',
'Sentry Logger [log]: Patching res.end()',
'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request',
'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on',
// later...
'Sentry Logger [log]: Captured error event `test error`',
// later...
'Sentry Logger [log]: Flushing events before Vercel Lambda freeze',
'Sentry Logger [log]: SpanExporter exported 0 spans, 0 spans are waiting for their parent spans to finish',
];

// Test that the order of logs is correct
for (const log of actualLogs) {
if (expectedLogs.length === 0) {
break;
}

if (log === expectedLogs[0]) {
expectedLogs.shift();
}
}

if (expectedLogs.length > 0) {
// eslint-disable-next-line no-console
console.log(actualLogs);
expect(expectedLogs).toEqual([]);
}
});
},
);
});
});
Loading
Loading