risedev/task/
grafana_service.rs1use std::env;
16use std::path::{Path, PathBuf};
17use std::process::Command;
18
19use anyhow::{Result, anyhow};
20
21use super::{ExecuteContext, Task};
22use crate::util::stylized_risedev_subcmd;
23use crate::{GrafanaConfig, GrafanaGen};
24
25pub struct GrafanaService {
26 config: GrafanaConfig,
27 grafana_root: PathBuf,
28}
29
30impl GrafanaService {
31 pub fn new(config: GrafanaConfig) -> Result<Self> {
32 let grafana_root = Self::grafana_root_path()?;
33 Ok(Self {
34 config,
35 grafana_root,
36 })
37 }
38
39 fn grafana_root_path() -> Result<PathBuf> {
40 let prefix_bin = env::var("PREFIX_BIN")?;
41 Ok(Path::new(&prefix_bin).join("grafana"))
42 }
43
44 fn grafana_server_path(&self) -> Result<PathBuf> {
45 Ok(self.grafana_root.join("bin").join("grafana-server"))
46 }
47
48 fn grafana(&self) -> Result<Command> {
49 let mut command = Command::new(self.grafana_root.join("bin").join("grafana-server"));
50 command.current_dir(self.grafana_root.join("bin"));
51 Ok(command)
52 }
53
54 pub fn write_config_files(
55 config: &GrafanaConfig,
56 config_root: impl AsRef<Path>,
57 prefix_config: impl AsRef<Path>,
58 ) -> Result<()> {
59 let config_root = config_root.as_ref();
60
61 fs_err::write(
62 config_root.join("custom.ini"),
63 GrafanaGen.gen_custom_ini(config),
64 )?;
65
66 let config_datasources_dir = config_root.join("provisioning").join("datasources");
67 fs_err::remove_dir_all(&config_datasources_dir)?;
68 fs_err::create_dir_all(&config_datasources_dir)?;
69
70 fs_err::write(
71 config_datasources_dir.join("risedev-prometheus.yml"),
72 GrafanaGen.gen_prometheus_datasource_yml(config)?,
73 )?;
74
75 if !config.provide_tempo.as_ref().unwrap().is_empty() {
76 fs_err::write(
77 config_datasources_dir.join("risedev-tempo.yml"),
78 GrafanaGen.gen_tempo_datasource_yml(config)?,
79 )?;
80 }
81
82 let prefix_config = prefix_config.as_ref();
83 let config_dashboards_dir = config_root.join("provisioning").join("dashboards");
84 fs_err::remove_dir_all(&config_dashboards_dir)?;
85 fs_err::create_dir_all(&config_dashboards_dir)?;
86 fs_err::write(
87 config_dashboards_dir.join("risingwave-dashboard.yaml"),
88 GrafanaGen.gen_dashboard_yml(config, prefix_config, prefix_config)?,
89 )?;
90 Ok(())
96 }
97}
98
99impl Task for GrafanaService {
100 fn execute(&mut self, ctx: &mut ExecuteContext<impl std::io::Write>) -> anyhow::Result<()> {
101 ctx.service(self);
102 ctx.pb.set_message("starting...");
103
104 let path = self.grafana_server_path()?;
105 if !path.exists() {
106 return Err(anyhow!(
107 "grafana-server binary not found in {:?}\nDid you enable monitoring feature in `{}`?",
108 path,
109 stylized_risedev_subcmd("configure")
110 ));
111 }
112
113 Self::write_config_files(
114 &self.config,
115 self.grafana_root.join("conf"),
116 env::var("PREFIX_CONFIG")?,
117 )?;
118
119 let cmd = self.grafana()?;
120
121 ctx.run_command(ctx.tmux_run(cmd)?)?;
122
123 ctx.pb.set_message("started");
124
125 Ok(())
126 }
127
128 fn id(&self) -> String {
129 self.config.id.clone()
130 }
131}