Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

1.9 KB
Raw Blame
import type { Request, Response } from "express";
import { getRedisConnection } from "../../../services/queue-service";
import { getDbPoolMetrics } from "../../../db/connection";
import { nuqFdbGetMetrics } from "../../../services/worker/nuq-fdb";
import { nuqGetLocalMetrics } from "../../../services/worker/nuq";
import { scrapeQueue } from "../../../services/worker/nuq-router";
import { teamConcurrencySemaphore } from "../../../services/worker/team-semaphore";

export async function metricsController(_: Request, res: Response) {
  let cursor: string = "0";
  let totalJobCount = 0;
  let teamCount = 0;
  do {
    const res = await getRedisConnection().sscan(
      "concurrency-limit-queues",
      cursor,
    );
    cursor = res[0];

    const keys = res[1];

    for (const key of keys) {
      const jobCount = await getRedisConnection().zcard(key);

      if (jobCount === 0) {
        await getRedisConnection().srem("concurrency-limit-queues", key);
      } else {
        totalJobCount += jobCount;
        teamCount++;
      }
    }
  } while (cursor !== "0");

  const semaphoreMetrics = await teamConcurrencySemaphore.getMetrics();

  res.contentType("text/plain").send(`\
# HELP concurrency_limit_queue_job_count_total The total number of jobs across all concurrency limit queues
# TYPE concurrency_limit_queue_job_count_total gauge
concurrency_limit_queue_job_count_total ${totalJobCount}

# HELP concurrency_limit_queue_team_count The number of teams with jobs in the concurrency limit queue
# TYPE concurrency_limit_queue_team_count gauge
concurrency_limit_queue_team_count ${teamCount}

${nuqGetLocalMetrics()}
${semaphoreMetrics}
${getDbPoolMetrics()}`);
}

export async function nuqMetricsController(_: Request, res: Response) {
  res.contentType("text/plain").send(await scrapeQueue.getMetrics());
}

export async function nuqFdbMetricsController(_: Request, res: Response) {
  res.contentType("text/plain").send(await nuqFdbGetMetrics());
}