From 6375c8e2c0be4ff50da8b29f8bfbdd16ba4213a5 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Thu, 10 Oct 2019 20:18:17 +0200 Subject: [PATCH] Add /metrics endpoint to be used with Prometheus --- TWANG32/config.h | 4 +- TWANG32/wifi_ap.h | 248 +++++++++++++++++++++++++++------------------- 2 files changed, 147 insertions(+), 105 deletions(-) diff --git a/TWANG32/config.h b/TWANG32/config.h index fa93e1e..a52cbb4 100644 --- a/TWANG32/config.h +++ b/TWANG32/config.h @@ -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 diff --git a/TWANG32/wifi_ap.h b/TWANG32/wifi_ap.h index d880693..faf6a24 100644 --- a/TWANG32/wifi_ap.h +++ b/TWANG32/wifi_ap.h @@ -1,7 +1,7 @@ #include #include "settings.h" -const char* ssid = "TWANG_AP"; +const char* ssid = "TWANG_AP"; const char* passphrase = "esp32rocks"; WiFiServer server(80); @@ -9,146 +9,186 @@ WiFiServer server(80); char linebuf[80]; int charcount=0; +enum PAGE_TO_SEND +{ + Stats, + Metrics +}; + void ap_setup() { - bool ret; - - + bool ret; - /* - * Set up an access point - * @param ssid Pointer to the SSID (max 63 char). - * @param passphrase (for WPA2 min 8 char, for open use NULL) - * @param channel WiFi channel number, 1 - 13. - * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) - */ - ret = WiFi.softAP(ssid, passphrase, 2, 0); - - //Serial.println("\r\nWiFi AP online ..."); - server.begin(); - + /* + * Set up an access point + * @param ssid Pointer to the SSID (max 63 char). + * @param passphrase (for WPA2 min 8 char, for open use NULL) + * @param channel WiFi channel number, 1 - 13. + * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) + */ + ret = WiFi.softAP(ssid, passphrase, 2, 0); + //Serial.println("\r\nWiFi AP online ..."); + server.begin(); } void sendStatsPage(WiFiClient client) { - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) - // and a content-type so the client knows what's coming, then a blank line: - client.println("HTTP/1.1 200 OK"); - client.println("Content-type:text/html"); - client.println(); - client.println(""); - client.println(""); - client.println("

TWANG32 Play Stats

"); - client.println("