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