Dynamic metrics
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().
also tried storing the Gauge in a object like const metrics = {}
metrics[metric_name] = feeAccountBalance
but that doesnt work
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.
@SimenB i think you can close this as wontfix. This should be using labels instead.
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.