split core library into opentelemetry.core and opentelemetry

This commit is contained in:
Simon Cruanes 2025-12-04 09:31:06 -05:00
parent 1f275c21d0
commit b8228dfe25
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
22 changed files with 90 additions and 56 deletions

View file

@ -1,4 +1,4 @@
module Otel = Opentelemetry
open Opentelemetry_util
type 'a t = {
mutable size: int;
@ -47,7 +47,7 @@ let ready_to_pop ~force ~now self =
let pop_if_ready ?(force = false) ~now (self : _ t) : _ list option =
let rev_batch_opt =
Otel.Util_mutex.protect self.mutex @@ fun () ->
Util_mutex.protect self.mutex @@ fun () ->
if ready_to_pop ~force ~now self then (
assert (self.q <> []);
let batch = self.q in
@ -72,7 +72,7 @@ let rec push_unprotected (self : _ t) ~(elems : _ list) : unit =
push_unprotected self ~elems:xs
let push (self : _ t) elems : [ `Dropped | `Ok ] =
Otel.Util_mutex.protect self.mutex @@ fun () ->
Util_mutex.protect self.mutex @@ fun () ->
if self.size >= self.high_watermark then
(* drop this to prevent queue from growing too fast *)
`Dropped

View file

@ -1,21 +1,21 @@
module OT = Opentelemetry
open Common_
let enabled = Atomic.make false
let[@inline] add_event (scope : OT.Span.t) ev = OT.Span.add_event scope ev
let[@inline] add_event (scope : OTEL.Span.t) ev = OTEL.Span.add_event scope ev
let dummy_trace_id_ = OT.Trace_id.dummy
let dummy_trace_id_ = OTEL.Trace_id.dummy
let dummy_span_id = OT.Span_id.dummy
let dummy_span_id = OTEL.Span_id.dummy
(* FIXME: get an explicit tracer instead *)
let with_ ?kind ?attrs name f =
if Atomic.get enabled then
OT.Tracer.with_ ?kind ?attrs name f
OTEL.Tracer.with_ ?kind ?attrs name f
else (
(* A new scope is needed here because it might be modified *)
let span : OT.Span.t =
OT.Span.make ~trace_id:dummy_trace_id_ ~id:dummy_span_id ~start_time:0L
let span : OTEL.Span.t =
OTEL.Span.make ~trace_id:dummy_trace_id_ ~id:dummy_span_id ~start_time:0L
~end_time:0L name
in
f span

View file

@ -1,12 +1,14 @@
(** Mini tracing module (disabled if [config.self_trace=false]) *)
val add_event : Opentelemetry.Span.t -> Opentelemetry.Event.t -> unit
open Common_
val add_event : OTEL.Span.t -> OTEL.Event.t -> unit
val with_ :
?kind:Opentelemetry.Span_kind.t ->
?attrs:(string * Opentelemetry.value) list ->
?kind:OTEL.Span_kind.t ->
?attrs:(string * OTEL.value) list ->
string ->
(Opentelemetry.Span.t -> 'a) ->
(OTEL.Span.t -> 'a) ->
'a
val set_enabled : bool -> unit

View file

@ -1,10 +1,11 @@
(** A simple exporter that prints on stdout *)
open Common_
open OTEL
open Opentelemetry_util
open struct
let pp_span out (sp : Span.t) =
let pp_span out (sp : OTEL.Span.t) =
let open OTEL in
Format.fprintf out
"@[<2>SPAN@ trace_id: %a@ span_id: %a@ name: %S@ start: %a@ end: %a@]@."
Trace_id.pp

View file

@ -1,4 +1,4 @@
module UM = Opentelemetry.Util_mutex
module UM = Opentelemetry_util.Util_mutex
type 'a t = {
mutex: Mutex.t;

View file

@ -2,4 +2,3 @@ let spf = Printf.sprintf
module Proto = Opentelemetry_proto
module Atomic = Opentelemetry_atomic.Atomic
module Ambient_context = Opentelemetry_ambient_context

View file

@ -1,15 +1,14 @@
(library
(name opentelemetry)
(synopsis "API for opentelemetry instrumentation")
(name opentelemetry_core)
(public_name opentelemetry.core)
(synopsis "Core types and definitions for opentelemetry")
(flags :standard -warn-error -a+8 -open Opentelemetry_util)
(libraries
opentelemetry.proto
opentelemetry.util
opentelemetry.ambient-context
ptime
ptime.clock.os
pbrt
threads
opentelemetry.atomic
hmap)
(public_name opentelemetry))
hmap))

View file

@ -1,7 +1,7 @@
open Common_
open struct
let[@inline] bytes_per_word = Sys.word_size / 8
let bytes_per_word = Sys.word_size / 8
let[@inline] word_to_bytes n = n * bytes_per_word

View file

@ -40,9 +40,10 @@ let add_global_attribute (key : string) (v : Value.t) : unit =
(* add global attributes to this list *)
let merge_global_attributes_ into : _ list =
let open Key_value in
let not_redundant kv = List.for_all (fun kv' -> kv.key <> kv'.key) into in
List.rev_append (List.filter not_redundant !global_attributes) into
let not_redundant kv =
List.for_all Key_value.(fun kv' -> kv.key <> kv'.key) into
in
List.rev_append Key_value.(List.filter not_redundant !global_attributes) into
(** Default span kind in {!Span.create}. This will be used in all spans that do
not specify [~kind] explicitly; it is set to "internal", following

View file

@ -123,11 +123,3 @@ let set_status = span_set_status
let set_kind = span_set_kind
let k_context : t Context.key = Context.new_key ()
(** Find current span from ambient-context *)
let get_ambient () : t option = Ambient_context.get k_context
(** [with_ambient span f] runs [f()] with the current ambient span being set to
[span] *)
let[@inline] with_ambient (span : t) (f : unit -> 'a) : 'a =
Ambient_context.with_key_bound_to k_context span (fun _ -> f ())

View file

@ -108,9 +108,4 @@ val set_kind : t -> Span_kind.t -> unit
(** Set the span's kind.
@since 0.11 *)
val get_ambient : unit -> t option
(** Find current span from ambient-context *)
val with_ambient : t -> (unit -> 'a) -> 'a
(** [with_ambient span f] runs [f()] with the current ambient span being set to
[span] *)
val k_context : t Context.key

View file

@ -1,5 +1,3 @@
open Common_
type t = bytes
let[@inline] to_bytes self = self
@ -13,7 +11,13 @@ let create () : t =
Bytes.set b 0 (Char.unsafe_chr (Char.code (Bytes.get b 0) lor 1));
b
let is_valid = Util_bytes_.bytes_non_zero
let[@inline] is_zero (self : t) : bool =
(* try to reduce branches *)
assert (Bytes.length self = 8);
let n1 = Bytes.get_int64_ne self 0 in
n1 = 0L
let[@inline] is_valid self = not (is_zero self)
let[@inline] of_bytes b =
if Bytes.length b = 8 then

View file

@ -1,5 +1,3 @@
open Common_
type t = bytes
let[@inline] to_bytes self = self
@ -19,7 +17,14 @@ let[@inline] of_bytes b =
else
invalid_arg "trace ID must be 16 bytes in length"
let is_valid = Util_bytes_.bytes_non_zero
let[@inline] is_zero (self : t) : bool =
(* try to reduce branches *)
assert (Bytes.length self = 1);
let n1 = Bytes.get_int64_ne self 0 in
let n2 = Bytes.get_int64_ne self 8 in
n1 = 0L && n2 = 0L
let[@inline] is_valid self = not (is_zero self)
let to_hex = Util_bytes_.bytes_to_hex

9
src/lib/ambient_span.ml Normal file
View file

@ -0,0 +1,9 @@
(** Find current span from ambient-context *)
let[@inline] get () : Span.t option =
Opentelemetry_ambient_context.get Span.k_context
(** [with_ambient span f] runs [f()] with the current ambient span being set to
[span] *)
let[@inline] with_ambient (span : Span.t) (f : unit -> 'a) : 'a =
Opentelemetry_ambient_context.with_key_bound_to Span.k_context span (fun _ ->
f ())

6
src/lib/ambient_span.mli Normal file
View file

@ -0,0 +1,6 @@
val get : unit -> Span.t option
(** Find current span from ambient-context *)
val with_ambient : Span.t -> (unit -> 'a) -> 'a
(** [with_ambient span f] runs [f()] with the current ambient span being set to
[span] *)

25
src/lib/dune Normal file
View file

@ -0,0 +1,25 @@
(library
(name opentelemetry)
(public_name opentelemetry)
(synopsis "API for opentelemetry instrumentation")
(flags
:standard
-warn-error
-a+8
-open
Opentelemetry_util
-open
Opentelemetry_core
-open
Opentelemetry_core.Common_)
(libraries
opentelemetry.core
opentelemetry.proto
opentelemetry.util
opentelemetry.ambient-context
opentelemetry.atomic
ptime
ptime.clock.os
pbrt
threads
hmap))

View file

@ -1,16 +1,12 @@
(** Opentelemetry types and instrumentation *)
(** Main Opentelemetry API for libraries and user code. *)
module Rand_bytes = Rand_bytes
(** Generation of random identifiers. *)
module Core = Opentelemetry_core
(** Core types and definitions *)
module Alist = Alist
(** Atomic list, for internal usage
@since 0.7 *)
module Util_mutex = Util_mutex
(** Utilities for internal usage.
@since NEXT_RELEASE *)
(** {2 Wire format} *)
module Proto = Opentelemetry_proto

View file

@ -66,7 +66,7 @@ let with_thunk_and_finally ?(tracer = simple_main_exporter)
let parent =
match parent with
| Some _ -> parent
| None -> Span.get_ambient ()
| None -> Ambient_span.get ()
in
let trace_id =
match trace_id, parent with
@ -109,7 +109,7 @@ let with_thunk_and_finally ?(tracer = simple_main_exporter)
tracer#emit [ span ]
in
let thunk () = Span.with_ambient span (fun () -> cb span) in
let thunk () = Ambient_span.with_ambient span (fun () -> cb span) in
thunk, finally
(** Sync span guard.