Skip to content

feature(node): Add instrumentation to the handler in Hono #17428

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

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.552.0",
"@hapi/hapi": "^21.3.10",
"@hono/node-server": "^1.18.2",
"@nestjs/common": "11.1.3",
"@nestjs/core": "11.1.3",
"@nestjs/platform-express": "11.1.3",
Expand All @@ -45,6 +46,7 @@
"express": "^4.21.1",
"generic-pool": "^3.9.0",
"graphql": "^16.3.0",
"hono": "^4.8.12",
"http-terminator": "^3.2.0",
"ioredis": "^5.4.1",
"kafkajs": "2.2.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});
163 changes: 163 additions & 0 deletions dev-packages/node-integration-tests/suites/tracing/hono/scenario.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { serve } from '@hono/node-server';
import * as Sentry from '@sentry/node';
import { sendPortToRunner } from '@sentry-internal/node-core-integration-tests';
import { Hono } from 'hono';
import { HTTPException } from 'hono/http-exception';

const app = new Hono();

Sentry.setupHonoErrorHandler(app);

// Global middleware to capture all requests
app.use(async function global(c, next) {
await next();
});

const basePaths = ['/sync', '/async'];
const methods = ['get', 'post', 'put', 'delete', 'patch'];

basePaths.forEach(basePath => {
// Sub-path middleware to capture all requests under the basePath
app.use(`${basePath}/*`, async function base(c, next) {
await next();
});

const baseApp = new Hono();
methods.forEach(method => {
baseApp[method]('/', c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
});

baseApp[method](
'/middleware',
// anonymous middleware
async (c, next) => {
await next();
},
c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
},
);

baseApp.all('/all', c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
});

baseApp.all(
'/all/middleware',
// anonymous middleware
async (c, next) => {
await next();
},
c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
},
);

baseApp.on(method, '/on', c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
});

baseApp.on(
method,
'/on/middleware',
// anonymous middleware
async (c, next) => {
await next();
},
c => {
const response = c.text('response 200');
if (basePath === '/sync') return response;
return Promise.resolve(response);
},
);

baseApp[method]('/401', () => {
const response = new HTTPException(401, { message: 'response 401' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.all('/all/401', () => {
const response = new HTTPException(401, { message: 'response 401' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.on(method, '/on/401', () => {
const response = new HTTPException(401, { message: 'response 401' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp[method]('/402', () => {
const response = new HTTPException(402, { message: 'response 402' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.all('/all/402', () => {
const response = new HTTPException(402, { message: 'response 402' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.on(method, '/on/402', () => {
const response = new HTTPException(402, { message: 'response 402' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp[method]('/403', () => {
const response = new HTTPException(403, { message: 'response 403' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.all('/all/403', () => {
const response = new HTTPException(403, { message: 'response 403' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.on(method, '/on/403', () => {
const response = new HTTPException(403, { message: 'response 403' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp[method]('/500', () => {
const response = new HTTPException(500, { message: 'response 500' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.all('/all/500', () => {
const response = new HTTPException(500, { message: 'response 500' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});

baseApp.on(method, '/on/500', () => {
const response = new HTTPException(500, { message: 'response 500' });
if (basePath === '/sync') throw response;
return Promise.reject(response);
});
});

app.route(basePath, baseApp);
});

const port = 8787;
serve({ fetch: app.fetch, port });
sendPortToRunner(port);
179 changes: 179 additions & 0 deletions dev-packages/node-integration-tests/suites/tracing/hono/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { afterAll, describe, expect } from 'vitest';
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';

describe('hono tracing', () => {
afterAll(() => {
cleanupChildProcesses();
});

createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
describe.each(['/sync', '/async'] as const)('when using %s route', route => {
describe.each(['get', 'post', 'put', 'delete', 'patch'] as const)('when using %s method', method => {
describe.each(['/', '/all', '/on'])('when using %s path', path => {
test('should handle transaction', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: `${method.toUpperCase()} ${route}${path === '/' ? '' : path}`,
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'sentryRequestMiddleware',
'hono.type': 'middleware',
}),
description: 'sentryRequestMiddleware',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'sentryErrorMiddleware',
'hono.type': 'middleware',
}),
description: 'sentryErrorMiddleware',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'global',
'hono.type': 'middleware',
}),
description: 'global',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'base',
'hono.type': 'middleware',
}),
description: 'base',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': `${route}${path === '/' ? '' : path}`,
'hono.type': 'request_handler',
}),
description: `${route}${path === '/' ? '' : path}`,
op: 'request_handler.hono',
origin: 'auto.http.otel.hono',
}),
]),
},
})
.start();
runner.makeRequest(method, `${route}${path === '/' ? '' : path}`);
await runner.completed();
});

test('should handle transaction with anonymous middleware', async () => {
const runner = createRunner()
.expect({
transaction: {
transaction: `${method.toUpperCase()} ${route}${path === '/' ? '' : path}/middleware`,
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'sentryRequestMiddleware',
'hono.type': 'middleware',
}),
description: 'sentryRequestMiddleware',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'sentryErrorMiddleware',
'hono.type': 'middleware',
}),
description: 'sentryErrorMiddleware',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'global',
'hono.type': 'middleware',
}),
description: 'global',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'base',
'hono.type': 'middleware',
}),
description: 'base',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': 'anonymous',
'hono.type': 'middleware',
}),
description: 'anonymous',
op: 'middleware.hono',
origin: 'auto.http.otel.hono',
}),
expect.objectContaining({
data: expect.objectContaining({
'hono.name': `${route}${path === '/' ? '' : path}/middleware`,
'hono.type': 'request_handler',
}),
description: `${route}${path === '/' ? '' : path}/middleware`,
op: 'request_handler.hono',
origin: 'auto.http.otel.hono',
}),
]),
},
})
.start();
runner.makeRequest(method, `${route}${path === '/' ? '' : path}/middleware`);
await runner.completed();
});

test('should handle returned errors for %s path', async () => {
const runner = createRunner()
.ignore('transaction')
.expect({
event: {
exception: {
values: [
{
type: 'Error',
value: 'response 500',
},
],
},
},
})
.start();
runner.makeRequest(method, `${route}${path === '/' ? '' : path}/500`, { expectError: true });
await runner.completed();
});

test.each(['/401', '/402', '/403', '/does-not-exist'])(
'should ignores error %s path by default',
async (subPath: string) => {
const runner = createRunner()
.expect({
transaction: {
transaction: `${method.toUpperCase()} ${route}`,
},
})
.start();
runner.makeRequest(method, `${route}${path === '/' ? '' : path}${subPath}`, { expectError: true });
runner.makeRequest(method, route);
await runner.completed();
},
);
});
});
});
});
});
6 changes: 3 additions & 3 deletions dev-packages/node-integration-tests/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type StartResult = {
childHasExited(): boolean;
getLogs(): string[];
makeRequest<T>(
method: 'get' | 'post',
method: 'get' | 'post' | 'put' | 'delete' | 'patch',
path: string,
options?: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean },
): Promise<T | undefined>;
Expand Down Expand Up @@ -544,7 +544,7 @@ export function createRunner(...paths: string[]) {
return logs;
},
makeRequest: async function <T>(
method: 'get' | 'post',
method: 'get' | 'post' | 'put' | 'delete' | 'patch',
path: string,
options: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean } = {},
): Promise<T | undefined> {
Expand All @@ -563,7 +563,7 @@ export function createRunner(...paths: string[]) {
if (process.env.DEBUG) log('making request', method, url, headers, body);

try {
const res = await fetch(url, { headers, method, body });
const res = await fetch(url, { headers, method: method.toUpperCase(), body });

if (!res.ok) {
if (!expectError) {
Expand Down
Loading