siimon / prom-client

Prometheus client for node.js
Apache License 2.0
3.15k stars 378 forks source link

Dynamic metrics #646

Open freddiebity opened 4 days ago

freddiebity commented 4 days ago

Hi,

I'm trying to do something like this


const task = cron.schedule("*/2 * * * *", async () => {
    const fee_accounts = await getFeeAccounts();

    fee_accounts.forEach((feeAccount) => {
        let feeAccountBalance = new Gauge({
            name: `fee_account_${feeAccount.type}_balance`,
            help: `OGY fee account balance for ${feeAccount.weight}g NFT canister`,
        });

        prometheusClient.register.registerMetric(feeAccountBalance);
        feeAccountBalance.set(feeAccount.balance)
    })
})

the account type could be different each time and so i want to set up a new name each time. the problem is, this records once on start up and then the second time the cron schedule runs it never updates. I see the cron job runs correctly and i get fresh data each time too from getFeeAccounts().

freddiebity commented 4 days ago

also tried storing the Gauge in a object like const metrics = {}

 metrics[metric_name] = feeAccountBalance

but that doesnt work