fix(metrics): preserve Insights request paths (#17151)

This commit is contained in:
Ravi Kumar L 2026-10-29 15:56:39 +01:00 • committed by GitHub
parent 44638c06d2
commit 62e000894b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View file

@ -63,11 +63,18 @@ describe('normalizePath', () => {
// Static paths are not modified
['/api/auth/login', '/api/auth/login'],
['/api/config', '/api/config'],
['/api/insights', '/api/insights'],
['/api/insights/', '/api/insights'],
['/api/insights?agentIds=agent-private&search=private-query', '/api/insights'],
['/api/insights/access', '/api/insights/access'],
['/api/insights/access/?agentIds=agent-private', '/api/insights/access'],
['/health', '/health'],
['/metrics', '/metrics'],
['/', '/'],
// Unknown/user-generated routes collapse into bounded label buckets
['/api/not-a-real-route/user-generated-value', '/api/#path'],
['/api/insights/user-generated-value', '/api/#path'],
['/api/insights/access/user-generated-value', '/api/#path'],
['/images/user-123/avatar-1700000000000.png', '/images/#path'],
['/avatars/user-123/avatar-1700000000000.png', '/avatars/#path'],
['/t/tenant-a/images/user-123/avatar-1700000000000.png', '/t/#path'],
@ -139,6 +146,36 @@ describe('createMetrics', () => {
);
});
it.each([
['/api/insights', 200],
['/api/insights', 500],
['/api/insights/access', 200],
['/api/insights/access', 403],
])('exports Insights request metrics for %s with status %i', async (path, status) => {
process.env.METRICS_SECRET = 'test-secret';
const app = express();
const { metricsMiddleware, metricsRouter } = createMetrics();
app.use(metricsMiddleware);
app.get(path, (_req, res) => {
res.sendStatus(status);
});
app.use('/metrics', metricsRouter);
await request(app).get(`${path}/?agentIds=private-agent&search=private-query`).expect(status);
const response = await request(app)
.get('/metrics')
.set('Authorization', 'Bearer test-secret')
.expect(200);
const labels = `method="GET",path="${path}",status="${status}"`;
expect(response.text).toContain(`http_requests_total{${labels}} 1`);
expect(response.text).toContain(`http_request_duration_seconds_count{${labels}} 1`);
expect(response.text).toContain(`http_request_duration_seconds_sum{${labels}}`);
expect(response.text).not.toContain('private-agent');
expect(response.text).not.toContain('private-query');
expect(response.text).not.toContain('path="/api/#path"');
});
it('records locator traversal reasons and count distributions without content labels', async () => {
process.env.METRICS_SECRET = 'test-secret';
createMetrics();

View file

@ -9,6 +9,7 @@ import type { LocatorTraversalFailure } from '../protection/diagnostics';
import { agentStartupMilestones, agentStartupResults } from '~/agents/phases';
const PATH_NORMALIZATIONS: [RegExp, string][] = [
[/^(\/api\/insights(?:\/access)?)\/?$/, '$1'],
[/^\/api\/agents\/chat\/stream\/[^/]+(?=\/|$)/, '/api/agents/chat/stream/#id'],
[/^\/api\/agents\/chat\/status\/[^/]+(?=\/|$)/, '/api/agents/chat/status/#id'],
[/^\/api\/files\/code\/download\/[^/]+\/[^/]+(?=\/|$)/, '/api/files/code/download/#id/#id'],
@ -39,6 +40,8 @@ const STATIC_PATHS = new Set([
'/metrics',
'/api/auth/login',
'/api/config',
'/api/insights',
'/api/insights/access',
'/api/agents/chat/abort',
'/api/agents/chat/active',
'/api/agents/v1/chat/completions',