Lesson 5 of 8 · 2 min
Dashboard
A small page that reads your own metrics. Save as dashboard.py.
# run locally: streamlit run dashboard.py
import time, httpx, pandas as pd, streamlit as st
st.title("Service health")
text = httpx.get("http://127.0.0.1:8000/metrics").text
rows = [l.rsplit(" ", 1) for l in text.splitlines() if l and not l.startswith("#")]
data = {k: float(v) for k, v in rows}
ok = data.get('requests_total{status="ok"}', 0)
bad = data.get('requests_total{status="error"}', 0)
count = data.get("request_seconds_count", 0)
st.write(f"{int(ok)} answered, {int(bad)} failed")
st.write(f"average {1000 * data.get('request_seconds_sum', 0) / max(count, 1):.0f} ms")
st.bar_chart(pd.DataFrame({"requests": {k: v for k, v in data.items() if k.startswith("requests_total")}}))
st.caption(time.strftime("updated %H:%M:%S"))