From 341c70f18fb6175d78f20a55fc06a03d578c9a4b Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 21 Mar 2022 11:19:36 -0400 Subject: [PATCH] make add_event lazy --- src/opentelemetry.ml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/opentelemetry.ml b/src/opentelemetry.ml index aa82fafa..67b9b4ef 100644 --- a/src/opentelemetry.ml +++ b/src/opentelemetry.ml @@ -96,6 +96,9 @@ module Collector = struct let backend : backend option ref = ref None + (** Is there a configured backend? *) + let[@inline] has_backend () : bool = !backend != None + let send_trace (l:Trace.resource_spans list) ~over ~ret = match !backend with | None -> over(); ret() @@ -393,9 +396,14 @@ module Trace = struct mutable events: Event.t list; } - (** Add an event to the scope. It will be aggregated into the span *) - let[@inline] add_event (scope:scope) (ev:Event.t) : unit = - scope.events <- ev :: scope.events + (** Add an event to the scope. It will be aggregated into the span. + + Note that this takes a function that produces an event, and will only + call it if there is an instrumentation backend. *) + let[@inline] add_event (scope:scope) (ev:unit -> Event.t) : unit = + if Collector.has_backend() then ( + scope.events <- ev() :: scope.events + ) (** Sync span guard *) let with_