-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprometheus.rs
More file actions
101 lines (86 loc) · 3.04 KB
/
Copy pathprometheus.rs
File metadata and controls
101 lines (86 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Prometheus Configuration DTO (Application Layer)
//!
//! This module contains the DTO type for Prometheus configuration used in
//! environment creation. This type uses raw primitives (u32) for JSON
//! deserialization and converts to the rich domain type (`PrometheusConfig`).
//!
//! # Conversion Pattern
//!
//! Uses `TryFrom` for idiomatic Rust conversion from DTO to domain type.
//! See ADR: `docs/decisions/tryfrom-for-dto-to-domain-conversion.md`
use std::convert::TryFrom;
use std::num::NonZeroU32;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::application::command_handlers::create::config::errors::CreateConfigError;
use crate::domain::prometheus::PrometheusConfig;
/// Prometheus configuration section (DTO)
///
/// This is a simple DTO that deserializes from JSON numbers and validates
/// when converting to the domain `PrometheusConfig`.
///
/// # Examples
///
/// ```json
/// {
/// "scrape_interval_in_secs": 15
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct PrometheusSection {
/// Interval for Prometheus to scrape metrics from targets (in seconds)
///
/// Must be greater than 0. The Prometheus template adds the 's' suffix.
/// Examples: 15 (15 seconds), 30 (30 seconds), 60 (1 minute)
pub scrape_interval_in_secs: u32,
}
impl Default for PrometheusSection {
fn default() -> Self {
Self {
scrape_interval_in_secs: PrometheusConfig::default().scrape_interval_in_secs(),
}
}
}
impl TryFrom<PrometheusSection> for PrometheusConfig {
type Error = CreateConfigError;
fn try_from(section: PrometheusSection) -> Result<Self, Self::Error> {
let interval = NonZeroU32::new(section.scrape_interval_in_secs).ok_or_else(|| {
CreateConfigError::InvalidPrometheusConfig(format!(
"Scrape interval must be greater than 0, got: {}",
section.scrape_interval_in_secs
))
})?;
Ok(PrometheusConfig::new(interval))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_have_default_values() {
let section = PrometheusSection::default();
assert_eq!(section.scrape_interval_in_secs, 15);
}
#[test]
fn it_should_convert_to_prometheus_config() {
let section = PrometheusSection {
scrape_interval_in_secs: 30,
};
let config: PrometheusConfig = section.try_into().expect("Valid config");
assert_eq!(config.scrape_interval_in_secs(), 30);
}
#[test]
fn it_should_convert_default_section_to_default_config() {
let section = PrometheusSection::default();
let config: PrometheusConfig = section.try_into().expect("Valid config");
assert_eq!(config, PrometheusConfig::default());
}
#[test]
fn it_should_reject_zero_interval() {
let section = PrometheusSection {
scrape_interval_in_secs: 0,
};
let result: Result<PrometheusConfig, _> = section.try_into();
assert!(result.is_err());
}
}