meta_srv/
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
24use crate::cluster::MetaPeerClientRef;
25
26struct DistributedGreptimeDBTelemetryCollector {
27    meta_peer_client: MetaPeerClientRef,
28    uuid: Option<String>,
29    retry: i32,
30}
31
32#[async_trait]
33impl Collector for DistributedGreptimeDBTelemetryCollector {
34    fn get_mode(&self) -> VersionReporterMode {
35        VersionReporterMode::Distributed
36    }
37
38    async fn get_nodes(&self) -> Option<i32> {
39        self.meta_peer_client.get_node_cnt().await.ok()
40    }
41
42    fn get_retry(&self) -> i32 {
43        self.retry
44    }
45
46    fn inc_retry(&mut self) {
47        self.retry += 1;
48    }
49
50    fn set_uuid_cache(&mut self, uuid: String) {
51        self.uuid = Some(uuid);
52    }
53
54    fn get_uuid_cache(&self) -> Option<String> {
55        self.uuid.clone()
56    }
57}
58
59pub async fn get_greptimedb_telemetry_task(
60    working_home: Option<String>,
61    meta_peer_client: MetaPeerClientRef,
62    enable: bool,
63) -> Arc<GreptimeDBTelemetryTask> {
64    if !enable || cfg!(test) || cfg!(debug_assertions) {
65        return Arc::new(GreptimeDBTelemetryTask::disable());
66    }
67    // Controlled by meta server state, only leader reports the info.
68    let should_report = Arc::new(AtomicBool::new(false));
69    Arc::new(GreptimeDBTelemetryTask::enable(
70        TELEMETRY_INTERVAL,
71        Box::new(GreptimeDBTelemetry::new(
72            working_home.clone(),
73            Box::new(DistributedGreptimeDBTelemetryCollector {
74                meta_peer_client,
75                uuid: default_get_uuid(&working_home),
76                retry: 0,
77            }),
78            should_report.clone(),
79        )),
80        should_report,
81    ))
82}