-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [#1126] add support for prometheus text format on stats API end…
…point http://127.0.0.1:1212/api/v1/stats?token=MyAccessToken&format=prometheus ```text torrents 0 seeders 0 completed 0 leechers 0 tcp4_connections_handled 0 tcp4_announces_handled 0 tcp4_scrapes_handled 0 tcp6_connections_handled 0 tcp6_announces_handled 0 tcp6_scrapes_handled 0 udp4_connections_handled 0 udp4_announces_handled 0 udp4_scrapes_handled 0 udp4_errors_handled 0 udp6_connections_handled 0 udp6_announces_handled 0 udp6_scrapes_handled 0 udp6_errors_handled 0 ```
- Loading branch information
1 parent
b0af435
commit ff0fafc
Showing
2 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,86 @@ | ||
//! API responses for the [`stats`](crate::servers::apis::v1::context::stats) | ||
//! API context. | ||
use axum::response::Json; | ||
use axum::response::{IntoResponse, Json, Response}; | ||
|
||
use super::resources::Stats; | ||
use crate::core::services::statistics::TrackerMetrics; | ||
|
||
/// `200` response that contains the [`Stats`] resource as json. | ||
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> { | ||
Json(Stats::from(tracker_metrics)) | ||
#[must_use] | ||
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response { | ||
Json(Stats::from(tracker_metrics)).into_response() | ||
} | ||
|
||
/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format . | ||
#[must_use] | ||
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response { | ||
let mut lines = vec![]; | ||
|
||
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.torrents)); | ||
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.complete)); | ||
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.downloaded)); | ||
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.incomplete)); | ||
|
||
lines.push(format!( | ||
"tcp4_connections_handled {}", | ||
tracker_metrics.protocol_metrics.tcp4_connections_handled | ||
)); | ||
lines.push(format!( | ||
"tcp4_announces_handled {}", | ||
tracker_metrics.protocol_metrics.tcp4_announces_handled | ||
)); | ||
lines.push(format!( | ||
"tcp4_scrapes_handled {}", | ||
tracker_metrics.protocol_metrics.tcp4_scrapes_handled | ||
)); | ||
|
||
lines.push(format!( | ||
"tcp6_connections_handled {}", | ||
tracker_metrics.protocol_metrics.tcp6_connections_handled | ||
)); | ||
lines.push(format!( | ||
"tcp6_announces_handled {}", | ||
tracker_metrics.protocol_metrics.tcp6_announces_handled | ||
)); | ||
lines.push(format!( | ||
"tcp6_scrapes_handled {}", | ||
tracker_metrics.protocol_metrics.tcp6_scrapes_handled | ||
)); | ||
|
||
lines.push(format!( | ||
"udp4_connections_handled {}", | ||
tracker_metrics.protocol_metrics.udp4_connections_handled | ||
)); | ||
lines.push(format!( | ||
"udp4_announces_handled {}", | ||
tracker_metrics.protocol_metrics.udp4_announces_handled | ||
)); | ||
lines.push(format!( | ||
"udp4_scrapes_handled {}", | ||
tracker_metrics.protocol_metrics.udp4_scrapes_handled | ||
)); | ||
lines.push(format!( | ||
"udp4_errors_handled {}", | ||
tracker_metrics.protocol_metrics.udp4_errors_handled | ||
)); | ||
|
||
lines.push(format!( | ||
"udp6_connections_handled {}", | ||
tracker_metrics.protocol_metrics.udp6_connections_handled | ||
)); | ||
lines.push(format!( | ||
"udp6_announces_handled {}", | ||
tracker_metrics.protocol_metrics.udp6_announces_handled | ||
)); | ||
lines.push(format!( | ||
"udp6_scrapes_handled {}", | ||
tracker_metrics.protocol_metrics.udp6_scrapes_handled | ||
)); | ||
lines.push(format!( | ||
"udp6_errors_handled {}", | ||
tracker_metrics.protocol_metrics.udp6_errors_handled | ||
)); | ||
|
||
// Return the plain text response | ||
lines.join("\n").into_response() | ||
} |