Skip to content

Commit 40398c4

Browse files
authored
Refactor common unit test create_test_span function into trace-utils (#226)
1 parent a04191c commit 40398c4

File tree

1 file changed

+14
-74
lines changed

1 file changed

+14
-74
lines changed

src/trace_processor.rs

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ impl TraceProcessor for ServerlessTraceProcessor {
107107
#[cfg(test)]
108108
mod tests {
109109
use hyper::Request;
110-
use serde_json::json;
111110
use std::{
112111
collections::HashMap,
113112
sync::Arc,
@@ -120,70 +119,12 @@ mod tests {
120119
trace_processor::{self, TraceProcessor},
121120
};
122121
use datadog_trace_protobuf::pb;
123-
use datadog_trace_utils::trace_utils;
122+
use datadog_trace_utils::{
123+
trace_test_utils::{create_test_json_span, create_test_span},
124+
trace_utils,
125+
};
124126
use ddcommon::Endpoint;
125127

126-
fn create_test_span(start: i64, span_id: u64, parent_id: u64, is_top_level: bool) -> pb::Span {
127-
let mut span = pb::Span {
128-
trace_id: 111,
129-
span_id,
130-
service: "test-service".to_string(),
131-
name: "test_name".to_string(),
132-
resource: "test-resource".to_string(),
133-
parent_id,
134-
start,
135-
duration: 5,
136-
error: 0,
137-
meta: HashMap::from([
138-
("service".to_string(), "test-service".to_string()),
139-
("env".to_string(), "test-env".to_string()),
140-
(
141-
"runtime-id".to_string(),
142-
"afjksdljfkllksdj-28934889".to_string(),
143-
),
144-
]),
145-
metrics: HashMap::new(),
146-
r#type: "".to_string(),
147-
meta_struct: HashMap::new(),
148-
};
149-
if is_top_level {
150-
span.metrics.insert("_top_level".to_string(), 1.0);
151-
span.meta
152-
.insert("_dd.origin".to_string(), "cloudfunction".to_string());
153-
span.meta
154-
.insert("origin".to_string(), "cloudfunction".to_string());
155-
span.meta.insert(
156-
"functionname".to_string(),
157-
"dummy_function_name".to_string(),
158-
);
159-
span.r#type = "serverless".to_string();
160-
}
161-
span
162-
}
163-
164-
fn create_test_json_span(start: i64, span_id: u64, parent_id: u64) -> serde_json::Value {
165-
json!(
166-
{
167-
"trace_id": 111,
168-
"span_id": span_id,
169-
"service": "test-service",
170-
"name": "test_name",
171-
"resource": "test-resource",
172-
"parent_id": parent_id,
173-
"start": start,
174-
"duration": 5,
175-
"error": 0,
176-
"meta": {
177-
"service": "test-service",
178-
"env": "test-env",
179-
"runtime-id": "afjksdljfkllksdj-28934889",
180-
},
181-
"metrics": {},
182-
"meta_struct": {},
183-
}
184-
)
185-
}
186-
187128
fn get_current_timestamp_nanos() -> i64 {
188129
SystemTime::now()
189130
.duration_since(UNIX_EPOCH)
@@ -222,7 +163,7 @@ mod tests {
222163

223164
let start = get_current_timestamp_nanos();
224165

225-
let json_span = create_test_json_span(start, 222, 0);
166+
let json_span = create_test_json_span(11, 222, 333, start);
226167

227168
let bytes = rmp_serde::to_vec(&vec![vec![json_span]]).unwrap();
228169
let request = Request::builder()
@@ -255,11 +196,11 @@ mod tests {
255196
language_name: "nodejs".to_string(),
256197
language_version: "v19.7.0".to_string(),
257198
tracer_version: "4.0.0".to_string(),
258-
runtime_id: "afjksdljfkllksdj-28934889".to_string(),
199+
runtime_id: "test-runtime-id-value".to_string(),
259200
chunks: vec![pb::TraceChunk {
260201
priority: i8::MIN as i32,
261202
origin: "".to_string(),
262-
spans: vec![create_test_span(start, 222, 0, true)],
203+
spans: vec![create_test_span(11, 222, 333, start, true)],
263204
tags: HashMap::new(),
264205
dropped_trace: false,
265206
}],
@@ -285,9 +226,9 @@ mod tests {
285226
let start = get_current_timestamp_nanos();
286227

287228
let json_trace = vec![
288-
create_test_json_span(start, 333, 222),
289-
create_test_json_span(start, 222, 0),
290-
create_test_json_span(start, 444, 333),
229+
create_test_json_span(11, 333, 222, start),
230+
create_test_json_span(11, 222, 0, start),
231+
create_test_json_span(11, 444, 333, start),
291232
];
292233

293234
let bytes = rmp_serde::to_vec(&vec![json_trace]).unwrap();
@@ -321,14 +262,14 @@ mod tests {
321262
language_name: "nodejs".to_string(),
322263
language_version: "v19.7.0".to_string(),
323264
tracer_version: "4.0.0".to_string(),
324-
runtime_id: "afjksdljfkllksdj-28934889".to_string(),
265+
runtime_id: "test-runtime-id-value".to_string(),
325266
chunks: vec![pb::TraceChunk {
326267
priority: i8::MIN as i32,
327268
origin: "".to_string(),
328269
spans: vec![
329-
create_test_span(start, 333, 222, false),
330-
create_test_span(start, 222, 0, true),
331-
create_test_span(start, 444, 333, false),
270+
create_test_span(11, 333, 222, start, false),
271+
create_test_span(11, 222, 0, start, true),
272+
create_test_span(11, 444, 333, start, false),
332273
],
333274
tags: HashMap::new(),
334275
dropped_trace: false,
@@ -338,7 +279,6 @@ mod tests {
338279
hostname: "".to_string(),
339280
app_version: "".to_string(),
340281
};
341-
342282
assert_eq!(
343283
expected_tracer_payload,
344284
tracer_payload.unwrap().get_payloads()[0]

0 commit comments

Comments
 (0)