risingwave_common/telemetry/
pb_compatible.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 risingwave_pb::telemetry::{
16    ReportBase as PbTelemetryReportBase, SystemCpu as PbSystemCpu, SystemData as PbSystemData,
17    SystemMemory as PbSystemMemory, SystemOs as PbSystemOs,
18    TelemetryNodeType as PbTelemetryNodeType,
19};
20
21use crate::telemetry::{Cpu, Memory, Os, SystemData, TelemetryNodeType, TelemetryReportBase};
22
23pub trait TelemetryToProtobuf {
24    fn to_pb_bytes(self) -> Vec<u8>;
25}
26
27impl From<TelemetryReportBase> for PbTelemetryReportBase {
28    fn from(val: TelemetryReportBase) -> Self {
29        PbTelemetryReportBase {
30            tracking_id: val.tracking_id,
31            session_id: val.session_id,
32            system_data: Some(val.system_data.into()),
33            up_time: val.up_time,
34            report_time: val.time_stamp,
35            node_type: from_telemetry_node_type(val.node_type) as i32,
36            is_test: val.is_test,
37        }
38    }
39}
40
41fn from_telemetry_node_type(t: TelemetryNodeType) -> PbTelemetryNodeType {
42    match t {
43        TelemetryNodeType::Meta => PbTelemetryNodeType::Meta,
44        TelemetryNodeType::Compute => PbTelemetryNodeType::Compute,
45        TelemetryNodeType::Frontend => PbTelemetryNodeType::Frontend,
46        TelemetryNodeType::Compactor => PbTelemetryNodeType::Compactor,
47    }
48}
49
50impl From<TelemetryNodeType> for PbTelemetryNodeType {
51    fn from(val: TelemetryNodeType) -> Self {
52        match val {
53            TelemetryNodeType::Meta => PbTelemetryNodeType::Meta,
54            TelemetryNodeType::Compute => PbTelemetryNodeType::Compute,
55            TelemetryNodeType::Frontend => PbTelemetryNodeType::Frontend,
56            TelemetryNodeType::Compactor => PbTelemetryNodeType::Compactor,
57        }
58    }
59}
60
61impl From<Cpu> for PbSystemCpu {
62    fn from(val: Cpu) -> Self {
63        PbSystemCpu {
64            available: val.available,
65        }
66    }
67}
68
69impl From<Memory> for PbSystemMemory {
70    fn from(val: Memory) -> Self {
71        PbSystemMemory {
72            used: val.used as u64,
73            total: val.total as u64,
74        }
75    }
76}
77
78impl From<Os> for PbSystemOs {
79    fn from(val: Os) -> Self {
80        PbSystemOs {
81            name: val.name,
82            kernel_version: val.kernel_version,
83            version: val.version,
84        }
85    }
86}
87
88impl From<SystemData> for PbSystemData {
89    fn from(val: SystemData) -> Self {
90        PbSystemData {
91            memory: Some(val.memory.into()),
92            os: Some(val.os.into()),
93            cpu: Some(val.cpu.into()),
94        }
95    }
96}