@@ -43,31 +43,6 @@ pub const Span = struct {
4343 return span_result ;
4444 }
4545
46- pub fn initLegacy (span_name : []const u8 , parent_span_id : ? u64 , task_context_id : u64 ) Span {
47- assert (span_name .len > 0 );
48- assert (span_name .len < 256 );
49- assert (task_context_id >= 1 );
50- assert (parent_span_id == null or parent_span_id .? >= 1 );
51-
52- const trace_id_expanded = trace_mod .expand_short_to_trace_id (task_context_id );
53- const parent_id_bytes = trace_mod .generate_span_id ();
54- const trace_ctx = trace_mod.TraceContext {
55- .version = 0x00 ,
56- .trace_id = trace_id_expanded ,
57- .parent_id = parent_id_bytes ,
58- .trace_flags = trace_mod .TraceFlags .sampled_only (false ),
59- };
60-
61- var parent_span_bytes : ? [8 ]u8 = null ;
62- if (parent_span_id ) | pid | {
63- var pb : [8 ]u8 = undefined ;
64- std .mem .writeInt (u64 , & pb , pid , .big );
65- parent_span_bytes = pb ;
66- }
67-
68- return init (span_name , parent_span_bytes , trace_ctx );
69- }
70-
7146 pub fn getSpanIdBytes (self : * const Span ) [8 ]u8 {
7247 return self .trace_context .parent_id ;
7348 }
@@ -271,3 +246,143 @@ pub fn createChildTaskContext() TaskContext {
271246 assert (child_context .parent_id .? == parent_context .id );
272247 return child_context ;
273248}
249+
250+ const testing = std .testing ;
251+
252+ test "Span.init creates valid span" {
253+ const trace_ctx = trace_mod .TraceContext .init (true );
254+ const span = Span .init ("test_span" , null , trace_ctx );
255+
256+ try testing .expectEqualStrings ("test_span" , span .name );
257+ try testing .expect (span .start_time > 0 );
258+ try testing .expect (span .thread_id > 0 );
259+ try testing .expect (span .id > 0 );
260+ try testing .expect (span .parent_id == null );
261+ try testing .expect (span .task_id > 0 );
262+ }
263+
264+ test "Span.getSpanIdBytes returns correct bytes" {
265+ const trace_ctx = trace_mod .TraceContext .init (false );
266+ const span = Span .init ("test" , null , trace_ctx );
267+ const span_bytes = span .getSpanIdBytes ();
268+
269+ try testing .expect (span_bytes .len == 8 );
270+ try testing .expect (! trace_mod .is_all_zero_id (span_bytes [0.. ]));
271+ }
272+
273+ test "TaskContext.init creates valid context" {
274+ const context = TaskContext .init (null );
275+
276+ try testing .expect (context .id >= 1 );
277+ try testing .expect (context .parent_id == null );
278+ try testing .expect (context .span_stack .len == 0 );
279+ try testing .expect (context .span_stack .capacity () == 32 );
280+ }
281+
282+ test "TaskContext.fromTraceContext creates valid context" {
283+ const trace_ctx = trace_mod .TraceContext .init (true );
284+ const context = TaskContext .fromTraceContext (trace_ctx );
285+
286+ try testing .expect (context .id >= 1 );
287+ try testing .expect (context .parent_id == null );
288+ try testing .expect (context .span_stack .len == 0 );
289+ }
290+
291+ test "TaskContext span stack operations" {
292+ var context = TaskContext .init (null );
293+ const span_bytes = [_ ]u8 { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
294+
295+ try testing .expect (context .currentSpan () == null );
296+ try testing .expect (context .popSpan () == null );
297+
298+ try context .pushSpan (span_bytes );
299+ try testing .expect (context .span_stack .len == 1 );
300+
301+ const current = context .currentSpan ().? ;
302+ try testing .expect (std .mem .eql (u8 , & current , & span_bytes ));
303+
304+ const popped = context .popSpan ().? ;
305+ try testing .expect (std .mem .eql (u8 , & popped , & span_bytes ));
306+ try testing .expect (context .span_stack .len == 0 );
307+ }
308+
309+ test "TaskContext legacy span operations" {
310+ var context = TaskContext .init (null );
311+ const span_id : u64 = 12345 ;
312+
313+ try context .pushSpanLegacy (span_id );
314+ try testing .expect (context .span_stack .len == 1 );
315+
316+ const current_legacy = context .currentSpanLegacy ().? ;
317+ try testing .expect (current_legacy == span_id );
318+
319+ const popped_legacy = context .popSpanLegacy ().? ;
320+ try testing .expect (popped_legacy == span_id );
321+ try testing .expect (context .span_stack .len == 0 );
322+ }
323+
324+ test "TaskContext.createChildTraceContext" {
325+ const context = TaskContext .init (null );
326+ const child_trace = context .createChildTraceContext (true );
327+
328+ try testing .expect (std .mem .eql (u8 , & child_trace .trace_id , & context .trace_context .trace_id ));
329+ try testing .expect (! std .mem .eql (u8 , & child_trace .parent_id , & context .trace_context .parent_id ));
330+ try testing .expect (child_trace .trace_flags .sampled == true );
331+ }
332+
333+ test "CorrelationContext.fromTraceContext" {
334+ const trace_ctx = trace_mod .TraceContext .init (false );
335+ const corr_ctx = CorrelationContext .fromTraceContext (trace_ctx , null , .info );
336+
337+ try testing .expect (corr_ctx .task_id >= 1 );
338+ try testing .expect (corr_ctx .span_id > 0 );
339+ try testing .expect (corr_ctx .thread_id > 0 );
340+ try testing .expect (corr_ctx .level == .info );
341+ }
342+
343+ test "CorrelationContext.fromIds" {
344+ const corr_ctx = CorrelationContext .fromIds (12345 , 67890 , .warn );
345+
346+ try testing .expect (corr_ctx .task_id == @as (u32 , @truncate (12345 )));
347+ try testing .expect (corr_ctx .span_id == @as (u32 , @truncate (67890 )));
348+ try testing .expect (corr_ctx .thread_id > 0 );
349+ try testing .expect (corr_ctx .level == .warn );
350+ }
351+
352+ test "generate_task_id produces unique IDs" {
353+ const id1 = generate_task_id ();
354+ const id2 = generate_task_id ();
355+
356+ try testing .expect (id1 >= 1 );
357+ try testing .expect (id2 >= 1 );
358+ try testing .expect (id1 != id2 );
359+ try testing .expect (id2 > id1 );
360+ }
361+
362+ test "getCurrentTaskContext returns valid context" {
363+ const context = getCurrentTaskContext ();
364+
365+ try testing .expect (context .id >= 1 );
366+ try testing .expect (context .span_stack .capacity () == 32 );
367+ }
368+
369+ test "setTaskContext and getCurrentTaskContext" {
370+ var custom_context = TaskContext .init (null );
371+ const original_id = custom_context .id ;
372+
373+ setTaskContext (& custom_context );
374+ const retrieved_context = getCurrentTaskContext ();
375+
376+ try testing .expect (retrieved_context .id == original_id );
377+ }
378+
379+ test "createChildTaskContext creates child with parent reference" {
380+ var parent_context = TaskContext .init (null );
381+ setTaskContext (& parent_context );
382+
383+ const child_context = createChildTaskContext ();
384+
385+ try testing .expect (child_context .id >= 1 );
386+ try testing .expect (child_context .parent_id .? == parent_context .id );
387+ try testing .expect (child_context .id != parent_context .id );
388+ }
0 commit comments