Uploaded metrics plugin file structure

This commit is contained in:
Csaba 2023-12-07 14:50:07 +01:00
parent d9cb5db041
commit 68a37fd1f4
5 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1 @@
__path__ = __import__('pkgutil').extend_path(__path__, __name__)

View file

@ -0,0 +1 @@
from .metrics import *

View file

@ -0,0 +1,49 @@
import json
import logging
import os
import random
from typing import Callable
from fastapi import APIRouter, HTTPException, Depends, Request
from datetime import datetime
from prometheus_client.exposition import generate_latest
from prometheus_client import Gauge, Counter
from prometheus_fastapi_instrumentator.metrics import Info
from fastapi import Depends, HTTPException, FastAPI
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.responses import PlainTextResponse
from amarillo.app.services.secrets import secrets
logger = logging.getLogger(__name__)
security = HTTPBasic()
def amarillo_trips_number_total() -> Callable[[Info], None]:
METRIC = Gauge("amarillo_trips_number_total", "Total number of trips.")
def instrumentation(info: Info) -> None:
trips_count = sum([len(files) for r, d, files in os.walk("./data/carpool")])
METRIC.set(trips_count)
return instrumentation
router = APIRouter(
prefix="/metrics",
tags=["amarillo_metrics"]
)
@router.get("/")
def metrics(credentials: HTTPBasicCredentials = Depends(security)):
if (credentials.username != secrets.metrics_user
or credentials.password != secrets.metrics_password):
raise HTTPException(
status_code=401,
detail="Unauthorized",
headers={"WWW-Authenticate": "Basic"},
)
# total_requests_metric.labels(endpoint="/amarillo-metrics").inc()
return PlainTextResponse(content=generate_latest())

9
pyproject.toml Normal file
View file

@ -0,0 +1,9 @@
[project]
name = "amarillo-metrics"
version = "0.0.1"
dependencies = [
"fastapi",
"prometheus-fastapi-instrumentator",
"prometheus-client"
]

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
fastapi
prometheus-fastapi-instrumentator
prometheus-client