Skip to content

Add worker support#693

Merged
SimenB merged 11 commits into
siimon:masterfrom
jdmarshall:workers
Dec 9, 2025
Merged

Add worker support#693
SimenB merged 11 commits into
siimon:masterfrom
jdmarshall:workers

Conversation

@jdmarshall

@jdmarshall jdmarshall commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

Similar to cluster support, but also allows for the 'main' thread to not be responsible for aggregation. The user can opt to send all aggregation to a dedicated worker, thereby avoiding crowding up the main thread, as is the problem in #628.

This answers #401

Prep work for supporting Workers.
It starts children on ports already in use.
Unfortunately there seems to be no way to enumerate all of the workers
running. The alternative would be a broadcast channel, but then all
of the siblings would have to listen to each other's metrics, which
would not be good.

fixes siimon#656
…ent.

This implementation uses BroadcastChannels to announce Workers that can
export prometheus telemetry, and to collect the data.

Supports workers coming online before the collector by re-announcing on
seeing the 'primary' come online.
@jdmarshall jdmarshall changed the title Workers Add worker support Jul 29, 2025
@jdmarshall

Copy link
Copy Markdown
Contributor Author

This should get faster as well in a few Node.js versions:

https://v8.dev/blog/json-stringify

@ehsanghorbani190

Copy link
Copy Markdown

@jdmarshall Hi! I would like to know if this PR will be accepted soon enough :)
Also, I have a question. Can you explain what is the current behavior of prom-client when I observe metrics in the worker? They don't merge with the main application metrics, right?
Also, if this PR merges, how should I use it with the worker pools like piscinajs? Is there any additional boilerplate to write? Or any new configs?

@jdmarshall

jdmarshall commented Aug 10, 2025

Copy link
Copy Markdown
Contributor Author

Like the cluster code, you need to fire up the aggregator in each worker. It’s using broadcast channels because the NodeJS team is still working on arbitrary communication between workers other than parent child.

It should work fine with piscina.

@ehsanghorbani190

Copy link
Copy Markdown

@jdmarshall Interesting! Can you provide me a simple example of doing this?

@SimenB SimenB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I forgot to submit this review 🙈

Comment thread example/cluster.js
Comment thread example/cluster.js
Comment thread example/worker.js Outdated
Comment thread lib/registry.js Outdated
Comment thread lib/worker.js
@jdmarshall jdmarshall reopened this Nov 28, 2025
@SimenB SimenB merged commit d4d2dcb into siimon:master Dec 9, 2025
26 checks passed
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.

3 participants