forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.rs
More file actions
598 lines (529 loc) · 18.6 KB
/
telemetry.rs
File metadata and controls
598 lines (529 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Optional usage ping so we know which commands people run most.
use super::constants::RTK_DATA_DIR;
use crate::core::config;
use crate::core::tracking;
use sha2::{Digest, Sha256};
use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite;
use std::path::PathBuf;
use std::sync::OnceLock;
static CACHED_SALT: OnceLock<String> = OnceLock::new();
const TELEMETRY_URL: Option<&str> = option_env!("RTK_TELEMETRY_URL");
const TELEMETRY_TOKEN: Option<&str> = option_env!("RTK_TELEMETRY_TOKEN");
const PING_INTERVAL_SECS: u64 = 23 * 3600; // 23 hours
/// Send a telemetry ping if enabled and not already sent today.
/// Fire-and-forget: errors are silently ignored.
pub fn maybe_ping() {
// No URL compiled in → telemetry disabled
if TELEMETRY_URL.is_none() {
return;
}
// Check opt-out: env var
if std::env::var("RTK_TELEMETRY_DISABLED").unwrap_or_default() == "1" {
return;
}
// Load config once (avoid double disk read)
let cfg = match config::Config::load() {
Ok(c) => c,
Err(_) => return,
};
// RGPD: require explicit consent before any telemetry
match cfg.telemetry.consent_given {
Some(true) => {}
Some(false) | None => return,
}
// Check opt-out: config.toml
if !cfg.telemetry.enabled {
return;
}
// Check last ping time
let marker = telemetry_marker_path();
if let Ok(metadata) = std::fs::metadata(&marker) {
if let Ok(modified) = metadata.modified() {
if let Ok(elapsed) = modified.elapsed() {
if elapsed.as_secs() < PING_INTERVAL_SECS {
return;
}
}
}
}
// Touch marker file immediately (before sending) to avoid double-ping
touch_marker(&marker);
// Spawn thread so we never block the CLI
std::thread::spawn(|| {
let _ = send_ping();
});
}
fn send_ping() -> Result<(), Box<dyn std::error::Error>> {
let url = TELEMETRY_URL.ok_or("no telemetry URL")?;
let device_hash = generate_device_hash();
let version = env!("CARGO_PKG_VERSION").to_string();
let os = std::env::consts::OS.to_string();
let arch = std::env::consts::ARCH.to_string();
let install_method = detect_install_method();
// Get stats from tracking DB (single connection for both basic + enriched)
let tracker = tracking::Tracker::new().ok();
let (commands_24h, top_commands, savings_pct, tokens_saved_24h, tokens_saved_total) =
match &tracker {
Some(t) => get_stats(t),
None => (0, vec![], None, 0, 0),
};
let enriched = match &tracker {
Some(t) => get_enriched_stats(t),
None => EnrichedStats {
passthrough_top: vec![],
parse_failures_24h: 0,
low_savings_commands: vec![],
avg_savings_per_command: 0.0,
hook_type: detect_hook_type(),
custom_toml_filters: count_custom_toml_filters(),
first_seen_days: 0,
active_days_30d: 0,
commands_total: 0,
ecosystem_mix: serde_json::json!({}),
tokens_saved_30d: 0,
estimated_savings_usd_30d: 0.0,
has_config_toml: detect_has_config(),
exclude_commands_count: count_exclude_commands(),
projects_count: 0,
meta_usage: serde_json::json!({}),
},
};
let payload = serde_json::json!({
"device_hash": device_hash,
"version": version,
"os": os,
"arch": arch,
"install_method": install_method,
"commands_24h": commands_24h,
"top_commands": top_commands,
"savings_pct": savings_pct,
"tokens_saved_24h": tokens_saved_24h,
"tokens_saved_total": tokens_saved_total,
// Quality: identify gaps and weak filters
"passthrough_top": enriched.passthrough_top,
"parse_failures_24h": enriched.parse_failures_24h,
"low_savings_commands": enriched.low_savings_commands,
"avg_savings_per_command": enriched.avg_savings_per_command,
// Adoption: which tools and configs
"hook_type": enriched.hook_type,
"custom_toml_filters": enriched.custom_toml_filters,
// Retention: engagement signals
"first_seen_days": enriched.first_seen_days,
"active_days_30d": enriched.active_days_30d,
"commands_total": enriched.commands_total,
// Ecosystem: where to invest filters
"ecosystem_mix": enriched.ecosystem_mix,
// Economics: value delivered
"tokens_saved_30d": enriched.tokens_saved_30d,
"estimated_savings_usd_30d": enriched.estimated_savings_usd_30d,
// Configuration: user maturity
"has_config_toml": enriched.has_config_toml,
"exclude_commands_count": enriched.exclude_commands_count,
"projects_count": enriched.projects_count,
// Meta-commands: feature adoption
"meta_usage": enriched.meta_usage,
});
let mut req = ureq::post(url).set("Content-Type", "application/json");
if let Some(token) = TELEMETRY_TOKEN {
req = req.set("X-RTK-Token", token);
}
// 2 second timeout — if server is down, we move on
req.timeout(std::time::Duration::from_secs(2))
.send_string(&payload.to_string())?;
Ok(())
}
pub fn generate_device_hash() -> String {
let salt = get_or_create_salt();
let mut hasher = Sha256::new();
hasher.update(salt.as_bytes());
format!("{:x}", hasher.finalize())
}
fn get_or_create_salt() -> String {
CACHED_SALT
.get_or_init(|| {
let salt_path = salt_file_path();
if let Ok(contents) = std::fs::read_to_string(&salt_path) {
let trimmed = contents.trim().to_string();
if trimmed.len() == 64 && trimmed.chars().all(|c| c.is_ascii_hexdigit()) {
return trimmed;
}
}
let salt = random_salt();
if let Some(parent) = salt_path.parent() {
let _ = std::fs::create_dir_all(parent);
}
if let Ok(mut f) = std::fs::File::create(&salt_path) {
let _ = f.write_all(salt.as_bytes());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(
&salt_path,
std::fs::Permissions::from_mode(0o600),
);
}
}
salt
})
.clone()
}
fn random_salt() -> String {
let mut buf = [0u8; 32];
if getrandom::fill(&mut buf).is_err() {
let fallback = format!("{:?}:{}", std::time::SystemTime::now(), std::process::id());
let mut hasher = Sha256::new();
hasher.update(fallback.as_bytes());
return format!("{:x}", hasher.finalize());
}
buf.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{b:02x}");
output
})
}
pub fn salt_file_path() -> PathBuf {
dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("/tmp"))
.join("rtk")
.join(".device_salt")
}
fn get_stats(tracker: &tracking::Tracker) -> (i64, Vec<String>, Option<f64>, i64, i64) {
let since_24h = chrono::Utc::now() - chrono::Duration::hours(24);
let commands_24h = tracker.count_commands_since(since_24h).unwrap_or(0);
let top_commands = tracker.top_commands(5).unwrap_or_default();
let savings_pct = tracker.overall_savings_pct().ok();
let tokens_saved_24h = tracker.tokens_saved_24h(since_24h).unwrap_or(0);
let tokens_saved_total = tracker.total_tokens_saved().unwrap_or(0);
(
commands_24h,
top_commands,
savings_pct,
tokens_saved_24h,
tokens_saved_total,
)
}
struct EnrichedStats {
// Quality: identify gaps and weak filters
passthrough_top: Vec<String>,
parse_failures_24h: i64,
low_savings_commands: Vec<String>,
avg_savings_per_command: f64,
// Adoption: which tools and configs
hook_type: String,
custom_toml_filters: usize,
// Retention: engagement signals
first_seen_days: i64,
active_days_30d: i64,
commands_total: i64,
// Ecosystem: where to invest filters
ecosystem_mix: serde_json::Value,
// Economics: value delivered
tokens_saved_30d: i64,
estimated_savings_usd_30d: f64,
// Configuration: user maturity
has_config_toml: bool,
exclude_commands_count: usize,
projects_count: i64,
// Meta-commands: feature adoption
meta_usage: serde_json::Value,
}
fn get_enriched_stats(tracker: &tracking::Tracker) -> EnrichedStats {
let since_24h = chrono::Utc::now() - chrono::Duration::hours(24);
let passthrough_top = tracker
.top_passthrough(5)
.unwrap_or_default()
.into_iter()
.map(|(cmd, count)| format!("{}:{}", cmd, count))
.collect();
let parse_failures_24h = tracker.parse_failures_since(since_24h).unwrap_or(0);
let low_savings_commands = tracker
.low_savings_commands(5)
.unwrap_or_default()
.into_iter()
.map(|(cmd, pct)| format!("{}:{:.0}%", cmd, pct))
.collect();
let avg_savings_per_command = tracker.avg_savings_per_command().unwrap_or(0.0);
let first_seen_days = tracker.first_seen_days().unwrap_or(0);
let active_days_30d = tracker.active_days_30d().unwrap_or(0);
let commands_total = tracker.commands_total().unwrap_or(0);
let ecosystem_mix = serde_json::Value::Object(
tracker
.ecosystem_mix()
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, serde_json::json!(v)))
.collect(),
);
let tokens_saved_30d = tracker.tokens_saved_30d().unwrap_or(0);
// Estimate USD savings: tokens_saved are input tokens (CLI output compressed before
// reaching the LLM). Use input pricing: Claude Sonnet $3/Mtok.
let estimated_savings_usd_30d = tokens_saved_30d as f64 / 1_000_000.0 * 3.0;
let projects_count = tracker.projects_count().unwrap_or(0);
let meta_usage = build_meta_usage(tracker);
EnrichedStats {
passthrough_top,
parse_failures_24h,
low_savings_commands,
avg_savings_per_command,
hook_type: detect_hook_type(),
custom_toml_filters: count_custom_toml_filters(),
first_seen_days,
active_days_30d,
commands_total,
ecosystem_mix,
tokens_saved_30d,
estimated_savings_usd_30d,
projects_count,
has_config_toml: detect_has_config(),
exclude_commands_count: count_exclude_commands(),
meta_usage,
}
}
/// Build meta-command usage counts (gain, discover, proxy, verify, learn, init).
fn build_meta_usage(tracker: &tracking::Tracker) -> serde_json::Value {
let meta_cmds = ["gain", "discover", "proxy", "verify", "learn", "init"];
let mut usage = serde_json::Map::new();
for meta in &meta_cmds {
let count = tracker.count_meta_command(meta).unwrap_or(0);
if count > 0 {
usage.insert(meta.to_string(), serde_json::json!(count));
}
}
serde_json::Value::Object(usage)
}
/// Check if user has a config.toml file.
fn detect_has_config() -> bool {
dirs::config_dir()
.map(|d| d.join("rtk/config.toml").exists())
.unwrap_or(false)
}
/// Count commands in exclude_commands config.
fn count_exclude_commands() -> usize {
crate::core::config::Config::load()
.map(|c| c.hooks.exclude_commands.len())
.unwrap_or(0)
}
/// Detect which AI agent hook is installed.
fn detect_hook_type() -> String {
let home = match dirs::home_dir() {
Some(h) => h,
None => return "unknown".to_string(),
};
// Check in order of popularity
let checks = [
(home.join(".claude/hooks/rtk-rewrite.sh"), "claude"),
(home.join(".claude/hooks/rtk-rewrite.json"), "claude"),
(home.join(".gemini/hooks/rtk-hook.sh"), "gemini"),
(home.join(".codex/AGENTS.md"), "codex"),
(home.join(".cursor/hooks/rtk-rewrite.json"), "cursor"),
];
for (path, name) in &checks {
if path.exists() {
return name.to_string();
}
}
// Check project-level hooks
if let Ok(cwd) = std::env::current_dir() {
if cwd.join(".claude/hooks/rtk-rewrite.sh").exists() {
return "claude".to_string();
}
}
"none".to_string()
}
/// Count user-defined TOML filter files (project-local + global).
fn count_custom_toml_filters() -> usize {
let mut count = 0;
// Project-local: .rtk/filters/*.toml
if let Ok(cwd) = std::env::current_dir() {
if let Ok(entries) = std::fs::read_dir(cwd.join(".rtk/filters")) {
count += entries
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "toml"))
.count();
}
}
// Global: ~/.config/rtk/filters/*.toml
if let Some(config_dir) = dirs::config_dir() {
if let Ok(entries) = std::fs::read_dir(config_dir.join("rtk/filters")) {
count += entries
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "toml"))
.count();
}
}
count
}
fn detect_install_method() -> &'static str {
let exe = match std::env::current_exe() {
Ok(p) => p,
Err(_) => return "unknown",
};
let real_path = std::fs::canonicalize(&exe)
.unwrap_or(exe)
.to_string_lossy()
.to_string();
install_method_from_path(&real_path)
}
fn install_method_from_path(path: &str) -> &'static str {
if path.contains("/Cellar/rtk/") || path.contains("/homebrew/") {
"homebrew"
} else if path.contains("/.cargo/bin/") || path.contains("\\.cargo\\bin\\") {
"cargo"
} else if path.contains("/.local/bin/") || path.contains("\\.local\\bin\\") {
"script"
} else if path.contains("/nix/store/") {
"nix"
} else {
"other"
}
}
pub fn telemetry_marker_path() -> PathBuf {
let data_dir = dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("/tmp"))
.join(RTK_DATA_DIR);
let _ = std::fs::create_dir_all(&data_dir);
data_dir.join(".telemetry_last_ping")
}
fn touch_marker(path: &PathBuf) {
let _ = std::fs::write(path, b"");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_device_hash_is_stable() {
let h1 = generate_device_hash();
let h2 = generate_device_hash();
assert_eq!(h1, h2);
assert_eq!(h1.len(), 64);
}
#[test]
fn test_device_hash_is_valid_hex() {
let hash = generate_device_hash();
assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
fn test_salt_is_persisted() {
let s1 = get_or_create_salt();
let s2 = get_or_create_salt();
assert_eq!(s1, s2);
assert_eq!(s1.len(), 64);
assert!(s1.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
fn test_random_salt_uniqueness() {
let s1 = random_salt();
let s2 = random_salt();
assert_ne!(s1, s2);
assert_eq!(s1.len(), 64);
assert_eq!(s2.len(), 64);
}
#[test]
fn test_salt_file_path_is_in_rtk_dir() {
let path = salt_file_path();
assert!(path.to_string_lossy().contains("rtk"));
assert!(path.to_string_lossy().contains(".device_salt"));
}
#[test]
fn test_marker_path_exists() {
let path = telemetry_marker_path();
assert!(path.to_string_lossy().contains("rtk"));
}
#[test]
fn test_install_method_unix_paths() {
assert_eq!(
install_method_from_path("/opt/homebrew/Cellar/rtk/0.28.0/bin/rtk"),
"homebrew"
);
assert_eq!(
install_method_from_path("/usr/local/homebrew/bin/rtk"),
"homebrew"
);
assert_eq!(
install_method_from_path("/home/user/.cargo/bin/rtk"),
"cargo"
);
assert_eq!(
install_method_from_path("/home/user/.local/bin/rtk"),
"script"
);
assert_eq!(
install_method_from_path("/nix/store/abc123-rtk/bin/rtk"),
"nix"
);
assert_eq!(install_method_from_path("/usr/bin/rtk"), "other");
}
#[test]
fn test_install_method_windows_paths() {
assert_eq!(
install_method_from_path("C:\\Users\\user\\.cargo\\bin\\rtk.exe"),
"cargo"
);
assert_eq!(
install_method_from_path("C:\\Users\\user\\.local\\bin\\rtk.exe"),
"script"
);
assert_eq!(
install_method_from_path("C:\\Program Files\\rtk\\rtk.exe"),
"other"
);
}
#[test]
fn test_detect_install_method_returns_known_value() {
let method = detect_install_method();
assert!(
["homebrew", "cargo", "script", "nix", "other", "unknown"].contains(&method),
"Unexpected install method: {}",
method
);
}
#[test]
fn test_get_stats_returns_tuple() {
let tracker = match tracking::Tracker::new() {
Ok(t) => t,
Err(_) => return, // No DB — skip
};
let (cmds, top, pct, saved_24h, saved_total) = get_stats(&tracker);
assert!(cmds >= 0);
assert!(top.len() <= 5);
assert!(saved_24h >= 0);
assert!(saved_total >= 0);
if let Some(p) = pct {
assert!((0.0..=100.0).contains(&p));
}
}
#[test]
fn test_enriched_stats_returns_valid_data() {
let tracker = match tracking::Tracker::new() {
Ok(t) => t,
Err(_) => return,
};
let stats = get_enriched_stats(&tracker);
assert!(stats.passthrough_top.len() <= 5);
assert!(stats.parse_failures_24h >= 0);
assert!(stats.low_savings_commands.len() <= 5);
assert!((0.0..=100.0).contains(&stats.avg_savings_per_command));
assert!(
["claude", "gemini", "codex", "cursor", "none", "unknown"]
.iter()
.any(|&h| stats.hook_type.starts_with(h)),
"Unexpected hook type: {}",
stats.hook_type
);
}
#[test]
fn test_detect_hook_type_returns_known() {
let ht = detect_hook_type();
assert!(
["claude", "gemini", "codex", "cursor", "none", "unknown"].contains(&ht.as_str()),
"Unexpected hook type: {}",
ht
);
}
#[test]
fn test_count_custom_toml_filters() {
// Should not panic even if directories don't exist
let count = count_custom_toml_filters();
assert!(count < 10000); // sanity check
}
}