import promClient from "prom-client";

const registry = new promClient.Registry();
// 修复：在本地 registry 上设置默认标签（原代码误用全局 register）
registry.setDefaultLabels({ service: "msg" });

registry.registerMetric(
  new promClient.Counter({
    name: "msg_requests_total",
    help: "Total number of msg requests",
    labelNames: ["method", "endpoint", "status"],
  }),
);

registry.registerMetric(
  new promClient.Histogram({
    name: "msg_request_duration_seconds",
    help: "Msg request duration in seconds",
    labelNames: ["method", "endpoint"],
    buckets: [0.01, 0.05, 0.1, 0.3, 0.5, 1, 3, 5],
  }),
);

// 自动收集 Node.js 进程级指标（CPU/内存/事件循环/GC等）
// 这些指标无需业务代码埋点，prom-client 自动采集
promClient.collectDefaultMetrics({ register: registry });

export { registry as metricsRegistry };
