Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.

Commit 1be9eb0

Browse files
committed
Prometheus Exporter Endpoint
1 parent e30879a commit 1be9eb0

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/endpoints/global_data.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use reqwest::{Client, StatusCode};
55
use serde::Serialize;
66
use serde_json::Value;
77
use sqlx::{query, PgPool};
8-
use std::fs::read_to_string;
8+
use std::{fmt::Write, fs::read_to_string};
99

1010
const PROJECT_ID: &str = "p2rxzX0q";
1111

@@ -128,6 +128,30 @@ pub async fn get(
128128
Ok(Json(data))
129129
}
130130

131+
pub async fn metrics(
132+
State(ApiState {
133+
database, online_users, ..
134+
}): State<ApiState>,
135+
) -> Result<String, ApiError> {
136+
let lifetime_players = get_total_players(&database).await?;
137+
let online_players = online_users.len();
138+
139+
let mut response = String::new();
140+
141+
#[rustfmt::skip]
142+
#[expect(unused)]
143+
{
144+
writeln!(response, "# This endpoint is intended for internal use with Prometheus. It is not part of the documented stable API and may be");
145+
writeln!(response, "# removed without notice. The `/v1/global_data` endpoint should be preferred, see the following:");
146+
writeln!(response, "# https://github.com/AxolotlClient/AxolotlClient-API/blob/main/docs/api_documentation.md#get-global_data");
147+
writeln!(response, "");
148+
writeln!(response, "lifetime_players {lifetime_players}");
149+
writeln!(response, "online_players {online_players}");
150+
};
151+
152+
Ok(response)
153+
}
154+
131155
async fn get_total_players(database: &PgPool) -> Result<u32, ApiError> {
132156
Ok(query!("SELECT reltuples AS estimate FROM pg_class where relname = 'players'")
133157
.fetch_one(database)

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ async fn main() -> anyhow::Result<()> {
118118

119119
let router = Router::new()
120120
.route("/global_data", get(global_data::get))
121+
.route("/metrics", get(global_data::metrics))
121122
.route("/authenticate", get(get_authenticate))
122123
.route("/gateway", any(gateway))
123124
.route("/user/{uuid}", get(user::get).post(user::post))

0 commit comments

Comments
 (0)