Skip to content

Commit

Permalink
Measure duration of the POST /usage request and time of execution of …
Browse files Browse the repository at this point in the history
…the handler (#6363)
  • Loading branch information
kamilkisiela authored Jan 15, 2025
1 parent f5ed6b0 commit fba35b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/services/usage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { env } from './environment';
import {
collectDuration,
droppedReports,
httpRequestDuration,
httpRequestHandlerDuration,
httpRequests,
httpRequestsWithNoAccess,
httpRequestsWithNonExistingToken,
Expand All @@ -26,6 +28,12 @@ import { createUsage } from './usage';
import { usageProcessorV1 } from './usage-processor-1';
import { usageProcessorV2 } from './usage-processor-2';

declare module 'fastify' {
interface FastifyRequest {
onRequestHRTime: [number, number];
}
}

async function main() {
if (env.sentry) {
Sentry.init({
Expand Down Expand Up @@ -84,12 +92,26 @@ async function main() {
},
);

server.route<{
Body: unknown;
}>({
server.route<
{
Body: unknown;
},
{
onRequestTime: number;
}
>({
method: 'POST',
url: '/',
async handler(req, res) {
onRequest(req, _, done) {
req.onRequestHRTime = process.hrtime();
done();
},
onResponse(req, _, done) {
const delta = process.hrtime(req.onRequestHRTime);
httpRequestDuration.observe(delta[0] + delta[1] / 1e9);
done();
},
handler: measureHandler(async function usageHandler(req, res) {
httpRequests.inc();
let token: string | undefined;
const legacyToken = req.headers['x-api-token'] as string;
Expand All @@ -102,8 +124,9 @@ async function main() {
usedAPIVersion.labels({ version: '1' }).inc();
} else if (apiVersion === '2') {
usedAPIVersion.labels({ version: '2' }).inc();
} else {
usedAPIVersion.labels({ version: 'invalid' }).inc();
}
usedAPIVersion.labels({ version: 'invalid' }).inc();
} else {
usedAPIVersion.labels({ version: 'none' }).inc();
}
Expand Down Expand Up @@ -285,7 +308,7 @@ async function main() {
});
void res.status(500).send();
}
},
}),
});

server.route({
Expand Down Expand Up @@ -343,3 +366,15 @@ function measureParsing<T>(fn: () => T, version: 'v1' | 'v2'): T {
stop();
}
}

function measureHandler<$Req, $Res>(fn: (_req: $Req, _res: $Res) => Promise<void>) {
return async function (req: $Req, res: $Res) {
const stop = httpRequestHandlerDuration.startTimer();

try {
await fn(req, res);
} finally {
stop();
}
};
}
10 changes: 10 additions & 0 deletions packages/services/usage/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export const httpRequests = new metrics.Counter({
help: 'Number of http requests',
});

export const httpRequestDuration = new metrics.Histogram({
name: 'usage_http_request_duration_seconds',
help: 'Duration of an HTTP Request in seconds',
});

export const httpRequestHandlerDuration = new metrics.Histogram({
name: 'usage_http_request_handler_duration_seconds',
help: 'Duration of an HTTP Request handler in seconds',
});

export const httpRequestsWithoutToken = new metrics.Counter({
name: 'usage_http_requests_no_token',
help: 'Number of http requests without a token',
Expand Down

0 comments on commit fba35b4

Please sign in to comment.