feat(metrics): count api requests auth failures and 5xxs
CI / Type Check & Lint (push) Successful in 6s
CI / Build Test (Current Platform) (push) Successful in 6s
CI / Build All Platforms (push) Successful in 39s

This commit is contained in:
2026-04-21 13:15:34 +00:00
parent 6541b2db1c
commit 3762fc661e
2 changed files with 132 additions and 0 deletions
+83
View File
@@ -86,6 +86,89 @@ Deno.test('ApiServer serves health metrics and authenticated model listings', as
assertEquals(authenticatedModels.status, 200);
assertEquals(authenticatedBody.object, 'list');
assertEquals(authenticatedBody.data[0].id, 'meta-llama/Llama-3.1-8B-Instruct');
const metricsAfterRequests = await fetch(`http://127.0.0.1:${port}/metrics`);
const metricsAfterRequestsBody = await metricsAfterRequests.text();
assertEquals(
metricsAfterRequestsBody.includes('modelgrid_api_requests_total{path="/v1/models"} 2'),
true,
);
assertEquals(
metricsAfterRequestsBody.includes('modelgrid_api_auth_failures_total{path="/v1/models"} 1'),
true,
);
} finally {
await server.stop();
}
});
Deno.test('ApiServer metrics expose 5xx counts for failing endpoints', async () => {
const port = 19100 + Math.floor(Math.random() * 1000);
let failModelListing = true;
const server = new ApiServer(
{
host: '127.0.0.1',
port,
apiKeys: ['valid-key'],
cors: false,
corsOrigins: [],
},
{
async getAllStatus() {
return new Map();
},
async getAllAvailableModels() {
if (failModelListing) {
failModelListing = false;
throw new Error('models unavailable');
}
return new Map();
},
} as never,
{
async getAllModels() {
return [];
},
} as never,
{} as never,
{
getStatus() {
return {
localNode: null,
nodes: [],
models: {},
desiredDeployments: [],
};
},
} as never,
);
(server as unknown as {
gpuDetector: { detectGpus: () => Promise<unknown[]> };
}).gpuDetector = {
async detectGpus() {
return [];
},
};
await server.start();
try {
const failedModels = await fetch(`http://127.0.0.1:${port}/v1/models`, {
headers: {
Authorization: 'Bearer valid-key',
},
});
assertEquals(failedModels.status, 500);
await failedModels.text();
const metricsResponse = await fetch(`http://127.0.0.1:${port}/metrics`);
const metricsBody = await metricsResponse.text();
assertEquals(
metricsBody.includes('modelgrid_api_server_errors_total{path="/v1/models"} 1'),
true,
);
} finally {
await server.stop();
}