This commit is contained in:
Robin Appelman 2024-02-12 19:55:42 +01:00
commit 6de6e7dc3a
3 changed files with 23 additions and 17 deletions

View file

@ -1,11 +1,12 @@
[package] [package]
name = "prometheus-edge-detector" name = "prometheus-edge-detector"
version = "0.2.0" version = "0.3.0"
authors = ["Robin Appelman <robin@icewind.nl>"] authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018" edition = "2018"
description = "Find the most recent rising or dropping edge from a prometheus query" description = "Find the most recent rising or dropping edge from a prometheus query"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
repository = "https://github.com/icewind1991/prometheus-edge-detector" repository = "https://github.com/icewind1991/prometheus-edge-detector"
rust-version = "1.63.0"
[dependencies] [dependencies]
tokio = { version = "1.36", features = ["time"] } tokio = { version = "1.36", features = ["time"] }

View file

@ -17,7 +17,7 @@
}; };
in rec { in rec {
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [cargo rustc clippy bacon cargo-edit cargo-msrv pkg-config openssl]; nativeBuildInputs = with pkgs; [cargo rustc clippy bacon cargo-edit cargo-semver-checks pkg-config openssl];
}; };
}); });
} }

View file

@ -6,6 +6,7 @@ use std::time::SystemTime;
use tokio::time::Duration; use tokio::time::Duration;
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error { pub enum Error {
#[error("Network error: {0}")] #[error("Network error: {0}")]
Network(reqwest::Error), Network(reqwest::Error),
@ -13,21 +14,25 @@ pub enum Error {
MalformedResponse(reqwest::Error), MalformedResponse(reqwest::Error),
#[error("Data point is not an integer: {0}")] #[error("Data point is not an integer: {0}")]
NonNumericDataPoint(String), NonNumericDataPoint(String),
#[error("Prometheus returned an error for the query")] #[error("Prometheus returned a {error_type} error for the query: {error}")]
PrometheusError PrometheusError{
error_type: String,
error: String,
}
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(tag = "status")]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum QueryResultStatus { enum QueryResult {
Success, Success {
Error, data: QueryResultData
},
Error {
#[serde(rename = "errorType")]
error_type: String,
error: String,
} }
#[derive(Debug, Clone, Deserialize)]
struct QueryResult {
status: QueryResultStatus,
data: QueryResultData,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -36,7 +41,7 @@ enum QueryResultDataType {
Matrix, Matrix,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct QueryResultData { struct QueryResultData {
result: Vec<QueryResultDataResult>, result: Vec<QueryResultDataResult>,
@ -73,9 +78,9 @@ async fn query_prometheus(
.await .await
.map_err(Error::MalformedResponse)?; .map_err(Error::MalformedResponse)?;
match result.status { match result {
QueryResultStatus::Success => Ok(result.data), QueryResult::Success{data} => Ok(data),
QueryResultStatus::Error => Err(Error::PrometheusError), QueryResult::Error {error, error_type} => Err(Error::PrometheusError{error_type, error}),
} }
} }