Skip to content

Commit 0e032d6

Browse files
committed
remove incremental naming of middlewares
1 parent 0f39821 commit 0e032d6

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

packages/hono/src/shared/patchAppUse.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import type { Hono, MiddlewareHandler } from 'hono';
1010

1111
const MIDDLEWARE_ORIGIN = 'auto.middleware.hono';
1212

13-
// Module-level counter for anonymous middleware span names
14-
let MIDDLEWARE_IDX = 0;
15-
1613
/**
1714
* Patches `app.use` so that every middleware registered through it is automatically
1815
* wrapped in a Sentry span. Supports both forms: `app.use(...handlers)` and `app.use(path, ...handlers)`.
@@ -22,16 +19,12 @@ export function patchAppUse(app: Hono): void {
2219
apply(target: typeof app.use, thisArg: typeof app, args: Parameters<typeof app.use>): ReturnType<typeof app.use> {
2320
const [first, ...rest] = args as [unknown, ...MiddlewareHandler[]];
2421

25-
const getSpanName = (handler: MiddlewareHandler): string => handler.name || `<anonymous.${MIDDLEWARE_IDX++}>`;
26-
2722
if (typeof first === 'string') {
28-
const wrappedHandlers = rest.map(handler => wrapMiddlewareWithSpan(handler, getSpanName(handler)));
23+
const wrappedHandlers = rest.map(handler => wrapMiddlewareWithSpan(handler));
2924
return Reflect.apply(target, thisArg, [first, ...wrappedHandlers]);
3025
}
3126

32-
const allHandlers = [first as MiddlewareHandler, ...rest].map(handler =>
33-
wrapMiddlewareWithSpan(handler, getSpanName(handler)),
34-
);
27+
const allHandlers = [first as MiddlewareHandler, ...rest].map(handler => wrapMiddlewareWithSpan(handler));
3528
return Reflect.apply(target, thisArg, allHandlers);
3629
},
3730
});
@@ -42,10 +35,10 @@ export function patchAppUse(app: Hono): void {
4235
* Uses startInactiveSpan so that all middleware spans are siblings under the request/transaction
4336
* (onion order: A → B → handler → B → A does not nest B under A in the trace).
4437
*/
45-
function wrapMiddlewareWithSpan(handler: MiddlewareHandler, spanName: string): MiddlewareHandler {
38+
function wrapMiddlewareWithSpan(handler: MiddlewareHandler): MiddlewareHandler {
4639
return async function sentryTracedMiddleware(context, next) {
4740
const span = startInactiveSpan({
48-
name: spanName,
41+
name: handler.name || '<anonymous>',
4942
op: 'middleware.hono',
5043
onlyIfParent: true,
5144
attributes: {

packages/hono/test/shared/patchAppUse.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('patchAppUse (middleware spans)', () => {
7777

7878
expect(startInactiveSpanMock).toHaveBeenCalledTimes(1);
7979
const name = startInactiveSpanMock.mock.calls[0][0].name;
80-
expect(name).toMatch(/^<anonymous\.\d+>$/);
80+
expect(name).toMatch('<anonymous>');
8181
});
8282
});
8383

@@ -112,9 +112,6 @@ describe('patchAppUse (middleware spans)', () => {
112112
});
113113

114114
it('creates sibling spans for multiple middlewares (onion order, not parent-child)', async () => {
115-
vi.resetModules(); // resets the module-level counter variable
116-
const { patchAppUse } = await import('../../src/shared/patchAppUse');
117-
118115
const app = new Hono();
119116
patchAppUse(app);
120117

@@ -132,9 +129,9 @@ describe('patchAppUse (middleware spans)', () => {
132129
const [firstCall, secondCall, thirdCall] = startInactiveSpanMock.mock.calls;
133130
expect(firstCall[0]).toMatchObject({ op: 'middleware.hono' });
134131
expect(secondCall[0]).toMatchObject({ op: 'middleware.hono' });
135-
expect(firstCall[0].name).toMatch('<anonymous.0>');
132+
expect(firstCall[0].name).toMatch('<anonymous>');
136133
expect(secondCall[0].name).toBe('namedMiddleware');
137-
expect(thirdCall[0].name).toBe('<anonymous.1>');
134+
expect(thirdCall[0].name).toBe('<anonymous>');
138135
expect(firstCall[0].name).not.toBe(secondCall[0].name);
139136
});
140137

0 commit comments

Comments
 (0)