Skip to content

Add hook HookQueueStateCount + read middleware as hooks and vice versa#1203

Open
brandur wants to merge 1 commit intomasterfrom
brandur-queue-state-count
Open

Add hook HookQueueStateCount + read middleware as hooks and vice versa#1203
brandur wants to merge 1 commit intomasterfrom
brandur-queue-state-count

Conversation

@brandur
Copy link
Copy Markdown
Contributor

@brandur brandur commented Apr 12, 2026

Here, add a new hook called HookQueueStateCount which gets invoked to
produce job queue count statistics. We do this by adding a new
maintenance service which like other maintenance services, runs only on
the leader, so we only have one client performing counts at any given
time. Furthermore, in order to not introduce a potential operational
problem without opt-in from River users, the counts only run if a
HookQueueStateCount hook/middleware has been added to the client.

The reason we do all this to to implement a feature requested by one of
users: for otelriver in contrib to be able to emit queue count
metrics, which seems like a pretty reasonable ask for the package to be
able to do, and something that every River user would likely want access
to in their ops charts.

A slight oddity, but which I think is probably okay, is that the new
hook ideally stays a hook, but the existing otelriver middleware is a
middleware. It'd be nice not to have to put otelriver.Middleware into
both a client's Hooks and Middleware configuration, so we modify
client to allow for hooks that middleware and middleware which are
hooks. This lets otelriver.Middleware continue doing what it was
already doing, but also to start producing new counts as a hook.

@brandur brandur force-pushed the brandur-queue-state-count branch 3 times, most recently from af432f2 to f202436 Compare April 12, 2026 05:56
brandur added a commit to riverqueue/rivercontrib that referenced this pull request Apr 12, 2026
See [1] for context, but here we implement `HookQueueStateCount` in the
existing OTEL middleware so that it can emit metrics around queue
counts, a pretty handy feature that we have on definitive request for,
but which I'd guess most River users would like to have access to.

[1] riverqueue/river#1203
@brandur
Copy link
Copy Markdown
Contributor Author

brandur commented Apr 12, 2026

@codex review

@brandur brandur force-pushed the brandur-queue-state-count branch from f202436 to 109e1fe Compare April 12, 2026 06:09
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f2024369e5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@brandur brandur force-pushed the brandur-queue-state-count branch 3 times, most recently from 7266cb8 to b7694fa Compare April 12, 2026 06:44
brandur added a commit to riverqueue/rivercontrib that referenced this pull request Apr 12, 2026
See [1] for context, but here we implement `HookQueueStateCount` in the
existing OTEL middleware so that it can emit metrics around queue
counts, a pretty handy feature that we have on definitive request for,
but which I'd guess most River users would like to have access to.

[1] riverqueue/river#1203
@brandur brandur requested a review from bgentry April 12, 2026 06:53
Here, add a new hook called `HookQueueStateCount` which gets invoked to
produce job queue count statistics. We do this by adding a new
maintenance service which like other maintenance services, runs only on
the leader, so we only have one client performing counts at any given
time. Furthermore, in order to not introduce a potential operational
problem without opt-in from River users, the counts only run if a
`HookQueueStateCount` hook/middleware has been added to the client.

The reason we do all this to to implement a feature requested by one of
users: for `otelriver` in contrib to be able to emit queue count
metrics, which seems like a pretty reasonable ask for the package to be
able to do, and something that every River user would likely want access
to in their ops charts.

A slight oddity, but which I think is _probably_ okay, is that the new
hook ideally stays a hook, but the existing `otelriver` middleware is a
middleware. It'd be nice not to have to put `otelriver.Middleware` into
both a client's `Hooks` and `Middleware` configuration, so we modify
client to allow for hooks that middleware and middleware which are
hooks. This lets `otelriver.Middleware` continue doing what it was
already doing, but also to start producing new counts as a hook.
@brandur brandur force-pushed the brandur-queue-state-count branch from b7694fa to 3d3c21a Compare April 12, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant