Merge pull request #5 from nilsding/add-prometheus-metrics

Add /metrics endpoint which can be used with Prometheus
This commit is contained in:
bdring
2019-11-03 18:59:30 -06:00
committed by GitHub
3 changed files with 137 additions and 92 deletions

View File

@@ -1196,7 +1196,6 @@ void getInput(){
// -------------- SFX -------------- // -------------- SFX --------------
// --------------------------------- // ---------------------------------
/*
/* /*
This is used sweep across (up or down) a frequency range for a specified duration. This is used sweep across (up or down) a frequency range for a specified duration.
A sin based warble is added to the frequency. This function is meant to be called A sin based warble is added to the frequency. This function is meant to be called

View File

@@ -64,6 +64,8 @@
#define MIN_REDRAW_INTERVAL 1000.0 / 60.0 // divide by frames per second..if you tweak adjust player speed #define MIN_REDRAW_INTERVAL 1000.0 / 60.0 // divide by frames per second..if you tweak adjust player speed
#endif #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 #endif

View File

@@ -9,7 +9,14 @@ WiFiServer server(80);
char linebuf[80]; char linebuf[80];
int charcount=0; int charcount=0;
enum PAGE_TO_SEND
{
Stats,
Metrics
};
void ap_setup() { void ap_setup() {
bool ret; bool ret;
@@ -30,8 +37,7 @@ void ap_setup() {
Serial.println(passphrase); Serial.println(passphrase);
Serial.println("Web Server Address: http://192.168.4.1"); Serial.println("Web Server Address: http://192.168.4.1");
}
}
void sendStatsPage(WiFiClient client) { void sendStatsPage(WiFiClient client) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
@@ -44,7 +50,6 @@ void sendStatsPage(WiFiClient client) {
client.println("<h1>TWANG32 Play Stats</h1>"); client.println("<h1>TWANG32 Play Stats</h1>");
client.println("<ul>"); client.println("<ul>");
client.print("<li>Games played: "); client.print(user_settings.games_played); client.println("</li>"); client.print("<li>Games played: "); client.print(user_settings.games_played); client.println("</li>");
if (user_settings.games_played > 0) { // prevent divide by 0 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>"); client.print("<li>Average score: "); client.print(user_settings.total_points / user_settings.games_played); client.println("</li>");
@@ -90,41 +95,76 @@ void sendStatsPage(WiFiClient client) {
client.print("</table>"); 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("</body>");
client.println("</html>"); client.println("</html>");
client.println(); 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(){ void ap_client_check(){
int cnt; int cnt;
bool newconn=false; bool newconn=false;
int stat; int stat;
WiFiClient client = server.available(); // listen for incoming clients WiFiClient client = server.available(); // listen for incoming clients
//if (client) { // if you get a client,
// sendStatsPage(client);
// Serial.println("printUploadForm");
//}
bool currentLineIsBlank = true; bool currentLineIsBlank = true;
PAGE_TO_SEND page_to_send = Stats;
while (client.connected()) { while (client.connected()) {
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
//Serial.write(c);
linebuf[charcount]=c; linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) if (charcount<sizeof(linebuf)-1)
charcount++; charcount++;
if (c == '\n' && currentLineIsBlank) { if (c == '\n' && currentLineIsBlank)
{
switch (page_to_send)
{
case Stats:
sendStatsPage(client); sendStatsPage(client);
break; break;
#ifdef ENABLE_PROMETHEUS_METRICS_ENDPOINT
case Metrics:
sendMetricsPage(client);
break;
#endif // ENABLE_PROMETHEUS_METRICS_ENDPOINT
}
break;
} }
if (c == '\n') { if (c == '\n') {
// you're starting a new line // you're starting a new line
currentLineIsBlank = true; currentLineIsBlank = true;
if (strstr(linebuf,"GET /?") > 0) if (strstr(linebuf,"GET /?") > 0)
{ {
String line = String(linebuf); String line = String(linebuf);
@@ -140,8 +180,14 @@ void ap_client_check(){
change_setting(paramCode, val.toInt()); 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 // you're starting a new line
currentLineIsBlank = true; currentLineIsBlank = true;
@@ -151,8 +197,6 @@ void ap_client_check(){
// you've gotten a character on the current line // you've gotten a character on the current line
currentLineIsBlank = false; currentLineIsBlank = false;
} }
} }
} }
} }