-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprometheus.rs
More file actions
127 lines (112 loc) · 4.18 KB
/
prometheus.rs
File metadata and controls
127 lines (112 loc) · 4.18 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Prometheus Template Rendering Service
//!
//! This service is responsible for rendering Prometheus configuration templates.
//! It's used by multiple contexts (render command, release steps) to prepare
//! prometheus.yml configuration files.
use std::path::PathBuf;
use std::sync::Arc;
use thiserror::Error;
use tracing::info;
use crate::domain::prometheus::PrometheusConfig;
use crate::domain::template::TemplateManager;
use crate::domain::tracker::TrackerConfig;
use crate::infrastructure::templating::prometheus::{
PrometheusProjectGenerator, PrometheusProjectGeneratorError,
};
use crate::shared::Clock;
/// Errors that can occur during Prometheus template rendering
#[derive(Error, Debug)]
pub enum PrometheusTemplateRenderingServiceError {
/// Template rendering failed
#[error("Failed to render Prometheus templates: {reason}")]
RenderingFailed {
/// Detailed reason for the failure
reason: String,
},
}
impl From<PrometheusProjectGeneratorError> for PrometheusTemplateRenderingServiceError {
fn from(error: PrometheusProjectGeneratorError) -> Self {
Self::RenderingFailed {
reason: error.to_string(),
}
}
}
/// Service for rendering Prometheus configuration templates
///
/// This service encapsulates the logic for rendering prometheus.yml configuration
/// files. It's designed to be shared across command handlers and steps that need
/// to prepare Prometheus configuration.
pub struct PrometheusTemplateRenderingService {
build_dir: PathBuf,
template_manager: Arc<TemplateManager>,
clock: Arc<dyn Clock>,
}
impl PrometheusTemplateRenderingService {
/// Build a `PrometheusTemplateRenderingService` from environment paths
///
/// # Arguments
///
/// * `templates_dir` - Directory containing the source templates
/// * `build_dir` - Directory where rendered templates will be written
/// * `clock` - The clock for generating timestamps
///
/// # Returns
///
/// Returns a configured `PrometheusTemplateRenderingService` ready for template rendering
#[must_use]
pub fn from_paths(templates_dir: PathBuf, build_dir: PathBuf, clock: Arc<dyn Clock>) -> Self {
let template_manager = Arc::new(TemplateManager::new(templates_dir));
Self {
build_dir,
template_manager,
clock,
}
}
/// Render Prometheus configuration templates
///
/// This renders the prometheus.yml configuration file to the build directory.
/// Returns `None` if Prometheus is not configured.
///
/// # Arguments
///
/// * `prometheus_config` - Prometheus configuration from user inputs (optional)
/// * `tracker_config` - Tracker configuration (needed for API token and port)
///
/// # Returns
///
/// Returns the path to the rendered Prometheus build directory, or `None` if not configured
///
/// # Errors
///
/// Returns `PrometheusTemplateRenderingServiceError::RenderingFailed` if template rendering fails.
pub fn render(
&self,
prometheus_config: Option<&PrometheusConfig>,
tracker_config: &TrackerConfig,
) -> Result<Option<PathBuf>, PrometheusTemplateRenderingServiceError> {
let Some(prometheus_config) = prometheus_config else {
info!(
reason = "prometheus_not_configured",
"Skipping Prometheus template rendering - not configured"
);
return Ok(None);
};
info!(
templates_dir = %self.template_manager.templates_dir().display(),
build_dir = %self.build_dir.display(),
"Rendering Prometheus configuration templates"
);
let generator = PrometheusProjectGenerator::new(
&self.build_dir,
self.template_manager.clone(),
self.clock.clone(),
);
generator.render(prometheus_config, tracker_config)?;
let prometheus_build_dir = self.build_dir.join("storage/prometheus/etc");
info!(
prometheus_build_dir = %prometheus_build_dir.display(),
"Prometheus configuration templates rendered successfully"
);
Ok(Some(prometheus_build_dir))
}
}