Skip to content
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
7 changes: 7 additions & 0 deletions dev-packages/e2e-tests/test-applications/fastify-5/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"imports": {
"@sentry/deno/import": "./node_modules/@sentry/deno/build/import.mjs"
},
"nodeModulesDir": "manual",
"unstable": ["sloppy-imports"]
}
41 changes: 41 additions & 0 deletions dev-packages/e2e-tests/test-applications/fastify-5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "fastify-5",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "tsc",
"start": "node dist/main.js",
"start:bun": "bun dist/main.js",
"start:deno": "deno run -A --preload=@sentry/deno/import --preload=./src/instrument.ts src/main.ts",
"test": "playwright test",
"clean": "npx rimraf node_modules pnpm-lock.yaml",
"test:build": "pnpm install && pnpm build",
"test:assert": "pnpm test"
},
"dependencies": {
"@sentry/bun": "file:../../packed/sentry-bun-packed.tgz",
"@sentry/deno": "file:../../packed/sentry-deno-packed.tgz",
"@sentry/node": "file:../../packed/sentry-node-packed.tgz",
"@types/node": "^18.19.1",
"fastify": "^5.7.0",
"typescript": "5.6.3"
},
"devDependencies": {
"@playwright/test": "1.64.1",
"@sentry-internal/test-utils": "file:../../packed/sentry-internal-test-utils-packed.tgz",
"@sentry/core": "file:../../packed/sentry-core-packed.tgz"
},
"sentryTest": {
"optionalVariants": [
{
"runtime": "bun",
"bun-version": "1.4.5",
"label": "fastify-5 (bun)"
},

Check warning on line 34 in dev-packages/e2e-tests/test-applications/fastify-5/package.json

View check run for this annotation

@sentry/warden / warden: code-review

Bun optional variant fails on http.response.status_text assertions

Enabling the bun optional variant will fail spans/propagation tests that require `http.response.status_text: OK`. Bun leaves `statusMessage` unset on `node:http` responses; gate that attribute with `RUNTIME === 'bun'` like express-5 and nestjs-12.
Comment on lines +30 to +34

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bun optional variant fails on http.response.status_text assertions

Enabling the bun optional variant will fail spans/propagation tests that require http.response.status_text: OK. Bun leaves statusMessage unset on node:http responses; gate that attribute with RUNTIME === 'bun' like express-5 and nestjs-12.

Evidence
  • optionalVariants adds runtime: "bun" (with start:bun → bun dist/main.js), so CI runs the existing Playwright suite under Bun.
  • HTTP server spans set http.response.status_text only from response.statusMessage?.toUpperCase() (packages/core/src/integrations/http/server-subscription.ts).
  • express-5 and nestjs-12 document Bun leaving statusMessage unset and omit the attribute when RUNTIME === 'bun'.
  • fastify-5 tests/spans.test.ts and tests/propagation.test.ts still require 'http.response.status_text': { value: 'OK', type: 'string' } with no Bun branch, so those expects fail under the new variant.

Identified by Warden · code-review · L8N-WT2

{
"runtime": "deno",
"label": "fastify-5 (deno)"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getPlaywrightConfig, getRuntime } from '@sentry-internal/test-utils';

const startCommands = {
node: 'pnpm start',
bun: 'pnpm start:bun',
deno: 'pnpm start:deno',
};

const config = getPlaywrightConfig({
startCommand: startCommands[getRuntime()],
});

export default config;
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
import type * as S from '@sentry/node';
const Sentry = require('@sentry/node') as typeof S;

// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}

return target.apply(thisArg, argumentsList);
},
});

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [
Sentry.fastifyIntegration({
shouldHandleError: (_error, request, _reply) => {
if (request.routeOptions?.url?.includes('/test-error-not-captured')) {
// Errors from this path will not be captured by Sentry
return false;
}

if (request.routeOptions?.url?.includes('/test-error-ignored') && _reply.statusCode === 500) {
return false;
}

return true;
},
}),
],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});

import type * as H from 'http';
import type * as F from 'fastify';

// Make sure fastify is imported after Sentry is initialized
const { fastify } = require('fastify') as typeof F;
const http = require('http') as typeof H;

const app = fastify();
const port = 3030;
import type { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';
import { fastify } from 'fastify';
import * as http from 'node:http';
import * as Sentry from './sentry';

export const app = fastify();
const port2 = 3040;

app.get('/test-success', function (_req, res) {
Expand Down Expand Up @@ -97,8 +54,8 @@ app.get('/test-error', async function (req, res) {
// Regression test for https://github.com/fastify/fastify/issues/6409
// The error diagnostic channel was always sending 200 unless explicitly changed.
// This was fixed in Fastify 5.7.0
app.register((childApp: F.FastifyInstance, _options: F.FastifyPluginOptions, next: (err?: Error) => void) => {
childApp.setErrorHandler((error: Error, _request: F.FastifyRequest, reply: F.FastifyReply) => {
app.register((childApp: FastifyInstance, _options: FastifyPluginOptions, next: (err?: Error) => void) => {
childApp.setErrorHandler((error: Error, _request: FastifyRequest, reply: FastifyReply) => {
reply.send({ ok: false });
});

Expand Down Expand Up @@ -142,6 +99,19 @@ app.get('/test-outgoing-http-external-disallowed', async function (req, res) {
res.send(data);
});

app.get('/test-log', function (_req, res) {
Sentry.logger.debug('Accessed /test-log route');
res.send({ message: 'Log sent' });
});

app.get<{ Params: { id: string } }>('/test-isolation/:id', async function (req, res) {
Sentry.setTag('isolation-id', req.params.id);
// Lets the concurrent requests of the isolation test overlap.
await new Promise(resolve => setTimeout(resolve, 200));
Sentry.captureException(new Error(`Isolated error ${req.params.id}`));
res.send({});
});

app.post('/test-post', function (req, res) {
res.send({ status: 'ok', body: req.body });
});
Expand All @@ -151,10 +121,8 @@ app.get('/flush', async function (_req, res) {
res.send({ ok: true });
});

app.listen({ port: port });

// A second app so we can test header propagation between external URLs
const app2 = fastify();
export const app2 = fastify();
app2.get('/external-allowed', function (req, res) {
const headers = req.headers;

Expand All @@ -167,8 +135,6 @@ app2.get('/external-disallowed', function (req, res) {
res.send({ headers, route: '/external-disallowed' });
});

app2.listen({ port: port2 });

function makeHttpRequest(url: string) {
return new Promise(resolve => {
const data: any[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/bun';
import { shouldHandleError } from './shouldHandleError';

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [Sentry.fastifyIntegration({ shouldHandleError })],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/deno';
import { shouldHandleError } from './shouldHandleError';

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [Sentry.fastifyIntegration({ shouldHandleError })],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as Sentry from '@sentry/node';
import { shouldHandleError } from './shouldHandleError';

// We wrap console.warn to find out if a warning is incorrectly logged
console.warn = new Proxy(console.warn, {
apply: function (target, thisArg, argumentsList) {
const msg = argumentsList[0];
if (typeof msg === 'string' && msg.startsWith('[Sentry]')) {
console.error(`Sentry warning was triggered: ${msg}`);
process.exit(1);
}

return target.apply(thisArg, argumentsList);
},
});

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: process.env.E2E_TEST_DSN,
integrations: [Sentry.fastifyIntegration({ shouldHandleError })],
tracesSampleRate: 1,
tunnel: 'http://localhost:3031/', // proxy server
tracePropagationTargets: ['http://localhost:3030', '/external-allowed'],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './instrument';

import { app, app2 } from './app';

app.listen({ port: 3030 });
app2.listen({ port: 3040 });
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@sentry/bun';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@sentry/deno';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// The app imports the Sentry API from this file. Each other runtime has a `sentry.<runtime>.ts` with its own SDK.
export * from '@sentry/node';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { fastifyIntegration } from './sentry';

type ShouldHandleError = NonNullable<NonNullable<Parameters<typeof fastifyIntegration>[0]>['shouldHandleError']>;

export const shouldHandleError: ShouldHandleError = (_error, request, reply) => {
if (request.routeOptions?.url?.includes('/test-error-not-captured')) {
return false;
}

if (request.routeOptions?.url?.includes('/test-error-ignored') && reply.statusCode === 500) {
return false;
}

return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'node-fastify-5',
proxyServerName: 'fastify-5',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getRuntime } from '@sentry-internal/test-utils';

export const RUNTIME = getRuntime();
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { expect, test } from '@playwright/test';
import { waitForError, waitForStreamedSpan, collectStreamedSpansUntilSegment } from '@sentry-internal/test-utils';
import { RUNTIME } from './constants';

test('Sends correct error event', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-fastify-5', event => {
const errorEventPromise = waitForError('fastify-5', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

const segmentEventPromise = collectStreamedSpansUntilSegment('node-fastify-5', 'GET /test-exception/:id');
const segmentEventPromise = collectStreamedSpansUntilSegment('fastify-5', 'GET /test-exception/:id');

await fetch(`${baseURL}/test-exception/123`);

Expand Down Expand Up @@ -56,15 +57,15 @@ test('Sends correct error event', async ({ baseURL }) => {
test('Does not send error when shouldHandleError returns false', async ({ baseURL }) => {
let errorEventOccurred = false;

waitForError('node-fastify-5', event => {
waitForError('fastify-5', event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured') {
errorEventOccurred = true;
}
return event?.transaction === 'GET /test-error-not-captured';
});

const segmentEventPromise = waitForStreamedSpan(
'node-fastify-5',
'fastify-5',
segment => segment.is_segment && segment.name === 'GET /test-error-not-captured',
);

Expand All @@ -83,17 +84,21 @@ test('Does not send error when shouldHandleError returns false', async ({ baseUR
// The error diagnostic channel was always sending 200 unless explicitly changed.
// This was fixed in Fastify 5.7.0
test('Error in child plugin with rethrown error handler reports correct 500 status', async ({ baseURL }) => {
// Fastify sets the 500 status only when its `fastify.request.handler` tracing channel has subscribers. Bun up to
// 1.4.5 has no `TracingChannel#hasSubscribers` (oven-sh/bun#49415), so Fastify skips the channel and sends 200.
test.fail(RUNTIME === 'bun');

let errorEventOccurred = false;

waitForError('node-fastify-5', event => {
waitForError('fastify-5', event => {
if (!event.type && event.exception?.values?.[0]?.value === 'This is an error that will not be captured') {
errorEventOccurred = true;
}
return event?.transaction === 'GET /test-error-ignored';
});

const segmentEventPromise = waitForStreamedSpan(
'node-fastify-5',
'fastify-5',
segment => segment.is_segment && segment.name === 'GET /test-error-ignored',
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from '@playwright/test';
import { collectStreamedSpansUntilSegment, waitForEnvelopeItem } from '@sentry-internal/test-utils';
import type { SerializedLogContainer } from '@sentry/core';

test('Sends logs with the trace of the request', async ({ baseURL }) => {
const logEnvelopePromise = waitForEnvelopeItem('fastify-5', envelope => {
return envelope[0].type === 'log' && (envelope[1] as SerializedLogContainer).items[0]?.level === 'debug';
});
const spansPromise = collectStreamedSpansUntilSegment('fastify-5', 'GET /test-log');

await fetch(`${baseURL}/test-log`);

const logEnvelope = await logEnvelopePromise;
const log = (logEnvelope[1] as SerializedLogContainer).items[0];
expect(log?.level).toBe('debug');
expect(log?.body).toBe('Accessed /test-log route');

const segmentSpan = (await spansPromise).find(span => span.is_segment)!;
expect(log?.trace_id).toBe(segmentSpan.trace_id);
});
Loading
Loading