Uploaded metrics plugin file structure
This commit is contained in:
parent
d9cb5db041
commit
68a37fd1f4
1
amarillo/plugins/__init__.py
Normal file
1
amarillo/plugins/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
|
||||
1
amarillo/plugins/metrics/__init__.py
Normal file
1
amarillo/plugins/metrics/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .metrics import *
|
||||
49
amarillo/plugins/metrics/metrics.py
Normal file
49
amarillo/plugins/metrics/metrics.py
Normal 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
9
pyproject.toml
Normal 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
3
requirements.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fastapi
|
||||
prometheus-fastapi-instrumentator
|
||||
prometheus-client
|
||||
Reference in a new issue