diff --git a/src/client/emitter_add_batching.mli b/src/client/emitter_add_batching.mli index 1e919b6a..817f06c0 100644 --- a/src/client/emitter_add_batching.mli +++ b/src/client/emitter_add_batching.mli @@ -1,3 +1,12 @@ +(** Add a batch in front of an emitter. + + The batch accumulates signals until it's full or too old, at which points + all the accumulated signals are emitted at once. Pushing into a batch is + generally very fast (amortized), in most cases; the slow path is only when + the batch needs to be emitted. + + @since NEXT_RELEASE *) + open Opentelemetry_emitter val wrap_emitter_with_batch : 'a Batch.t -> 'a Emitter.t -> 'a Emitter.t diff --git a/src/client/emitter_limit_interval.ml b/src/client/emitter_limit_interval.ml new file mode 100644 index 00000000..be6bc91a --- /dev/null +++ b/src/client/emitter_limit_interval.ml @@ -0,0 +1,8 @@ +open Common_.OTEL + +let add_interval_limiter il (e : _ Emitter.t) : _ Emitter.t = + let emit xs = if Interval_limiter.make_attempt il then Emitter.emit e xs in + { e with emit } + +let limit_interval ~min_interval (e : _ Emitter.t) : _ Emitter.t = + add_interval_limiter (Interval_limiter.create ~min_interval ()) e diff --git a/src/client/emitter_limit_interval.mli b/src/client/emitter_limit_interval.mli new file mode 100644 index 00000000..43ec32d0 --- /dev/null +++ b/src/client/emitter_limit_interval.mli @@ -0,0 +1,25 @@ +(** Limit frequency at which the emitter emits. + + This puts a hard floor on the interval between two consecutive successful + [emit]. Attempts to emit too early are simply discarded. + + The use case for this is metrics: it's possible, for a gauge, to just drop + some entries if we've been emitting them too frequently. + + {b NOTE}: it's better to do [limit_interval ~min_interval (add_batching e)] + than [add_batching (limit_interval ~min_interval e)], because in the later + case we might be dismissing a whole large batch at ine + + @since NEXT_RELEASE *) + +open Common_.OTEL + +val add_interval_limiter : Interval_limiter.t -> 'a Emitter.t -> 'a Emitter.t +(** [add_interval_limiter il e] is a new emitter [e'] that can only emit signals + less frequently than [Interval_limiter.min_interval il]. + + Trying to emit too early will simply drop the signal. *) + +val limit_interval : min_interval:Mtime.span -> 'a Emitter.t -> 'a Emitter.t +(** [limit_interval ~min_interval e] is + [add_interval_limiter (Interval_limiter.create ~min_interval ()) e] *)