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

fix(protocol): Properly document which timestamps are accepted #1152

Merged
merged 8 commits into from
Dec 15, 2021

Conversation

untitaker
Copy link
Member

@untitaker untitaker commented Dec 9, 2021

Also remove use of a FromStr impl where chrono's documentation says
nothing about the format that is being parsed.

It is currently unclear to me whether the timestamp format we accept in prod is even all of RFC 3339.

Also remove use of a `FromStr` impl where chrono's documentation says
nothing about the format that is being parsed.
@untitaker untitaker requested a review from a team December 9, 2021 17:24
@untitaker untitaker changed the title fix(protocol): Properly document what timestamps are accepted fix(protocol): Properly document which timestamps are accepted Dec 9, 2021
@@ -976,9 +976,9 @@ impl FromValue for Timestamp {
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
let rv = match value {
Annotated(Some(Value::String(value)), mut meta) => {
let parsed = match value.parse::<NaiveDateTime>() {
let parsed = match NaiveDateTime::parse_from_str(&value, "%Y-%m-%dT%H:%M:%S%.f") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let parsed = match NaiveDateTime::parse_from_str(&value, "%Y-%m-%dT%H:%M:%S%.f") {
// Use the same format as `NaiveDateTime::FromStr`: https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDateTime.html#impl-FromStr
let parsed = match NaiveDateTime::parse_from_str(&value, "%Y-%m-%dT%H:%M:%S%.f") {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is lenient against spaces, but it's not obvious from the documentation or the format string. Can you please add a test case for something like "2015- 09-18T23:56:04"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jan-auer is this still relevant? are you saying FromStr is lenient as well?

Ok(dt) => Ok(DateTime::from_utc(dt, Utc)),
Err(_) => value.parse(),
Err(_) => DateTime::parse_from_rfc3339(&value).map(|dt| dt.with_timezone(&Utc)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a stricter parser, or is it exactly the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a more lenient parser afaict, but chrono literally does not document what FromStr actually accepts. So, not sure. Will sit on this PR for a bit.

Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 on the documentation changes.

However, I'm conflicted whether the explicit date time format is better than using the built-in FromStr, which is both documented and doesn't require parsing a format string over and over again. I would suggest that if we're touching the implementation, we should rather look into parsing full ISO 8601 in addition to RFC 3339 and then add sufficient tests.

Ok(dt) => Ok(DateTime::from_utc(dt, Utc)),

// XXX: This actually accepts more than RFC 3339. SDKs are strongly discouraged
// from exercising that freedom. We should only support RFC3339.
Copy link
Member

@jan-auer jan-auer Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation suggestion for SDKs is in a very hidden spot. If we keep current behavior, I would suggest removing the comment here and moving it to a doc comment or develop docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "real" docs already tell the SDKs to use RFC 3339, this code comment is intended to explain why the actual relay code accepts more.

@@ -976,9 +976,9 @@ impl FromValue for Timestamp {
fn from_value(value: Annotated<Value>) -> Annotated<Self> {
let rv = match value {
Annotated(Some(Value::String(value)), mut meta) => {
let parsed = match value.parse::<NaiveDateTime>() {
let parsed = match NaiveDateTime::parse_from_str(&value, "%Y-%m-%dT%H:%M:%S%.f") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is lenient against spaces, but it's not obvious from the documentation or the format string. Can you please add a test case for something like "2015- 09-18T23:56:04"?

@@ -304,14 +304,23 @@ pub struct Event {
/// value representing the number of seconds that have elapsed since the [Unix
/// epoch](https://en.wikipedia.org/wiki/Unix_time).
///
/// Timezone is assumed to be UTC if unclear.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Timezone is assumed to be UTC if unclear.
/// Timezone is assumed to be UTC if missing.

/// Sub-microsecond precision is not preserved with numeric values due to precision
/// limitations with floats (at least in our systems). With that caveat in mind, just send
/// whatever is easiest to produce.
///
/// All timestamps in the event protocol are formatted this way.
/// All timestamps in the event protocol, be it errors, transactions or the spans within a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would revert this sentence, it reads strange and doesn't add any insight.

@untitaker
Copy link
Member Author

@jan-auer I have reverted most changes and left it at adding code comments

@untitaker untitaker merged commit 429918a into master Dec 15, 2021
@untitaker untitaker deleted the fix/timestamps branch December 15, 2021 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants