mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-08 20:07:55 -04:00
make otel-trace a bit more lightweight
This commit is contained in:
parent
03d9a6f9a2
commit
14b9f440d1
2 changed files with 36 additions and 47 deletions
|
|
@ -2,6 +2,10 @@ module Otel = Opentelemetry
|
||||||
module Otrace = Trace_core (* ocaml-trace *)
|
module Otrace = Trace_core (* ocaml-trace *)
|
||||||
module TLS = Ambient_context_tls.TLS
|
module TLS = Ambient_context_tls.TLS
|
||||||
|
|
||||||
|
open struct
|
||||||
|
let spf = Printf.sprintf
|
||||||
|
end
|
||||||
|
|
||||||
module Well_known = struct
|
module Well_known = struct
|
||||||
let spankind_key = "otrace.spankind"
|
let spankind_key = "otrace.spankind"
|
||||||
|
|
||||||
|
|
@ -41,19 +45,18 @@ end
|
||||||
|
|
||||||
open Well_known
|
open Well_known
|
||||||
|
|
||||||
|
let on_internal_error =
|
||||||
|
ref (fun msg -> Printf.eprintf "error in Opentelemetry_trace: %s\n%!" msg)
|
||||||
|
|
||||||
module Internal = struct
|
module Internal = struct
|
||||||
type span_begin = {
|
type span_begin = {
|
||||||
id: Otel.Span_id.t;
|
|
||||||
start_time: int64;
|
start_time: int64;
|
||||||
name: string;
|
name: string;
|
||||||
data: (string * Otrace.user_data) list;
|
|
||||||
__FILE__: string;
|
__FILE__: string;
|
||||||
__LINE__: int;
|
__LINE__: int;
|
||||||
__FUNCTION__: string option;
|
__FUNCTION__: string option;
|
||||||
trace_id: Otel.Trace_id.t;
|
|
||||||
scope: Otel.Scope.t;
|
scope: Otel.Scope.t;
|
||||||
parent_id: Otel.Span_id.t option;
|
parent: Otel.Span_ctx.t option;
|
||||||
parent_scope: Otel.Scope.t option;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module Active_span_tbl = Hashtbl.Make (struct
|
module Active_span_tbl = Hashtbl.Make (struct
|
||||||
|
|
@ -62,6 +65,10 @@ module Internal = struct
|
||||||
let hash : t -> int = Hashtbl.hash
|
let hash : t -> int = Hashtbl.hash
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
(** key to access a OTEL scope from an explicit span *)
|
||||||
|
let k_explicit_scope : Otel.Scope.t Otrace.Meta_map.Key.t =
|
||||||
|
Otrace.Meta_map.Key.create ()
|
||||||
|
|
||||||
(** Per-thread set of active spans. *)
|
(** Per-thread set of active spans. *)
|
||||||
module Active_spans = struct
|
module Active_spans = struct
|
||||||
type t = { tbl: span_begin Active_span_tbl.t } [@@unboxed]
|
type t = { tbl: span_begin Active_span_tbl.t } [@@unboxed]
|
||||||
|
|
@ -96,32 +103,28 @@ module Internal = struct
|
||||||
| Some sc -> sc.trace_id
|
| Some sc -> sc.trace_id
|
||||||
| None -> Trace_id.create ()
|
| None -> Trace_id.create ()
|
||||||
in
|
in
|
||||||
let parent_id =
|
let parent =
|
||||||
match explicit_parent, parent_scope with
|
match explicit_parent, parent_scope with
|
||||||
| Some p, _ -> Some (otel_of_otrace p)
|
| Some p, _ ->
|
||||||
| None, Some parent -> Some parent.span_id
|
Some (Otel.Span_ctx.make ~trace_id ~parent_id:(otel_of_otrace p) ())
|
||||||
|
| None, Some parent -> Some (Otel.Scope.to_span_ctx parent)
|
||||||
| None, None -> None
|
| None, None -> None
|
||||||
in
|
in
|
||||||
|
|
||||||
let new_scope =
|
let new_scope =
|
||||||
{ Scope.span_id = otel_id; trace_id; events = []; attrs = [] }
|
{ Scope.span_id = otel_id; trace_id; events = []; attrs = data }
|
||||||
in
|
in
|
||||||
|
|
||||||
let start_time = Timestamp_ns.now_unix_ns () in
|
let start_time = Timestamp_ns.now_unix_ns () in
|
||||||
|
|
||||||
let sb =
|
let sb =
|
||||||
{
|
{
|
||||||
id = otel_id;
|
|
||||||
start_time;
|
start_time;
|
||||||
name;
|
name;
|
||||||
data;
|
|
||||||
__FILE__;
|
__FILE__;
|
||||||
__LINE__;
|
__LINE__;
|
||||||
__FUNCTION__;
|
__FUNCTION__;
|
||||||
trace_id;
|
|
||||||
scope = new_scope;
|
scope = new_scope;
|
||||||
parent_id;
|
parent;
|
||||||
parent_scope;
|
|
||||||
}
|
}
|
||||||
in
|
in
|
||||||
|
|
||||||
|
|
@ -131,22 +134,10 @@ module Internal = struct
|
||||||
otrace_id, sb
|
otrace_id, sb
|
||||||
|
|
||||||
let exit_span_
|
let exit_span_
|
||||||
{
|
{ start_time; name; __FILE__; __LINE__; __FUNCTION__; scope; parent } =
|
||||||
id = otel_id;
|
|
||||||
start_time;
|
|
||||||
name;
|
|
||||||
data;
|
|
||||||
__FILE__;
|
|
||||||
__LINE__;
|
|
||||||
__FUNCTION__;
|
|
||||||
trace_id;
|
|
||||||
scope = _;
|
|
||||||
parent_id;
|
|
||||||
parent_scope = _;
|
|
||||||
} =
|
|
||||||
let open Otel in
|
let open Otel in
|
||||||
let end_time = Timestamp_ns.now_unix_ns () in
|
let end_time = Timestamp_ns.now_unix_ns () in
|
||||||
let kind, attrs = otel_attrs_of_otrace_data data in
|
let kind, attrs = otel_attrs_of_otrace_data scope.attrs in
|
||||||
|
|
||||||
let attrs =
|
let attrs =
|
||||||
match __FUNCTION__ with
|
match __FUNCTION__ with
|
||||||
|
|
@ -168,8 +159,10 @@ module Internal = struct
|
||||||
]
|
]
|
||||||
@ attrs
|
@ attrs
|
||||||
in
|
in
|
||||||
Span.create ~kind ~trace_id ?parent:parent_id ~id:otel_id ~start_time
|
|
||||||
~end_time ~attrs name
|
let parent_id = Option.map Otel.Span_ctx.parent_id parent in
|
||||||
|
Span.create ~kind ~trace_id:scope.trace_id ?parent:parent_id
|
||||||
|
~id:scope.span_id ~start_time ~end_time ~attrs name
|
||||||
|> fst
|
|> fst
|
||||||
|
|
||||||
let exit_span' otrace_id otel_span_begin =
|
let exit_span' otrace_id otel_span_begin =
|
||||||
|
|
@ -231,9 +224,7 @@ module Internal = struct
|
||||||
let exit_manual_span Otrace.{ span = otrace_id; _ } =
|
let exit_manual_span Otrace.{ span = otrace_id; _ } =
|
||||||
let active_spans = Active_spans.get () in
|
let active_spans = Active_spans.get () in
|
||||||
match Active_span_tbl.find_opt active_spans.tbl otrace_id with
|
match Active_span_tbl.find_opt active_spans.tbl otrace_id with
|
||||||
| None ->
|
| None -> !on_internal_error (spf "no active span with ID %Ld" otrace_id)
|
||||||
(* FIXME: some kind of error/debug logging *)
|
|
||||||
()
|
|
||||||
| Some sb ->
|
| Some sb ->
|
||||||
let otel_span = exit_span' otrace_id sb in
|
let otel_span = exit_span' otrace_id sb in
|
||||||
Otel.Trace.emit [ otel_span ]
|
Otel.Trace.emit [ otel_span ]
|
||||||
|
|
@ -241,15 +232,14 @@ module Internal = struct
|
||||||
let add_data_to_span otrace_id data =
|
let add_data_to_span otrace_id data =
|
||||||
let active_spans = Active_spans.get () in
|
let active_spans = Active_spans.get () in
|
||||||
match Active_span_tbl.find_opt active_spans.tbl otrace_id with
|
match Active_span_tbl.find_opt active_spans.tbl otrace_id with
|
||||||
| None ->
|
| None -> !on_internal_error (spf "no active span with ID %Ld" otrace_id)
|
||||||
(* FIXME: some kind of error/debug logging *)
|
| Some sb -> sb.scope.attrs <- List.rev_append data sb.scope.attrs
|
||||||
()
|
|
||||||
| Some sb ->
|
|
||||||
Active_span_tbl.replace active_spans.tbl otrace_id
|
|
||||||
{ sb with data = sb.data @ data }
|
|
||||||
|
|
||||||
let add_data_to_manual_span Otrace.{ span = otrace_id; _ } data =
|
let add_data_to_manual_span (span : Otrace.explicit_span) data : unit =
|
||||||
add_data_to_span otrace_id data
|
match Otrace.Meta_map.find_exn k_explicit_scope span.meta with
|
||||||
|
| exception _ ->
|
||||||
|
!on_internal_error (spf "manual span does not a contain an OTEL scope")
|
||||||
|
| scope -> scope.attrs <- List.rev_append data scope.attrs
|
||||||
|
|
||||||
let message ?span ~data:_ msg : unit =
|
let message ?span ~data:_ msg : unit =
|
||||||
(* gather information from context *)
|
(* gather information from context *)
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ module TLS := Ambient_context_tls.TLS
|
||||||
]}
|
]}
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
val on_internal_error : (string -> unit) ref
|
||||||
|
(** Callback to print errors in the library itself (ie bugs) *)
|
||||||
|
|
||||||
val setup : unit -> unit
|
val setup : unit -> unit
|
||||||
(** Install the OTEL backend as a Trace collector *)
|
(** Install the OTEL backend as a Trace collector *)
|
||||||
|
|
||||||
|
|
@ -156,17 +159,13 @@ module Internal : sig
|
||||||
end
|
end
|
||||||
|
|
||||||
type span_begin = {
|
type span_begin = {
|
||||||
id: Otel.Span_id.t;
|
|
||||||
start_time: int64;
|
start_time: int64;
|
||||||
name: string;
|
name: string;
|
||||||
data: (string * Otrace.user_data) list;
|
|
||||||
__FILE__: string;
|
__FILE__: string;
|
||||||
__LINE__: int;
|
__LINE__: int;
|
||||||
__FUNCTION__: string option;
|
__FUNCTION__: string option;
|
||||||
trace_id: Otel.Trace_id.t;
|
|
||||||
scope: Otel.Scope.t;
|
scope: Otel.Scope.t;
|
||||||
parent_id: Otel.Span_id.t option;
|
parent: Otel.Span_ctx.t option;
|
||||||
parent_scope: Otel.Scope.t option;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module Active_span_tbl : Hashtbl.S with type key = Otrace.span
|
module Active_span_tbl : Hashtbl.S with type key = Otrace.span
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue