Skip to content

Commit

Permalink
fix: metric bucket start time should be reset everytime it is sent (#219
Browse files Browse the repository at this point in the history
)

* fix: metric bucket start time should be reset everytime it is sent

* chore: add test to cover that time is correctly advanced by metrics

---------

Co-authored-by: Simon Hornby <[email protected]>
  • Loading branch information
ivarconr and sighphyre authored Feb 19, 2025
1 parent 4537811 commit feddb5d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion unleash-yggdrasil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub struct EngineState {
compiled_state: Option<CompiledState>,
previous_state: ClientFeatures,
toggle_metrics: DashMap<String, Metric>,
toggle_metrics_start: DateTime<Utc>,
pub started: DateTime<Utc>,
}

Expand All @@ -233,6 +234,7 @@ impl Default for EngineState {
Self {
compiled_state: Default::default(),
toggle_metrics: Default::default(),
toggle_metrics_start: Utc::now(),
previous_state: Default::default(),
started: Utc::now(),
}
Expand Down Expand Up @@ -354,9 +356,11 @@ impl EngineState {
.collect();

if !metrics.is_empty() {
let start = self.toggle_metrics_start;
self.toggle_metrics_start = Utc::now();
Some(MetricBucket {
toggles: metrics,
start: self.started,
start,
stop: Utc::now(),
})
} else {
Expand Down Expand Up @@ -1083,6 +1087,27 @@ mod test {
assert!(metrics.is_none());
}

#[test]
pub fn getting_metrics_restarts_time() {
let compiled_state = HashMap::new();
let mut state = EngineState {
compiled_state: Some(compiled_state),
..Default::default()
};

state.count_toggle("some-test-toggle", true);

let metrics = state.get_metrics().unwrap();
let start = metrics.start;
std::thread::sleep(std::time::Duration::from_millis(1));

state.count_toggle("some-test-toggle", true);
let metrics = state.get_metrics().unwrap();
let new_start = metrics.start;

assert!(new_start > start);
}

#[test]
pub fn unknown_features_and_variants_get_metrics() {
let mut compiled_state = HashMap::new();
Expand Down

0 comments on commit feddb5d

Please sign in to comment.