Parse, edit and merge Prometheus metrics exposition format

Overview

promerge

Promerge provides minimalistic and easy to use API to parse and manipulate Prometheus metrics. A simple usecase could be collecting metrics from several servers and combining and exposing them as single metrics endpoint, adding namespace and custom (Key,Value) pairs to each Prometheus exposition line.

Example:

use promerge::promerge::Context;

fn main() {
    // example from: https://prometheus.io/docs/instrumenting/exposition_formats/
    let metrics = r#"# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
http_requests_total{method="post",code="400"}    3 1395066363000

# Escaping in label values:
msdos_file_access_time_seconds{path="C:\\DIR\\FILE.TXT",error="Cannot find file:\n\"FILE.TXT\""} 1.458255915e9

# Minimalistic line:
metric_without_timestamp_and_labels 12.47

# A weird metric from before the epoch:
something_weird{problem="division by zero"} +Inf -3982045

# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320

# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693"#;

    // add new (k,v) pairs into Prometheus metric exposition
    let pairs = [("custom_key".into(), "custom_value".into())];
    // add a prefix to metrics
    let mut metrics = Context::with_prefix_and_pairs(&metrics, "some_prefix_", &pairs);
	
    // parse and evaluate
    {
	let ctx = &mut metrics;
	match ctx.run() {
	    Ok(output) => {
		println!("first run: {}", output);
	    },
	    Err(err) => {
		eprintln!("{}", err);
	    }
	};
    }
	
    // append new metrics with difference prefix
    {
        let block = r#"# New minimalistic line:
new_metric_without_timestamp_and_labels 24.81"#;	
	let ctx = &mut metrics;
	match ctx.combine_with_prefix(block, "second_prefix_") {
	    Ok(output) => {
		println!("second run: {}", output);
	    },
	    Err(err) => {
		eprintln!("{}", err);
	    }
	};
    }

    /* Final Output ->
    
    # HELP some_prefix_http_requests_total The total number of HTTP requests.
    # TYPE some_prefix_http_requests_total counter
    some_prefix_http_requests_total{method="post",code="200",custom_key="custom_value"} 1027 1395066363000
    some_prefix_http_requests_total{method="post",code="400",custom_key="custom_value"} 3 1395066363000

    # Escaping in label values:
    some_prefix_msdos_file_access_time_seconds{path="C:\\DIR\\FILE.TXT",error="Cannot find file:\n\"FILE.TXT\"",custom_key="custom_value"} 1.458255915e9

    # Minimalistic line:
    some_prefix_metric_without_timestamp_and_labels{custom_key="custom_value"} 12.47

    # A weird metric from before the epoch:
    some_prefix_something_weird{problem="division by zero",custom_key="custom_value"} +Inf -3982045

    # A histogram, which has a pretty complex representation in the text format:
    # HELP some_prefix_http_request_duration_seconds A histogram of the request duration.
    # TYPE some_prefix_http_request_duration_seconds histogram
    some_prefix_http_request_duration_seconds_bucket{le="0.05",custom_key="custom_value"} 24054
    some_prefix_http_request_duration_seconds_bucket{le="0.1",custom_key="custom_value"} 33444
    some_prefix_http_request_duration_seconds_bucket{le="0.2",custom_key="custom_value"} 100392
    some_prefix_http_request_duration_seconds_bucket{le="0.5",custom_key="custom_value"} 129389
    some_prefix_http_request_duration_seconds_bucket{le="1",custom_key="custom_value"} 133988
    some_prefix_http_request_duration_seconds_bucket{le="+Inf",custom_key="custom_value"} 144320
    some_prefix_http_request_duration_seconds_sum 53423
    some_prefix_http_request_duration_seconds_count 144320

    # Finally a summary, which has a complex representation, too:
    # HELP some_prefix_rpc_duration_seconds A summary of the RPC duration in seconds.
    # TYPE some_prefix_rpc_duration_seconds summary
    some_prefix_rpc_duration_seconds{quantile="0.01",custom_key="custom_value"} 3102
    some_prefix_rpc_duration_seconds{quantile="0.05",custom_key="custom_value"} 3272
    some_prefix_rpc_duration_seconds{quantile="0.5",custom_key="custom_value"} 4773
    some_prefix_rpc_duration_seconds{quantile="0.9",custom_key="custom_value"} 9001
    some_prefix_rpc_duration_seconds{quantile="0.99",custom_key="custom_value"} 76656
    some_prefix_rpc_duration_seconds_sum 1.7560473e+07
    some_prefix_rpc_duration_seconds_count 2693

    # New minimalistic line:
    second_prefix_new_metric_without_timestamp_and_labels 24.81
    
     */
}
You might also like...
A Prometheus Aggregation Gateway for FAAS applications

Gravel Gateway Gravel Gateway is a Prometheus Push Gateway for FAAS applications. In particular it allows aggregation to be controlled by the incoming

A Prometheus exporter for WireGuard

wireguard_exporter An asynchronous Prometheus exporter for wireguard wireguard_exporter runs wg show [..] and scrapes the output to build Prometheus m

Export statistics of Mosquitto MQTT broker (topic: $SYS) to Prometheus

Preface The Mosquitto MQTT broker provides a number of statistics on the special $SYS/# topic (see mosquitto(8)). Build requirements As a Rust program

Prometheus instrumentation service for the NGINX RTMP module.

nginx-rtmp-exporter Prometheus instrumentation service for the NGINX RTMP module. Usage nginx-rtmp-exporter [OPTIONS] --scrape-url SCRAPE_URL O

A metrics collection application for Linux machines. Created for MSCS 710 Project at Marist College.

Linux-Metrics-Collector A metrics collection application for Linux machines. Created for MSCS 710 Project at Marist College. Development Environment S

🚀 10x easier, 🚀 10x cheaper, 🚀 high performance,  🚀 petabyte scale - Elasticsearch/Splunk/Datadog alternative for 🚀 (logs, metrics, traces).
🚀 10x easier, 🚀 10x cheaper, 🚀 high performance, 🚀 petabyte scale - Elasticsearch/Splunk/Datadog alternative for 🚀 (logs, metrics, traces).

🚀 10x easier, 🚀 10x cheaper, 🚀 petabyte scale - Elasticsearch/Splunk/Datadog alternative for 🚀 (logs, metrics, traces). ZincObserve ZincObserve is

An end-to-end encrypted, anonymous IP-hiding, decentralized, audio/video/file sharing/offline messaging multi-device platform built for both communications and application security and performance.

An end-to-end encrypted, anonymous IP-hiding, decentralized, audio/video/file sharing/offline messaging multi-device platform built for both communications and application security and performance.

A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption
A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption

A fast, stable, efficient, and lightweight intranet penetration, port forwarding tool supports multiple connections, cascading proxy, and transmission encryption

Owner
Mike Taghavi
Software Developer
Mike Taghavi
An asynchronous dumb exporter proxy for prometheus. This aggregates all the metrics and exposes as a single scrape endpoint.

A dumb light weight asynchronous exporter proxy This is a dumb lightweight asynchronous exporter proxy that will help to expose multiple application m

Dark streams 3 Dec 4, 2022
Easily add metrics to your system -- and actually understand them using automatically customized Prometheus queries

Autometrics ?? ✨ Autometrics is a macro that makes it trivial to add useful metrics to any function in your codebase. Easily understand and debug your

Fiberplane 341 Feb 6, 2023
Easily add metrics to your system -- and actually understand them using automatically customized Prometheus queries

A Rust macro that makes it easy to understand the error rate, response time, and production usage of any function in your code. Jump from your IDE to

Autometrics 462 Mar 6, 2023
A minimal, allocation-free Prometheus/OpenMetrics metrics implementation for `no-std` and embedded Rust.

tinymetrics a minimal, allocation-free Prometheus/OpenMetrics metrics implementation for no-std and embedded projects. why should you use it? you may

Eliza Weisman 282 Apr 16, 2023
`prometheus` backend for `metrics` crate

metrics + prometheus = ❤️ API Docs | Changelog prometheus backend for metrics crate. Motivation Rust has at least two ecosystems regarding metrics col

Instrumentisto Team 2 Dec 17, 2022
🔍 Fully-featured metrics collection agent for First Tech Challenge competitions. Supports Prometheus.

Scout Scout is a fully-featured free and open source metrics collector for FTC competitions. The project is licensed under the GNU LGPLv3 license. Fea

hivemind 3 Oct 24, 2023
Druid Exporter plays a fundamental role as a receiver of metrics events coming from Druid clusters, adopting the HTTP format as a means of communication

Druid Exporter plays a fundamental role as a receiver of metrics events coming from Druid clusters, adopting the HTTP format as a means of communication. In addition to this capability, its primary function is to export these metrics to Prometheus, thus allowing the creation of meaningful graphs and visualizations.

Kiwfy 3 Sep 21, 2023
Druid Exporter plays a fundamental role as a receiver of metrics events coming from Druid clusters, adopting the HTTP format as a means of communication.

Druid Exporter plays a fundamental role as a receiver of metrics events coming from Druid clusters, adopting the HTTP format as a means of communication. In addition to this capability, its primary function is to export these metrics to Prometheus, thus allowing the creation of meaningful graphs and visualizations.

Not Empty Free Software Foundation 5 Oct 24, 2023
An asynchronous Prometheus exporter for iptables

iptables_exporter An asynchronous Prometheus exporter for iptables iptables_exporter runs iptables-save --counter and scrapes the output to build Prom

Kevin K. 21 Dec 29, 2022
A Prometheus Aggregation Gateway for FAAS applications

Gravel Gateway Gravel Gateway is a Prometheus Push Gateway for FAAS applications. In particular it allows aggregation to be controlled by the incoming

Colin Douch 85 Nov 23, 2022