Add /metrics endpoint to be used with Prometheus
This commit is contained in:
@@ -64,6 +64,8 @@
|
||||
#define MIN_REDRAW_INTERVAL 1000.0 / 60.0 // divide by frames per second..if you tweak adjust player speed
|
||||
#endif
|
||||
|
||||
|
||||
// Comment or remove the next #define to disable the /metrics endpoint on the HTTP server.
|
||||
// This endpoint provides the Twang32 stats for ingestion via Prometheus.
|
||||
#define ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,11 +9,15 @@ WiFiServer server(80);
|
||||
char linebuf[80];
|
||||
int charcount=0;
|
||||
|
||||
enum PAGE_TO_SEND
|
||||
{
|
||||
Stats,
|
||||
Metrics
|
||||
};
|
||||
|
||||
void ap_setup() {
|
||||
bool ret;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Set up an access point
|
||||
* @param ssid Pointer to the SSID (max 63 char).
|
||||
@@ -25,8 +29,6 @@ void ap_setup() {
|
||||
|
||||
//Serial.println("\r\nWiFi AP online ...");
|
||||
server.begin();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void sendStatsPage(WiFiClient client) {
|
||||
@@ -40,7 +42,6 @@ void sendStatsPage(WiFiClient client) {
|
||||
client.println("<h1>TWANG32 Play Stats</h1>");
|
||||
client.println("<ul>");
|
||||
|
||||
|
||||
client.print("<li>Games played: "); client.print(user_settings.games_played); client.println("</li>");
|
||||
if (user_settings.games_played > 0) { // prevent divide by 0
|
||||
client.print("<li>Average score: "); client.print(user_settings.total_points / user_settings.games_played); client.println("</li>");
|
||||
@@ -86,41 +87,76 @@ void sendStatsPage(WiFiClient client) {
|
||||
|
||||
client.print("</table>");
|
||||
|
||||
#ifdef ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
client.println("<ul><li><a href=\"/metrics\">Metrics</a></li></ul>");
|
||||
#endif // ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
|
||||
client.println("</body>");
|
||||
client.println("</html>");
|
||||
client.println();
|
||||
|
||||
}
|
||||
|
||||
#ifdef ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
// We need to use print() here since println() prints newlines as CR/LF, which
|
||||
// Prometheus cannot handle.
|
||||
#define __prom_metric(metric_name, metric_description, value) \
|
||||
client.print("# HELP " metric_name " " metric_description "\n"); \
|
||||
client.print("# TYPE " metric_name " gauge\n"); \
|
||||
client.print(metric_name " "); \
|
||||
client.print(value); \
|
||||
client.print("\n");
|
||||
|
||||
static void sendMetricsPage(WiFiClient client)
|
||||
{
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-Type: text/plain; charset=utf-8");
|
||||
client.println("Server: twang_exporter");
|
||||
client.println();
|
||||
__prom_metric("twang_games_played", "Number of games played", user_settings.games_played);
|
||||
__prom_metric("twang_total_points", "Total points", user_settings.total_points);
|
||||
__prom_metric("twang_high_score", "High score", user_settings.high_score);
|
||||
__prom_metric("twang_boss_kills", "Boss kills", user_settings.boss_kills);
|
||||
}
|
||||
|
||||
#undef __prom_metric
|
||||
#endif // ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
|
||||
void ap_client_check(){
|
||||
int cnt;
|
||||
bool newconn=false;
|
||||
int stat;
|
||||
WiFiClient client = server.available(); // listen for incoming clients
|
||||
|
||||
//if (client) { // if you get a client,
|
||||
// sendStatsPage(client);
|
||||
// Serial.println("printUploadForm");
|
||||
//}
|
||||
bool currentLineIsBlank = true;
|
||||
PAGE_TO_SEND page_to_send = Stats;
|
||||
|
||||
while (client.connected()) {
|
||||
if (client.available()) {
|
||||
char c = client.read();
|
||||
//Serial.write(c);
|
||||
linebuf[charcount]=c;
|
||||
if (charcount<sizeof(linebuf)-1)
|
||||
charcount++;
|
||||
|
||||
if (c == '\n' && currentLineIsBlank) {
|
||||
if (c == '\n' && currentLineIsBlank)
|
||||
{
|
||||
switch (page_to_send)
|
||||
{
|
||||
case Stats:
|
||||
sendStatsPage(client);
|
||||
break;
|
||||
#ifdef ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
case Metrics:
|
||||
sendMetricsPage(client);
|
||||
break;
|
||||
#endif // ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (c == '\n') {
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
|
||||
|
||||
if (strstr(linebuf,"GET /?") > 0)
|
||||
{
|
||||
String line = String(linebuf);
|
||||
@@ -136,8 +172,14 @@ void ap_client_check(){
|
||||
|
||||
change_setting(paramCode, val.toInt());
|
||||
|
||||
|
||||
page_to_send = Stats;
|
||||
}
|
||||
#ifdef ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
else if (strstr(linebuf, "GET /metrics"))
|
||||
{
|
||||
page_to_send = Metrics;
|
||||
}
|
||||
#endif // ENABLE_PROMETHEUS_METRICS_ENDPOINT
|
||||
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
@@ -147,8 +189,6 @@ void ap_client_check(){
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user