add metrics endpoint
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
|||||||
import httpx
|
import httpx
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from flask import Flask, request, jsonify, abort
|
from flask import Flask, request, jsonify, abort, Response
|
||||||
from sqlalchemy import create_engine, Column, Integer, String
|
from sqlalchemy import create_engine, Column, Integer, String
|
||||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||||
|
|
||||||
@@ -43,7 +43,6 @@ Base.metadata.create_all(engine)
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_auth_header():
|
def get_auth_header():
|
||||||
auth = request.headers.get("Authorization", "")
|
auth = request.headers.get("Authorization", "")
|
||||||
if not auth.startswith("Bearer "):
|
if not auth.startswith("Bearer "):
|
||||||
@@ -173,6 +172,26 @@ def notify():
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/metrics", methods=["GET"])
|
||||||
|
def metrics():
|
||||||
|
session = SessionLocal()
|
||||||
|
|
||||||
|
try:
|
||||||
|
token_count = session.query(DeviceToken).count()
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
metrics_output = (
|
||||||
|
"# HELP apns_device_tokens_total Number of registered APNs device tokens\n"
|
||||||
|
"# TYPE apns_device_tokens_total gauge\n"
|
||||||
|
f"apns_device_tokens_total {token_count}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
metrics_output,
|
||||||
|
mimetype="text/plain; version=0.0.4"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=3000, debug=True)
|
app.run(host="0.0.0.0", port=3000, debug=True)
|
||||||
Reference in New Issue
Block a user