From 97daf5b4022917b1267fdebebcad6a5dedda023f Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 14:45:50 -0500 Subject: [PATCH 1/7] compat with trace 0.6 --- src/trace/opentelemetry_trace.ml | 35 ++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/trace/opentelemetry_trace.ml b/src/trace/opentelemetry_trace.ml index 1d727a57..bbc5391d 100644 --- a/src/trace/opentelemetry_trace.ml +++ b/src/trace/opentelemetry_trace.ml @@ -130,7 +130,7 @@ module Internal = struct otrace_id, sb - let exit_span' otrace_id + let exit_span_ { id = otel_id; start_time; @@ -145,11 +145,7 @@ module Internal = struct parent_scope = _; } = let open Otel in - let active_spans = Active_spans.get () in - Active_span_tbl.remove active_spans.tbl otrace_id; - let end_time = Timestamp_ns.now_unix_ns () in - let kind, attrs = otel_attrs_of_otrace_data data in let attrs = @@ -176,6 +172,19 @@ module Internal = struct ~end_time ~attrs name |> fst + let exit_span' otrace_id otel_span_begin = + let active_spans = Active_spans.get () in + Active_span_tbl.remove active_spans.tbl otrace_id; + exit_span_ otel_span_begin + + let exit_span_from_id otrace_id = + let active_spans = Active_spans.get () in + match Active_span_tbl.find_opt active_spans.tbl otrace_id with + | None -> None + | Some otel_span_begin -> + Active_span_tbl.remove active_spans.tbl otrace_id; + Some (exit_span_ otel_span_begin) + module M = struct let with_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name cb = let otrace_id, sb = @@ -187,9 +196,23 @@ module Internal = struct let otel_span = exit_span' otrace_id sb in Otel.Trace.emit [ otel_span ]; - rv + let enter_span ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name : + Trace_core.span = + let otrace_id, _sb = + enter_span' ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name + in + (* NOTE: we cannot enter ambient scope in a disjoint way + with the exit, because we only have [Ambient_context.with_binding], + no [set_binding] *) + otrace_id + + let exit_span otrace_id = + match exit_span_from_id otrace_id with + | None -> () + | Some otel_span -> Otel.Trace.emit [ otel_span ] + let enter_manual_span ~(parent : Otrace.explicit_span option) ~flavor:_ ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name : Otrace.explicit_span = let otrace_id, sb = From 086c7a71df93379537ebb5d21375e9c0ca0a3500 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 14:46:42 -0500 Subject: [PATCH 2/7] CI --- .github/workflows/main.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e29237b4..0e05033f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,6 +49,12 @@ jobs: - run: opam exec -- dune build @install -p opentelemetry,opentelemetry-lwt,opentelemetry-client-ocurl,opentelemetry-cohttp-lwt,opentelemetry-client-cohttp-lwt + - run: opam install trace.0.4 + - run: opam exec dune build @install -p opentelemetry + + - run: opam install trace.0.6 + - run: opam exec dune build @install -p opentelemetry + - run: opam install ocaml-protoc - run: opam exec -- dune build @lint From 8d0f815fe2f31e6b1b66dc60b6fbf653061b0675 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 14:57:50 -0500 Subject: [PATCH 3/7] CI --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0e05033f..c03066fe 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -50,10 +50,10 @@ jobs: - run: opam exec -- dune build @install -p opentelemetry,opentelemetry-lwt,opentelemetry-client-ocurl,opentelemetry-cohttp-lwt,opentelemetry-client-cohttp-lwt - run: opam install trace.0.4 - - run: opam exec dune build @install -p opentelemetry + - run: opam exec -- dune build @install -p opentelemetry - run: opam install trace.0.6 - - run: opam exec dune build @install -p opentelemetry + - run: opam exec -- dune build @install -p opentelemetry - run: opam install ocaml-protoc - run: opam exec -- dune build @lint From abafac8361ee92db5cb56d6446c685141e94b043 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 14:58:55 -0500 Subject: [PATCH 4/7] use dune-generated version number in instrumentation name --- src/core/opentelemetry.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index e8568863..4127568e 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -429,7 +429,8 @@ module Globals = struct let service_instance_id = ref None let instrumentation_library = - default_instrumentation_scope ~version:"0.2" ~name:"ocaml-otel" () + default_instrumentation_scope ~version:"%%VERSION_NUM%%" ~name:"ocaml-otel" + () (** Global attributes, initially set via OTEL_RESOURCE_ATTRIBUTES and modifiable From 479f1f39c84b755eb9c4a808df44a8e8d77079a0 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 15:21:57 -0500 Subject: [PATCH 5/7] feat: record backtraces in error spans --- src/core/opentelemetry.ml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index 4127568e..b5e8b25d 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -750,7 +750,7 @@ module Trace = struct let add_attrs = Scope.add_attrs [@@deprecated "use Scope.add_attrs"] - let with_' ?(force_new_trace_id = false) ?trace_state ?service_name + let with_ ?(force_new_trace_id = false) ?trace_state ?service_name ?(attrs : (string * [< value ]) list = []) ?kind ?trace_id ?parent ?scope ?links name cb = let scope = @@ -781,7 +781,13 @@ module Trace = struct let status = match res with | Ok () -> default_status ~code:Status_code_ok () - | Error e -> default_status ~code:Status_code_error ~message:e () + | Error (e, bt) -> + (* add backtrace *) + add_event scope (fun () -> + Event.make "error" + ~attrs: + [ "backtrace", `String (Printexc.raw_backtrace_to_string bt) ]); + default_status ~code:Status_code_error ~message:e () in let span, _ = (* TODO: should the attrs passed to with_ go on the Span @@ -819,7 +825,7 @@ module Trace = struct let with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind ?trace_id ?parent ?scope ?links name (cb : Scope.t -> 'a) : 'a = let thunk, finally = - with_' ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind + with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind ?trace_id ?parent ?scope ?links name cb in @@ -828,7 +834,8 @@ module Trace = struct finally (Ok ()); rv with e -> - finally (Error (Printexc.to_string e)); + let bt = Printexc.get_raw_backtrace () in + finally (Error (Printexc.to_string e, bt)); raise e end From 7d40554025c43b008adc5e84044a1cbaad447526 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2024 15:52:00 -0500 Subject: [PATCH 6/7] fix --- src/core/opentelemetry.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index b5e8b25d..dfd1122a 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -750,7 +750,7 @@ module Trace = struct let add_attrs = Scope.add_attrs [@@deprecated "use Scope.add_attrs"] - let with_ ?(force_new_trace_id = false) ?trace_state ?service_name + let with_' ?(force_new_trace_id = false) ?trace_state ?service_name ?(attrs : (string * [< value ]) list = []) ?kind ?trace_id ?parent ?scope ?links name cb = let scope = @@ -825,7 +825,7 @@ module Trace = struct let with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind ?trace_id ?parent ?scope ?links name (cb : Scope.t -> 'a) : 'a = let thunk, finally = - with_ ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind + with_' ?force_new_trace_id ?trace_state ?service_name ?attrs ?kind ?trace_id ?parent ?scope ?links name cb in From 0d8c3ab9bf375d91ce1502f96b09e1eb5227a183 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 12 Feb 2024 09:47:17 -0500 Subject: [PATCH 7/7] fix --- src/lwt/opentelemetry_lwt.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lwt/opentelemetry_lwt.ml b/src/lwt/opentelemetry_lwt.ml index 887fe6f7..1d495f2d 100644 --- a/src/lwt/opentelemetry_lwt.ml +++ b/src/lwt/opentelemetry_lwt.ml @@ -28,7 +28,8 @@ module Trace = struct let () = finally (Ok ()) in Lwt.return rv with e -> - let () = finally (Error (Printexc.to_string e)) in + let bt = Printexc.get_raw_backtrace () in + let () = finally (Error (Printexc.to_string e, bt)) in Lwt.fail e end