# ๐Ÿ“Š Metrics Service > A lightweight FastAPI microservice for user registration analytics and metrics [![Python](https://img.shields.io/badge/Python-3.12+-blue.svg)](https://python.org) [![FastAPI](https://img.shields.io/badge/FastAPI-0.114+-green.svg)](https://fastapi.tiangolo.com) [![Docker](https://img.shields.io/badge/Docker-Ready-blue.svg)](https://docker.com) The Metrics service provides real-time APIs for querying user registration data (via StarRocks) and querying monitoring metrics (via Prometheus). ## โœจ Features ### ๐Ÿ“Š Registration Analytics (StarRocks) - Date Range Query (start_date ~ end_date) - Typed responses with Pydantic models ### ๐Ÿ“ˆ Prometheus Metrics - Predefined PromQL metrics per product - Time-range queries and metric info discovery ### ๐Ÿ”ง Technical Features - Data Models: Pydantic v2 - Async HTTP and robust error handling - Structured logging - Auto-generated Swagger/OpenAPI docs ## ๐Ÿ“ Project Structure ``` metrics/ โ”œโ”€โ”€ backend/ # Business logic layer โ”‚ โ”œโ”€โ”€ infra/ # Infrastructure components โ”‚ โ”‚ โ””โ”€โ”€ external_service/ โ”‚ โ”‚ โ”œโ”€โ”€ prometheus_client.py โ”‚ โ”‚ โ””โ”€โ”€ starrocks_client.py โ”‚ โ”œโ”€โ”€ models/ # Data models โ”‚ โ”‚ โ””โ”€โ”€ user_registration_models.py โ”‚ โ””โ”€โ”€ services/ # Business services โ”‚ โ”œโ”€โ”€ prometheus_metrics_service.py โ”‚ โ””โ”€โ”€ registration_analytics_service.py โ”œโ”€โ”€ webapi/ # API layer โ”‚ โ”œโ”€โ”€ routes/ # API endpoints โ”‚ โ”‚ โ”œโ”€โ”€ metrics/ # Aggregated routers (prefix: /api/metrics) โ”‚ โ”‚ โ”œโ”€โ”€ prometheus_metrics/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ available_metrics.py โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ metric_info.py โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ metrics_query.py โ”‚ โ”‚ โ””โ”€โ”€ starrocks_metrics/ โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py โ”‚ โ”‚ โ”œโ”€โ”€ available_metrics.py โ”‚ โ”‚ โ”œโ”€โ”€ metric_info.py โ”‚ โ”‚ โ””โ”€โ”€ metrics_query.py โ”‚ โ”œโ”€โ”€ bootstrap/ โ”‚ โ”‚ โ””โ”€โ”€ application.py โ”‚ โ””โ”€โ”€ main.py โ”œโ”€โ”€ common/ โ”œโ”€โ”€ requirements.txt โ”œโ”€โ”€ Dockerfile โ”œโ”€โ”€ local.env โ””โ”€โ”€ README.md ``` ## ๐Ÿš€ Quick Start ### Prerequisites - Python 3.12+ or Docker - Access to StarRocks database (for registration analytics) - Access to Prometheus (for monitoring metrics) ### ๐Ÿ Python Setup ```bash # 1. Install dependencies pip install -r requirements.txt # 2. Start the service python3 -m uvicorn webapi.main:app --host 0.0.0.0 --port 8009 --reload ``` ### ๐Ÿณ Docker Setup ```bash # 1. Build image docker build -t metrics:latest . # 2. Run container (env from baked local.env) docker run --rm -p 8009:8009 metrics:latest # Alternatively: run with a custom env file # (this overrides envs copied into the image) docker run --rm -p 8009:8009 --env-file local.env metrics:latest ``` ### ๐Ÿ“– Access Documentation Visit `http://localhost:8009/docs` for interactive API documentation. ## ๐Ÿ“Š API Endpoints All endpoints are exposed under the API base prefix `/api/metrics`. ### StarRocks (Registration Analytics) - POST `/api/metrics/starrocks/dru_query` โ€” Query daily registered users time series for a date range - GET `/api/metrics/starrocks/product/{product_id}/available-metrics` โ€” List available StarRocks-backed metrics for the product - GET `/api/metrics/starrocks/product/{product_id}/metric/{metric_name}/info` โ€” Get metric info for the product Example request: ```bash curl -X POST "http://localhost:8009/api/metrics/starrocks/dru_query" \ -H "Content-Type: application/json" \ -d '{ "product_id": "freeleaps", "start_date": "2024-09-10", "end_date": "2024-09-20" }' ``` ### Prometheus - POST `/api/metrics/prometheus/metrics_query` โ€” Query metric time series by product/metric - GET `/api/metrics/prometheus/product/{product_id}/available-metrics` โ€” List available metrics for product - GET `/api/metrics/prometheus/product/{product_id}/metric/{metric_name}/info` โ€” Get metric info ## ๐Ÿงช Testing ```bash # Health check curl http://localhost:8009/ ``` ## โš™๏ธ Configuration ### Environment Variables ```bash # Server Configuration SERVICE_API_ACCESS_HOST=0.0.0.0 SERVICE_API_ACCESS_PORT=8009 # StarRocks Database STARROCKS_HOST=freeleaps-starrocks-cluster-fe-service.freeleaps-data-platform.svc STARROCKS_PORT=9030 STARROCKS_USER=root STARROCKS_PASSWORD= STARROCKS_DATABASE=freeleaps # Logging LOG_BASE_PATH=./logs BACKEND_LOG_FILE_NAME=metrics APPLICATION_ACTIVITY_LOG=metrics-activity # Prometheus PROMETHEUS_ENDPOINT=http://localhost:9090 ``` > Tip: Copy `local.env` to `.env` and modify as needed for your environment. ### ๐Ÿณ Docker Deployment ```bash # Build and run docker build -t metrics:latest . docker run --rm -p 8009:8009 metrics:latest # Run with custom environment docker run --rm -p 8009:8009 --env-file local.env metrics:latest # Run in background docker run -d --name metrics-service -p 8009:8009 metrics:latest ``` **Image Details:** - Base: Python 3.12-slim - Port: 8009 - Working Dir: `/app` ## ๐Ÿ”ง Development ```bash # Setup development environment python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate pip install -r requirements.txt # Run with auto-reload python -m uvicorn webapi.main:app --reload ``` ## ๐Ÿ“ API Documentation - Swagger UI: `http://localhost:8009/docs` - ReDoc: `http://localhost:8009/redoc` - OpenAPI JSON: `http://localhost:8009/openapi.json` ## โš ๏ธ Important Notes - Date format: `YYYY-MM-DD` (single-digit month/day also accepted by API) - Default `product_id`: "freeleaps" - Requires StarRocks database access and/or Prometheus endpoint ## ๐Ÿ› Troubleshooting | Issue | Solution | |-------|----------| | Port in use | `docker stop $(docker ps -q --filter ancestor=metrics:latest)` | | Import errors | Check dependencies: `pip install -r requirements.txt` | | DB connection | Verify StarRocks cluster accessibility | | Container issues | Check logs: `docker logs ` | ## ๐Ÿ“„ License Part of the Freeleaps platform.