datanode/
greptimedb_telemetry.rs

1// Copyright 2023 Greptime Team
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::atomic::AtomicBool;
16use std::sync::Arc;
17
18use async_trait::async_trait;
19use common_greptimedb_telemetry::{
20    default_get_uuid, Collector, GreptimeDBTelemetry, GreptimeDBTelemetryTask,
21    Mode as VersionReporterMode, TELEMETRY_INTERVAL,
22};
23use servers::Mode;
24
25struct StandaloneGreptimeDBTelemetryCollector {
26    uuid: Option<String>,
27    retry: i32,
28}
29#[async_trait]
30impl Collector for StandaloneGreptimeDBTelemetryCollector {
31    fn get_mode(&self) -> VersionReporterMode {
32        VersionReporterMode::Standalone
33    }
34
35    async fn get_nodes(&self) -> Option<i32> {
36        Some(1)
37    }
38
39    fn get_retry(&self) -> i32 {
40        self.retry
41    }
42
43    fn inc_retry(&mut self) {
44        self.retry += 1;
45    }
46
47    fn set_uuid_cache(&mut self, uuid: String) {
48        self.uuid = Some(uuid);
49    }
50
51    fn get_uuid_cache(&self) -> Option<String> {
52        self.uuid.clone()
53    }
54}
55
56pub async fn get_greptimedb_telemetry_task(
57    working_home: Option<String>,
58    mode: &Mode,
59    enable: bool,
60) -> Arc<GreptimeDBTelemetryTask> {
61    if !enable || cfg!(test) || cfg!(debug_assertions) {
62        return Arc::new(GreptimeDBTelemetryTask::disable());
63    }
64    // Always enable.
65    let should_report = Arc::new(AtomicBool::new(true));
66
67    match mode {
68        Mode::Standalone => Arc::new(GreptimeDBTelemetryTask::enable(
69            TELEMETRY_INTERVAL,
70            Box::new(GreptimeDBTelemetry::new(
71                working_home.clone(),
72                Box::new(StandaloneGreptimeDBTelemetryCollector {
73                    uuid: default_get_uuid(&working_home),
74                    retry: 0,
75                }),
76                should_report.clone(),
77            )),
78            should_report,
79        )),
80        Mode::Distributed => Arc::new(GreptimeDBTelemetryTask::disable()),
81    }
82}