Skip to content

Commit ddd04d0

Browse files
committed
Add timestamp to unstable feature usage metrics
1 parent 60493b8 commit ddd04d0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

compiler/rustc_feature/src/unstable.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! List of the unstable feature gates.
22
33
use std::path::PathBuf;
4+
use std::time::{SystemTime, UNIX_EPOCH};
45

56
use rustc_data_structures::fx::FxHashSet;
67
use rustc_span::{Span, Symbol, sym};
@@ -681,11 +682,13 @@ impl Features {
681682
) -> Result<(), Box<dyn std::error::Error>> {
682683
#[derive(serde::Serialize)]
683684
struct LibFeature {
685+
timestamp: u128,
684686
symbol: String,
685687
}
686688

687689
#[derive(serde::Serialize)]
688690
struct LangFeature {
691+
timestamp: u128,
689692
symbol: String,
690693
since: Option<String>,
691694
}
@@ -699,10 +702,20 @@ impl Features {
699702
let metrics_file = std::fs::File::create(metrics_path)?;
700703
let metrics_file = std::io::BufWriter::new(metrics_file);
701704

705+
let now = || {
706+
SystemTime::now()
707+
.duration_since(UNIX_EPOCH)
708+
.expect("system time should always be greater than the unix epoch")
709+
.as_nanos()
710+
};
711+
702712
let lib_features = self
703713
.enabled_lib_features
704714
.iter()
705-
.map(|EnabledLibFeature { gate_name, .. }| LibFeature { symbol: gate_name.to_string() })
715+
.map(|EnabledLibFeature { gate_name, .. }| LibFeature {
716+
symbol: gate_name.to_string(),
717+
timestamp: now(),
718+
})
706719
.collect();
707720

708721
let lang_features = self
@@ -711,6 +724,7 @@ impl Features {
711724
.map(|EnabledLangFeature { gate_name, stable_since, .. }| LangFeature {
712725
symbol: gate_name.to_string(),
713726
since: stable_since.map(|since| since.to_string()),
727+
timestamp: now(),
714728
})
715729
.collect();
716730

tests/run-make/unstable-feature-usage-metrics/rmake.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ fn test_metrics_dump() {
5858
);
5959

6060
let message = rfs::read_to_string(json_path);
61-
let parsed: serde_json::Value =
61+
let mut parsed: serde_json::Value =
6262
serde_json::from_str(&message).expect("metrics should be dumped as json");
63+
// remove timestamps
64+
assert!(parsed["lib_features"][0]["timestamp"].is_number());
65+
assert!(parsed["lang_features"][0]["timestamp"].is_number());
66+
parsed["lib_features"][0]["timestamp"] = serde_json::json!(null);
67+
parsed["lang_features"][0]["timestamp"] = serde_json::json!(null);
6368
let expected = serde_json::json!(
6469
{
65-
"lib_features":[{"symbol":"ascii_char"}],
66-
"lang_features":[{"symbol":"box_patterns","since":null}]
70+
"lib_features":[{"symbol":"ascii_char", "timestamp":null}],
71+
"lang_features":[{"symbol":"box_patterns","since":null, "timestamp":null}]
6772
}
6873
);
6974

0 commit comments

Comments
 (0)