Skip to content
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

feat: allow specifying timeout for cluster metrics aggregation #645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ export class AggregatorRegistry<
> extends Registry<T> {
/**
* Gets aggregated metrics for all workers.
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation

Choose a reason for hiding this comment

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

Suggested change
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation
* @param {number} [options.timeoutMs=5000] Timeout to wait for all workers to respond before error.

* @return {Promise<string>} Promise that resolves with the aggregated
* metrics.
*/
clusterMetrics(): Promise<string>;
clusterMetrics(options?: ClusterMetricsOptions): Promise<string>;

/**
* Creates a new Registry instance from an array of metrics that were
Expand Down Expand Up @@ -192,6 +193,10 @@ export enum MetricType {
Summary,
}

export type ClusterMetricsOptions = {
timeoutMs?: number;
Copy link

@BourgoisMickael BourgoisMickael Dec 13, 2024

Choose a reason for hiding this comment

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

Suggested change
timeoutMs?: number;
/** Timeout to wait for all workers to respond before error. */
timeoutMs?: number;

This can be documented as well

};

type CollectFunction<T> = (this: T) => void | Promise<void>;

interface MetricObject {
Expand Down
5 changes: 3 additions & 2 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ class AggregatorRegistry extends Registry {
/**
* Gets aggregated metrics for all workers. The optional callback and
* returned Promise resolve with the same value; either may be used.
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation.
Copy link

@BourgoisMickael BourgoisMickael Dec 13, 2024

Choose a reason for hiding this comment

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

Suggested change
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation.
* @param {ClusterMetricsOptions|undefined} options Additional options for cluster metrics aggregation.
* @param {number} [options.timeoutMs=5000] Timeout to wait for all workers to respond before error.

You can document the timeoutMs field as well

* @return {Promise<string>} Promise that resolves with the aggregated
* metrics.
*/
clusterMetrics() {
clusterMetrics(options = { timeoutMs: 5000 }) {
const requestId = requestCtr++;

return new Promise((resolve, reject) => {
Expand All @@ -58,7 +59,7 @@ class AggregatorRegistry extends Registry {
errorTimeout: setTimeout(() => {
const err = new Error('Operation timed out.');
request.done(err);
}, 5000),
}, options.timeoutMs || 5000),
};
requests.set(requestId, request);

Expand Down