Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/.pytest_cache/
/.mypy_cache/
/.ruff_cache/
/.idea
uv.lock
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Documentation

This directory contains project documentation, organized by stage and purpose:

## Directories

- **`brd/` (Business Requirements Documents)**
Ideas and high-level business requirements describing *what* needs to be built and *why*.
*Naming example:* `BRD001-runtime-metrics.md`

- **`prd/` (Product Requirements Documents)**
Detailed product and technical requirements used as actionable input for coding agents describing *how* features should be implemented.
*Naming example:* `PRD001-metrics-collector.md`
16 changes: 16 additions & 0 deletions docs/brd/.sample-BRD001-runtime-metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# BRD001: SDK Runtime Metrics

## Problem
Users running `proofline-sdk` in production have no built-in way to observe request latency, retry counts, or error rates without wrapping every client call manually.

## Goal
Provide lightweight runtime metrics hooks in `ProoflineClient` and `AsyncProoflineClient` so applications can track SDK request performance and reliability.

## Business Value
- Faster troubleshooting of slow or failing API calls.
- Easy integration with standard monitoring tools (Prometheus, OpenTelemetry, Datadog) without adding heavy SDK dependencies.

## Success Criteria
- Users can pass an optional metrics callback/collector to the client.
- Every HTTP request records duration, status code, and retry count.
- Zero overhead when metrics collection is not configured.
16 changes: 16 additions & 0 deletions docs/brd/BRD001-runtime-metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# BRD001: SDK Runtime Metrics

## Problem
Users running `proofline-sdk` in production have no built-in way to observe request latency, retry counts, or error rates without wrapping every client call manually.

## Goal
Provide lightweight runtime metrics hooks in `ProoflineClient` and `AsyncProoflineClient` so applications can track SDK request performance and reliability.

## Business Value
- Faster troubleshooting of slow or failing API calls.
- Easy integration with standard monitoring tools (Prometheus, OpenTelemetry, Datadog) without adding heavy SDK dependencies.

## Success Criteria
- Users can pass an optional metrics callback/collector to the client.
- Every HTTP request records duration, status code, and retry count.
- Zero overhead when metrics collection is not configured.
35 changes: 35 additions & 0 deletions docs/prd/.sample-PRD001-metrics-collector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# PRD001: Metrics Collector

**BRD:** [BRD001: SDK Runtime Metrics](../brd/.sample-BRD001-runtime-metrics.md)

## Overview
Add an optional metrics collector interface to `ProoflineClient` and `AsyncProoflineClient` that receives a telemetry event after each HTTP request completes or fails.

## Specification

### 1. Data Model (`proofline/metrics.py`)
Define an immutable event and collector protocol:
- `RequestMetric(method: str, path: str, status_code: int | None, duration_ms: float, retries: int, error: str | None)` (frozen dataclass)
- `MetricsCollector`: `Protocol` with `record(self, metric: RequestMetric) -> None`

### 2. Transport Integration (`proofline/_transport.py`, `proofline/client.py`)
- Accept `metrics: MetricsCollector | None = None` in `ProoflineClient.__init__` and `AsyncProoflineClient.__init__`.
- Measure request wall-clock time using `time.perf_counter()`.
- Emit `RequestMetric` via `metrics.record(...)` once per completed operation (including retries).
- Never let exceptions raised inside `metrics.record` break SDK calls (ignore or log safely).
- Redact API token from `error` if present.

### 3. Public Exports (`proofline/__init__.py`)
- Export `RequestMetric` and `MetricsCollector` in `__all__`.

## Target Files
- `proofline/metrics.py` (new)
- `proofline/_transport.py`
- `proofline/client.py`
- `proofline/__init__.py`
- `tests/test_metrics.py` (new)

## Acceptance Criteria
- [ ] Sync and async clients emit `RequestMetric` with accurate status, retry count, and `duration_ms >= 0`.
- [ ] Faulty `MetricsCollector.record` raising an exception does not fail the client request.
- [ ] `pytest` and `mypy` pass with 100% type coverage on new code.
61 changes: 61 additions & 0 deletions docs/prd/PRD001-metrics-collector.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# PRD001: Metrics Collector

**BRD:** [BRD001: SDK Runtime Metrics](../brd/BRD001-runtime-metrics.md)

## Overview
Add a metrics collector and proxy architecture to `ProoflineClient` and `AsyncProoflineClient`. `MetricsCollector` captures runtime and caller metadata for each SDK operation and pushes events to a `MetricsProxy` backend. By default, `MetricsCollector` uses `LocalStorageMetricsProxy`, which appends records to a daily CSV report (`metrics/{date}.csv`).

## Specification

### 1. Data Model & Proxy Abstraction (`proofline/metrics.py`)
- **`RequestMetric` (frozen dataclass):**
- `timestamp: str` (ISO-8601 UTC)
- `file_path: str` (full path to the source file)
- `class_name: str | None` (enclosing class name if applicable, else `None`/empty)
- `method_name: str` (function or method name)
- `http_method: str`
- `path: str`
- `status_code: int | None`
- `duration_ms: float`
- `retries: int`
- `error: str | None`
- **`MetricsProxy` (abstract base class `ABC`):**
- Responsible for forwarding metrics to a concrete backend (e.g., Prometheus, OpenTelemetry, local storage).
- Defines `@abstractmethod def push(self, metric: RequestMetric) -> None`.
- **`LocalStorageMetricsProxy(MetricsProxy)`:**
- Only concrete proxy implemented in this PRD; used as the **default** proxy.
- Writes to `metrics/{date}.csv` (with `{date}` formatted as `YYYY-MM-DD`) in append-only mode (`"a"`).
- Automatically creates the `metrics/` directory and writes the CSV header row when creating a new file.
- CSV columns: `timestamp,file_path,class_name,method_name,http_method,path,status_code,duration_ms,retries,error`.
- **`MetricsCollector`:**
- Initialized with `proxy: MetricsProxy | None = None`, defaulting to `LocalStorageMetricsProxy()`.
- `record(self, metric: RequestMetric) -> None` pushes the collected metric to `self.proxy.push(metric)`.

### 2. Transport & Client Integration (`proofline/_transport.py`, `proofline/client.py`)
- Accept `metrics: MetricsCollector | None = None` in `ProoflineClient.__init__` and `AsyncProoflineClient.__init__` (or default `MetricsCollector(LocalStorageMetricsProxy())` when enabled).
- Capture caller/execution context (`file_path` as full resolved path, `class_name` if called on a class instance, `method_name`) and measure request wall-clock time via `time.perf_counter()`.
- Emit `RequestMetric` via `metrics.record(...)` once per completed operation (including retries), which delegates to `proxy.push(...)`.
- Never let exceptions raised inside `metrics.record` or `proxy.push` break SDK calls (ignore or log safely).
- Redact API token from `error` if present.

### 3. Public Exports (`proofline/__init__.py`)
- Export `RequestMetric`, `MetricsProxy`, `LocalStorageMetricsProxy`, and `MetricsCollector` in `__all__`.

### 4. Testing (`tests/test_metrics.py`)
- Implement all metrics collector and proxy tests in a dedicated test file (`tests/test_metrics.py`), separate from existing client/transport test suites.

## Target Files
- `proofline/metrics.py` (new)
- `proofline/_transport.py`
- `proofline/client.py`
- `proofline/__init__.py`
- `tests/test_metrics.py` (new, dedicated test file for metrics collector)

## Acceptance Criteria
- [ ] `MetricsProxy` is defined as an `ABC` and `MetricsCollector` pushes metrics to its configured proxy.
- [ ] `MetricsCollector()` defaults to `LocalStorageMetricsProxy`.
- [ ] `LocalStorageMetricsProxy` appends metrics to `metrics/{date}.csv` with header and full file path, class (if applicable), method, and request metrics data.
- [ ] Sync and async clients emit `RequestMetric` with accurate status, retry count, and `duration_ms >= 0`.
- [ ] Faulty `MetricsProxy.push` raising an exception does not fail the client request.
- [ ] All metrics collector tests are isolated in their own test file (`tests/test_metrics.py`).
- [ ] `pytest` and `mypy` pass with 100% type coverage on new code.
5 changes: 5 additions & 0 deletions proofline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from ._transport import ProoflineError, RunNowRefusal
from .client import AsyncProoflineClient, ProoflineClient, ReviewFilters
from .exports import ExportDataset, ExportFormat, ExportPage
from .metrics import LocalStorageMetricsProxy, MetricsCollector, MetricsProxy, RequestMetric
from .outcomes import AssessmentGap, CheckGap, ReviewOutcome

__all__ = [
Expand All @@ -55,13 +56,17 @@
"ExportPage",
"FileReviewRequest",
"FileReviewResponse",
"LocalStorageMetricsProxy",
"MetricsCollector",
"MetricsProxy",
"PatchReviewRequest",
"PatchReviewResponse",
"PatchTriggerSource",
"ProoflineClient",
"ProoflineError",
"RepositoryGuidance",
"RepositoryNames",
"RequestMetric",
"RerunResponse",
"ReviewFilters",
"ReviewOutcome",
Expand Down
Loading