risingwave_frontend/
telemetry.rs1use prost::Message;
16use risingwave_common::telemetry::pb_compatible::TelemetryToProtobuf;
17use risingwave_common::telemetry::report::TelemetryReportCreator;
18use risingwave_common::telemetry::{
19    SystemData, TelemetryNodeType, TelemetryReportBase, TelemetryResult, current_timestamp,
20    report_event_common,
21};
22use risingwave_pb::telemetry::{PbTelemetryDatabaseObject, PbTelemetryEventStage};
23use serde::{Deserialize, Serialize};
24
25const TELEMETRY_FRONTEND_REPORT_TYPE: &str = "frontend";
26
27pub(crate) fn report_event(
28    event_stage: PbTelemetryEventStage,
29    event_name: &str,
30    catalog_id: i64,
31    connector_name: Option<String>,
32    component: Option<PbTelemetryDatabaseObject>,
33    attributes: Option<jsonbb::Value>, ) {
35    report_event_common(
36        event_stage,
37        event_name,
38        catalog_id,
39        connector_name,
40        component,
41        attributes,
42        TELEMETRY_FRONTEND_REPORT_TYPE.to_owned(),
43    );
44}
45
46#[derive(Clone, Copy)]
47pub(crate) struct FrontendTelemetryCreator {}
48
49impl FrontendTelemetryCreator {
50    pub(crate) fn new() -> Self {
51        Self {}
52    }
53}
54
55#[async_trait::async_trait]
56impl TelemetryReportCreator for FrontendTelemetryCreator {
57    #[allow(refining_impl_trait)]
58    async fn create_report(
59        &self,
60        tracking_id: String,
61        session_id: String,
62        up_time: u64,
63    ) -> TelemetryResult<FrontendTelemetryReport> {
64        Ok(FrontendTelemetryReport::new(
65            tracking_id,
66            session_id,
67            up_time,
68        ))
69    }
70
71    fn report_type(&self) -> &str {
72        TELEMETRY_FRONTEND_REPORT_TYPE
73    }
74}
75
76#[derive(Serialize, Deserialize, Debug)]
77pub(crate) struct FrontendTelemetryReport {
78    #[serde(flatten)]
79    base: TelemetryReportBase,
80}
81
82impl TelemetryToProtobuf for FrontendTelemetryReport {
83    fn to_pb_bytes(self) -> Vec<u8> {
84        let pb_report = risingwave_pb::telemetry::FrontendReport {
85            base: Some(self.base.into()),
86        };
87        pb_report.encode_to_vec()
88    }
89}
90
91impl FrontendTelemetryReport {
92    pub(crate) fn new(tracking_id: String, session_id: String, up_time: u64) -> Self {
93        Self {
94            base: TelemetryReportBase {
95                tracking_id,
96                session_id,
97                system_data: SystemData::new(),
98                up_time,
99                time_stamp: current_timestamp(),
100                node_type: TelemetryNodeType::Frontend,
101                is_test: false,
102            },
103        }
104    }
105}