feat(metrics): count api requests auth failures and 5xxs
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user