wip: fix tests

This commit is contained in:
Simon Cruanes 2025-12-07 22:12:12 -05:00
parent 576ce9637b
commit 0a32049b4c
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 37 additions and 29 deletions

View file

@ -27,9 +27,10 @@ let i = ref 0
let run_job job_id : unit Lwt.t = let run_job job_id : unit Lwt.t =
while%lwt not @@ Atomic.get stop do while%lwt not @@ Atomic.get stop do
let tracer = T.Tracer.get_main () in
let@ scope = let@ scope =
Atomic.incr num_tr; Atomic.incr num_tr;
T.Trace.with_ ~kind:T.Span.Span_kind_producer "loop.outer" T.Tracer.with_ tracer ~kind:T.Span.Span_kind_producer "loop.outer"
~attrs:[ "i", `Int job_id ] ~attrs:[ "i", `Int job_id ]
in in
@ -39,9 +40,9 @@ let run_job job_id : unit Lwt.t =
Lwt.return @@ Atomic.set stop true Lwt.return @@ Atomic.set stop true
else else
(* parent scope is found via thread local storage *) (* parent scope is found via thread local storage *)
let@ scope = let@ span =
Atomic.incr num_tr; Atomic.incr num_tr;
T.Trace.with_ ~scope ~kind:T.Span.Span_kind_internal T.Tracer.with_ tracer ~parent:scope ~kind:T.Span.Span_kind_internal
~attrs:[ "j", `Int j ] ~attrs:[ "j", `Int j ]
"loop.inner" "loop.inner"
in in
@ -49,19 +50,20 @@ let run_job job_id : unit Lwt.t =
let* () = Lwt_unix.sleep !sleep_outer in let* () = Lwt_unix.sleep !sleep_outer in
Atomic.incr num_sleep; Atomic.incr num_sleep;
T.Logs.( Opentelemetry_emitter.Emitter.emit (T.Logger.get_main ())
emit [
[ T.Log_record.make_strf ~trace_id:(T.Span.trace_id span)
make_strf ~trace_id:scope.trace_id ~span_id:scope.span_id ~span_id:(T.Span.id span) ~severity:Severity_number_info
~severity:Severity_number_info "inner at %d" j; "inner at %d" j;
]); ];
incr i; incr i;
try%lwt try%lwt
Atomic.incr num_tr; Atomic.incr num_tr;
let@ scope = let@ scope =
T.Trace.with_ ~kind:T.Span.Span_kind_internal ~scope "alloc" T.Tracer.with_ tracer ~kind:T.Span.Span_kind_internal ~parent:span
"alloc"
in in
(* allocate some stuff *) (* allocate some stuff *)
if !stress_alloc_ then ( if !stress_alloc_ then (
@ -75,22 +77,23 @@ let run_job job_id : unit Lwt.t =
(* simulate a failure *) (* simulate a failure *)
if j = 4 && !i mod 13 = 0 then failwith "oh no"; if j = 4 && !i mod 13 = 0 then failwith "oh no";
Opentelemetry.Scope.add_event scope (fun () -> T.Span.add_event scope (T.Event.make "done with alloc");
T.Event.make "done with alloc");
Lwt.return () Lwt.return ()
with Failure _ -> Lwt.return () with Failure _ -> Lwt.return ()
done done
done done
let run () : unit Lwt.t = let run () : unit Lwt.t =
T.GC_metrics.basic_setup (); T.Gc_metrics.setup_on_main_exporter ();
T.Metrics_callbacks.register (fun () -> T.Metrics_callbacks.(
T.Metrics. with_set_added_to_main_exporter (fun set ->
[ add_metrics_cb set (fun () ->
sum ~name:"num-sleep" ~is_monotonic:true T.Metrics.
[ int (Atomic.get num_sleep) ]; [
]); sum ~name:"num-sleep" ~is_monotonic:true
[ int (Atomic.get num_sleep) ];
])));
let n_jobs = max 1 !n_jobs in let n_jobs = max 1 !n_jobs in
Printf.printf "run %d jobs\n%!" n_jobs; Printf.printf "run %d jobs\n%!" n_jobs;

View file

@ -22,8 +22,9 @@ let varied_tag_set =
|> add string_list_tag [ "foo"; "bar"; "baz" ]) |> add string_list_tag [ "foo"; "bar"; "baz" ])
let run () = let run () =
Opentelemetry.Globals.service_name := "emit_logs";
let otel_reporter = let otel_reporter =
Opentelemetry_logs.otel_reporter ~service_name:"emit_logs" Opentelemetry_logs.otel_reporter
~attributes:[ "my_reporter_attr", `String "foo" ] ~attributes:[ "my_reporter_attr", `String "foo" ]
() ()
in in
@ -35,7 +36,9 @@ let run () =
Logs.err (fun m -> m "emit_logs: error log"); Logs.err (fun m -> m "emit_logs: error log");
Logs.app (fun m -> m "emit_logs: app log"); Logs.app (fun m -> m "emit_logs: app log");
let%lwt () = let%lwt () =
T.Trace.with_ ~kind:T.Span.Span_kind_producer "my_scope" (fun _scope -> let tracer = T.Tracer.get_main () in
T.Tracer.with_ tracer ~kind:T.Span.Span_kind_producer "my_scope"
(fun _scope ->
Logs.info (fun m -> Logs.info (fun m ->
m ~tags:varied_tag_set m ~tags:varied_tag_set
"emit_logs: this log is emitted with varied tags from a span"); "emit_logs: this log is emitted with varied tags from a span");
@ -50,7 +53,8 @@ let run () =
let fmt_logger = Logs_fmt.reporter ~dst:Format.err_formatter () in let fmt_logger = Logs_fmt.reporter ~dst:Format.err_formatter () in
let combined_logger = let combined_logger =
Opentelemetry_logs.attach_otel_reporter ~service_name:"emit_logs_fmt" Opentelemetry_logs.attach_otel_reporter
(* FIXME ~service_name:"emit_logs_fmt" *)
~attributes:[ "my_fmt_attr", `String "bar" ] ~attributes:[ "my_fmt_attr", `String "bar" ]
fmt_logger fmt_logger
in in

View file

@ -1,5 +1,5 @@
open Alcotest open Alcotest
module Config = Opentelemetry_client.Config module Config = Opentelemetry_client.Client_config
let test_config_printing () = let test_config_printing () =
let module Env = Config.Env () in let module Env = Config.Env () in
@ -7,11 +7,12 @@ let test_config_printing () =
Format.asprintf "%a" Config.pp @@ Env.make (fun common () -> common) () Format.asprintf "%a" Config.pp @@ Env.make (fun common () -> common) ()
in in
let expected = let expected =
{|{ debug=false; "{ debug=false;\n\
self_trace=false; url_traces="http://localhost:4318/v1/traces"; \ self_trace=false; url_traces=\"http://localhost:4318/v1/traces\";\n\
url_metrics="http://localhost:4318/v1/metrics"; \ url_metrics=\"http://localhost:4318/v1/metrics\";\n\
url_logs="http://localhost:4318/v1/logs"; headers=; batch_traces=400; \ url_logs=\"http://localhost:4318/v1/logs\"; headers=[]; batch_traces=400;\n\
batch_metrics=20; batch_logs=400; batch_timeout_ms=2000 }|} \ batch_metrics=20; batch_logs=400; batch_timeout_ms=2000;\n\
\ http_concurrency_level=None }"
in in
check' string ~msg:"is rendered correctly" ~actual ~expected check' string ~msg:"is rendered correctly" ~actual ~expected

View file

@ -111,7 +111,7 @@ module Tested_program = struct
end end
let default_port = let default_port =
String.split_on_char ':' Client.Config.default_url |> function String.split_on_char ':' Client.Client_config.default_url |> function
(* Extracting the port from 'http://foo:<port>' *) (* Extracting the port from 'http://foo:<port>' *)
| [ _; _; port ] -> int_of_string port | [ _; _; port ] -> int_of_string port
| _ -> failwith "unexpected format in Client.Config.default_url" | _ -> failwith "unexpected format in Client.Config.default_url"