1
0
Fork 0
forked from s3lph/matemat

Merge branch 'dirty-metrics' into 'staging'

Quick-and-dirty Prometheus metrics exposing

See merge request s3lph/matemat!71
This commit is contained in:
s3lph 2020-03-03 21:13:22 +00:00
commit 61d6331e09
2 changed files with 24 additions and 0 deletions

View file

@ -11,6 +11,7 @@ from .touchkey import touchkey_page
from .buy import buy from .buy import buy
from .deposit import deposit from .deposit import deposit
from .admin import admin from .admin import admin
from .metrics import metrics
from .moduser import moduser from .moduser import moduser
from .modproduct import modproduct from .modproduct import modproduct
from .userbootstrap import userbootstrap from .userbootstrap import userbootstrap

View file

@ -0,0 +1,23 @@
from bottle import abort, response, route
from matemat.db import MatematDatabase
from matemat.webserver.config import get_app_config
@route('/metrics')
def metrics():
config = get_app_config()
if config.get('MetricsEnabled', '0') != '1':
return abort(403,
'Prometheus metrics are disable in configuration. '
'If you believe this is an error, contact your administrator.')
with MatematDatabase(config['DatabaseFile']) as db:
products = db.list_products()
m = '''# TYPE matemat_beverage_supply_count gauge
# HELP matemat_beverage_supply_count Number of bottles available
'''
for product in products:
name = product.name.replace('"', '\\"')
m += f'matemat_beverage_supply_count{{product="{name}"}} {product.stock}\n'
response.headers['Content-Type'] = 'text/plain'
return m