Skip to content

Commit e37f0a1

Browse files
authored
fix parsing metrics with decimal timestamp (#17)
1 parent 5d04f1c commit e37f0a1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

crates/dogstatsd/src/metric.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ pub fn parse(input: &str) -> Result<Metric, ParseError> {
268268
.unwrap_or_default();
269269
// let Metric::new() handle bucketing the timestamp
270270
let parsed_timestamp: i64 = match caps.name("timestamp") {
271-
Some(ts) => timestamp_to_bucket(ts.as_str().parse().unwrap_or(now)),
271+
Some(ts) => {
272+
let sec = ts.as_str().parse::<f64>().unwrap_or(now as f64).round() as i64;
273+
timestamp_to_bucket(sec)
274+
}
272275
None => timestamp_to_bucket(now),
273276
};
274277
let metric_value = match t {
@@ -625,6 +628,13 @@ mod tests {
625628
assert_eq!(metric.timestamp, 1656581400);
626629
}
627630

631+
#[test]
632+
fn parse_decimal_metric_timestamp() {
633+
let input = "page.views:15|c|#env:dev|T1656581409.123";
634+
let metric = parse(input).unwrap();
635+
assert_eq!(metric.timestamp, 1656581400);
636+
}
637+
628638
#[test]
629639
fn parse_metric_no_timestamp() {
630640
// *wince* this could be a race condition

0 commit comments

Comments
 (0)