Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,64 @@
|
||||
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.stripeWebhookEventsTotal.labels({ event_type: "invoice.paid", status: "received" }).inc();
|
||||
metrics.mailSentTotal.labels({ provider: "mailjet", status: "delivered" }).inc();
|
||||
metrics.webhookDeliveryAttemptsTotal.labels({ status: "success" }).inc();
|
||||
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",
|
||||
"stripe_webhook_events_total",
|
||||
"mail_sent_total",
|
||||
"webhook_delivery_attempts_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));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user