mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-09 12:23:32 -04:00
feat: adapt to trace 0.10
This commit is contained in:
parent
45fae39c29
commit
9b5f3cd0c3
5 changed files with 83 additions and 31 deletions
|
|
@ -48,7 +48,7 @@
|
||||||
(depopts trace lwt eio)
|
(depopts trace lwt eio)
|
||||||
(conflicts
|
(conflicts
|
||||||
(trace
|
(trace
|
||||||
(< 0.9)))
|
(< 0.10)))
|
||||||
(tags
|
(tags
|
||||||
(instrumentation tracing opentelemetry datadog jaeger)))
|
(instrumentation tracing opentelemetry datadog jaeger)))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ depends: [
|
||||||
]
|
]
|
||||||
depopts: ["trace" "lwt" "eio"]
|
depopts: ["trace" "lwt" "eio"]
|
||||||
conflicts: [
|
conflicts: [
|
||||||
"trace" {< "0.9"}
|
"trace" {< "0.10"}
|
||||||
]
|
]
|
||||||
build: [
|
build: [
|
||||||
["dune" "subst"] {dev}
|
["dune" "subst"] {dev}
|
||||||
|
|
|
||||||
|
|
@ -369,7 +369,7 @@ module Span_id : sig
|
||||||
end = struct
|
end = struct
|
||||||
type t = bytes
|
type t = bytes
|
||||||
|
|
||||||
let to_bytes self = self
|
let[@inline] to_bytes self = self
|
||||||
|
|
||||||
let dummy : t = Bytes.make 8 '\x00'
|
let dummy : t = Bytes.make 8 '\x00'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,49 @@ open struct
|
||||||
let spf = Printf.sprintf
|
let spf = Printf.sprintf
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Conv = struct
|
||||||
|
let[@inline] trace_id_of_otel (id : Otel.Trace_id.t) : Otrace.trace_id =
|
||||||
|
if id == Otel.Trace_id.dummy then
|
||||||
|
Otrace.Collector.dummy_trace_id
|
||||||
|
else
|
||||||
|
Bytes.unsafe_to_string (Otel.Trace_id.to_bytes id)
|
||||||
|
|
||||||
|
let[@inline] trace_id_to_otel (id : Otrace.trace_id) : Otel.Trace_id.t =
|
||||||
|
if id == Otrace.Collector.dummy_trace_id then
|
||||||
|
Otel.Trace_id.dummy
|
||||||
|
else
|
||||||
|
Otel.Trace_id.of_bytes @@ Bytes.unsafe_of_string id
|
||||||
|
|
||||||
|
let[@inline] span_id_of_otel (id : Otel.Span_id.t) : Otrace.span =
|
||||||
|
if id == Otel.Span_id.dummy then
|
||||||
|
Otrace.Collector.dummy_span
|
||||||
|
else
|
||||||
|
Bytes.get_int64_le (Otel.Span_id.to_bytes id) 0
|
||||||
|
|
||||||
|
let[@inline] span_id_to_otel (id : Otrace.span) : Otel.Span_id.t =
|
||||||
|
if id == Otrace.Collector.dummy_span then
|
||||||
|
Otel.Span_id.dummy
|
||||||
|
else (
|
||||||
|
let b = Bytes.create 8 in
|
||||||
|
Bytes.set_int64_le b 0 id;
|
||||||
|
Otel.Span_id.of_bytes b
|
||||||
|
)
|
||||||
|
|
||||||
|
let[@inline] ctx_to_otel (self : Otrace.explicit_span_ctx) : Otel.Span_ctx.t =
|
||||||
|
Otel.Span_ctx.make
|
||||||
|
~trace_id:(trace_id_to_otel self.trace_id)
|
||||||
|
~parent_id:(span_id_to_otel self.span)
|
||||||
|
()
|
||||||
|
|
||||||
|
let[@inline] ctx_of_otel (ctx : Otel.Span_ctx.t) : Otrace.explicit_span_ctx =
|
||||||
|
{
|
||||||
|
trace_id = trace_id_of_otel (Otel.Span_ctx.trace_id ctx);
|
||||||
|
span = span_id_of_otel (Otel.Span_ctx.parent_id ctx);
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
open Conv
|
||||||
|
|
||||||
module Well_known = struct
|
module Well_known = struct
|
||||||
let spankind_key = "otrace.spankind"
|
let spankind_key = "otrace.spankind"
|
||||||
|
|
||||||
|
|
@ -42,9 +85,9 @@ module Well_known = struct
|
||||||
in
|
in
|
||||||
!kind, data
|
!kind, data
|
||||||
|
|
||||||
(** Key to store an error [Otel.Span.status] with the message.
|
(** Key to store an error [Otel.Span.status] with the message. Set
|
||||||
Set ["otrace.error" = "mymsg"] in a span data to set the span's status
|
["otrace.error" = "mymsg"] in a span data to set the span's status to
|
||||||
to [{message="mymsg"; code=Error}]. *)
|
[{message="mymsg"; code=Error}]. *)
|
||||||
let status_error_key = "otrace.error"
|
let status_error_key = "otrace.error"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -101,12 +144,7 @@ module Internal = struct
|
||||||
assert (Bytes.length bs = 8);
|
assert (Bytes.length bs = 8);
|
||||||
Bytes.get_int64_le bs 0
|
Bytes.get_int64_le bs 0
|
||||||
|
|
||||||
let otel_of_otrace (id : int64) : Otel.Span_id.t =
|
let enter_span' ?(explicit_parent : Otrace.explicit_span_ctx option) ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
|
||||||
let bs = Bytes.create 8 in
|
|
||||||
Bytes.set_int64_le bs 0 id;
|
|
||||||
Otel.Span_id.of_bytes bs
|
|
||||||
|
|
||||||
let enter_span' ?explicit_parent ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
|
|
||||||
=
|
=
|
||||||
let open Otel in
|
let open Otel in
|
||||||
let otel_id = Span_id.create () in
|
let otel_id = Span_id.create () in
|
||||||
|
|
@ -121,7 +159,7 @@ module Internal = struct
|
||||||
let parent =
|
let parent =
|
||||||
match explicit_parent, parent_scope with
|
match explicit_parent, parent_scope with
|
||||||
| Some p, _ ->
|
| Some p, _ ->
|
||||||
Some (Otel.Span_ctx.make ~trace_id ~parent_id:(otel_of_otrace p) ())
|
Some (Otel.Span_ctx.make ~trace_id ~parent_id:(span_id_to_otel p.span) ())
|
||||||
| None, Some parent -> Some (Otel.Scope.to_span_ctx parent)
|
| None, Some parent -> Some (Otel.Scope.to_span_ctx parent)
|
||||||
| None, None -> None
|
| None, None -> None
|
||||||
in
|
in
|
||||||
|
|
@ -237,13 +275,13 @@ module Internal = struct
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| Some otel_span -> Otel.Trace.emit [ otel_span ]
|
| Some otel_span -> Otel.Trace.emit [ otel_span ]
|
||||||
|
|
||||||
let enter_manual_span ~(parent : Otrace.explicit_span option) ~flavor:_
|
let enter_manual_span ~(parent : Otrace.explicit_span_ctx option) ~flavor:_
|
||||||
~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name : Otrace.explicit_span =
|
~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name : Otrace.explicit_span =
|
||||||
let otrace_id, sb =
|
let otrace_id, sb =
|
||||||
match parent with
|
match parent with
|
||||||
| None -> enter_span' ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
|
| None -> enter_span' ~__FUNCTION__ ~__FILE__ ~__LINE__ ~data name
|
||||||
| Some { span; _ } ->
|
| Some parent ->
|
||||||
enter_span' ~explicit_parent:span ~__FUNCTION__ ~__FILE__ ~__LINE__
|
enter_span' ~explicit_parent:parent ~__FUNCTION__ ~__FILE__ ~__LINE__
|
||||||
~data name
|
~data name
|
||||||
in
|
in
|
||||||
|
|
||||||
|
|
@ -253,6 +291,7 @@ module Internal = struct
|
||||||
Otrace.
|
Otrace.
|
||||||
{
|
{
|
||||||
span = otrace_id;
|
span = otrace_id;
|
||||||
|
trace_id = trace_id_of_otel sb.scope.trace_id;
|
||||||
meta = Meta_map.(empty |> add k_explicit_scope sb.scope);
|
meta = Meta_map.(empty |> add k_explicit_scope sb.scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -283,7 +322,7 @@ module Internal = struct
|
||||||
|
|
||||||
let span_id =
|
let span_id =
|
||||||
match span with
|
match span with
|
||||||
| Some id -> Some (otel_of_otrace id)
|
| Some id -> Some (span_id_to_otel id)
|
||||||
| None -> Option.map (fun sc -> sc.Otel.Scope.span_id) old_scope
|
| None -> Option.map (fun sc -> sc.Otel.Scope.span_id) old_scope
|
||||||
in
|
in
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,20 @@ module Otel := Opentelemetry
|
||||||
module Otrace := Trace_core
|
module Otrace := Trace_core
|
||||||
module TLS := Thread_local_storage
|
module TLS := Thread_local_storage
|
||||||
|
|
||||||
|
module Conv : sig
|
||||||
|
val trace_id_of_otel : Otel.Trace_id.t -> string
|
||||||
|
|
||||||
|
val trace_id_to_otel : string -> Otel.Trace_id.t
|
||||||
|
|
||||||
|
val span_id_of_otel : Otel.Span_id.t -> int64
|
||||||
|
|
||||||
|
val span_id_to_otel : int64 -> Otel.Span_id.t
|
||||||
|
|
||||||
|
val ctx_to_otel : Otrace.explicit_span_ctx -> Otel.Span_ctx.t
|
||||||
|
|
||||||
|
val ctx_of_otel : Otel.Span_ctx.t -> Otrace.explicit_span_ctx
|
||||||
|
end
|
||||||
|
|
||||||
(** [opentelemetry.trace] implements a {!Trace_core.Collector} for
|
(** [opentelemetry.trace] implements a {!Trace_core.Collector} for
|
||||||
{{:https://v3.ocaml.org/p/trace} ocaml-trace}.
|
{{:https://v3.ocaml.org/p/trace} ocaml-trace}.
|
||||||
|
|
||||||
|
|
@ -28,13 +42,13 @@ module TLS := Thread_local_storage
|
||||||
the {!Opentelemetry.Span.kind} of the emitted span. (See
|
the {!Opentelemetry.Span.kind} of the emitted span. (See
|
||||||
{!Internal.spankind_of_string} for the list of supported values.)
|
{!Internal.spankind_of_string} for the list of supported values.)
|
||||||
|
|
||||||
{[ocaml
|
{[
|
||||||
|
ocaml
|
||||||
let describe () = [ Opentelemetry_trace.(spankind_key, client) ] in
|
let describe () = [ Opentelemetry_trace.(spankind_key, client) ] in
|
||||||
Trace_core.with_span ~__FILE__ ~__LINE__ ~data:describe "my-span"
|
Trace_core.with_span ~__FILE__ ~__LINE__ ~data:describe "my-span"
|
||||||
@@ fun _ ->
|
@@ fun _ ->
|
||||||
(* ... *)
|
(* ... *)
|
||||||
]}
|
]} *)
|
||||||
*)
|
|
||||||
|
|
||||||
val on_internal_error : (string -> unit) ref
|
val on_internal_error : (string -> unit) ref
|
||||||
(** Callback to print errors in the library itself (ie bugs) *)
|
(** Callback to print errors in the library itself (ie bugs) *)
|
||||||
|
|
@ -103,10 +117,11 @@ module Internal : sig
|
||||||
{!Opentelemetry.Trace.with_}, and requires configuration of
|
{!Opentelemetry.Trace.with_}, and requires configuration of
|
||||||
{!Ambient_context}.
|
{!Ambient_context}.
|
||||||
|
|
||||||
@see <https://github.com/ELLIOTTCABLE/ocaml-ambient-context> ambient-context docs *)
|
@see <https://github.com/ELLIOTTCABLE/ocaml-ambient-context>
|
||||||
|
ambient-context docs *)
|
||||||
|
|
||||||
val enter_manual_span :
|
val enter_manual_span :
|
||||||
parent:Otrace.explicit_span option ->
|
parent:Otrace.explicit_span_ctx option ->
|
||||||
flavor:'a ->
|
flavor:'a ->
|
||||||
__FUNCTION__:string option ->
|
__FUNCTION__:string option ->
|
||||||
__FILE__:string ->
|
__FILE__:string ->
|
||||||
|
|
@ -135,8 +150,8 @@ module Internal : sig
|
||||||
Generally, the best practice is to only use these [manual] functions at
|
Generally, the best practice is to only use these [manual] functions at
|
||||||
the 'leaves' of your callstack: that is, don't invoke user callbacks
|
the 'leaves' of your callstack: that is, don't invoke user callbacks
|
||||||
from within them; or if you do, make sure to pass the [explicit_span]
|
from within them; or if you do, make sure to pass the [explicit_span]
|
||||||
you recieve from this function onwards to the user callback, so they can create further
|
you recieve from this function onwards to the user callback, so they can
|
||||||
child-spans. *)
|
create further child-spans. *)
|
||||||
|
|
||||||
val exit_manual_span : Otrace.explicit_span -> unit
|
val exit_manual_span : Otrace.explicit_span -> unit
|
||||||
(** Implements {!Trace_core.Collector.S.exit_manual_span}, with the
|
(** Implements {!Trace_core.Collector.S.exit_manual_span}, with the
|
||||||
|
|
@ -197,10 +212,8 @@ module Internal : sig
|
||||||
|
|
||||||
val otrace_of_otel : Otel.Span_id.t -> Otrace.span
|
val otrace_of_otel : Otel.Span_id.t -> Otrace.span
|
||||||
|
|
||||||
val otel_of_otrace : Otrace.span -> Otel.Span_id.t
|
|
||||||
|
|
||||||
val enter_span' :
|
val enter_span' :
|
||||||
?explicit_parent:Otrace.span ->
|
?explicit_parent:Otrace.explicit_span_ctx ->
|
||||||
__FUNCTION__:string option ->
|
__FUNCTION__:string option ->
|
||||||
__FILE__:string ->
|
__FILE__:string ->
|
||||||
__LINE__:int ->
|
__LINE__:int ->
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue