-
Notifications
You must be signed in to change notification settings - Fork 621
MQ-1154 Add metrics() method to Queue binding #6246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) 2023 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| import assert from 'node:assert'; | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx) { | ||
| const { pathname } = new URL(request.url); | ||
| if (pathname === '/metrics') { | ||
| assert.strictEqual(request.method, 'GET'); | ||
| return Response.json({ | ||
| backlogCount: 100, | ||
| backlogBytes: 2048, | ||
| oldestMessageTimestamp: 1000000, | ||
| }); | ||
| } | ||
| return new Response(); | ||
| }, | ||
|
|
||
| async test(ctrl, env, ctx) { | ||
| const metricsEnabled = env.METRICS_FLAG; | ||
| if (metricsEnabled) { | ||
| // Flag ON → metrics() should exist and return data | ||
| assert.strictEqual(typeof env.QUEUE.metrics, 'function'); | ||
| const metrics = await env.QUEUE.metrics(); | ||
| assert.strictEqual(metrics.backlogCount, 100); | ||
| assert.strictEqual(metrics.backlogBytes, 2048); | ||
| assert.strictEqual(metrics.oldestMessageTimestamp, 1000000); | ||
| } else { | ||
| // Flag OFF → metrics() should not be exposed on the binding | ||
| assert.strictEqual(typeof env.QUEUE.metrics, 'undefined'); | ||
| } | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const unitTests :Workerd.Config = ( | ||
| services = [ | ||
| ( name = "queue-metrics-enabled", | ||
| worker = ( | ||
| modules = [ | ||
| ( name = "worker-metrics-enabled", esModule = embed "queue-metrics-test.js" ) | ||
| ], | ||
| bindings = [ | ||
| ( name = "QUEUE", queue = "queue-metrics-enabled" ), | ||
| ( name = "METRICS_FLAG", json = "true" ), | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat", "experimental", "capture_async_api_throws"], | ||
| ) | ||
| ), | ||
| ( name = "queue-metrics-disabled", | ||
| worker = ( | ||
| modules = [ | ||
| ( name = "worker-metrics-disabled", esModule = embed "queue-metrics-test.js" ) | ||
| ], | ||
| bindings = [ | ||
| ( name = "QUEUE", queue = "queue-metrics-disabled" ), | ||
| ( name = "METRICS_FLAG", json = "false" ), | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat", "capture_async_api_throws"], | ||
| ) | ||
| ), | ||
| ], | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2398,6 +2398,7 @@ export interface Queue<Body = unknown> { | |
| messages: Iterable<MessageSendRequest<Body>>, | ||
| options?: QueueSendBatchOptions, | ||
| ): Promise<void>; | ||
| metrics(): Promise<QueueMetrics>; | ||
| } | ||
| export interface QueueSendOptions { | ||
| contentType?: QueueContentType; | ||
|
|
@@ -2411,6 +2412,11 @@ export interface MessageSendRequest<Body = unknown> { | |
| contentType?: QueueContentType; | ||
| delaySeconds?: number; | ||
| } | ||
| export interface QueueMetrics { | ||
| backlogCount: number; | ||
| backlogBytes: number; | ||
| oldestMessageTimestamp: number; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for coming here late, but is the units of this timestamp documented anywhere? Times should generally either have an obvious type (like Date) or a clear unit (is this in seconds since the epoch? milliseconds? something else?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example the field 5 lines up is called
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, it may not be very clear. On the HTTP side, it will be documented in the OpenAPI schema, what would be the best way to document it in workerd?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at it more closely, the best answer here would be to actually return this as a This would parallel the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, thank you for pointing it out! I will get that change made. |
||
| } | ||
| export interface QueueRetryBatch { | ||
| retry: boolean; | ||
| delaySeconds?: number; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.