Skip to content

Commit f979c33

Browse files
committed
init
1 parent 06d5848 commit f979c33

File tree

12 files changed

+207
-4
lines changed

12 files changed

+207
-4
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ members = [
114114
"src/bendsave",
115115
"src/bendpy",
116116
"src/meta/app-storage",
117+
"src/common/telemetry",
117118
]
118119

119120
# Workspace dependencies
@@ -188,6 +189,7 @@ databend-common-storages-stage = { path = "src/query/storages/stage" }
188189
databend-common-storages-stream = { path = "src/query/storages/stream" }
189190
databend-common-storages-system = { path = "src/query/storages/system" }
190191
databend-common-storages-view = { path = "src/query/storages/view" }
192+
databend-common-telemetry = { path = "src/common/telemetry" }
191193
databend-common-tracing = { path = "src/common/tracing" }
192194
databend-common-users = { path = "src/query/users" }
193195
databend-common-vector = { path = "src/common/vector" }

src/common/building/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn add_building_env_vars() {
5959
add_env_version();
6060
add_env_license();
6161
add_license_public_key();
62+
add_env_telemetry();
6263
}
6364

6465
pub fn set_env_config() {
@@ -103,6 +104,17 @@ pub fn add_license_public_key() {
103104
println!("cargo:rustc-env=DATABEND_ENTERPRISE_LICENSE_PUBLIC_KEY={v}");
104105
}
105106

107+
pub fn add_env_telemetry() {
108+
println!("cargo:rerun-if-env-changed=DATABEND_TELEMETRY_ENDPOINT");
109+
let endpoint = env::var("DATABEND_TELEMETRY_ENDPOINT")
110+
.unwrap_or_else(|_| "https://telemetry.databend.com/v1/report".to_string());
111+
println!("cargo:rustc-env=DATABEND_TELEMETRY_ENDPOINT={endpoint}");
112+
113+
println!("cargo:rerun-if-env-changed=DATABEND_TELEMETRY_API_KEY");
114+
let api_key = env::var("DATABEND_TELEMETRY_API_KEY").unwrap_or_default();
115+
println!("cargo:rustc-env=DATABEND_TELEMETRY_API_KEY={api_key}");
116+
}
117+
106118
pub fn add_env_commit_authors(repo: &Repository) {
107119
match git::get_commit_authors(repo) {
108120
Ok(authors) => println!("cargo:rustc-env=DATABEND_COMMIT_AUTHORS={}", authors),

src/common/telemetry/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "databend-common-telemetry"
3+
version = { workspace = true }
4+
authors = { workspace = true }
5+
license = { workspace = true }
6+
publish = { workspace = true }
7+
edition = { workspace = true }
8+
9+
[dependencies]
10+
databend-common-version = { workspace = true }
11+
reqwest = { workspace = true, features = ["json", "rustls-tls"] }
12+
serde_json = { workspace = true }
13+
tokio = { workspace = true }

src/common/telemetry/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 Datafuse 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+
15+
pub mod simple;
16+
17+
pub use simple::report_node_telemetry;

src/common/telemetry/src/simple.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Datafuse 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+
15+
use std::time::Duration;
16+
17+
use databend_common_version::DATABEND_TELEMETRY_API_KEY;
18+
use databend_common_version::DATABEND_TELEMETRY_ENDPOINT;
19+
20+
pub async fn report_node_telemetry(payload: serde_json::Value) {
21+
let client = match reqwest::Client::builder()
22+
.timeout(Duration::from_secs(3))
23+
.connect_timeout(Duration::from_secs(2))
24+
.build()
25+
{
26+
Ok(client) => client,
27+
Err(_) => return,
28+
};
29+
30+
let version = payload
31+
.get("version")
32+
.and_then(|v| v.as_str())
33+
.unwrap_or("unknown");
34+
35+
let mut request = client
36+
.post(DATABEND_TELEMETRY_ENDPOINT)
37+
.header("Content-Type", "application/json")
38+
.header("User-Agent", format!("Databend/{}", version));
39+
40+
#[allow(clippy::const_is_empty)]
41+
if !DATABEND_TELEMETRY_API_KEY.is_empty() {
42+
request = request.header(
43+
"Authorization",
44+
format!("Bearer {}", DATABEND_TELEMETRY_API_KEY),
45+
);
46+
}
47+
48+
let _ = tokio::time::timeout(Duration::from_secs(3), request.json(&payload).send()).await;
49+
}

src/common/version/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ pub const DATABEND_ENTERPRISE_LICENSE_PUBLIC_KEY: &str =
4646

4747
pub const DATABEND_CARGO_CFG_TARGET_FEATURE: &str = env!("DATABEND_CARGO_CFG_TARGET_FEATURE");
4848

49+
pub const DATABEND_TELEMETRY_ENDPOINT: &str = env!("DATABEND_TELEMETRY_ENDPOINT");
50+
51+
pub const DATABEND_TELEMETRY_API_KEY: &str = env!("DATABEND_TELEMETRY_API_KEY");
52+
4953
pub static DATABEND_SEMVER: LazyLock<Version> = LazyLock::new(|| {
5054
let build_semver = DATABEND_GIT_SEMVER;
5155
let semver = build_semver.expect("DATABEND_GIT_SEMVER can not be None");

src/query/config/src/config.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use databend_common_tracing::FileConfig as InnerFileLogConfig;
4848
use databend_common_tracing::HistoryConfig as InnerHistoryConfig;
4949
use databend_common_tracing::HistoryTableConfig as InnerHistoryTableConfig;
5050
use databend_common_tracing::OTLPConfig as InnerOTLPLogConfig;
51+
// TelemetryConfig moved here to avoid circular dependency
5152
use databend_common_tracing::OTLPEndpointConfig as InnerOTLPEndpointConfig;
5253
use databend_common_tracing::OTLPProtocol;
5354
use databend_common_tracing::ProfileLogConfig as InnerProfileLogConfig;
@@ -63,6 +64,39 @@ use serfig::collectors::from_env;
6364
use serfig::collectors::from_file;
6465
use serfig::collectors::from_self;
6566

67+
/// Telemetry configuration for node information reporting
68+
///
69+
/// Note: The `enabled` flag only works with Enterprise Edition license.
70+
/// Without EE license, telemetry is always enabled.
71+
/// With EE license, users can set `enabled=false` to disable telemetry reporting.
72+
/// The endpoint is fixed and not configurable by users.
73+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Args)]
74+
#[serde(default)]
75+
pub struct TelemetryConfig {
76+
/// Enable/disable telemetry reporting (only works with EE license)
77+
#[clap(long = "telemetry-enabled", value_name = "BOOL")]
78+
#[serde(default = "default_telemetry_enabled")]
79+
pub enabled: bool,
80+
}
81+
82+
fn default_telemetry_enabled() -> bool {
83+
true
84+
}
85+
86+
impl Default for TelemetryConfig {
87+
fn default() -> Self {
88+
Self {
89+
enabled: default_telemetry_enabled(),
90+
}
91+
}
92+
}
93+
94+
impl TelemetryConfig {
95+
pub fn is_enabled(&self) -> bool {
96+
self.enabled
97+
}
98+
}
99+
66100
use super::inner;
67101
use super::inner::CatalogConfig as InnerCatalogConfig;
68102
use super::inner::CatalogHiveConfig as InnerCatalogHiveConfig;
@@ -128,6 +162,10 @@ pub struct Config {
128162
#[clap(flatten)]
129163
pub spill: SpillConfig,
130164

165+
// telemetry Config
166+
#[clap(flatten)]
167+
pub telemetry: TelemetryConfig,
168+
131169
/// external catalog config.
132170
///
133171
/// - Later, catalog information SHOULD be kept in KV Service
@@ -3582,6 +3620,7 @@ mod cache_config_converters {
35823620
.collect(),
35833621
cache: inner.cache.into(),
35843622
spill: inner.spill.into(),
3623+
telemetry: inner.telemetry,
35853624
}
35863625
}
35873626
}
@@ -3599,6 +3638,7 @@ mod cache_config_converters {
35993638
catalog,
36003639
cache,
36013640
spill,
3641+
telemetry,
36023642
catalogs: input_catalogs,
36033643
..
36043644
} = self;
@@ -3628,6 +3668,7 @@ mod cache_config_converters {
36283668
catalogs,
36293669
cache: cache.try_into()?,
36303670
spill,
3671+
telemetry,
36313672
})
36323673
}
36333674
}

src/query/config/src/inner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use databend_common_tracing::Config as LogConfig;
3737

3838
use super::config::Config;
3939
use super::config::ResourcesManagementConfig;
40+
use super::config::TelemetryConfig;
4041
use crate::BuiltInConfig;
4142

4243
/// Inner config for query.
@@ -67,6 +68,9 @@ pub struct InnerConfig {
6768

6869
// Spill Config
6970
pub spill: SpillConfig,
71+
72+
// Telemetry Config
73+
pub telemetry: TelemetryConfig,
7074
}
7175

7276
impl InnerConfig {

src/query/config/src/mask.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl Config {
5151
catalog: self.catalog,
5252
cache: self.cache,
5353
spill: self.spill.mask_display(),
54+
telemetry: self.telemetry,
5455
catalogs: self.catalogs,
5556
}
5657
}

0 commit comments

Comments
 (0)