Skip to content

Commit ce96e8a

Browse files
committed
unit tests for span link processing in trace_processor.rs
1 parent 0f2a069 commit ce96e8a

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

bottlecap/src/traces/trace_processor.rs

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl TraceProcessor for ServerlessTraceProcessor {
191191
#[cfg(test)]
192192
mod tests {
193193
use datadog_trace_obfuscation::obfuscation_config::ObfuscationConfig;
194+
use datadog_trace_utils::tracer_payload::TraceChunkProcessor;
194195
use std::{
195196
collections::HashMap,
196197
sync::Arc,
@@ -199,10 +200,12 @@ mod tests {
199200

200201
use crate::config::Config;
201202
use crate::tags::provider::Provider;
202-
use crate::traces::trace_processor::{self, TraceProcessor};
203+
use crate::traces::trace_processor::{self, TraceProcessor, ChunkProcessor};
203204
use crate::LAMBDA_RUNTIME_SLUG;
205+
use crate::span_pointers::SpanPointer;
204206
use datadog_trace_protobuf::pb;
205207
use datadog_trace_utils::{tracer_header_tags, tracer_payload::TracerPayloadCollection};
208+
use serde_json::json;
206209

207210
fn get_current_timestamp_nanos() -> i64 {
208211
i64::try_from(
@@ -342,4 +345,107 @@ mod tests {
342345
received_payload.expect("no payload received")
343346
);
344347
}
348+
349+
struct SpanPointerTestCase {
350+
test_name: &'static str,
351+
span_name: &'static str,
352+
span_pointers: Option<Vec<SpanPointer>>,
353+
expected_links: Option<serde_json::Value>,
354+
}
355+
356+
#[test]
357+
fn test_span_pointer_processing() {
358+
let test_cases = vec![
359+
SpanPointerTestCase {
360+
test_name: "adds span links to lambda span",
361+
span_name: "aws.lambda",
362+
span_pointers: Some(vec![
363+
SpanPointer { hash: "hash1".to_string(), kind: "test.kind1".to_string() },
364+
SpanPointer { hash: "hash2".to_string(), kind: "test.kind2".to_string() },
365+
]),
366+
expected_links: Some(json!([
367+
{
368+
"attributes": {
369+
"link.kind": "span-pointer",
370+
"ptr.dir": "u",
371+
"ptr.hash": "hash1",
372+
"ptr.kind": "test.kind1"
373+
},
374+
"span_id": "0",
375+
"trace_id": "0"
376+
},
377+
{
378+
"attributes": {
379+
"link.kind": "span-pointer",
380+
"ptr.dir": "u",
381+
"ptr.hash": "hash2",
382+
"ptr.kind": "test.kind2"
383+
},
384+
"span_id": "0",
385+
"trace_id": "0"
386+
}
387+
])),
388+
},
389+
SpanPointerTestCase {
390+
test_name: "ignores non-lambda span",
391+
span_name: "not.lambda",
392+
span_pointers: Some(vec![
393+
SpanPointer { hash: "hash1".to_string(), kind: "test.kind1".to_string() }
394+
]),
395+
expected_links: None,
396+
},
397+
SpanPointerTestCase {
398+
test_name: "handles empty span pointers",
399+
span_name: "aws.lambda",
400+
span_pointers: Some(vec![]),
401+
expected_links: None,
402+
},
403+
SpanPointerTestCase {
404+
test_name: "handles none span pointers",
405+
span_name: "aws.lambda",
406+
span_pointers: None,
407+
expected_links: None,
408+
},
409+
];
410+
411+
for case in test_cases {
412+
let tags_provider = create_tags_provider(create_test_config());
413+
414+
let span = pb::Span {
415+
name: case.span_name.to_string(),
416+
meta: HashMap::new(),
417+
..create_test_span(11, 222, 333, get_current_timestamp_nanos(), true, tags_provider.clone())
418+
};
419+
420+
let mut processor = ChunkProcessor {
421+
obfuscation_config: Arc::new(ObfuscationConfig::new().expect("Failed to create ObfuscationConfig for test")),
422+
tags_provider,
423+
span_pointers: case.span_pointers,
424+
};
425+
426+
let mut chunk = pb::TraceChunk {
427+
priority: 0,
428+
origin: String::new(),
429+
spans: vec![span],
430+
tags: HashMap::new(),
431+
dropped_trace: false,
432+
};
433+
434+
processor.process(&mut chunk, 0);
435+
436+
match case.expected_links {
437+
Some(expected) => {
438+
let span_links = chunk.spans[0].meta.get("_dd.span_links")
439+
.unwrap_or_else(|| panic!("[{}] Span links should be present", case.test_name));
440+
let actual_links: serde_json::Value = serde_json::from_str(span_links)
441+
.expect("Should be valid JSON");
442+
assert_eq!(actual_links, expected, "Failed test case: {}", case.test_name);
443+
},
444+
None => {
445+
assert!(!chunk.spans[0].meta.contains_key("_dd.span_links"),
446+
"Failed test case: {}", case.test_name);
447+
}
448+
}
449+
}
450+
}
345451
}

0 commit comments

Comments
 (0)