use torrust_clock::DurationSinceUnixEpoch;
use crate::metric_collection::Error as MetricCollectionError;
use crate::sample_collection::Error as SampleCollectionError;
pub trait PrometheusSerializable {
fn to_prometheus(&self) -> String;
}
impl<T: PrometheusSerializable> PrometheusSerializable for &T {
fn to_prometheus(&self) -> String {
(*self).to_prometheus()
}
}
pub trait PrometheusDeserializable: Sized {
fn from_prometheus(input: &str, now: DurationSinceUnixEpoch) -> Result<Self, PrometheusDeserializationError>;
}
#[derive(thiserror::Error, Debug, Clone)]
pub enum PrometheusDeserializationError {
#[error("Failed to parse Prometheus exposition text: {message}")]
ParseError { message: String },
#[error("Unsupported Prometheus metric type '{metric_type}' for metric '{metric_name}'")]
UnsupportedType { metric_name: String, metric_type: String },
#[error("Unknown Prometheus metric type for metric '{metric_name}'")]
UnknownType { metric_name: String },
#[error("Value mismatch for metric '{metric_name}': expected {expected_type}, got {actual}")]
ValueMismatch {
metric_name: String,
expected_type: String,
actual: String,
},
#[error("Unknown value for metric '{metric_name}'")]
UnknownValue { metric_name: String },
#[error("Failed to convert label set for metric '{metric_name}': {message}")]
LabelConversion { metric_name: String, message: String },
#[error("Failed to build collection data: {message}")]
CollectionError { message: String },
}
impl From<MetricCollectionError> for PrometheusDeserializationError {
fn from(error: MetricCollectionError) -> Self {
Self::CollectionError {
message: error.to_string(),
}
}
}
impl From<SampleCollectionError> for PrometheusDeserializationError {
fn from(error: SampleCollectionError) -> Self {
Self::CollectionError {
message: error.to_string(),
}
}
}