Skip to content

Commit d4d646b

Browse files
committed
fixup! feat(cloudflare): Support tracing for queue producer
1 parent b9b0533 commit d4d646b

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

packages/cloudflare/src/instrumentations/worker/instrumentQueueProducer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ export function instrumentQueueProducer<T extends Queue>(queue: T, bindingName:
8484
options?: QueueSendBatchOptions,
8585
): Promise<void> {
8686
const messageArray = Array.from(messages);
87-
const totalBodySize = messageArray.reduce<number>((acc, m) => {
87+
const totalBodySize = messageArray.reduce<number | undefined>((acc, m) => {
8888
const size = getBodySize(m.body);
89-
return size === undefined ? acc : acc + size;
90-
}, 0);
89+
if (size === undefined) {
90+
return acc;
91+
}
92+
return (acc ?? 0) + size;
93+
}, undefined);
9194

9295
return startPublishSpan({ bindingName, bodySize: totalBodySize, messageCount: messageArray.length }, () =>
9396
Reflect.apply(original, target, [messageArray, options]),

packages/cloudflare/test/instrumentations/worker/instrumentQueueProducer.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ describe('instrumentQueueProducer', () => {
139139
expect(Array.isArray(passed)).toBe(true);
140140
expect(passed).toHaveLength(2);
141141
});
142+
143+
test('omits body size when all payloads cannot be serialized', async () => {
144+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan');
145+
const queue = createMockQueue();
146+
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE');
147+
148+
const circular1: Record<string, unknown> = {};
149+
circular1.self = circular1;
150+
const circular2: Record<string, unknown> = {};
151+
circular2.self = circular2;
152+
153+
await wrapped.sendBatch([{ body: circular1 }, { body: circular2 }]);
154+
155+
const attrs = startSpanSpy.mock.calls[0]![0].attributes!;
156+
expect(attrs['messaging.message.body.size']).toBeUndefined();
157+
});
158+
159+
test('sums only sizable bodies when batch contains mixed payloads', async () => {
160+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan');
161+
const queue = createMockQueue();
162+
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE');
163+
164+
const circular: Record<string, unknown> = {};
165+
circular.self = circular;
166+
167+
await wrapped.sendBatch([{ body: 'aa' }, { body: circular }, { body: 'bbb' }]);
168+
169+
const attrs = startSpanSpy.mock.calls[0]![0].attributes!;
170+
expect(attrs['messaging.message.body.size']).toBe(5);
171+
});
142172
});
143173

144174
test('forwards unknown property accesses transparently', () => {

0 commit comments

Comments
 (0)