Skip to content

Commit 58bb050

Browse files
committed
only increment on anonymous middleware
1 parent a09b775 commit 58bb050

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

packages/hono/src/shared/patchAppUse.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export function patchAppUse(app: Hono): void {
2222
apply(target: typeof app.use, thisArg: typeof app, args: Parameters<typeof app.use>): ReturnType<typeof app.use> {
2323
const [first, ...rest] = args as [unknown, ...MiddlewareHandler[]];
2424

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

3032
const allHandlers = [first as MiddlewareHandler, ...rest].map(handler =>
31-
wrapMiddlewareWithSpan(handler, MIDDLEWARE_IDX++),
33+
wrapMiddlewareWithSpan(handler, getSpanName(handler)),
3234
);
3335
return Reflect.apply(target, thisArg, allHandlers);
3436
},
@@ -40,9 +42,7 @@ export function patchAppUse(app: Hono): void {
4042
* Uses startInactiveSpan so that all middleware spans are siblings under the request/transaction
4143
* (onion order: A → B → handler → B → A does not nest B under A in the trace).
4244
*/
43-
function wrapMiddlewareWithSpan(handler: MiddlewareHandler, index: number): MiddlewareHandler {
44-
const spanName = handler.name || `<anonymous.${index}>`;
45-
45+
function wrapMiddlewareWithSpan(handler: MiddlewareHandler, spanName: string): MiddlewareHandler {
4646
return async function sentryTracedMiddleware(context, next) {
4747
const span = startInactiveSpan({
4848
name: spanName,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,29 @@ 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+
115118
const app = new Hono();
116119
patchAppUse(app);
117120

118121
app.use(
119122
async (_c: unknown, next: () => Promise<void>) => next(),
123+
async function namedMiddleware(_c: unknown, next: () => Promise<void>) {
124+
await next();
125+
},
120126
async (_c: unknown, next: () => Promise<void>) => next(),
121127
);
122128

123129
await app.fetch(new Request('http://localhost/'));
124130

125-
expect(startInactiveSpanMock).toHaveBeenCalledTimes(2);
126-
const [firstCall, secondCall] = startInactiveSpanMock.mock.calls;
131+
expect(startInactiveSpanMock).toHaveBeenCalledTimes(3);
132+
const [firstCall, secondCall, thirdCall] = startInactiveSpanMock.mock.calls;
127133
expect(firstCall[0]).toMatchObject({ op: 'middleware.hono' });
128134
expect(secondCall[0]).toMatchObject({ op: 'middleware.hono' });
129-
expect(firstCall[0].name).toMatch(/^<anonymous\.\d+>$/);
130-
expect(secondCall[0].name).toMatch(/^<anonymous\.\d+>$/);
135+
expect(firstCall[0].name).toMatch('<anonymous.0>');
136+
expect(secondCall[0].name).toBe('namedMiddleware');
137+
expect(thirdCall[0].name).toBe('<anonymous.1>');
131138
expect(firstCall[0].name).not.toBe(secondCall[0].name);
132139
});
133140

0 commit comments

Comments
 (0)