risingwave_frontend/
telemetry.rs

1// Copyright 2023 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 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>, // any json string
34) {
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    async fn create_report(
58        &self,
59        tracking_id: String,
60        session_id: String,
61        up_time: u64,
62    ) -> TelemetryResult<FrontendTelemetryReport> {
63        Ok(FrontendTelemetryReport::new(
64            tracking_id,
65            session_id,
66            up_time,
67        ))
68    }
69
70    fn report_type(&self) -> &str {
71        TELEMETRY_FRONTEND_REPORT_TYPE
72    }
73}
74
75#[derive(Serialize, Deserialize, Debug)]
76pub(crate) struct FrontendTelemetryReport {
77    #[serde(flatten)]
78    base: TelemetryReportBase,
79}
80
81impl TelemetryToProtobuf for FrontendTelemetryReport {
82    fn to_pb_bytes(self) -> Vec<u8> {
83        let pb_report = risingwave_pb::telemetry::FrontendReport {
84            base: Some(self.base.into()),
85        };
86        pb_report.encode_to_vec()
87    }
88}
89
90impl FrontendTelemetryReport {
91    pub(crate) fn new(tracking_id: String, session_id: String, up_time: u64) -> Self {
92        Self {
93            base: TelemetryReportBase {
94                tracking_id,
95                session_id,
96                system_data: SystemData::new(),
97                up_time,
98                time_stamp: current_timestamp(),
99                node_type: TelemetryNodeType::Frontend,
100                is_test: false,
101            },
102        }
103    }
104}