risedev/config_gen/
prometheus_gen.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use chrono::prelude::*;
16use itertools::Itertools;
17
18use crate::PrometheusConfig;
19
20pub struct PrometheusGen;
21
22impl PrometheusGen {
23    pub fn gen_prometheus_yml(&self, config: &PrometheusConfig) -> String {
24        let prometheus_host = &config.address;
25        let prometheus_port = &config.port;
26        let scrape_interval = &config.scrape_interval;
27        let compute_node_targets = config
28            .provide_compute_node
29            .as_ref()
30            .unwrap()
31            .iter()
32            .map(|node| format!("\"{}:{}\"", node.address, node.exporter_port))
33            .join(",");
34
35        let frontend_targets = config
36            .provide_frontend
37            .as_ref()
38            .unwrap()
39            .iter()
40            .map(|node| format!("\"{}:{}\"", node.address, node.exporter_port))
41            .join(",");
42
43        let meta_node_targets = config
44            .provide_meta_node
45            .as_ref()
46            .unwrap()
47            .iter()
48            .map(|node| format!("\"{}:{}\"", node.address, node.exporter_port))
49            .join(",");
50
51        let minio_targets = config
52            .provide_minio
53            .as_ref()
54            .unwrap()
55            .iter()
56            .map(|node| format!("\"{}:{}\"", node.address, node.port))
57            .join(",");
58
59        let compactor_targets = config
60            .provide_compactor
61            .as_ref()
62            .unwrap()
63            .iter()
64            .map(|node| format!("\"{}:{}\"", node.address, node.exporter_port))
65            .join(",");
66
67        let now = Local::now().format("%Y%m%d-%H%M%S");
68
69        let remote_write = if config.remote_write {
70            let remote_write_region = &config.remote_write_region;
71            let remote_write_url = &config.remote_write_url;
72            format!(
73                r#"
74remote_write:
75  -
76    url: {remote_write_url}
77    queue_config:
78        max_samples_per_send: 1000
79        max_shards: 200
80        capacity: 2500
81    sigv4:
82         region: {remote_write_region}
83"#,
84            )
85        } else {
86            String::from("")
87        };
88
89        format!(
90            r#"# --- THIS FILE IS AUTO GENERATED BY RISEDEV ---
91global:
92  scrape_interval: {scrape_interval}
93  evaluation_interval: 60s
94  external_labels:
95    rw_cluster: {now}
96{remote_write}
97
98scrape_configs:
99  - job_name: prometheus
100    static_configs:
101      - targets: ["{prometheus_host}:{prometheus_port}"]
102
103  - job_name: compute
104    static_configs:
105      - targets: [{compute_node_targets}]
106
107  - job_name: meta
108    static_configs:
109      - targets: [{meta_node_targets}]
110
111  - job_name: minio
112    metrics_path: /minio/v2/metrics/cluster
113    static_configs:
114    - targets: [{minio_targets}]
115
116  - job_name: compactor
117    static_configs:
118      - targets: [{compactor_targets}]
119
120  - job_name: frontend
121    static_configs:
122      - targets: [{frontend_targets}]
123"#,
124        )
125    }
126}