Compare commits

..

6 commits

Author SHA1 Message Date
Simon Cruanes
b92159c11e
add Meter_provider.emit_l
Some checks are pending
format / format (push) Waiting to run
build / build (4.08.x, ubuntu-latest) (push) Waiting to run
build / build (4.13.x, ubuntu-latest) (push) Waiting to run
build / build (5.0.x, ubuntu-latest) (push) Waiting to run
build / build (5.3.x, ubuntu-latest) (push) Waiting to run
2026-03-05 15:20:38 -05:00
Simon Cruanes
0c29da4302
meter: add emit 2026-03-05 15:07:28 -05:00
Simon Cruanes
a28f24d14f
metrics: use main clock by default for data points 2026-03-05 14:46:46 -05:00
Simon Cruanes
e97088f6f7
fix warning 2026-03-05 14:46:33 -05:00
Simon Cruanes
05ef03b39d
Span.record_exception must also set the span status to error 2026-03-05 10:22:57 -05:00
Simon Cruanes
e2fe0f6683
dune shenanigan 2026-03-05 10:11:24 -05:00
8 changed files with 23 additions and 14 deletions

View file

@ -11,7 +11,7 @@
-open
Opentelemetry_atomic)
(libraries
opentelemetry.proto
(re_export opentelemetry.proto)
opentelemetry.util
(re_export opentelemetry.atomic)
(re_export opentelemetry.emitter)

View file

@ -16,15 +16,15 @@ type t = Metrics.metric
let pp = Proto.Metrics.pp_metric
(** Number data point, as a float *)
let float ?start_time_unix_nano ?(attrs = []) ~(now : Timestamp_ns.t)
let float ?start_time_unix_nano ?(attrs = []) ?(now = Clock.now_main ())
(d : float) : number_data_point =
let attributes = attrs |> List.map Key_value.conv in
make_number_data_point ?start_time_unix_nano ~time_unix_nano:now ~attributes
~value:(As_double d) ()
(** Number data point, as an int *)
let int ?start_time_unix_nano ?(attrs = []) ~(now : Timestamp_ns.t) (i : int) :
number_data_point =
let int ?start_time_unix_nano ?(attrs = []) ?(now = Clock.now_main ()) (i : int)
: number_data_point =
let attributes = attrs |> List.map Key_value.conv in
make_number_data_point ?start_time_unix_nano ~time_unix_nano:now ~attributes
~value:(As_int (Int64.of_int i))
@ -61,7 +61,7 @@ type histogram_data_point = Metrics.histogram_data_point
length 0)
@param explicit_bounds strictly increasing list of bounds for the buckets *)
let histogram_data_point ?start_time_unix_nano ?(attrs = []) ?(exemplars = [])
~explicit_bounds ?sum ~(now : Timestamp_ns.t) ~bucket_counts ~count () :
~explicit_bounds ?sum ?(now = Clock.now_main ()) ~bucket_counts ~count () :
histogram_data_point =
let attributes = attrs |> List.map Key_value.conv in
make_histogram_data_point ?start_time_unix_nano ~time_unix_nano:now

View file

@ -103,17 +103,21 @@ let add_event' self ev : unit =
let record_exception (self : t) (exn : exn) (bt : Printexc.raw_backtrace) : unit
=
if is_not_dummy self then (
let exn_msg = Printexc.to_string exn in
let ev =
Event.make "exception"
~attrs:
[
"exception.message", `String (Printexc.to_string exn);
"exception.message", `String exn_msg;
"exception.type", `String (Printexc.exn_slot_name exn);
( "exception.stacktrace",
`String (Printexc.raw_backtrace_to_string bt) );
]
in
add_event self ev
add_event self ev;
let status = make_status ~code:Status_code_error ~message:exn_msg () in
span_set_status self status
)
let add_attrs (self : t) (attrs : Key_value.t list) : unit =

View file

@ -94,6 +94,8 @@ val add_event' : t -> (unit -> Event.t) -> unit
it if there is an instrumentation backend. *)
val record_exception : t -> exn -> Printexc.raw_backtrace -> unit
(** Record an exception occurring inside the span. This creates a span event
{b and} also sets the span status to error. *)
val add_links : t -> Span_link.t list -> unit

View file

@ -10,6 +10,8 @@ let dummy : t = { emit = Emitter.dummy; clock = Clock.ptime_clock }
let[@inline] enabled (self : t) = Emitter.enabled self.emit
let[@inline] emit self ms : unit = Emitter.emit self.emit ms
let[@inline] emit1 (self : t) (m : Metrics.t) : unit =
Emitter.emit self.emit [ m ]

View file

@ -21,6 +21,9 @@ val enabled : t -> bool
val of_exporter : Exporter.t -> t
(** Create a meter from an exporter *)
val emit : t -> Metrics.t list -> unit
(** Emit metrics directly, bypassing the instrument registry *)
val emit1 : t -> Metrics.t -> unit
(** Emit a single metric directly, bypassing the instrument registry *)

View file

@ -46,6 +46,9 @@ let get_meter ?name ?version ?(attrs : (string * [< Value.t ]) list = [])
(** Emit with current meter *)
let[@inline] emit (m : Metrics.t) : unit = Emitter.emit (get ()).emit [ m ]
(** Emit a list of metrics with current meter *)
let[@inline] emit_l (ms : Metrics.t list) : unit = Emitter.emit (get ()).emit ms
(** A Meter.t that lazily reads the global at emit time *)
let default_meter : Meter.t = get_meter ()

View file

@ -87,19 +87,14 @@ let with_thunk_and_finally (self : Tracer.t) ?(force_new_trace_id = false)
(* called once we're done, to emit a span *)
let finally res =
let end_time = Clock.now self.clock in
Proto.Trace.span_set_end_time_unix_nano span end_time;
span_set_end_time_unix_nano span end_time;
(match Span.status span with
| Some _ -> ()
| None ->
(match res with
| Ok () -> ()
| Error (e, bt) ->
Span.record_exception span e bt;
let status =
make_status ~code:Status_code_error ~message:(Printexc.to_string e) ()
in
Span.set_status span status));
| Error (e, bt) -> Span.record_exception span e bt));
Emitter.emit self.emit [ span ]
in