Skip to content

Commit

Permalink
fix: only start timers if they are enabled via configuration option (#97
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Neriuzz committed Nov 28, 2023
1 parent d2d97c0 commit 6f0b8e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 25 deletions.
60 changes: 60 additions & 0 deletions src/__tests__/route-metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,66 @@ describe('route metrics', () => {
});
});

describe('{ enabled = {} }', () => {
let app = fastify();

afterEach(async () => {
await app.close();
});

beforeEach(async () => {
app = fastify();

await app.register(fastifyPlugin, {
endpoint: '/metrics',
routeMetrics: {
enabled: {},
},
});
app.get('/test', async () => {
return 'get test';
});
app.post('/test', async () => {
return 'post test';
});
await app.ready();
});

test('metrics exposed', async () => {
await expect(
app.inject({
method: 'GET',
url: '/test',
})
).resolves.toBeDefined();

await expect(
app.inject({
method: 'POST',
url: '/test',
})
).resolves.toBeDefined();

const metrics = await app.inject({
method: 'GET',
url: '/metrics',
});

expect(typeof metrics.payload).toBe('string');

const lines = metrics.payload.split('\n');

expect(lines).toEqual(
expect.arrayContaining([
'http_request_duration_seconds_count{method="GET",route="/test",status_code="200"} 1',
'http_request_duration_seconds_count{method="POST",route="/test",status_code="200"} 1',
'http_request_summary_seconds_count{method="GET",route="/test",status_code="200"} 1',
'http_request_summary_seconds_count{method="POST",route="/test",status_code="200"} 1',
])
);
});
});

describe('{ registeredRoutesOnly = false }', () => {
let app = fastify();

Expand Down
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,
sum: !(this.options.routeMetrics.enabled.summary === false)
? this.routeMetrics.routeSum.startTimer()
: 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 6f0b8e2

Please sign in to comment.