All files / web/src/app/api/metrics route.ts

0% Statements 0/44
0% Branches 0/1
0% Functions 0/1
0% Lines 0/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
/**
 * Prometheus Metrics Endpoint
 *
 * GET /api/metrics
 *
 * Returns Prometheus-formatted metrics for scraping by Prometheus.
 * This endpoint should be called by the Prometheus server, not by users directly.
 *
 * Metrics exposed:
 * - HTTP request duration and counts
 * - Socket.IO connection metrics
 * - Node.js runtime metrics (memory, CPU, event loop)
 * - Practice session metrics
 * - Vision recording metrics
 */

import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { metricsRegistry, initSmokeTestMetrics, initCoverageMetrics } from '@/lib/metrics'

export const dynamic = 'force-dynamic'

export const GET = withAuth(async () => {
  try {
    // Initialize metrics from DB on first scrape (runs once per pod)
    await initSmokeTestMetrics()
    await initCoverageMetrics()

    const metrics = await metricsRegistry.metrics()

    return new NextResponse(metrics, {
      status: 200,
      headers: {
        'Content-Type': metricsRegistry.contentType,
        // Prevent caching of metrics
        'Cache-Control': 'no-store, no-cache, must-revalidate',
      },
    })
  } catch (error) {
    console.error('Error collecting metrics:', error)

    return NextResponse.json({ error: 'Failed to collect metrics' }, { status: 500 })
  }
})