prom-client icon indicating copy to clipboard operation
prom-client copied to clipboard

Dynamic metrics

Open freddiebity opened this issue 1 year ago • 4 comments

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 avatar Nov 21 '24 15:11 freddiebity

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

 metrics[metric_name] = feeAccountBalance

but that doesnt work

freddiebity avatar Nov 21 '24 15:11 freddiebity

Don’t do this.

Do:

feeAccountBallance.set(feeAccount.balance, { type: feeAccount.type } );

Prometheus and OTEL generally don’t do stat differentiation by string interpolation. It’s a pain in the butt to make grafana dashboards with interpolated stats anyway. You end up using regular expressions to generate the labels and yuck.

jdmarshall avatar Jul 02 '25 17:07 jdmarshall

@SimenB i think you can close this as wontfix. This should be using labels instead.

jdmarshall avatar Jul 13 '25 14:07 jdmarshall

https://prometheus.io/docs/practices/naming/

Do not put the label names in the metric name, as this introduces redundancy and will cause confusion if the respective labels are aggregated away.

jdmarshall avatar Jul 16 '25 06:07 jdmarshall