Inephany client library to use Metrana.
Project description
Metrana Client Library
Metrana is a metrics tracking client for ML/RL training runs. It provides a simple three-function API to log metrics from training loops to the Metrana ingestion service, with asynchronous batching, configurable backpressure handling, and automatic retry on failure.
Installation
pip install metrana
The metrana-protobuf dependency is pulled in automatically.
Quick Start
import metrana
metrana.init(
api_key="your-api-key",
workspace_name="my-workspace",
project_name="my-project",
run_name="run-001",
)
for step in range(1000):
loss, accuracy = train_step()
metrana.log("loss", loss)
metrana.log("accuracy", accuracy)
metrana.close()
The API key can also be provided via the METRANA_API_KEY environment variable, in which case api_key can be omitted from init().
API Reference
metrana.init()
Initialises the logger. Must be called once before log() or close().
metrana.init(
api_key: str,
workspace_name: str,
project_name: str,
run_name: str,
experiment_name: str | None = None,
# Behavioural strategies (can also be set via environment variables)
resume_strategy: str | None = None, # "Never" | "Allow"
backpressure_strategy: str | None = None, # "DropNew" | "Block" | "Raise"
error_strategy: str | None = None, # "Silent" | "Warn" | "RaiseOnLog" | "RaiseOnClose"
close_strategy: str | None = None, # "Immediate" | "CompletePending" | "CompleteAll"
log_level: str | None = None, # "Trace" | "Debug" | "Info" | "Success" | "Warn" | "Error" | "Critical" | "Off"
# Advanced
num_dispatch_workers: int = 4,
ingestion_url: str | None = None, # Overrides the default API endpoint
)
metrana.log()
Logs a single metric value. Thread-safe and non-blocking by default.
metrana.log(
metric_name: str, # Name of the metric series
value: float | int, # Metric value
scale: str | None = None, # "ML_STEP" | "EPISODE" | "ENVIRONMENT_STEP" (default: "ML_STEP")
step: int | None = None, # Explicit step index; auto-increments per series if omitted
labels: dict[str, str] | None = None, # Additional series labels
timestamp: int | None = None, # Unix nanoseconds; defaults to now
)
metrana.close()
Shuts down the logger. Behaviour depends on the configured close_strategy.
metrana.close()
Metric Scales
| Scale | Use when |
|---|---|
ML_STEP |
One entry per gradient update / training step (default) |
EPISODE |
One entry per RL episode |
ENVIRONMENT_STEP |
One entry per RL environment interaction |
Pass the scale name as a string or use metrana.StandardMetricScale:
from metrana import StandardMetricScale
metrana.log("reward", reward, scale=StandardMetricScale.EPISODE)
Strategies
Backpressure strategy
Controls what happens when the internal event queue is full.
| Value | Behaviour |
|---|---|
DropNew |
Silently discard the incoming event (default) |
Block |
Block the calling thread until space is available |
Raise |
Raise MetranaEventQueueFullError |
Error strategy
Controls how API errors are surfaced to the caller.
| Value | Behaviour |
|---|---|
Silent |
Ignore errors |
Warn |
Log a warning and continue (default) |
RaiseOnLog |
Raise on the next log() call if errors have occurred |
RaiseOnClose |
Raise on close() if errors have occurred |
Resume strategy
Controls what happens when a run with the same name already exists.
| Value | Behaviour |
|---|---|
Allow |
Create a new run or resume an existing one (default) |
Never |
Always create a new run; raise if it already exists |
Close strategy
Controls how pending events are handled on shutdown.
| Value | Behaviour |
|---|---|
Immediate |
Shut down immediately, discarding pending events |
CompletePending |
Complete API requests already in flight, but discard events still queued (default) |
CompleteAll |
Wait for all queued events including those not yet dispatched |
Environment Variables
All strategies and several other settings can be configured without code changes:
| Variable | Default | Accepted values |
|---|---|---|
METRANA_API_KEY |
— | Your API key |
METRANA_BACKPRESSURE_STRATEGY |
DropNew |
DropNew, Block, Raise |
METRANA_ERROR_MODES |
Warn |
Silent, Warn, RaiseOnLog, RaiseOnClose |
METRANA_RESUME_STRATEGY |
Allow |
Allow, Never |
METRANA_CLOSE_STRATEGY |
CompletePending |
Immediate, CompletePending, CompleteAll |
METRANA_LOG_LEVEL |
Success |
Trace, Debug, Info, Success, Warn, Error, Critical, Off |
METRANA_EVENT_QUEUE_MAX_SIZE |
unbounded | Integer (0 = unbounded) |
METRANA_DISPATCH_QUEUE_MAX_SIZE |
unbounded | Integer (0 = unbounded) |
METRANA_ERROR_QUEUE_MAX_SIZE |
unbounded | Integer (0 = unbounded) |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metrana-0.0.4.tar.gz.
File metadata
- Download URL: metrana-0.0.4.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5152b2bf3e1c54f0830165b86161880aa665ace0c1b431a1d2c457ee03172259
|
|
| MD5 |
ca33ed91e00b0a89d7a6b6053ccb0d6b
|
|
| BLAKE2b-256 |
a622bfc0e04fb44216b0a10716fbc61a988f63a633f80b6d2d3691c0ffae5999
|
File details
Details for the file metrana-0.0.4-py3-none-any.whl.
File metadata
- Download URL: metrana-0.0.4-py3-none-any.whl
- Upload date:
- Size: 31.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d55769c9a6e1fc5a4d946b67dd8ff7fdbd9982c3be36c381064b46036b377335
|
|
| MD5 |
f3fa2141fe56e2ebef5ab114b24b7948
|
|
| BLAKE2b-256 |
ebef0c3903e997f4ee6c407715a9a61a8b83022d90977cc143f409f9a3e11a95
|