import { getForkChoiceMetrics } from "@lodestar/fork-choice";
import { getMetrics } from "@lodestar/state-transition";
import { createBeaconMetrics } from "./metrics/beacon.js";
import { createLodestarMetrics } from "./metrics/lodestar.js";
import { collectNodeJSMetrics } from "./nodeJsMetrics.js";
import { RegistryMetricCreator } from "./utils/registryMetricCreator.js";
export function createMetrics(opts, genesisTime, externalRegistries = []) {
    const register = new RegistryMetricCreator();
    const beacon = createBeaconMetrics(register);
    const forkChoice = getForkChoiceMetrics(register);
    const lodestar = createLodestarMetrics(register, opts.metadata, genesisTime);
    const stateTransition = getMetrics(register);
    const onUnhandledRejection = (_error) => {
        lodestar.unhandledPromiseRejections.inc();
    };
    process.on("unhandledRejection", onUnhandledRejection);
    const nodeJsMetricsClose = collectNodeJSMetrics(register);
    const close = () => {
        process.removeListener("unhandledRejection", onUnhandledRejection);
        nodeJsMetricsClose();
    };
    // Merge external registries
    for (const externalRegister of externalRegistries) {
        for (const metric of externalRegister.getMetricsAsArray()) {
            register.registerMetric(metric);
        }
    }
    return {
        ...beacon,
        ...forkChoice,
        ...lodestar,
        ...stateTransition,
        register,
        close,
    };
}
//# sourceMappingURL=metrics.js.map