Skip to content

Commit

Permalink
fix: only start timers if they are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Neriuzz committed Nov 28, 2023
1 parent b046bef commit b7cc922
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/fastify-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ interface IConstructiorDeps {
}

interface IReqMetrics<T extends string> {
hist: (labels?: LabelValues<T>) => number;
sum: (labels?: LabelValues<T>) => void;
hist?: (labels?: LabelValues<T>) => number;
sum?: (labels?: LabelValues<T>) => void;
}

interface IRouteMetrics {
Expand Down Expand Up @@ -289,6 +289,32 @@ export class FastifyMetrics implements IFastifyMetrics {
return { routeHist, routeSum, labelNames };
}

/**
* Create timers for histogram and summary based on enabled configuration
* option
*/
private createTimers(request: FastifyRequest): void {
if (this.options.routeMetrics.enabled instanceof Object) {
this.metricStorage.set(request, {
hist: !(this.options.routeMetrics.enabled.histogram === false)
? this.routeMetrics.routeHist.startTimer()
: undefined,

Check warning on line 301 in src/fastify-metrics.ts

View check run for this annotation

Codecov / codecov/patch

src/fastify-metrics.ts#L301

Added line #L301 was not covered by tests
sum: !(this.options.routeMetrics.enabled.summary === false)
? this.routeMetrics.routeSum.startTimer()

Check warning on line 303 in src/fastify-metrics.ts

View check run for this annotation

Codecov / codecov/patch

src/fastify-metrics.ts#L303

Added line #L303 was not covered by tests
: undefined,
});
return;
}

if (!(this.options.routeMetrics.enabled === false)) {
this.metricStorage.set(request, {
hist: this.routeMetrics.routeHist.startTimer(),
sum: this.routeMetrics.routeSum.startTimer(),
});
}
return;
}

/** Collect per-route metrics */
private collectRouteMetrics(): void {
this.deps.fastify
Expand All @@ -308,10 +334,7 @@ export class FastifyMetrics implements IFastifyMetrics {
request.method
)
) {
this.metricStorage.set(request, {
hist: this.routeMetrics.routeHist.startTimer(),
sum: this.routeMetrics.routeSum.startTimer(),
});
this.createTimers(request);
}

return done();
Expand All @@ -325,10 +348,7 @@ export class FastifyMetrics implements IFastifyMetrics {
})
)
) {
this.metricStorage.set(request, {
hist: this.routeMetrics.routeHist.startTimer(),
sum: this.routeMetrics.routeSum.startTimer(),
});
this.createTimers(request);
}

return done();
Expand All @@ -353,21 +373,8 @@ export class FastifyMetrics implements IFastifyMetrics {
...this.collectCustomLabels(request, reply),
};

if (this.options.routeMetrics.enabled instanceof Object) {
if (!(this.options.routeMetrics.enabled.summary === false)) {
metrics.sum(labels);
}
if (!(this.options.routeMetrics.enabled.histogram === false)) {
metrics.hist(labels);
}
done();
return;
}

if (!(this.options.routeMetrics.enabled === false)) {
metrics.sum(labels);
metrics.hist(labels);
}
if (metrics.hist) metrics.hist(labels);
if (metrics.sum) metrics.sum(labels);

done();
});
Expand Down

0 comments on commit b7cc922

Please sign in to comment.