Skip to content

Commit

Permalink
Add support for collecting prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Mar 13, 2024
1 parent 5791ecf commit a16ad7f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions ooniapi/common/src/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ class Settings(BaseSettings):
statsd_port: int = 8125
statsd_prefix: str = "ooniapi"
jwt_encryption_key: str = "CHANGEME"
prometheus_metrics_password: str = "CHANGEME"
42 changes: 42 additions & 0 deletions ooniapi/common/src/common/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import secrets

from typing import Annotated

from fastapi import FastAPI, Response, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials

from .dependencies import get_settings
from prometheus_client import (
CONTENT_TYPE_LATEST,
CollectorRegistry,
generate_latest,
)

security = HTTPBasic()


def mount_metrics(app: FastAPI, registry: CollectorRegistry):
def metrics(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
settings=Depends(get_settings),
):
is_correct_username = secrets.compare_digest(
credentials.username.encode("utf8"), b"prom"
)
is_correct_password = secrets.compare_digest(
credentials.password.encode("utf8"),
settings.prometheus_metrics_password.encode("utf-8"),
)
if not (is_correct_username and is_correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)

resp = Response(content=generate_latest(registry))
resp.headers["Content-Type"] = CONTENT_TYPE_LATEST
return resp

endpoint = "/metrics"
app.get(endpoint, include_in_schema=True, tags=None)(metrics)
2 changes: 2 additions & 0 deletions ooniapi/services/oonirun/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies = [
"httpx ~= 0.26.0",
"pyjwt ~= 2.8.0",
"alembic ~= 1.13.1",
"prometheus-fastapi-instrumentator ~= 6.1.0",
"prometheus-client",
]

readme = "README.md"
Expand Down
9 changes: 8 additions & 1 deletion ooniapi/services/oonirun/src/oonirun/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from functools import lru_cache
from fastapi import FastAPI

from .routers import oonirun

from .dependencies import get_settings
from .common.version import get_build_label, get_pkg_version
from .common.metrics import mount_metrics
from fastapi.middleware.cors import CORSMiddleware

from prometheus_fastapi_instrumentator import Instrumentator

from contextlib import asynccontextmanager

import logging
Expand All @@ -21,11 +23,16 @@
async def lifespan(app: FastAPI):
settings = get_settings()
logging.basicConfig(level=getattr(logging, settings.log_level.upper()))
mount_metrics(app, instrumentor.registry)
yield


app = FastAPI(lifespan=lifespan)

instrumentor = Instrumentator().instrument(
app, metric_namespace="ooniapi", metric_subsystem="oonirun"
)

# TODO: temporarily enable all
origins = ["*"]
app.add_middleware(
Expand Down
6 changes: 1 addition & 5 deletions ooniapi/services/oonirun/src/oonirun/routers/oonirun.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"""

from datetime import datetime, timedelta, timezone, date
from os import urandom
from sys import byteorder
from typing import Dict, Any, List, Optional, Tuple
from typing import Dict, List, Optional, Tuple
import logging

import sqlalchemy as sa
Expand All @@ -17,7 +15,6 @@
from pydantic import BaseModel as PydandicBaseModel
from typing_extensions import Annotated


from .. import models

from ..common.dependencies import get_settings, role_required
Expand All @@ -28,7 +25,6 @@
)
from ..dependencies import get_postgresql_session


ISO_FORMAT_DATETIME = "%Y-%m-%dT%H:%M:%S.%fZ"
ISO_FORMAT_DATE = "%Y-%m-%d"

Expand Down

0 comments on commit a16ad7f

Please sign in to comment.