问题
I have a metric for my Minecraft server, mc_player_online
. It shows 1
if a player is online, or 0
otherwise.
I can graph it to show how many players have been online.
I want to calculate the play time of a player on the Minecraft server. The output could be tabulated to show "Player Steve's Playtime = 2d 3h 45m".
How can I calculate how long a player is online for?
I'd also like to calculate the utilisation of the server. So if the server is running from 12:00 to 13:00, and 10 players log on simultaneously at 12:00 and play until 12:10, then the utilisation should be 10 minutes (not 10 * 10 minutes).
How can I calculate how long a metric (mc_player_online
) is above a threshold (0)?
回答1:
I assume mc_player_online
is a metric per player (with the name of the player as a label).
A little precision: the graph you showed only indicates when a player is online. If you want to see the number of players at any one time, the expression would be:
sum(mc_player_online)
To calculate the time a player spends on Minicraft, you can only do so on a sliding window expressed as the last N <unit of time>
(last 7 days, 1 month ...). Taking as an example the time played over the last week in seconds (7 days is 604800s), it would be:
avg_over_time(mc_player_online[7d]) * 604800
You can have a nice display using a table display in Grafana. If you want to accumulate from forever, you'll have to have a recording rule and let Prometheus do the recording.
The computation of the utilization is similar to the computation of the time spent by a user but with an expression evaluating to 1 when at least one user is online (using the bool
modifier):
sum(mc_player_online) > bool 0
This become (over the last hour):
avg_over_time((sum(mc_player_online) > bool 0)[1h:]) * 86400
Note that this expression requires Prometheus version which supports subquery (version >= 2.7
). If your Prometheus is older, you can make do with recording rules.
来源:https://stackoverflow.com/questions/60024968/measure-time-metric-is-above-threshold