Skip to content
Closed
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 @@ -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, receiver) as unknown;

return typeof value === 'function' ? value.bind(target) : value;
},
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Queue } from '@cloudflare/workers-types';
import type { Queue, QueueMetrics } from '@cloudflare/workers-types';
import * as SentryCore from '@sentry/core';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { instrumentQueueProducer } from '../../../src/instrumentations/worker/instrumentQueueProducer';
Expand All @@ -10,6 +10,15 @@ function createMockQueue(): Queue {
} as unknown as Queue;
}

// `metrics()` reads a private field, so it throws when `this` is the Proxy, as workerd's native methods do.
class BrandCheckedQueue {
readonly #metrics: QueueMetrics = { backlogCount: 3, backlogBytes: 2048 };

public metrics(): Promise<QueueMetrics> {
return Promise.resolve(this.#metrics);
}
}

describe('instrumentQueueProducer', () => {
beforeEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -178,4 +187,13 @@ describe('instrumentQueueProducer', () => {
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE') as Queue & { customMethod: () => string };
expect(wrapped.customMethod()).toBe('hi');
});

test('calls non-instrumented methods on the underlying queue', async () => {
const queue = new BrandCheckedQueue() as unknown as Queue;
const wrapped = instrumentQueueProducer(queue, 'MY_QUEUE');

const metrics = await wrapped.metrics();

expect(metrics).toEqual({ backlogCount: 3, backlogBytes: 2048 });
});
});