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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default Sentry.withSentry(
return new Response('enqueued batch');
}

if (url.pathname === '/metrics') {
return Response.json(await env.MY_QUEUE.metrics());
}

return new Response('not found', { status: 404 });
},
async queue(batch: MessageBatch<{ trigger?: 'error'; payload?: string }>) {
Expand Down
16 changes: 16 additions & 0 deletions dev-packages/cloudflare-integration-tests/suites/queue/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ it('emits a queue.publish span with batch attributes on env.MY_QUEUE.sendBatch',
await runner.makeRequest('post', '/enqueue/batch');
await runner.completed();
});

it('calls non-instrumented Queue methods like metrics() on the underlying binding', async ({ signal }) => {
const runner = createRunner(__dirname)
.expect((envelope: Envelope) => {
expect(envelopeItemType(envelope)).toBe('transaction');
expect(envelopeItem(envelope)).toMatchObject({
transaction: 'GET /metrics',
contexts: { trace: { status: 'ok' } },
});
})
.start(signal);

const metrics = await runner.makeRequest('get', '/metrics');
expect(metrics).toMatchObject({ backlogCount: expect.any(Number), backlogBytes: expect.any(Number) });
await runner.completed();
});
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export function instrumentQueueProducer<T extends Queue>(queue: T, bindingName:
};
}

return Reflect.get(target, prop, receiver);
const value = Reflect.get(target, prop) as unknown;

return typeof value === 'function' ? value.bind(target) : value;
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,19 @@ describe('instrumentQueueProducer', () => {
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE') as Queue & { customMethod: () => string };
expect(wrapped.customMethod()).toBe('hi');
});

test('calls pass-through methods with the underlying queue as `this`', async () => {
const metrics = { backlogCount: 3, backlogBytes: 42 };
const queue = Object.assign(createMockQueue(), {
metrics(this: unknown) {
if (this !== queue) {
throw new TypeError('Illegal invocation: function called with incorrect `this` reference.');
}
return Promise.resolve(metrics);
},
}) as unknown as Queue & { metrics: () => Promise<typeof metrics> };
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE') as typeof queue;

await expect(wrapped.metrics()).resolves.toEqual(metrics);
});
});
Loading