torrust_metrics/
prometheus.rs1use torrust_clock::DurationSinceUnixEpoch;
2
3use crate::metric_collection::Error as MetricCollectionError;
4use crate::sample_collection::Error as SampleCollectionError;
5
6pub trait PrometheusSerializable {
7 fn to_prometheus(&self) -> String;
13}
14
15impl<T: PrometheusSerializable> PrometheusSerializable for &T {
17 fn to_prometheus(&self) -> String {
18 (*self).to_prometheus()
19 }
20}
21
22pub trait PrometheusDeserializable: Sized {
23 fn from_prometheus(input: &str, now: DurationSinceUnixEpoch) -> Result<Self, PrometheusDeserializationError>;
33}
34
35#[derive(thiserror::Error, Debug, Clone)]
36pub enum PrometheusDeserializationError {
37 #[error("Failed to parse Prometheus exposition text: {message}")]
39 ParseError { message: String },
40
41 #[error("Unsupported Prometheus metric type '{metric_type}' for metric '{metric_name}'")]
44 UnsupportedType { metric_name: String, metric_type: String },
45
46 #[error("Unknown Prometheus metric type for metric '{metric_name}'")]
48 UnknownType { metric_name: String },
49
50 #[error("Value mismatch for metric '{metric_name}': expected {expected_type}, got {actual}")]
52 ValueMismatch {
53 metric_name: String,
54 expected_type: String,
55 actual: String,
56 },
57
58 #[error("Unknown value for metric '{metric_name}'")]
60 UnknownValue { metric_name: String },
61
62 #[error("Failed to convert label set for metric '{metric_name}': {message}")]
64 LabelConversion { metric_name: String, message: String },
65
66 #[error("Failed to build collection data: {message}")]
68 CollectionError { message: String },
69}
70
71impl From<MetricCollectionError> for PrometheusDeserializationError {
72 fn from(error: MetricCollectionError) -> Self {
73 Self::CollectionError {
74 message: error.to_string(),
75 }
76 }
77}
78
79impl From<SampleCollectionError> for PrometheusDeserializationError {
80 fn from(error: SampleCollectionError) -> Self {
81 Self::CollectionError {
82 message: error.to_string(),
83 }
84 }
85}