prometheus-parse

A simple, dependency-light Rust parser for the Prometheus exposition text format.

Library
Cargo
v0.2.5
15stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
22/100Needs Attention
Development Activity0
Maintenance0
Community16
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture68
Code Quality72
Innovation74
Learning Curve80

prometheus-parse is a focused Rust library that reads the Prometheus scrape (exposition) text format and turns it into strongly typed metric samples. It handles HELP and TYPE metadata lines, counters, gauges, histograms, summaries, and untyped metrics, resolving labels, values, and timestamps into ready-to-use structures.

Built as a single, self-contained module with minimal dependencies, it is designed to be dropped into monitoring tooling, exporters, and scrapers that need to consume metrics emitted by Prometheus-compatible endpoints without pulling in a full client stack.

What You Get

  • A Scrape::parse entry point that consumes an iterator of lines and returns typed samples
  • Typed SampleType and Value enums covering counters, gauges, histograms, summaries, and untyped metrics
  • Automatic parsing of HELP and TYPE metadata plus label, value, and timestamp extraction
  • A small dependency footprint (chrono, regex, itertools, once_cell) suitable for embedding

Common Use Cases

  • Building custom Prometheus exporters or scrapers that read metrics from remote endpoints
  • Writing monitoring and alerting tools that need to inspect scrape output programmatically
  • Testing and validating metrics emitted by services against expected samples

Under The Hood

Architecture - The entire library lives in a single src/lib.rs module. Parsing is driven by three lazily-compiled regexes (HELP_RE, TYPE_RE, SAMPLE_RE) wrapped in once_cell Lazy statics; LineInfo::parse classifies each line into Doc, Type, Sample, Empty, or Ignored, and Scrape::parse folds those into a map of metric families and a vector of typed Sample values with resolved labels, values, and timestamps.

Tech Stack - Written for Rust 2018 edition with a deliberately small dependency set: chrono (clock feature) for timestamps, regex for line parsing, itertools for iterator ergonomics, and once_cell for lazy statics. It builds as a plain cargo library crate with no build scripts.

Code Quality - The code is compact and idiomatic, using enums to model line and sample types and borrowing (&str slices) to avoid unnecessary allocation on the hot path. A #[cfg(test)] module provides five unit tests covering line classification and full scrape parsing.

API Design - The public surface is small and predictable: construct a Scrape from an iterator of io::Result<String> lines via Scrape::parse, then read strongly typed Sample structs. This keeps integration boilerplate minimal and mirrors how a caller would stream an HTTP response body line by line.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search