From 48cb5c02c62202e2862c23ceeb706a840932e9cd Mon Sep 17 00:00:00 2001 From: Jeff Kreeftmeijer Date: Mon, 22 Apr 2024 17:00:42 +0200 Subject: [PATCH 1/7] Build tags in extract_metrics For adding new metric types (like for Kubernetes pods), the tag keys need to be different ("pod" instead of "node"). Previously, the tag keys were hardcoded in AppSignalMetric::new. This patch moves the tag creation to extract_metrics, which passes the tags as a HashMap. --- src/main.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e4eb1d..0cdcb6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,11 +20,7 @@ struct AppsignalMetric { } impl AppsignalMetric { - pub fn new(metric_name: &str, node_name: &str, value: &serde_json::Value) -> AppsignalMetric { - // Create tags - let mut tags = HashMap::with_capacity(1); - tags.insert("node".to_owned(), node_name.to_owned()); - + pub fn new(metric_name: &str, tags: HashMap, value: &serde_json::Value) -> AppsignalMetric { // See if we can use value let value = match value { Value::Number(value) => match value.as_f64() { @@ -166,7 +162,10 @@ fn extract_metrics(results: Value, node_name: &str, out: &mut Vec Date: Mon, 22 Apr 2024 17:15:29 +0200 Subject: [PATCH 2/7] Rename extract_metrics to extract_node_metrics --- src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0cdcb6f..9cd8eb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ async fn run() -> Result<(), Error> { .request::(kube_request) .await?; - extract_metrics(kube_response, &name, &mut out); + extract_node_metrics(kube_response, &name, &mut out); } let json = serde_json::to_string(&out).expect("Could not serialize JSON"); @@ -89,7 +89,7 @@ async fn run() -> Result<(), Error> { Ok(()) } -fn extract_metrics(results: Value, node_name: &str, out: &mut Vec) { +fn extract_node_metrics(results: Value, node_name: &str, out: &mut Vec) { for (metric_name, metric_value) in [ ( "node_cpu_usage_nano_cores", @@ -171,15 +171,15 @@ fn extract_metrics(results: Value, node_name: &str, out: &mut Vec Date: Tue, 23 Apr 2024 13:17:15 +0200 Subject: [PATCH 3/7] Format src/main.rs cargo-fmt --- src/main.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9cd8eb6..5014359 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,11 @@ struct AppsignalMetric { } impl AppsignalMetric { - pub fn new(metric_name: &str, tags: HashMap, value: &serde_json::Value) -> AppsignalMetric { + pub fn new( + metric_name: &str, + tags: HashMap, + value: &serde_json::Value, + ) -> AppsignalMetric { // See if we can use value let value = match value { Value::Number(value) => match value.as_f64() { @@ -45,7 +49,7 @@ async fn main() -> Result<(), Error> { let mut interval = tokio::time::interval(duration); loop { - interval.tick().await; + interval.tick().await; run().await.expect("Failed to extract metrics.") } } @@ -70,7 +74,8 @@ async fn run() -> Result<(), Error> { let json = serde_json::to_string(&out).expect("Could not serialize JSON"); - let endpoint = env::var("APPSIGNAL_ENDPOINT").unwrap_or("https://appsignal-endpoint.net".to_owned()); + let endpoint = + env::var("APPSIGNAL_ENDPOINT").unwrap_or("https://appsignal-endpoint.net".to_owned()); let api_key = env::var("APPSIGNAL_API_KEY").expect("APPSIGNAL_API_KEY not set"); let base = Url::parse(&endpoint).expect("Could not parse endpoint"); let path = format!("metrics/json?api_key={}", api_key); @@ -162,7 +167,6 @@ fn extract_node_metrics(results: Value, node_name: &str, out: &mut Vec Date: Thu, 2 May 2024 16:26:49 +0200 Subject: [PATCH 4/7] Add ci workflow --- .github/workflows/ci.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..dbfe814 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,13 @@ +name: CI + +on: + push: + pull_request: + +jobs: + ci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: cargo build --verbose + - run: cargo test --verbose From af89a8c190e448c9ccdd93e3ca4caf653f384247 Mon Sep 17 00:00:00 2001 From: Jeff Kreeftmeijer Date: Thu, 2 May 2024 16:29:47 +0200 Subject: [PATCH 5/7] Add clippy to ci --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dbfe814..82272e8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -11,3 +11,4 @@ jobs: - uses: actions/checkout@v4 - run: cargo build --verbose - run: cargo test --verbose + - run: cargo clippy --verbose From c26788d43b2a1e8e34fa29e8d339e4a159adb29d Mon Sep 17 00:00:00 2001 From: Jeff Kreeftmeijer Date: Thu, 2 May 2024 16:30:47 +0200 Subject: [PATCH 6/7] Add cargo fmt to ci --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 82272e8..dea8095 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,3 +12,4 @@ jobs: - run: cargo build --verbose - run: cargo test --verbose - run: cargo clippy --verbose + - run: cargo fmt --verbose From b7e2aa2e6c70721dca833f9b47d4bc1f34acb6f3 Mon Sep 17 00:00:00 2001 From: Jeff Kreeftmeijer Date: Thu, 2 May 2024 16:33:07 +0200 Subject: [PATCH 7/7] Add workflow_dispatch to workflow to allow manual triggering --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dea8095..45bf872 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,6 +3,7 @@ name: CI on: push: pull_request: + workflow_dispatch: jobs: ci: