import { describe, it, expect } from "vitest"; import { HISTOGRAM_BUCKETS_SECONDS, createDefaultMetrics } from "./defaults.js"; import { createMetricsRegistry } from "./registry.js"; describe("createDefaultMetrics", () => { it("registers every canonical metric on the registry", async () => { const registry = createMetricsRegistry({ serviceName: "api", collectDefaultProcessMetrics: false }); const metrics = createDefaultMetrics(registry); // Touch each metric so the registry has at least one sample to render. metrics.httpRequestsTotal.labels({ method: "GET", route: "/v1/x", status: "200" }).inc(); metrics.httpRequestDurationSeconds.labels({ method: "GET", route: "/v1/x", status: "200" }).observe(0.05); metrics.prismaQueryDurationSeconds.labels({ model: "User", action: "findMany" }).observe(0.01); metrics.redisCommandDurationSeconds.labels({ command: "GET" }).observe(0.001); metrics.queueJobsActive.labels({ queue: "email" }).set(2); metrics.queueJobsWaiting.labels({ queue: "email" }).set(5); metrics.queueJobsEnqueuedTotal.labels({ queue: "email", priority: "critical" }).inc(); metrics.queueJobsCompletedTotal.labels({ queue: "email", job_name: "send" }).inc(); metrics.queueJobsFailedTotal.labels({ queue: "email", job_name: "send", error_type: "Error" }).inc(); metrics.queueJobDurationSeconds.labels({ queue: "email", job_name: "send" }).observe(0.5); metrics.outboxDispatchTotal.labels({ event_type: "workflow.trigger.event", status: "ok" }).inc(); metrics.webSsrLoaderDurationSeconds.labels({ route: "root", status: "ok" }).observe(0.02); metrics.aiTokensConsumedTotal.labels({ provider: "openai", model: "gpt-4", tenant_bucket: "other" }).inc(1234); metrics.billingProviderWebhookEventsTotal.labels({ event_type: "invoice.paid", status: "received" }).inc(); metrics.mailSentTotal.labels({ provider: "mailjet", status: "delivered" }).inc(); metrics.webhookDeliveryAttemptsTotal.labels({ status: "success" }).inc(); metrics.storageAuthorizedDownloadsTotal.labels({ surface: "files", delivery: "signed_redirect", size_known: "known" }).inc(); metrics.storageAuthorizedDownloadObjectBytesTotal.labels({ surface: "files", delivery: "signed_redirect" }).inc(1024); metrics.csrfTokenRotationTotal.inc(); metrics.subscriptionActive.labels({ plan_id: "pro" }).set(42); metrics.activeTenants.set(10); metrics.activeUsers24h.set(123); const text = await registry.metrics(); const expected = [ "http_requests_total", "http_request_duration_seconds", "prisma_query_duration_seconds", "redis_command_duration_seconds", "queue_jobs_active", "queue_jobs_waiting", "queue_jobs_enqueued_total", "queue_jobs_completed_total", "queue_jobs_failed_total", "queue_job_duration_seconds", "outbox_dispatch_total", "web_ssr_loader_duration_seconds", "ai_tokens_consumed_total", "billing_provider_webhook_events_total", "mail_sent_total", "webhook_delivery_attempts_total", "storage_authorized_downloads_total", "storage_authorized_download_object_bytes_total", "csrf_token_rotation_total", "subscription_active", "active_tenants", "active_users_24h", ]; for (const name of expected) { expect(text).toContain(name); } }); it("uses the shared histogram bucket spread for latency metrics", () => { expect(HISTOGRAM_BUCKETS_SECONDS[0]).toBe(0.005); expect(HISTOGRAM_BUCKETS_SECONDS[HISTOGRAM_BUCKETS_SECONDS.length - 1]).toBe(10); expect(HISTOGRAM_BUCKETS_SECONDS).toEqual([...HISTOGRAM_BUCKETS_SECONDS].sort((a, b) => a - b)); }); });