diff --git a/packages/api/src/app/metrics.spec.ts b/packages/api/src/app/metrics.spec.ts index 25141f6c25..00b7fbce1d 100644 --- a/packages/api/src/app/metrics.spec.ts +++ b/packages/api/src/app/metrics.spec.ts @@ -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(); diff --git a/packages/api/src/app/metrics.ts b/packages/api/src/app/metrics.ts index 1016bf2bb8..bfd00134cf 100644 --- a/packages/api/src/app/metrics.ts +++ b/packages/api/src/app/metrics.ts @@ -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',