test: add test to check Span.dummy never gets modified

This commit is contained in:
Simon Cruanes 2026-03-03 17:52:40 -05:00
parent 20e395abf4
commit e72b986f14
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 75 additions and 1 deletions

View file

@ -1,4 +1,4 @@
(tests (tests
(names test_trace_context t_size t_histogram) (names test_trace_context t_size t_histogram test_span_dummy)
(package opentelemetry) (package opentelemetry)
(libraries pbrt opentelemetry opentelemetry-client)) (libraries pbrt opentelemetry opentelemetry-client))

View file

@ -0,0 +1,27 @@
ok: add_attrs
ok: add_attrs'
ok: add_event
ok: add_event'
ok: add_links
ok: add_links'
ok: set_status
ok: set_kind
ok: record_exception
span dummy at the end: { trace_id = <bytes len=16>;
span_id = <bytes len=8>;
trace_state = "" (* absent *);
parent_span_id = <bytes len=0> (* absent *);
flags = 0 (* absent *);
name = "" (* absent *);
kind = Span_kind_unspecified (* absent *);
start_time_unix_nano = 0 (* absent *);
end_time_unix_nano = 0 (* absent *);
attributes = [];
dropped_attributes_count = 0 (* absent *);
events = [];
dropped_events_count = 0 (* absent *);
links = [];
dropped_links_count = 0 (* absent *);
status = None;
}
all ok

View file

@ -0,0 +1,47 @@
open Opentelemetry
(** Check that Span.dummy is never modified by mutation functions *)
let check_pristine () =
let d = Span.dummy in
assert (Span.attrs d = []);
assert (Span.events d = []);
assert (Span.links d = []);
assert (Span.status d = None);
assert (Span.kind d = None);
assert (not (Span.is_not_dummy d))
let check name f =
f ();
check_pristine ();
Printf.printf "ok: %s\n" name
let trace_id = Trace_id.create ()
let span_id = Span_id.create ()
let () =
check_pristine ();
check "add_attrs" (fun () -> Span.add_attrs Span.dummy [ "k", `String "v" ]);
check "add_attrs'" (fun () ->
Span.add_attrs' Span.dummy (fun () -> [ "k", `Int 42 ]));
check "add_event" (fun () -> Span.add_event Span.dummy (Event.make "ev"));
check "add_event'" (fun () ->
Span.add_event' Span.dummy (fun () -> Event.make "ev"));
check "add_links" (fun () ->
Span.add_links Span.dummy [ Span_link.make ~trace_id ~span_id () ]);
check "add_links'" (fun () ->
Span.add_links' Span.dummy (fun () ->
[ Span_link.make ~trace_id ~span_id () ]));
check "set_status" (fun () ->
Span.set_status Span.dummy
(Span_status.make ~message:"err" ~code:Span_status.Status_code_error));
check "set_kind" (fun () -> Span.set_kind Span.dummy Span_kind_server);
check "record_exception" (fun () ->
try raise Exit
with exn ->
let bt = Printexc.get_raw_backtrace () in
Span.record_exception Span.dummy exn bt);
Format.printf "span dummy at the end: %a@." Opentelemetry_proto.Trace.pp_span
Span.dummy;
print_endline "all ok"