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(general): Don't fail events containing a span with missing timestamp #1690

Merged
merged 15 commits into from
Dec 14, 2022
75 changes: 67 additions & 8 deletions relay-general/src/store/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ pub fn validate_timestamps(
}
}

pub fn validate_transaction(event: &mut Event) -> ProcessingResult {
if event.ty.value() != Some(&EventType::Transaction) {
return Ok(());
}

fn validate_transaction(event: &mut Event) -> ProcessingResult {
validate_timestamps(event)?;

let err_trace_context_required = Err(ProcessingAction::InvalidTransaction(
Expand Down Expand Up @@ -275,18 +271,23 @@ impl Processor for TransactionsProcessor {
normalize_transaction_name(&mut event.transaction)?;
}

validate_transaction(event)?;

let spans = event.spans.value_mut().get_or_insert_with(|| Vec::new());

for span in spans {
if span.value().is_none() {
if let Some(val) = span.value_mut() {
if val.timestamp.value().is_none() {
val.timestamp.set_value(event.timestamp.value().cloned());
val.status = Annotated::new(SpanStatus::DeadlineExceeded);
TBS1996 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
return Err(ProcessingAction::InvalidTransaction(
"spans must be valid in transaction event",
));
}
}

validate_transaction(event)?;

set_default_transaction_source(event);

event.process_child_values(self, state)?;
Expand Down Expand Up @@ -344,6 +345,7 @@ impl Processor for TransactionsProcessor {

#[cfg(test)]
mod tests {

use chrono::offset::TimeZone;
use chrono::Utc;
use similar_asserts::assert_eq;
Expand Down Expand Up @@ -421,6 +423,63 @@ mod tests {
);
}

#[test]
fn test_replace_missing_timestamp() {
let span = Span {
start_timestamp: Annotated::new(Utc.ymd(1968, 1, 1).and_hms_nano(0, 0, 1, 0).into()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
..Default::default()
};

let mut event = new_test_event().0.unwrap();
event.spans = Annotated::new(vec![Annotated::new(span)]);

TransactionsProcessor::default()
.process_event(
&mut event,
&mut Meta::default(),
&ProcessingState::default(),
)
.unwrap();

assert_eq!(
event.spans.value().unwrap()[0].value().unwrap().timestamp,
event.timestamp
);
}

#[test]
fn test_deadline_exceed_status_when_missing_timestamp() {
let span = Span {
start_timestamp: Annotated::new(Utc.ymd(1968, 1, 1).and_hms_nano(0, 0, 1, 0).into()),
trace_id: Annotated::new(TraceId("4c79f60c11214eb38604f4ae0781bfb2".into())),
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())),
..Default::default()
};

let mut event = new_test_event().0.unwrap();
event.spans = Annotated::new(vec![Annotated::new(span)]);

TransactionsProcessor::default()
.process_event(
&mut event,
&mut Meta::default(),
&ProcessingState::default(),
)
.unwrap();

assert_eq!(
event.spans.value().unwrap()[0]
.value()
.unwrap()
.status
.value()
.unwrap(),
&SpanStatus::DeadlineExceeded
);
TBS1996 marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn test_discards_when_missing_start_timestamp() {
let mut event = Annotated::new(Event {
Expand Down