Metadata-Version: 2.4
Name: cognitio-pondus
Version: 0.1.0
Summary: Coleta de métricas genéricas (duração, tamanho, CPU/Memória, dimensões livres) para APIs FastAPI/ASGI, persistidas em PostgreSQL com TTL automático.
Author-email: Rafael Afonso Porto <cognitio@raporto.tech>
License: BSD 3-Clause License
        
        Copyright (c) 2026, Rafael Afonso Porto
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Project-URL: Homepage, https://github.com/raportoh/pondus
Project-URL: Repository, https://github.com/raportoh/pondus.git
Project-URL: Issues, https://github.com/raportoh/pondus/issues
Project-URL: Documentation, https://github.com/raportoh/pondus#readme
Keywords: metrics,fastapi,asgi,postgresql,monitoring,cost,telemetry
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Framework :: AsyncIO
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: starlette>=0.30
Requires-Dist: asyncpg>=0.28
Provides-Extra: psutil
Requires-Dist: psutil>=5.9; extra == "psutil"
Provides-Extra: dev
Requires-Dist: fastapi>=0.100; extra == "dev"
Requires-Dist: httpx; extra == "dev"
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio; extra == "dev"
Dynamic: license-file

<p align="center">
  <img src="https://raw.githubusercontent.com/raportoh/pondus/master/assets/logo.jpg" alt="Pondus" width="360">
</p>

# Pondus

> **pondus** — do latim: *peso, carga, massa*.  
> Cada métrica carrega o peso de um evento ou operação.

Lib de coleta de métricas para **FastAPI/ASGI**, persistidas em
**PostgreSQL**, com **TTL automático** e **dimensões de negócio
livres** (JSONB).

---

## Fluxo de dados (Mermaid)

### 1. Arquitetura geral

```mermaid
flowchart TB
    subgraph Cliente
        REQ[Requisição HTTP]
    end

    subgraph "Aplicação FastAPI"
        MID[MetricsMiddleware]
        END[Endpoint /processar]
        DEC["@track_metrics"]
        CTX[(contextvars)]
    end

    subgraph "PostgreSQL"
        TBL[(api_metrics<br/>partitioned by range)]
        TTL[TTL Maintenance<br/>DROP partições > 10d]
    end

    REQ -->|X-Request-ID| MID
    MID -->|propaga request_id| CTX
    MID --> END
    END -->|add_dimension| CTX
    END --> DEC
    DEC -->|lê contexto| CTX
    MID -->|"record()"| QUEUE{{asyncio.Queue}}
    DEC -->|"record()"| QUEUE
    QUEUE -->|batch flush| TBL
    TBL -->|a cada 6h| TTL
```

### 2. Ciclo de vida do decorator

```mermaid
sequenceDiagram
    autonumber
    participant C as Cliente
    participant M as Middleware
    participant E as Endpoint
    participant D as @track_metrics
    participant W as MetricsWriter
    participant DB as PostgreSQL

    C->>M: POST /processar
    M->>M: gera request_id
    M->>E: call_next()
    E->>E: add_dimension("uf", "RJ")
    E->>D: processa_arquivo()
    D->>D: t0 = perf_counter()
    D->>D: executa função
    D->>D: t1 = perf_counter()
    D->>W: record(MetricRecord)
    E->>M: retorna response
    M->>W: record(MetricRecord)
    W->>W: batch_size >= 50?
    W->>DB: executemany (flush)
    M->>C: 200 OK + X-Request-ID
```

### 3. TTL / Partitionamento

```mermaid
gantt
    title Partições diárias (ttl_days=10)
    dateFormat  YYYY-MM-DD
    axisFormat  %d/%m

    section Ativas
    Partição 01 :active, p1, 2024-01-01, 1d
    Partição 02 :active, p2, 2024-01-02, 1d
    Partição 03 :active, p3, 2024-01-03, 1d
    Partição 10 :active, p10, 2024-01-10, 1d

    section Expiradas
    Partição 00 :done, p0, 2023-12-30, 1d
```

---

## Instalação

```bash
pip install pondus                 # core
pip install "pondus[psutil]"       # + medição de CPU/memória
pip install "pondus[dev]"          # + fastapi/pytest para dev
```

---

## Uso rápido

```python
from fastapi import FastAPI
from pondus import MetricsConfig, setup_metrics, track_metrics, add_dimension

app = FastAPI()
config = MetricsConfig(
    dsn="postgresql://user:pass@host:5432/db",
    ttl_days=10,               # default
    enable_partitioning=True,  # recomendado para produção
)
writer = setup_metrics(app, config)

@app.on_event("startup")
async def _startup():
    await writer.start()

@app.on_event("shutdown")
async def _shutdown():
    await writer.stop()

@track_metrics(
    process="processa_arquivo",
    size_kind="generated_file",
    size_extractor=lambda *a, result, **kw: len(result.encode()),
)
async def processa_arquivo(conteudo: str) -> str:
    return conteudo.upper()

@app.post("/processar")
async def processar(payload: dict):
    add_dimension("uf", payload["uf"])
    add_dimension("store", payload["store"])
    return {"resultado": await processa_arquivo(payload["conteudo"])}
```

---

## O que é capturado automaticamente (middleware)

| Métrica | Fonte |
|---|---|
| `request_id` | Header `X-Request-ID` ou UUID v4 gerado |
| `route`, `method`, `status_code` | Objeto `Request`/`Response` |
| `duration_s` | `time.perf_counter()` |
| `size_bytes` / `size_kind="request_payload"` | Header `Content-Length` |
| `size_bytes` / `size_kind="response_payload"` | Materialização do body |
| `success` / `error_type` | Exceções e status >= 500 |

---

## O que você adiciona manualmente

| API | Descrição |
|---|---|
| `@track_metrics(...)` | Mede funções: duração, tamanho customizado, CPU/mem |
| `add_dimension(k, v)` | Dimensão de negócio — mesclada em **toda** métrica do contexto |
| `add_dimensions({...})` | Múltiplas dimensões de uma vez |

Dimensões são persistidas como **JSONB** — consulte diretamente:

```sql
SELECT * FROM api_metrics
WHERE dimensions->>'uf' = 'RJ'
  AND timestamp > now() - interval '1 day';
```

---

## TTL (Time-To-Live)

O default é **10 dias**. Configurável via `MetricsConfig(ttl_days=N)`.

Com `enable_partitioning=True` (default):
- A tabela é **partitioned by range** (`timestamp`).
- Partições diárias são criadas automaticamente.
- Uma rotina de background dropa partições mais antigas que `ttl_days`.
- **Zero custo de VACUUM**; liberação de espaço imediata.

Com `enable_partitioning=False`:
- Usa `DELETE` + `timestamp < now() - interval 'N days'`.
- Requer `pg_cron` ou job externo para eficiência.

---

## Fórmulas matemáticas

A lib coleta métricas brutas para alimentar modelos de
cost-forecasting (ex: GLM Gamma).

### 1. Custo por requisição (rateio em GKE)

$$C_{req} = \\frac{C_{cluster} \\times w_{req}}{\\sum_{i} w_{i}}$$

onde o peso $w_{req}$ é:

$$w_{req} = \\alpha \\cdot vCPU_{s} + \\beta \\cdot RAM_{GB \\cdot s} + \\gamma \\cdot \\frac{bytes}{10^{6}}$$

### 2. vCPU-seconds (aproximação)

$$vCPU_{s} \\approx \\sum_{k=1}^{n} cpu_{pct}^{(k)} \\times \\Delta t_{k} \\times vCPU_{alloc}$$

### 3. RAM-GB-seconds (aproximação)

$$RAM_{GB \\cdot s} \\approx \\sum_{k=1}^{n} \\frac{RSS_{MB}^{(k)}}{1024} \\times \\Delta t_{k}$$

### 4. Intervalo de confiança 95% (GLM Gamma, link log)

$$CI_{95} = \\left[ \\hat{\\mu} \\cdot e^{-1.96 \\cdot SE}, \\; \\hat{\\mu} \\cdot e^{+1.96 \\cdot SE} \\right]$$

---

## Schema PostgreSQL

```sql
CREATE TABLE IF NOT EXISTS api_metrics (
    id           BIGSERIAL,
    timestamp    TIMESTAMPTZ NOT NULL DEFAULT now(),
    request_id   UUID,
    process      TEXT NOT NULL,
    route        TEXT,
    method       TEXT,
    status_code  INTEGER,
    duration_s   DOUBLE PRECISION,
    cpu_time_s   DOUBLE PRECISION,
    mem_peak_mb  DOUBLE PRECISION,
    size_bytes   BIGINT,
    size_kind    TEXT,
    success      BOOLEAN,
    error_type   TEXT,
    dimensions   JSONB NOT NULL DEFAULT '{}'::jsonb
) PARTITION BY RANGE (timestamp);
```

Índices:
- `request_id`, `process`, `timestamp`
- GIN em `dimensions`

---

## Escrita no banco (writer)

- Fila `asyncio.Queue` (max 10.000 registros).
- Flush por **batch size** (50) ou **intervalo** (2s).
- `record()` é `put_nowait` — nunca bloqueia a requisição.
- Se a fila estiver cheia, a métrica é **descartada** com warning.
- `stop()` faz flush final — chame no shutdown.

---

## Testes

```bash
pip install -e ".[dev]"
pytest tests/ -v
```

Os testes usam `FakeWriter` (sem PostgreSQL real).

---

## Estrutura do projeto

```
pondus/
├── __init__.py          # API pública
├── config.py            # MetricsConfig + fórmulas
├── context.py           # contextvars
├── models.py            # MetricRecord
├── resource_usage.py    # CPU/mem (psutil/fallback)
├── db.py                # MetricsWriter + TTL + partitioning
├── decorator.py         # @track_metrics
└── middleware.py        # MetricsMiddleware
examples/
└── app_example.py       # App FastAPI completa
tests/
└── test_smoke.py        # Testes sem dependência de Postgres
```

---

## Licença

Este projeto é distribuído sob a licença **BSD 3-Clause**.

Consulte o arquivo [`LICENSE`](LICENSE) para mais detalhes.

---
