|
1 | 1 | //! API responses for the [`stats`](crate::servers::apis::v1::context::stats)
|
2 | 2 | //! API context.
|
3 |
| -use axum::response::Json; |
| 3 | +use axum::response::{IntoResponse, Json, Response}; |
4 | 4 |
|
5 | 5 | use super::resources::Stats;
|
6 | 6 | use crate::core::services::statistics::TrackerMetrics;
|
7 | 7 |
|
8 | 8 | /// `200` response that contains the [`Stats`] resource as json.
|
9 |
| -pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> { |
10 |
| - Json(Stats::from(tracker_metrics)) |
| 9 | +#[must_use] |
| 10 | +pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response { |
| 11 | + Json(Stats::from(tracker_metrics)).into_response() |
| 12 | +} |
| 13 | + |
| 14 | +/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format . |
| 15 | +#[must_use] |
| 16 | +pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response { |
| 17 | + let mut lines = vec![]; |
| 18 | + |
| 19 | + lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.torrents)); |
| 20 | + lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.complete)); |
| 21 | + lines.push(format!("completed {}", tracker_metrics.torrents_metrics.downloaded)); |
| 22 | + lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.incomplete)); |
| 23 | + |
| 24 | + lines.push(format!( |
| 25 | + "tcp4_connections_handled {}", |
| 26 | + tracker_metrics.protocol_metrics.tcp4_connections_handled |
| 27 | + )); |
| 28 | + lines.push(format!( |
| 29 | + "tcp4_announces_handled {}", |
| 30 | + tracker_metrics.protocol_metrics.tcp4_announces_handled |
| 31 | + )); |
| 32 | + lines.push(format!( |
| 33 | + "tcp4_scrapes_handled {}", |
| 34 | + tracker_metrics.protocol_metrics.tcp4_scrapes_handled |
| 35 | + )); |
| 36 | + |
| 37 | + lines.push(format!( |
| 38 | + "tcp6_connections_handled {}", |
| 39 | + tracker_metrics.protocol_metrics.tcp6_connections_handled |
| 40 | + )); |
| 41 | + lines.push(format!( |
| 42 | + "tcp6_announces_handled {}", |
| 43 | + tracker_metrics.protocol_metrics.tcp6_announces_handled |
| 44 | + )); |
| 45 | + lines.push(format!( |
| 46 | + "tcp6_scrapes_handled {}", |
| 47 | + tracker_metrics.protocol_metrics.tcp6_scrapes_handled |
| 48 | + )); |
| 49 | + |
| 50 | + lines.push(format!( |
| 51 | + "udp4_connections_handled {}", |
| 52 | + tracker_metrics.protocol_metrics.udp4_connections_handled |
| 53 | + )); |
| 54 | + lines.push(format!( |
| 55 | + "udp4_announces_handled {}", |
| 56 | + tracker_metrics.protocol_metrics.udp4_announces_handled |
| 57 | + )); |
| 58 | + lines.push(format!( |
| 59 | + "udp4_scrapes_handled {}", |
| 60 | + tracker_metrics.protocol_metrics.udp4_scrapes_handled |
| 61 | + )); |
| 62 | + lines.push(format!( |
| 63 | + "udp4_errors_handled {}", |
| 64 | + tracker_metrics.protocol_metrics.udp4_errors_handled |
| 65 | + )); |
| 66 | + |
| 67 | + lines.push(format!( |
| 68 | + "udp6_connections_handled {}", |
| 69 | + tracker_metrics.protocol_metrics.udp6_connections_handled |
| 70 | + )); |
| 71 | + lines.push(format!( |
| 72 | + "udp6_announces_handled {}", |
| 73 | + tracker_metrics.protocol_metrics.udp6_announces_handled |
| 74 | + )); |
| 75 | + lines.push(format!( |
| 76 | + "udp6_scrapes_handled {}", |
| 77 | + tracker_metrics.protocol_metrics.udp6_scrapes_handled |
| 78 | + )); |
| 79 | + lines.push(format!( |
| 80 | + "udp6_errors_handled {}", |
| 81 | + tracker_metrics.protocol_metrics.udp6_errors_handled |
| 82 | + )); |
| 83 | + |
| 84 | + // Return the plain text response |
| 85 | + lines.join("\n").into_response() |
11 | 86 | }
|
0 commit comments