prom-mock-rs
A mock Prometheus HTTP API server for integration testing, available both as a library and CLI application.
Overview
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.
Features
- Remote Write API: Compatible with Prometheus remote write protocol
- Query API: Basic query endpoint support for stored metrics
- Fixture System: YAML-based predefined responses
- Mock Behavior: Configurable latency and error rates
- In-Memory Storage: Temporary metrics storage for testing scenarios
- Library API: Use as a Rust library in your tests
- CLI Application: Standalone server for integration testing
Installation
Prerequisites
This library requires Rust 1.76.0 or later.
As a CLI tool
As a library dependency
[]
= "0.1.0"
Usage
CLI Application
Basic Usage
With Fixtures
Configuration Options
--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)
Library Usage
use Arc;
use ;
async
API Endpoints
POST /api/v1/write- Remote write endpointGET /api/v1/query- Query endpointGET /health- Health check
Fixture Format
version: 1
defaults:
status: 200
latency: "100ms"
routes:
- path: "/api/v1/query"
method: "GET"
response:
status: 200
body: |
{"status":"success","data":{"resultType":"vector","result":[]}}
Development
# Run tests
# Run the CLI with development settings
# Build the library
# Check library documentation
Library API
The library exposes several key components:
- Storage Traits:
StorageandMetadataStoragefor implementing custom backends - Memory Storage:
MemoryStorage- ready-to-use in-memory implementation - Query Engine:
SimpleQueryEnginefor parsing and executing basic PromQL queries - Label Matchers: Extensible label filtering (
EqualMatcher,RegexMatcher, etc.) - HTTP Layer:
build_router()andAppStatefor creating HTTP servers - Fixtures:
FixtureBookfor loading predefined responses