opentelemetry.trace: update to trace 0.12

This commit is contained in:
Simon Cruanes 2026-02-26 12:26:55 -05:00
parent 4e63b2a3a2
commit cfcae18452
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 53 additions and 29 deletions

View file

@ -1,6 +1,6 @@
module OTEL = Opentelemetry
module Otrace = Trace_core (* ocaml-trace *)
module Ambient_context = Opentelemetry_ambient_context
module Trace = Trace_core (* ocaml-trace *)
module Ambient_context = Ambient_context
let ( let@ ) = ( @@ )

View file

@ -5,7 +5,7 @@
(optional) ; trace
(flags :standard -open Opentelemetry_util -open Opentelemetry_atomic)
(libraries
(re_export opentelemetry.ambient-context)
(re_export ambient-context)
(re_export opentelemetry.util)
opentelemetry.atomic
(re_export opentelemetry)

View file

@ -1,19 +1,19 @@
open Common_
module Extensions = struct
type Otrace.span += Span_otel of OTEL.Span.t
type Trace.span += Span_otel of OTEL.Span.t
type Otrace.extension_event +=
| Ev_link_span of Otrace.span * OTEL.Span_ctx.t
type Trace.extension_event +=
| Ev_link_span of Trace.span * OTEL.Span_ctx.t
| Ev_record_exn of {
sp: Otrace.span;
sp: Trace.span;
exn: exn;
bt: Printexc.raw_backtrace;
}
| Ev_set_span_kind of Otrace.span * OTEL.Span_kind.t
| Ev_set_span_status of Otrace.span * OTEL.Span_status.t
| Ev_set_span_kind of Trace.span * OTEL.Span_kind.t
| Ev_set_span_status of Trace.span * OTEL.Span_status.t
type Otrace.metric +=
type Trace.metric +=
| Metric_hist of OTEL.Metrics.histogram_data_point
| Metric_sum_int of int
| Metric_sum_float of float
@ -39,12 +39,12 @@ open struct
OTEL.Span_ctx.k_ambient
let enter_span (self : state) ~__FUNCTION__ ~__FILE__ ~__LINE__ ~level:_
~params:_ ~(data : (_ * Otrace.user_data) list) ~parent name : Otrace.span
~params:_ ~(data : (_ * Trace.user_data) list) ~parent name : Trace.span
=
let start_time = OTEL.Clock.now self.clock in
let trace_id, parent_id =
match parent with
| Otrace.P_some (Span_otel sp) ->
| Trace.P_some (Span_otel sp) ->
OTEL.Span.trace_id sp, Some (OTEL.Span.id sp)
| _ ->
(match Ambient_context.get k_span_ctx with
@ -103,7 +103,7 @@ open struct
self.exporter.OTEL.Exporter.export (OTEL.Any_signal_l.Spans [ span ])
| _ -> ()
let add_data_to_span _self span (data : (_ * Otrace.user_data) list) =
let add_data_to_span _self span (data : (_ * Trace.user_data) list) =
match span with
| Span_otel sp -> OTEL.Span.add_attrs sp data
| _ -> ()
@ -177,16 +177,35 @@ open struct
let shutdown self = OTEL.Exporter.shutdown self.exporter
let callbacks : state Otrace.Collector.Callbacks.t =
Otrace.Collector.Callbacks.make ~enter_span ~exit_span ~add_data_to_span
let callbacks : state Trace.Collector.Callbacks.t =
Trace.Collector.Callbacks.make ~enter_span ~exit_span ~add_data_to_span
~message ~metric ~extension ~shutdown ()
end
module Ambient_span_provider_ = struct
let get_current_span () =
match OTEL.Ambient_span.get () with
| None -> None
| Some sp -> Some (Span_otel sp)
let with_current_span_set_to () span f =
match span with
| Span_otel sp -> OTEL.Ambient_span.with_ambient sp (fun () -> f span)
| _ -> f span
let callbacks : unit Trace.Ambient_span_provider.Callbacks.t =
{ get_current_span; with_current_span_set_to }
let provider = Trace.Ambient_span_provider.ASP_some ((), callbacks)
end
let ambient_span_provider = Ambient_span_provider_.provider
let collector_of_exporter (exporter : OTEL.Exporter.t) : Trace_core.collector =
let st = create_state ~exporter () in
Trace_core.Collector.C_some (st, callbacks)
let with_ambient_span (sp : Otrace.span) f =
let with_ambient_span (sp : Trace.span) f =
match sp with
| Span_otel sp ->
Ambient_context.with_key_bound_to k_span_ctx (OTEL.Span.to_span_ctx sp) f
@ -195,30 +214,30 @@ let with_ambient_span (sp : Otrace.span) f =
let with_ambient_span_ctx (sp : OTEL.Span_ctx.t) f =
Ambient_context.with_key_bound_to k_span_ctx sp f
let link_span_to_otel_ctx (sp1 : Otrace.span) (sp2 : OTEL.Span_ctx.t) : unit =
if Otrace.enabled () then Otrace.extension_event @@ Ev_link_span (sp1, sp2)
let link_span_to_otel_ctx (sp1 : Trace.span) (sp2 : OTEL.Span_ctx.t) : unit =
if Trace.enabled () then Trace.extension_event @@ Ev_link_span (sp1, sp2)
let link_spans (sp1 : Otrace.span) (sp2 : Otrace.span) : unit =
if Otrace.enabled () then (
let link_spans (sp1 : Trace.span) (sp2 : Trace.span) : unit =
if Trace.enabled () then (
match sp2 with
| Span_otel sp2 ->
Otrace.extension_event @@ Ev_link_span (sp1, OTEL.Span.to_span_ctx sp2)
Trace.extension_event @@ Ev_link_span (sp1, OTEL.Span.to_span_ctx sp2)
| _ -> ()
)
let[@inline] set_span_kind sp k : unit =
if Otrace.enabled () then Otrace.extension_event @@ Ev_set_span_kind (sp, k)
if Trace.enabled () then Trace.extension_event @@ Ev_set_span_kind (sp, k)
let[@inline] set_span_status sp status : unit =
if Otrace.enabled () then
Otrace.extension_event @@ Ev_set_span_status (sp, status)
if Trace.enabled () then
Trace.extension_event @@ Ev_set_span_status (sp, status)
let record_exception sp exn bt : unit =
if Otrace.enabled () then
Otrace.extension_event @@ Ev_record_exn { sp; exn; bt }
if Trace.enabled () then
Trace.extension_event @@ Ev_record_exn { sp; exn; bt }
(** Collector that forwards to the {b currently installed} OTEL exporter. *)
let collector_main_otel_exporter () : Otrace.collector =
let collector_main_otel_exporter () : Trace.collector =
(* Create a dynamic exporter that forwards to the currently installed main
exporter at call time. *)
let dynamic_exp : OTEL.Exporter.t =
@ -240,12 +259,14 @@ let (collector
=
collector_main_otel_exporter
let setup () = Otrace.setup_collector @@ collector_main_otel_exporter ()
let setup () =
Trace.set_ambient_context_provider Ambient_span_provider_.provider;
Trace.setup_collector @@ collector_main_otel_exporter ()
let setup_with_otel_exporter exp : unit =
let coll = collector_of_exporter exp in
OTEL.Sdk.set exp;
Otrace.setup_collector coll
Trace.setup_collector coll
let setup_with_otel_backend = setup_with_otel_exporter

View file

@ -59,6 +59,9 @@ val collector : unit -> Trace_core.collector
(** Make a Trace collector that uses the main OTEL backend to send spans and
logs *)
val ambient_span_provider : Trace_core.Ambient_span_provider.t
(** Uses {!Ambient_context} to provide contextual spans in {!Trace_core}.*)
val link_spans : Otrace.span -> Otrace.span -> unit
(** [link_spans sp1 sp2] modifies [sp1] by adding a span link to [sp2].
@since 0.11 *)