Skip to content

Commit

Permalink
feat: added archival status metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowHatpro committed Aug 14, 2024
1 parent 4e18762 commit 612c11d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
22 changes: 18 additions & 4 deletions grafana/dashboards/metrics-dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,35 @@
"range": true,
"refId": "B",
"useBackend": false
},
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"disableTextWrap": false,
"editorMode": "builder",
"expr": "archival_requests_total",
"fullMetaSearch": false,
"hide": false,
"includeNullMetadata": true,
"instant": false,
"legendFormat": "__auto",
"range": true,
"refId": "C",
"useBackend": false
}
],
"title": "Rust app metrics panel",
"type": "timeseries"
}
],
"refresh": "",
"schemaVersion": 39,
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "mb-ia-dashboard",
Expand Down
3 changes: 3 additions & 0 deletions src/archival/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::archival::utils::{

use crate::archival::error::ArchivalError;
use crate::configuration::Settings;
use crate::metrics::Metrics;
use crate::structs::internet_archive_urls::{ArchivalStatus, InternetArchiveUrls};
use sentry::Level::Error;
use sqlx::postgres::PgListener;
Expand Down Expand Up @@ -33,6 +34,7 @@ pub async fn handle_payload(
url_row: InternetArchiveUrls,
pool: &PgPool,
) -> Result<(), ArchivalError> {
let metrics = Metrics::new().await;
if let Some(url) = url_row.url {
let id = url_row.id;
if url_row.retry_count >= Some(3) {
Expand All @@ -46,6 +48,7 @@ pub async fn handle_payload(
set_status_with_message(pool, id, ArchivalStatus::Failed as i32, status_ext.as_str())
.await?;
sentry::capture_message(status_ext.as_str(), Error);
metrics.record_archival_status("failed").await;
} else {
let archival_result = archive(url, url_row.id, pool).await;
if let Err(e) = archival_result {
Expand Down
2 changes: 2 additions & 0 deletions src/archival/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub async fn schedule_status_check(
archival_status_response.status.as_str(),
)
.await?;
metrics.record_archival_status("success").await;
return Ok(());
} else {
if attempt == 3 {
Expand All @@ -169,6 +170,7 @@ pub async fn schedule_status_check(
)
.await?;
}
metrics.record_archival_status("error").await;
eprintln!("Could not archive: {} attempt", attempt)
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use prometheus::{labels, push_metrics, Counter, Opts, Registry};
use prometheus::{labels, push_metrics, Counter, CounterVec, Opts, Registry};
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::task::spawn_blocking;

pub struct Metrics {
pub db_poll_counter: Counter,
pub network_request_counter: Counter,
pub archival_status_counter: CounterVec, // New counter for archival statuses
pub registry: Arc<Mutex<Registry>>,
}

impl Metrics {
pub async fn new() -> Self {
let registry = Arc::new(Mutex::new(Registry::new()));

let poll_opts = Opts::new(
"db_polls_total",
"Total number of edit data and edit notes polls",
Expand All @@ -33,12 +35,26 @@ impl Metrics {
.await
.register(Box::new(network_request_counter.clone()))
.unwrap();

let status_opts = Opts::new(
"archival_requests_total",
"Total number of archival requests by status",
);
let archival_status_counter = CounterVec::new(status_opts, &["status"]).unwrap();
registry
.lock()
.await
.register(Box::new(archival_status_counter.clone()))
.unwrap();

Metrics {
db_poll_counter,
network_request_counter,
archival_status_counter, // Add the new CounterVec to the struct
registry,
}
}

pub async fn push_metrics(&self) {
let registry = self.registry.clone();
spawn_blocking(move || {
Expand All @@ -54,4 +70,11 @@ impl Metrics {
.await
.unwrap();
}

pub async fn record_archival_status(&self, status: &str) {
self.archival_status_counter
.with_label_values(&[status])
.inc();
self.push_metrics().await;
}
}

0 comments on commit 612c11d

Please sign in to comment.