Skip to content

Commit

Permalink
Fix JsonSpanIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
guilload committed Oct 24, 2023
1 parent 66c6919 commit 6c566b6
Showing 1 changed file with 80 additions and 17 deletions.
97 changes: 80 additions & 17 deletions quickwit/quickwit-opentelemetry/src/otlp/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub enum OtlpTraceError {
TraceId(#[from] TryFromTraceIdError),
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Span {
pub trace_id: TraceId,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -418,7 +418,7 @@ impl FromStr for SpanKind {
}

/// Concatenation of the service name, span kind, and span name.
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct SpanFingerprint(String);

impl SpanFingerprint {
Expand Down Expand Up @@ -488,7 +488,7 @@ impl SpanFingerprint {
}
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct SpanStatus {
pub code: OtlpStatusCode,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -588,7 +588,7 @@ impl Scope {
}
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Event {
pub event_timestamp_nanos: u64,
pub event_name: String,
Expand All @@ -600,7 +600,7 @@ pub struct Event {
pub event_dropped_attributes_count: u32,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Link {
pub link_trace_id: TraceId,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -837,8 +837,8 @@ pub struct JsonSpanIterator {
impl JsonSpanIterator {
fn new(spans: BTreeSet<OrdSpan>, num_bytes: usize) -> Self {
let num_spans = spans.len();
let avg_span_size = num_bytes / num_spans;
let avg_span_size_rem = avg_span_size + num_bytes % num_spans;
let avg_span_size = num_bytes.checked_div(num_spans).unwrap_or(0);
let avg_span_size_rem = avg_span_size + num_bytes.checked_rem(num_spans).unwrap_or(0);

Self {
spans: spans.into_iter(),
Expand Down Expand Up @@ -971,7 +971,7 @@ mod tests {
span_id: vec![2; 8],
parent_span_id: vec![3; 8],
trace_state: "".to_string(),
name: "publish_split".to_string(),
name: "publish_splits".to_string(),
kind: 2, // Server
start_time_unix_nano: 1_000_000_001,
end_time_unix_nano: 1_001_000_002,
Expand Down Expand Up @@ -1000,10 +1000,10 @@ mod tests {
assert_eq!(span.parent_span_id, Some(SpanId::new([3; 8])));
assert_eq!(span.span_id, SpanId::new([2; 8]));
assert_eq!(span.span_kind, 2);
assert_eq!(span.span_name, "publish_split");
assert_eq!(span.span_name, "publish_splits");
assert_eq!(
span.span_fingerprint.unwrap(),
SpanFingerprint::new(UNKNOWN_SERVICE, SpanKind(2), "publish_split")
SpanFingerprint::new(UNKNOWN_SERVICE, SpanKind(2), "publish_splits")
);
assert_eq!(span.span_start_timestamp_nanos, 1_000_000_001);
assert_eq!(span.span_end_timestamp_nanos, 1_001_000_002);
Expand Down Expand Up @@ -1069,7 +1069,7 @@ mod tests {
span_id: vec![2; 8],
parent_span_id: vec![3; 8],
trace_state: "key1=value1,key2=value2".to_string(),
name: "publish_split".to_string(),
name: "publish_splits".to_string(),
kind: 2, // Server
start_time_unix_nano: 1_000_000_001,
end_time_unix_nano: 1_001_000_002,
Expand Down Expand Up @@ -1107,10 +1107,10 @@ mod tests {
assert_eq!(span.parent_span_id, Some(SpanId::new([3; 8])));
assert_eq!(span.span_id, SpanId::new([2; 8]));
assert_eq!(span.span_kind, 2);
assert_eq!(span.span_name, "publish_split");
assert_eq!(span.span_name, "publish_splits");
assert_eq!(
span.span_fingerprint.unwrap(),
SpanFingerprint::new("quickwit", SpanKind(2), "publish_split")
SpanFingerprint::new("quickwit", SpanKind(2), "publish_splits")
);
assert_eq!(span.span_start_timestamp_nanos, 1_000_000_001);
assert_eq!(span.span_end_timestamp_nanos, 1_001_000_002);
Expand Down Expand Up @@ -1159,10 +1159,10 @@ mod tests {

#[test]
fn test_span_fingerprint() {
let span_fingerprint = SpanFingerprint::new("quickwit", SpanKind(2), "publish_split");
let span_fingerprint = SpanFingerprint::new("quickwit", SpanKind(2), "publish_splits");
assert_eq!(
span_fingerprint.as_str(),
"quickwit\u{0}2\u{0}publish_split"
"quickwit\u{0}2\u{0}publish_splits"
);

let start_key_opt = SpanFingerprint::start_key("", None);
Expand Down Expand Up @@ -1231,7 +1231,7 @@ mod tests {
scope_dropped_attributes_count: 0,
span_id: SpanId::new([2; 8]),
span_kind: 0,
span_name: "publish_split".to_string(),
span_name: "publish_splits".to_string(),
span_fingerprint: Some(SpanFingerprint::new(
"quickwit",
SpanKind(2),
Expand Down Expand Up @@ -1270,7 +1270,7 @@ mod tests {
scope_dropped_attributes_count: 1,
span_id: SpanId::new([2; 8]),
span_kind: 1,
span_name: "publish_split".to_string(),
span_name: "publish_splits".to_string(),
span_fingerprint: Some(SpanFingerprint::new(
"quickwit",
SpanKind(2),
Expand Down Expand Up @@ -1308,4 +1308,67 @@ mod tests {
assert_eq!(span, expected_span);
}
}

#[test]
fn test_json_span_iterator() {
let mut json_span_iterator = JsonSpanIterator::new(BTreeSet::new(), 0);
assert!(json_span_iterator.next().is_none());

let span_0 = Span {
trace_id: TraceId::new([1; 16]),
trace_state: None,
service_name: "quickwit".to_string(),
resource_attributes: HashMap::new(),
resource_dropped_attributes_count: 0,
scope_name: None,
scope_version: None,
scope_attributes: HashMap::new(),
scope_dropped_attributes_count: 0,
span_id: SpanId::new([2; 8]),
span_kind: 0,
span_name: "publish_splits".to_string(),
span_fingerprint: Some(SpanFingerprint::new(
"quickwit",
SpanKind(2),
"publish_splits",
)),
span_start_timestamp_nanos: 1_000_000_001,
span_end_timestamp_nanos: 1_000_000_002,
span_duration_millis: Some(1),
span_attributes: HashMap::new(),
span_dropped_attributes_count: 0,
span_dropped_events_count: 0,
span_dropped_links_count: 0,
span_status: SpanStatus::default(),
parent_span_id: None,
events: Vec::new(),
event_names: Vec::new(),
links: Vec::new(),
};

let spans = BTreeSet::from_iter([OrdSpan(span_0.clone())]);
let mut json_span_iterator = JsonSpanIterator::new(spans, 3);

assert_eq!(
json_span_iterator.next(),
Some((serde_json::to_value(&span_0).unwrap(), 3))
);
assert!(json_span_iterator.next().is_none());

let mut span_1 = span_0.clone();
span_1.span_id = SpanId::new([3; 8]);

let spans = BTreeSet::from_iter([OrdSpan(span_0.clone()), OrdSpan(span_1.clone())]);
let mut json_span_iterator = JsonSpanIterator::new(spans, 7);

assert_eq!(
json_span_iterator.next(),
Some((serde_json::to_value(&span_0).unwrap(), 3))
);
assert_eq!(
json_span_iterator.next(),
Some((serde_json::to_value(&span_1).unwrap(), 4))
);
assert!(json_span_iterator.next().is_none());
}
}

0 comments on commit 6c566b6

Please sign in to comment.