8000 unit tests for span link processing in `trace_processor.rs` · DataDog/datadog-lambda-extension@1553895 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1553895

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

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

bottlecap/src/traces/trace_processor.rs

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,21 @@ 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,
197198
time::{SystemTime, UNIX_EPOCH},
198199
};
199200

200201
use crate::config::Config;
202+
use crate::span_pointers::SpanPointer;
201203
use crate::tags::provider::Provider;
202-
use crate::traces::trace_processor::{self, TraceProcessor};
204+
use crate::traces::trace_processor::{self, ChunkProcessor, TraceProcessor};
203205
use crate::LAMBDA_RUNTIME_SLUG;
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,135 @@ 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 {
364+
hash: "hash1".to_string(),
365+
kind: "test.kind1".to_string(),
366+
},
367+
SpanPointer {
368+
hash: "hash2".to_string(),
369+
kind: "test.kind2".to_string(),
370+
},
371+
]),
372+
expected_links: Some(json!([
373+
{
374+
"attributes": {
375+
"link.kind": "span-pointer",
376+
"ptr.dir": "u",
377+
"ptr.hash": "hash1",
378+
"ptr.kind": "test.kind1"
379+
},
380+
"span_id": "0",
381+
"trace_id": "0"
382+
},
383+
{
384+
"attributes": {
385+
"link.kind": "span-pointer",
386+
"ptr.dir": "u",
387+
"ptr.hash": "hash2",
388+
"ptr.kind": "test.kind2"
389+
},
390+
"span_id": "0",
391+
"trace_id": "0"
392+
}
393+
])),
394+
},
395+
SpanPointerTestCase {
396+
test_name: "ignores non-lambda span",
397+
span_name: "not.lambda",
398+
span_pointers: Some(vec![SpanPointer {
399+
hash: "hash1".to_string(),
400+
kind: "test.kind1".to_string(),
401+
}]),
402+
expected_links: None,
403+
},
404+
SpanPointerTestCase {
405+
test_name: "handles empty span pointers",
406+
span_name: "aws.lambda",
407+
span_pointers: Some(vec![]),
408+
expected_links: None,
409+
},
410+
SpanPointerTestCase {
411+
test_name: "handles none span pointers",
412+
span_name: "aws.lambda",
413+
span_pointers: None,
414+
expected_links: None,
415+
},
416+
];
417+
418+
for case in test_cases {
419+
let tags_provider = create_tags_provider(create_test_config());
420+
421+
let span = pb::Span {
422+
name: case.span_name.to_string(),
423+
meta: HashMap::new(),
424+
..create_test_span(
425+
11,
426+
222,
427+
333,
428+
get_current_timestamp_nanos(),
429+
true,
430+
tags_provider.clone(),
431+
)
432+
};
433+
434+
let mut processor = ChunkProcessor {
435+
obfuscation_config: Arc::new(
436+
ObfuscationConfig::new().expect("Failed to create ObfuscationConfig for test"),
437+
),
438+
tags_provider,
439+
span_pointers: case.span_pointers,
440+
};
441+
442+
let mut chunk = pb::TraceChunk {
443+
priority: 0,
444+
origin: String::new(),
445+
spans: vec![span],
446+
tags: HashMap::new(),
447+
dropped_trace: false,
448+
};
449+
450+
processor.process(&mut chunk, 0);
451+
452+
match case.expected_links {
453+
Some(expected) => {
454+
let span_links =
455+
chunk.spans[0]
456+
.meta
457+
.get("_dd.span_links")
458+
.unwrap_or_else(|| {
459+
panic!("[{}] Span links should be present", case.test_name)
460+
});
461+
let actual_links: serde_json::Value =
462+
serde_json::from_str(span_links).expect("Should be valid JSON");
463+
assert_eq!(
464+
actual_links, expected,
465+
"Failed test case: {}",
466+
case.test_name
467+
);
468+
}
469+
None => {
470+
assert!(
471+
!chunk.spans[0].meta.contains_key("_dd.span_links"),
472+
"Failed test case: {}",
473+
case.test_name
474+
);
475+
}
476+
}
477+
}
478+
}
345479
}

0 commit comments

Comments
 (0)
0