{"id":19627720,"url":"https://github.com/maoertel/prometheus-push","last_synced_at":"2025-04-28T06:30:54.860Z","repository":{"id":206825931,"uuid":"717781467","full_name":"maoertel/prometheus-push","owner":"maoertel","description":"Extension for prometheus crates to push to a push-gateway","archived":false,"fork":false,"pushed_at":"2024-06-23T15:58:25.000Z","size":96,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-07T12:53:36.426Z","etag":null,"topics":["metrics","prometheus","pushgateway"],"latest_commit_sha":null,"homepage":"https://docs.rs/prometheus_push","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maoertel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-12T15:23:34.000Z","updated_at":"2024-08-15T13:15:10.000Z","dependencies_parsed_at":"2023-12-15T21:17:53.510Z","dependency_job_id":"d902f203-09f8-4e50-ac6e-d59e82add39e","html_url":"https://github.com/maoertel/prometheus-push","commit_stats":{"total_commits":33,"total_committers":2,"mean_commits":16.5,"dds":"0.24242424242424243","last_synced_commit":"d96891cf085c4bd08084441425977113debc9215"},"previous_names":["maoertel/prometheus-push"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maoertel%2Fprometheus-push","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maoertel%2Fprometheus-push/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maoertel%2Fprometheus-push/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maoertel%2Fprometheus-push/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maoertel","download_url":"https://codeload.github.com/maoertel/prometheus-push/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224099715,"owners_count":17255577,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["metrics","prometheus","pushgateway"],"created_at":"2024-11-11T11:52:39.891Z","updated_at":"2024-11-11T11:52:42.608Z","avatar_url":"https://github.com/maoertel.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prometheus Push :arrow_double_up:\n\n`prometheus_push` works as an extension to prometheus crates like [prometheus](https://crates.io/crates/prometheus) or\n[prometheus-client](https://crates.io/crates/prometheus-client) to be able to push non-blocking (default) or blocking to your Prometheus\npushgateway with a less dependent setup of `reqwest` (no `openssl` for example) or with an implementation of your own http client or even\nanother `prometheus` crate – this whole crate is completely generic so you are free to do whatever you want.\n\nIf you wanna use it with `reqwest`, `prometheus` or `prometheus-client` crates you literally do not have to implement anything (see\n[scenarios](#scenarios) below), as those common usages are already implemented as features within this crate.\n\nIn this crates stripped version you have to implement the `Push` trait (see [here](#implement-push-yourself)) to use it with your choice of\nhttp client or –as said– you can use the `with_reqwest` or `with_reqwest_blocking` features. These features already implement `Push` in a\n`PushClient` that leverages `reqwest` under the hood. Reqwest is set up without default features (minimal set) in this case so it should\nnot interfere with your own applications reqwest setup (e.g. `rust-tls`).\n\nAsync functionality (feature `non_blocking`) is considered the standard in this crate but you can enable the `blocking` feature to get the\nimplementation without async. You can enable the corresponding blocking `reqwest` implementation with the `with_reqwest_blocking` feature in\nwhich case you enable the `blocking` feature of the `reqwest` crate.\n\nIn terms of the underlying prometheus functionality you have to implement the `ConvertMetrics` trait  yourself (see [here](#implement-convertmetrics-yourself))\nor you use the already implemented feature `prometheus_crate` that leverages the [prometheus](https://crates.io/crates/prometheus) crate or\n`prometheus_client_crate` that uses the [prometheus-client](https://crates.io/crates/prometheus-client) crate.\n\n## Scenarios\n\n### 1. I use `reqwest` and `prometheus` crates in a **non-blocking** fashion\n\nIn your `Cargo.toml`:\n\n```toml\n[dependencies]\nprometheus_push = { version = \"\u003cversion\u003e\", default-features = false, features = [\"with_reqwest\", \"prometheus_crate\"] }\n```\n\n```rust\nuse prometheus::labels;\nuse prometheus_push::prometheus_crate::PrometheusMetricsPusher;\nuse reqwest::Client;\nuse url::Url;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let push_gateway: Url = Url::parse(\"\u003caddress to pushgateway\u003e\")?;\n    let client = Client::new();\n    let metrics_pusher = PrometheusMetricsPusher::from(client, \u0026push_gateway)?;\n    metrics_pusher\n        .push_all(\n            \"\u003cyour push jobs name\u003e\",\n            \u0026labels! { \"\u003clabel_name\u003e\" =\u003e \"\u003clabel_value\u003e\" },\n            prometheus::gather(),\n        )\n        .await?;\n\n    Ok(())\n}\n```\n\n### 2. I use `reqwest` and `prometheus` crates in a **blocking** fashion\n\nIn your `Cargo.toml`:\n\n```toml\n[dependencies]\nprometheus_push = { version = \"\u003cversion\u003e\", default-features = false, features = [\"with_reqwest_blocking\", \"prometheus_crate\"] }\n```\n\n```rust\nuse prometheus::labels;\nuse prometheus_push::prometheus_crate::PrometheusMetricsPusherBlocking;\nuse reqwest::blocking::Client;\nuse url::Url;\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let push_gateway: Url = Url::parse(\"\u003caddress to pushgateway\u003e\")?;\n    let client = Client::new();\n    let metrics_pusher = PrometheusMetricsPusherBlocking::from(client, \u0026push_gateway)?;\n    metrics_pusher\n        .push_all(\n            \"\u003cyour push jobs name\u003e\",\n            \u0026labels! { \"\u003clabel_name\u003e\" =\u003e \"\u003clabel_value\u003e\" },\n            prometheus::gather(),\n        )?;\n\n    Ok(())\n}\n```\n\n### 3. I use `reqwest` and `prometheus-client` crates in a **non-blocking** fashion\n\nIn your `Cargo.toml`:\n\n```toml\n[dependencies]\nprometheus_push = { version = \"\u003cversion\u003e\", default-features = false, features = [\"with_reqwest\", \"prometheus_client_crate\"] }\n```\n\n```rust\nuse prometheus_client::encoding::text::encode;\nuse prometheus_push::prometheus_client_crate::PrometheusClientMetricsPusher;\nuse reqwest::Client;\nuse url::Url;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let push_gateway: Url = Url::parse(\"\u003caddress to pushgateway\u003e\")?;\n    let client = Client::new();\n    let metrics_pusher = PrometheusClientMetricsPusher::create(client, \u0026push_gateway)?;\n    let grouping: HashMap\u003c\u0026str, \u0026str\u003e = HashMap::from([(\"\u003clabel_name\u003e\", \"\u003clabel_value\u003e\")]);\n    let mut metrics = String::new();\n    encode(\u0026mut metrics, \u0026registry)?;\n\n    metrics_pusher\n        .push_all(\n            \"\u003cyour push jobs name\u003e\",\n            \u0026grouping,\n            metrics,\n        )\n        .await?;\n\n    Ok(())\n}\n```\n\n### 4. I use `reqwest` and `prometheus-client` crates in a **blocking** fashion\n\nIn your `Cargo.toml`:\n\n```toml\n[dependencies]\nprometheus_push = { version = \"\u003cversion\u003e\", default-features = false, features = [\"with_reqwest_blocking\", \"prometheus_client_crate\"] }\n```\n\n```rust\nuse prometheus_client::encoding::text::encode;\nuse prometheus_push::prometheus_client_crate::PrometheusClientMetricsPusherBlocking;\nuse reqwest::blocking::Client;\nuse url::Url;\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let push_gateway: Url = Url::parse(\"\u003caddress to pushgateway\u003e\")?;\n    let client = Client::new();\n    let metrics_pusher = PrometheusClientMetricsPusherBlocking::create(client, \u0026push_gateway)?;\n    let grouping: HashMap\u003c\u0026str, \u0026str\u003e = HashMap::from([(\"\u003clabel_name\u003e\", \"\u003clabel_value\u003e\")]);\n    let mut metrics = String::new();\n    encode(\u0026mut metrics, \u0026registry)?;\n\n    metrics_pusher\n        .push_all(\n            \"\u003cyour push jobs name\u003e\",\n            \u0026grouping,\n            metrics,\n        )?;\n\n    Ok(())\n}\n```\n\n### 5. I want to implement everything myself\n\nIn case you wanna implement everything yourself you can do so by implementing the `Push` trait and the `ConvertMetrics` trait.\n\n#### Implement `Push` yourself\n\nIf you are not using reqwest as an http client you are free to implement the `Push` traits two methods yourself. As a guide you can use the\nimplementation of the `with_reqwest` feature (see [here](https://github.com/maoertel/prometheus-push/blob/7fe1946dd143f4870beb80e642b0acb7854a3cb8/src/with_reqwest.rs)).\nBasically it is as simple as that.\n\n```rust\nuse prometheus_push::Push;\n\npub struct YourPushClient;\n\nimpl Push\u003cVec\u003cu8\u003e\u003e for YourPushClient {\n    async fn push_all(\u0026self, url: \u0026Url, body: Vec\u003cu8\u003e, content_type: \u0026str) -\u003e Result\u003c()\u003e {\n        // implement a PUT request with your client with this body and `content_type` in header\n    }\n\n    async fn push_add(\u0026self, url: \u0026Url, body: Vec\u003cu8\u003e, content_type: \u0026str) -\u003e Result\u003c()\u003e {\n        // implement a POST request with your client with this body and `content_type` in header\n    }\n}\n```\n\n#### Implement `ConvertMetrics` yourself\n\nIn case you want to use another prometheus client implementation you can implement your own type that implements\nthe `ConvertMetrics` trait to inject it into your instance of `MetricsPusher`.\n\n```rust\nimpl ConvertMetrics\u003cVec\u003cYourMetricFamily\u003e, Vec\u003cBox\u003cdyn YourCollector\u003e\u003e, Vec\u003cu8\u003e\u003e for YourMetricsConverter {\n    fn metric_families_from(\n        \u0026self,\n        collectors: Vec\u003cBox\u003cdyn YourCollector\u003e\u003e,\n    ) -\u003e Result\u003cVec\u003cYourMetricFamily\u003e\u003e {\n        // implement the conversion from your Collectors to your MetricsFamilies, or whatever\n        // your generic `MF` type stands for\n    }\n\n    fn create_push_details(\n        \u0026self,\n        job: \u0026str,\n        url: \u0026Url,\n        grouping: \u0026HashMap\u003c\u0026str, \u0026str\u003e,\n        metric_families: Vec\u003cYourMetricFamily\u003e,\n    ) -\u003e Result\u003c(Url, Vec\u003cu8\u003e, String)\u003e {\n        // create your push details for the `Push` methods: Url, body and content type\n    }\n}\n```\n\n## Features\n\n- `default`: by default async functionality and no reqwest is enabled\n- `non_blocking`: this ennables the async functionality\n- `blocking`: on top of the default feature you get the same functionality in a blocking fashion\n- `with_reqwest`: this feature enables the `non_blocking` feature as well as `reqwest` in minimal configuration and enables the alredy implemented `PushClient`\n- `with_reqwest_blocking`: like `with_reqwest` but including `blocking` instead of `non_blocking`\n- `prometheus_crate`: enables the functionality of the [prometheus](https://crates.io/crates/prometheus) crate\n- `prometheus_client_crate`: enables the functionality of the [prometheus-client](https://crates.io/crates/prometheus-client) crate\n\n## License\n\n[MIT](./LICENSE-MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaoertel%2Fprometheus-push","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaoertel%2Fprometheus-push","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaoertel%2Fprometheus-push/lists"}