| Crates.io | prom-mock-rs |
| lib.rs | prom-mock-rs |
| version | 0.2.0 |
| created_at | 2025-08-14 20:19:52.037014+00 |
| updated_at | 2025-08-19 09:48:03.35314+00 |
| description | A mock Prometheus HTTP API server for integration testing |
| homepage | https://github.com/dsemak/prom-mock-rs |
| repository | https://github.com/dsemak/prom-mock-rs |
| max_upload_size | |
| id | 1795684 |
| size | 216,664 |
A mock Prometheus HTTP API server for integration testing, available both as a library and CLI application.
This project provides a lightweight mock implementation of the Prometheus HTTP API, designed for testing applications that integrate with Prometheus. It supports configurable response fixtures, artificial latency, error injection, and in-memory metrics storage.
This library requires Rust 1.76.0 or later.
cargo install prom-mock-rs
[dependencies]
prom-mock-rs = "0.1.0"
prom-mock --listen 127.0.0.1:9090
prom-mock --fixtures fixtures.yaml --listen 127.0.0.1:9090
--listen: Address to listen on (default: 127.0.0.1:19090)--fixtures: Path to YAML fixture file--latency: Artificial response delay (e.g., 100ms, 1s)--error-rate: Probability of 503 errors (0.0-1.0)--fixed-now: Fixed "now" time for testing (ISO-8601 format)use std::sync::Arc;
use prom_mock_rs::{MemoryStorage, http::build_router};
#[tokio::main]
async fn main() -> std::io::Result<()> {
// Create storage and app state
let storage = Arc::new(MemoryStorage::new());
let state = prom_mock_rs::http::AppState::builder()
.with_storage(storage)
.build()?;
// Build the router
let app = build_router(state);
// Serve with axum
let listener = tokio::net::TcpListener::bind("127.0.0.1:19090").await?;
axum::serve(listener, app).await
}
POST /api/v1/write - Remote write endpointGET /api/v1/query - Query endpointGET /health - Health checkversion: 1
defaults:
status: 200
latency: "100ms"
routes:
- path: "/api/v1/query"
method: "GET"
response:
status: 200
body: |
{"status":"success","data":{"resultType":"vector","result":[]}}
# Run tests
cargo test
# Run the CLI with development settings
cargo run --bin prom-mock -- --listen 127.0.0.1:9090 --fixtures tests/fixtures.yaml
# Build the library
cargo build
# Check library documentation
cargo doc --open
The library exposes several key components:
Storage and MetadataStorage for implementing custom backendsMemoryStorage - ready-to-use in-memory implementationSimpleQueryEngine for parsing and executing basic PromQL queriesEqualMatcher, RegexMatcher, etc.)build_router() and AppState for creating HTTP serversFixtureBook for loading predefined responses