OmniRoute/src/app/api/monitoring/health/route.ts
diegosouzapw 47468636e4 feat(dashboard): expand observability and provider tooling
Unify audit access under the logs experience and add richer active
request visibility with sanitized client and provider payload previews.

Expose provider warnings from upstream responses in compliance audit
logs, surface learned rate-limit header data in health monitoring, and
add MCP cache stats and cache flush tools with test coverage.

Also add local provider catalog support for SD WebUI and ComfyUI,
include video generation in endpoint docs and UI, add eval run storage
migrations, and refresh docs and translations to match the expanded
feature set.
2026-04-23 16:57:43 -03:00

87 lines
3.3 KiB
TypeScript

import { NextResponse } from "next/server";
import { getProviderConnections, getSettings } from "@/lib/localDb";
import { buildHealthPayload } from "@/lib/monitoring/observability";
import { APP_CONFIG } from "@/shared/constants/config";
import { AI_PROVIDERS } from "@/shared/constants/providers";
/**
* GET /api/monitoring/health — System health overview
*
* Returns system info, provider health (circuit breakers),
* rate limit status, and database stats.
*/
export async function GET() {
try {
const { getAllCircuitBreakerStatuses } = await import("@/shared/utils/circuitBreaker");
const { getAllRateLimitStatus, getLearnedLimits } =
await import("@omniroute/open-sse/services/rateLimitManager");
const { getAllModelLockouts } = await import("@omniroute/open-sse/services/accountFallback");
const { getInflightCount } = await import("@omniroute/open-sse/services/requestDedup.ts");
const { getQuotaMonitorSummary, getQuotaMonitorSnapshots } =
await import("@omniroute/open-sse/services/quotaMonitor.ts");
const { getActiveSessions, getAllActiveSessionCountsByKey } =
await import("@omniroute/open-sse/services/sessionManager.ts");
const settings = await getSettings();
const connections = await getProviderConnections();
const circuitBreakers = getAllCircuitBreakerStatuses();
const rateLimitStatus = getAllRateLimitStatus();
const learnedLimits = getLearnedLimits();
const lockouts = getAllModelLockouts();
const quotaMonitorSummary = getQuotaMonitorSummary();
const quotaMonitorMonitors = getQuotaMonitorSnapshots();
const activeSessions = getActiveSessions();
const activeSessionsByKey = getAllActiveSessionCountsByKey();
const { getAllHealthStatuses } = await import("@/lib/localHealthCheck");
const payload = buildHealthPayload({
appVersion: APP_CONFIG.version,
catalogCount: Object.keys(AI_PROVIDERS).length,
settings,
connections,
circuitBreakers,
rateLimitStatus,
learnedLimits,
lockouts,
localProviders: getAllHealthStatuses(),
inflightRequests: getInflightCount(),
quotaMonitorSummary,
quotaMonitorMonitors,
activeSessions,
activeSessionsByKey,
});
return NextResponse.json(payload);
} catch (error) {
console.error("[API] GET /api/monitoring/health error:", error);
return NextResponse.json({ status: "error", error: "Health check failed" }, { status: 500 });
}
}
/**
* DELETE /api/monitoring/health — Reset all circuit breakers
*
* Resets all provider circuit breakers to CLOSED state,
* clearing failure counts and persisted state.
*/
export async function DELETE() {
try {
const { resetAllCircuitBreakers, getAllCircuitBreakerStatuses } =
await import("@/shared/utils/circuitBreaker");
const before = getAllCircuitBreakerStatuses();
const resetCount = before.length;
resetAllCircuitBreakers();
console.log(`[API] DELETE /api/monitoring/health — Reset ${resetCount} circuit breakers`);
return NextResponse.json({
success: true,
message: `Reset ${resetCount} circuit breaker(s) to healthy state`,
resetCount,
});
} catch (error) {
console.error("[API] DELETE /api/monitoring/health error:", error);
return NextResponse.json({ error: "Failed to reset circuit breakers" }, { status: 500 });
}
}