How to use only the latest value when joining metrics in Prometheus?

  Kiến thức lập trình

I’m encountering a small issue with Prometheus. I’m relatively new to it, so I’m not yet very experienced.

I’ve developed a small Discord bot that exports two metrics:

  • The first one is the number of users per Discord server (identified by ID, discord_users).
  • The second one is the corresponding name for a server ID (discord_guild_names_info).

For example, this is what the bot exposes and Prometheus scrapes:

# HELP discord_guild_names_info Guild names
# TYPE discord_guild_names_info gauge
discord_guild_names_info{guild="12345",name="Stack"} 1.0
discord_guild_names_info{guild="54321",name="Overflow"} 1.0

# HELP discord_users Number of users per guild
# TYPE discord_users gauge
discord_users{guild="12345"} 595.0
discord_users{guild="54321"} 130.0

I intentionally didn’t make the metric based on the name to avoid issues if the name changes; since the ID is fixed, there won’t be any disruptions. When I want to display a metric on a graph, I perform a join between the two on the ID. This allows me to display the name in the legend.

So, I have this query: discord_users * on (guild) group_left(name) discord_guild_names_info.

I don’t really care about the name history in the user progression graphs, for example; I only want to display the latest name. The issue I’m facing is that it performs the join not only on the latest value but on the historical names.

In this case, for the screenshot, I renamed “Stack” to “kcatS”; I only wanted the name to change in the legend, not to “duplicate” the server.

Does anyone know how to ensure that it takes only the latest name?

Grafana screenshot showing that when the Discord server was renamed, there was a disruption in the graph

New contributor

Aeris One is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT