Skip to content

Commit

Permalink
feat: allow specifying timeout for cluster metrics aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vit Habada committed Nov 18, 2024
1 parent c1d76c5 commit b38c265
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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
* @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;
};

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.
* @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

0 comments on commit b38c265

Please sign in to comment.