Skip to main content

risingwave_rpc_client/
monitor_client.rs

1// Copyright 2026 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 std::sync::Arc;
16use std::time::Duration;
17
18use async_trait::async_trait;
19use risingwave_common::config::RpcClientConfig;
20use risingwave_common::monitor::EndpointExt as _;
21use risingwave_common::util::addr::HostAddr;
22use risingwave_pb::configured_monitor_service_client;
23use risingwave_pb::monitor_service::monitor_service_client::MonitorServiceClient;
24use risingwave_pb::monitor_service::{
25    AnalyzeHeapRequest, AnalyzeHeapResponse, GetLogFilterRequest, GetProfileStatsRequest,
26    GetProfileStatsResponse, GetStreamingStatsRequest, GetStreamingStatsResponse,
27    HeapProfilingRequest, HeapProfilingResponse, ListHeapProfilingRequest,
28    ListHeapProfilingResponse, LogFilterState, ProfilingRequest, ProfilingResponse,
29    SetLogFilterRequest, StackTraceRequest, StackTraceResponse,
30};
31use tonic::transport::Endpoint;
32
33use crate::channel::{Channel, WrappedChannelExt};
34use crate::error::{Result, RpcError};
35use crate::{RpcClient, RpcClientPool};
36
37/// Client for monitoring and profiling.
38///
39/// Can be applied to any type of worker node, though some methods may not be supported. See
40/// documentation of each method for availability.
41#[derive(Clone)]
42pub struct MonitorClient {
43    pub monitor_client: MonitorServiceClient<Channel>,
44}
45
46impl MonitorClient {
47    /// Return the process-wide stdout/stderr filter on any node type.
48    pub async fn get_log_filter(&self) -> Result<LogFilterState> {
49        Ok(self
50            .monitor_client
51            .clone()
52            .get_log_filter(GetLogFilterRequest {})
53            .await
54            .map_err(RpcError::from_monitor_status)?
55            .into_inner())
56    }
57
58    /// Replace startup `RUST_LOG` and runtime directives, or reset with `None`.
59    pub async fn set_log_filter(&self, filter: Option<String>) -> Result<LogFilterState> {
60        Ok(self
61            .monitor_client
62            .clone()
63            .set_log_filter(SetLogFilterRequest { filter })
64            .await
65            .map_err(RpcError::from_monitor_status)?
66            .into_inner())
67    }
68
69    pub async fn new(host_addr: HostAddr, opts: &RpcClientConfig) -> Result<Self> {
70        let channel = Endpoint::from_shared(format!("http://{}", host_addr))?
71            .connect_timeout(Duration::from_secs(opts.connect_timeout_secs))
72            .monitored_connect("grpc-monitor-client", Default::default())
73            .await?
74            .wrapped();
75
76        Ok(Self {
77            monitor_client: configured_monitor_service_client(MonitorServiceClient::new(channel)),
78        })
79    }
80
81    /// Available on meta node, compute node, and compactor node.
82    ///
83    /// Note: for meta node, it will gather await tree from all nodes in the cluster besides itself.
84    pub async fn await_tree(&self, req: StackTraceRequest) -> Result<StackTraceResponse> {
85        Ok(self
86            .monitor_client
87            .clone()
88            .stack_trace(req)
89            .await
90            .map_err(RpcError::from_monitor_status)?
91            .into_inner())
92    }
93
94    /// Available on compute node.
95    pub async fn get_streaming_stats(&self) -> Result<GetStreamingStatsResponse> {
96        Ok(self
97            .monitor_client
98            .clone()
99            .get_streaming_stats(GetStreamingStatsRequest::default())
100            .await
101            .map_err(RpcError::from_monitor_status)?
102            .into_inner())
103    }
104
105    /// Available on compute node.
106    pub async fn get_profile_stats(
107        &self,
108        request: GetProfileStatsRequest,
109    ) -> Result<GetProfileStatsResponse> {
110        Ok(self
111            .monitor_client
112            .clone()
113            .get_profile_stats(request)
114            .await
115            .map_err(RpcError::from_monitor_status)?
116            .into_inner())
117    }
118
119    /// Available on meta node, compute node, compactor node, and frontend node.
120    pub async fn profile(&self, sleep_s: u64) -> Result<ProfilingResponse> {
121        Ok(self
122            .monitor_client
123            .clone()
124            .profiling(ProfilingRequest { sleep_s })
125            .await
126            .map_err(RpcError::from_monitor_status)?
127            .into_inner())
128    }
129
130    /// Available on meta node, compute node, compactor node, and frontend node.
131    pub async fn heap_profile(&self, dir: String) -> Result<HeapProfilingResponse> {
132        Ok(self
133            .monitor_client
134            .clone()
135            .heap_profiling(HeapProfilingRequest { dir })
136            .await
137            .map_err(RpcError::from_monitor_status)?
138            .into_inner())
139    }
140
141    /// Available on meta node, compute node, compactor node, and frontend node.
142    pub async fn list_heap_profile(&self) -> Result<ListHeapProfilingResponse> {
143        Ok(self
144            .monitor_client
145            .clone()
146            .list_heap_profiling(ListHeapProfilingRequest {})
147            .await
148            .map_err(RpcError::from_monitor_status)?
149            .into_inner())
150    }
151
152    /// Available on meta node, compute node, compactor node, and frontend node.
153    pub async fn analyze_heap(&self, path: String) -> Result<AnalyzeHeapResponse> {
154        Ok(self
155            .monitor_client
156            .clone()
157            .analyze_heap(AnalyzeHeapRequest { path })
158            .await
159            .map_err(RpcError::from_monitor_status)?
160            .into_inner())
161    }
162}
163
164pub type MonitorClientPool = RpcClientPool<MonitorClient>;
165pub type MonitorClientPoolRef = Arc<MonitorClientPool>;
166
167#[async_trait]
168impl RpcClient for MonitorClient {
169    async fn new_client(host_addr: HostAddr, opts: &RpcClientConfig) -> Result<Self> {
170        Self::new(host_addr, opts).await
171    }
172}