Skip to content

Commit 7c6bbc9

Browse files
committed
feat(telemetry): simple metrics endpoint for prometheus
1 parent 281679a commit 7c6bbc9

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

web/monitoring/telemetry.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from prometheus_client import Counter, Gauge
2+
3+
REQUEST_COUNTER = Counter(
4+
"app_num_requests",
5+
"Total number of requests received",
6+
["endpoint", "method"],
7+
)
8+
9+
MEM_GAUGE = Gauge(
10+
"app_current_memory",
11+
"Total RAM consumed by application",
12+
)
13+
14+
UPTIME_GAUGE = Gauge(
15+
"app_uptime",
16+
"Uptime of the application",
17+
)
18+
19+
SEARCH_CACHE_HITS_GAUGE = Gauge(
20+
"app_search_cache_hits",
21+
"Total hits from search cache",
22+
)
23+
SEARCH_CACHE_MISSES_GAUGE = Gauge(
24+
"app_search_cache_misses",
25+
"Total misses from search cache",
26+
)
27+
28+
INDEX_CACHE_HITS_GAUGE = Gauge(
29+
"app_index_cache_hits",
30+
"Total hits from index cache",
31+
)
32+
INDEX_CACHE_MISSES_GAUGE = Gauge(
33+
"app_index_cache_misses",
34+
"Total misses from index cache",
35+
)
36+
37+
AUTOCOMPLETE_CACHE_HITS_GAUGE = Gauge(
38+
"app_autocomplete_cache_hits",
39+
"Total hits from autocomplete cache",
40+
)
41+
AUTOCOMPLETE_CACHE_MISSES_GAUGE = Gauge(
42+
"app_autocomplete_cache_misses",
43+
"Total misses from autocomplete cache",
44+
)

web/routes/metrics.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import datetime
2+
import os
3+
4+
import psutil
5+
from fastapi import APIRouter, Request, Response
6+
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
7+
8+
from web.monitoring.telemetry import (
9+
AUTOCOMPLETE_CACHE_HITS_GAUGE,
10+
AUTOCOMPLETE_CACHE_MISSES_GAUGE,
11+
INDEX_CACHE_HITS_GAUGE,
12+
INDEX_CACHE_MISSES_GAUGE,
13+
MEM_GAUGE,
14+
SEARCH_CACHE_HITS_GAUGE,
15+
SEARCH_CACHE_MISSES_GAUGE,
16+
UPTIME_GAUGE,
17+
)
18+
from web.routes.autocomplete import _get_autocomplete
19+
from web.routes.index import _get_letter_index
20+
from web.routes.search import _get_search
21+
22+
router = APIRouter(prefix="/metrics")
23+
24+
25+
@router.get(
26+
path="",
27+
summary="Returns metrics collected throughout the app for Prometheus.",
28+
)
29+
def metrics(request: Request) -> Response:
30+
"""
31+
TODO: Docstring this.
32+
"""
33+
rss = int(psutil.Process(os.getpid()).memory_info().rss // 1024**2)
34+
MEM_GAUGE.set(rss)
35+
startup_time = request.app.state.start_time
36+
uptime_seconds = (datetime.datetime.now() - startup_time).total_seconds()
37+
UPTIME_GAUGE.set(uptime_seconds)
38+
search_cache = _get_search.cache_info()
39+
index_cache = _get_letter_index.cache_info()
40+
autocomplete_cache = _get_autocomplete.cache_info()
41+
SEARCH_CACHE_HITS_GAUGE.set(search_cache.hits)
42+
SEARCH_CACHE_MISSES_GAUGE.set(search_cache.misses)
43+
INDEX_CACHE_HITS_GAUGE.set(index_cache.hits)
44+
INDEX_CACHE_MISSES_GAUGE.set(index_cache.misses)
45+
AUTOCOMPLETE_CACHE_HITS_GAUGE.set(autocomplete_cache.hits)
46+
AUTOCOMPLETE_CACHE_MISSES_GAUGE.set(autocomplete_cache.misses)
47+
return Response(content=generate_latest(), media_type=CONTENT_TYPE_LATEST)

0 commit comments

Comments
 (0)