refactor core

This commit is contained in:
Simon Cruanes 2025-12-04 01:06:42 -05:00
parent 6ccf554645
commit 959cf724fd
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 41 additions and 51 deletions

View file

@ -20,8 +20,6 @@ type kind = Span_kind.t =
| Span_kind_producer | Span_kind_producer
| Span_kind_consumer | Span_kind_consumer
val id : t -> Span_id.t
type key_value = Key_value.t type key_value = Key_value.t
val make : val make :
@ -45,6 +43,10 @@ val make :
list of links to other spans, each with their trace state (see list of links to other spans, each with their trace state (see
{{:https://www.w3.org/TR/trace-context/#tracestate-header} w3.org}) *) {{:https://www.w3.org/TR/trace-context/#tracestate-header} w3.org}) *)
val id : t -> Span_id.t
val trace_id : t -> Trace_id.t
val create_new : val create_new :
?kind:kind -> ?kind:kind ->
?id:Span_id.t -> ?id:Span_id.t ->

View file

@ -59,37 +59,38 @@ let (add_event [@deprecated "use Span.add_event"]) = Span.add_event
let (add_attrs [@deprecated "use Span.add_attrs"]) = Span.add_attrs let (add_attrs [@deprecated "use Span.add_attrs"]) = Span.add_attrs
let with_' ?(tracer = simple_main_exporter) ?(force_new_trace_id = false) let with_thunk_and_finally ?(tracer = simple_main_exporter)
?trace_state ?(attrs : (string * [< Value.t ]) list = []) ?kind ?trace_id ?(force_new_trace_id = false) ?trace_state
?parent ?scope ?(links = []) name cb = ?(attrs : (string * [< Value.t ]) list = []) ?kind ?trace_id ?parent ?links
let scope = name cb =
if force_new_trace_id then let parent =
None match parent with
else | Some _ -> parent
Scope.get_ambient_scope ?scope () | None -> Span.get_ambient ()
in in
let trace_id = let trace_id =
match trace_id, scope with match trace_id, parent with
| _ when force_new_trace_id -> Trace_id.create () | _ when force_new_trace_id -> Trace_id.create ()
| Some trace_id, _ -> trace_id | Some trace_id, _ -> trace_id
| None, Some scope -> scope.trace_id | None, Some p -> Span.trace_id p
| None, None -> Trace_id.create () | None, None -> Trace_id.create ()
in in
let parent =
match parent, scope with
| _ when force_new_trace_id -> None
| Some span_id, _ -> Some span_id
| None, Some scope -> Some scope.span_id
| None, None -> None
in
let start_time = Timestamp_ns.now_unix_ns () in let start_time = Timestamp_ns.now_unix_ns () in
let span_id = Span_id.create () in let span_id = Span_id.create () in
let scope = Scope.make ~trace_id ~span_id ~attrs ~links () in
let parent_id = Option.map Span.id parent in
let span : Span.t =
Span.make ?trace_state ?kind ?parent:parent_id ~trace_id ~id:span_id ~attrs
?links ~start_time ~end_time:start_time name
in
(* called once we're done, to emit a span *) (* called once we're done, to emit a span *)
let finally res = let finally res =
let status = let end_time = Timestamp_ns.now_unix_ns () in
match Scope.status scope with Proto.Trace.span_set_end_time_unix_nano span end_time;
| Some status -> Some status
(match Span.status span with
| Some _ -> ()
| None -> | None ->
(match res with (match res with
| Ok () -> | Ok () ->
@ -98,30 +99,17 @@ let with_' ?(tracer = simple_main_exporter) ?(force_new_trace_id = false)
rather than stick with the default of Unset (i.e., without error). rather than stick with the default of Unset (i.e., without error).
https://opentelemetry.io/docs/languages/go/instrumentation/#set-span-status *) https://opentelemetry.io/docs/languages/go/instrumentation/#set-span-status *)
None ()
| Error (e, bt) -> | Error (e, bt) ->
Scope.record_exception scope e bt; Span.record_exception span e bt;
Some let status =
(make_status ~code:Status_code_error ~message:(Printexc.to_string e) make_status ~code:Status_code_error ~message:(Printexc.to_string e) ()
()))
in
let span =
(* TODO: should the attrs passed to with_ go on the Span
(in Span.create) or on the ResourceSpan (in emit)?
(question also applies to Opentelemetry_lwt.Trace.with) *)
Span.make ?kind ~trace_id ?parent ~links:(Scope.links scope) ~id:span_id
?trace_state ~attrs:(Scope.attrs scope) ~events:(Scope.events scope)
~start_time
~end_time:(Timestamp_ns.now_unix_ns ())
?status name
in in
Span.set_status span status));
tracer#emit [ span ] tracer#emit [ span ]
in in
let thunk () = let thunk () = Span.with_ambient span (fun () -> cb span) in
(* set global scope in this thread *)
Scope.with_ambient_scope scope @@ fun () -> cb scope
in
thunk, finally thunk, finally
(** Sync span guard. (** Sync span guard.
@ -141,10 +129,10 @@ let with_' ?(tracer = simple_main_exporter) ?(force_new_trace_id = false)
[~scope] argument, nor [~trace_id], but will instead always create fresh [~scope] argument, nor [~trace_id], but will instead always create fresh
identifiers for this span *) identifiers for this span *)
let with_ ?tracer ?force_new_trace_id ?trace_state ?attrs ?kind ?trace_id let with_ ?tracer ?force_new_trace_id ?trace_state ?attrs ?kind ?trace_id
?parent ?scope ?links name (cb : Scope.t -> 'a) : 'a = ?parent ?links name (cb : Span.t -> 'a) : 'a =
let thunk, finally = let thunk, finally =
with_' ?tracer ?force_new_trace_id ?trace_state ?attrs ?kind ?trace_id with_thunk_and_finally ?tracer ?force_new_trace_id ?trace_state ?attrs ?kind
?parent ?scope ?links name cb ?trace_id ?parent ?links name cb
in in
try try