summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerge Bazanski <q3k@q3k.org>2021-07-14 00:05:38 +0200
committerSerge Bazanski <q3k@q3k.org>2021-07-14 00:05:38 +0200
commit713c7e6c1a8fd6147522c1a5e3067898a1d8bf7a (patch)
tree134fc3ac05a9408689e32ea5798ba8f25c3cdaa2
parent2c15b5e8da73331962c6a2a6cfa995aaa2799a79 (diff)
downloadcheckinator-development.tar.gz
checkinator-development.tar.bz2
checkinator-development.tar.xz
checkinator-development.zip
web: serve prometheus metricsdevelopment
-rw-r--r--at/web.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/at/web.py b/at/web.py
index e30c13e..34b6337 100644
--- a/at/web.py
+++ b/at/web.py
@@ -5,7 +5,8 @@ from datetime import datetime
from typing import NamedTuple, Iterable, Iterator, List
from functools import wraps
from flask import Flask, render_template, abort, g, \
- redirect, request, url_for, make_response, send_file
+ redirect, request, url_for, make_response, send_file, \
+ Response
from base64 import b64decode
@@ -136,7 +137,27 @@ def app(instance_path, devices_api, config):
@app.route('/')
def main_view():
return render_template('main.html', **now_at())
-
+
+ @app.route('/metrics')
+ def metrics_view():
+ """Render count of different entities, per kind, in Prometheus format."""
+ now = now_at()
+ lines = [
+ "# HELP entities present at the hackerspace according to checkinator / DHCP",
+ "# TYPE people gauge",
+ ]
+ for kind, devices in now.items():
+ # The kind is being directly text-pasted into the metric below -
+ # let's make sure a new kind with special characters doesn't mess
+ # things up too much.
+ if '"' in kind:
+ continue
+ # Not using formatting, as the Prometheus format contains '{' and
+ # '}' characters which throw off Python's .format().
+ line = 'checkinator_now_present_entities{entity_kind="' + kind + '"} '
+ line += str(len(devices))
+ lines.append(line)
+ return Response('\n'.join(lines), mimetype='text/plain')
@app.route('/api')
def list_all():