Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): Remove timestamp from text protocol #972

Merged
merged 13 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
**Internal**:

- Emit outcomes for rate limited attachments. ([#951](https://github.com/getsentry/relay/pull/951))
- Remove timestamp from metrics text protocol. ([#972](https://github.com/getsentry/relay/pull/972))
- Add max, min, sum, and count to gauge metrics. ([#974](https://github.com/getsentry/relay/pull/974))

## 21.3.1
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions relay-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ sentry-types = "0.20.0"
schemars = { version = "0.8.1", features = ["uuid", "chrono"], optional = true }
serde = { version = "1.0.114", features = ["derive"] }

[dev-dependencies]
serde_test = "1.0.125"

[features]
jsonschema = ["schemars"]
default = []
107 changes: 105 additions & 2 deletions relay-common/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::fmt;
use std::time::{Duration, Instant, SystemTime};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Converts an `Instant` into a `SystemTime`.
Expand Down Expand Up @@ -146,12 +147,114 @@ impl Serialize for UnixTimestamp {
}
}

struct UnixTimestampVisitor;

impl<'de> serde::de::Visitor<'de> for UnixTimestampVisitor {
type Value = UnixTimestamp;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a non-negative timestamp or datetime string")
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(UnixTimestamp::from_secs(v))
}

fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v < 0.0 || v > u64::MAX as f64 {
return Err(E::custom("timestamp out-of-range"));
}

Ok(UnixTimestamp::from_secs(v.trunc() as u64))
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let datetime = v.parse::<DateTime<Utc>>().map_err(E::custom)?;
let timestamp = datetime.timestamp();

if timestamp >= 0 {
Ok(UnixTimestamp(timestamp as u64))
} else {
Err(E::custom("timestamp out-of-range"))
}
}
}

impl<'de> Deserialize<'de> for UnixTimestamp {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let secs = u64::deserialize(deserializer)?;
Ok(Self::from_secs(secs))
deserializer.deserialize_any(UnixTimestampVisitor)
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};

#[test]
fn test_parse_timestamp_int() {
assert_tokens(&UnixTimestamp::from_secs(123), &[Token::U64(123)]);
}

#[test]
fn test_parse_timestamp_neg_int() {
assert_de_tokens_error::<UnixTimestamp>(
&[Token::I64(-1)],
"invalid type: integer `-1`, expected a non-negative timestamp or datetime string",
);
}

#[test]
fn test_parse_timestamp_float() {
assert_de_tokens(&UnixTimestamp::from_secs(123), &[Token::F64(123.4)]);
}

#[test]
fn test_parse_timestamp_large_float() {
assert_de_tokens_error::<UnixTimestamp>(
&[Token::F64(2.0 * (u64::MAX as f64))],
"timestamp out-of-range",
);
}

#[test]
fn test_parse_timestamp_neg_float() {
assert_de_tokens_error::<UnixTimestamp>(&[Token::F64(-1.0)], "timestamp out-of-range");
}

#[test]
fn test_parse_timestamp_str() {
assert_de_tokens(
&UnixTimestamp::from_secs(123),
&[Token::Str("1970-01-01T00:02:03Z")],
);
}

#[test]
fn test_parse_timestamp_other() {
assert_de_tokens_error::<UnixTimestamp>(
&[Token::Bool(true)],
"invalid type: boolean `true`, expected a non-negative timestamp or datetime string",
);
}

#[test]
fn test_parse_datetime_bogus() {
assert_de_tokens_error::<UnixTimestamp>(
&[Token::Str("adf3rt546")],
"input contains invalid characters",
);
}
}
2 changes: 1 addition & 1 deletion relay-metrics/src/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ mod tests {
})
.and_then(|_| {
// Wait until flush delay has passed
relay_test::delay(Duration::from_millis(1100)).map_err(|_| ())
relay_test::delay(Duration::from_millis(1500)).map_err(|_| ())
})
.and_then(|_| {
// After the flush delay has passed, the receiver should have the bucket:
Expand Down
19 changes: 17 additions & 2 deletions relay-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@
//! looks like this:
//!
//! ```text
//! endpoint.response_time@ms:57|d|'1615889449|#route:user_index
//! endpoint.hits:1|c|'1615889449|#route:user_index
//! endpoint.response_time@ms:57|d|#route:user_index
//! endpoint.hits:1|c|#route:user_index
//! ```
//!
//! The metric type is part of its signature just like the unit. Therefore, it is allowed to reuse a
//! metric name for multiple metric types, which will result in multiple metrics being recorded.
//!
//! # Metric Envelopes
//!
//! To send one or more metrics to Relay, the raw protocol is enclosed in an envelope item of type
//! `metrics`:
//!
//! ```text
//! {}
//! {"type": "metrics", "timestamp": 1615889440, ...}
//! endpoint.response_time@ms:57|d|#route:user_index
//! ...
//! ```
//!
//! The timestamp in the item header is used to send backdated metrics. If it is omitted,
//! the `received` time of the envelope is assumed.
//!
//! # Aggregation
//!
//! Relay accumulates all metrics in [time buckets](Bucket) before sending them onwards. Aggregation
Expand Down
Loading