From dbd08b7e0a2c202a85ec5b4db0ce0bd7d3fc2616 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 30 Nov 2022 17:05:22 -0500 Subject: [PATCH] timeout-based GC metrics collection --- .../opentelemetry_client_cohttp_lwt.ml | 17 ++++++++++++++--- .../opentelemetry_client_ocurl.ml | 19 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml index c5e7e6d3..fe3ce4bd 100644 --- a/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml +++ b/src/client-cohttp-lwt/opentelemetry_client_cohttp_lwt.ml @@ -10,14 +10,25 @@ include Common_ let needs_gc_metrics = Atomic.make false +let last_gc_metrics = Atomic.make (Mtime_clock.now ()) + +let timeout_gc_metrics = Mtime.Span.(10 * s) + let gc_metrics = ref [] (* side channel for GC, appended to {!E_metrics}'s data *) (* capture current GC metrics if {!needs_gc_metrics} is true, - and push them into {!gc_metrics} for later - collection *) + or it has been a long time since the last GC metrics collection, + and push them into {!gc_metrics} for later collection *) let sample_gc_metrics_if_needed () = - if Atomic.compare_and_set needs_gc_metrics true false then ( + let now = Mtime_clock.now () in + let alarm = Atomic.compare_and_set needs_gc_metrics true false in + let timeout () = + let elapsed = Mtime.span now (Atomic.get last_gc_metrics) in + Mtime.Span.compare elapsed timeout_gc_metrics > 0 + in + if alarm || timeout () then ( + Atomic.set last_gc_metrics now; let l = OT.Metrics.make_resource_metrics ~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ()) diff --git a/src/client-ocurl/opentelemetry_client_ocurl.ml b/src/client-ocurl/opentelemetry_client_ocurl.ml index 486c745d..df641cbe 100644 --- a/src/client-ocurl/opentelemetry_client_ocurl.ml +++ b/src/client-ocurl/opentelemetry_client_ocurl.ml @@ -9,14 +9,25 @@ include Common_ let needs_gc_metrics = Atomic.make false +let last_gc_metrics = Atomic.make (Mtime_clock.now ()) + +let timeout_gc_metrics = Mtime.Span.(10 * s) + let gc_metrics = AList.make () (* side channel for GC, appended to {!E_metrics}'s data *) -(* capture current GC metrics if {!needs_gc_metrics} is true, - and push them into {!gc_metrics} for later - collection *) +(* capture current GC metrics if {!needs_gc_metrics} is true + or it has been a long time since the last GC metrics collection, + and push them into {!gc_metrics} for later collection *) let sample_gc_metrics_if_needed () = - if Atomic.compare_and_set needs_gc_metrics true false then ( + let now = Mtime_clock.now () in + let alarm = Atomic.compare_and_set needs_gc_metrics true false in + let timeout () = + let elapsed = Mtime.span now (Atomic.get last_gc_metrics) in + Mtime.Span.compare elapsed timeout_gc_metrics > 0 + in + if alarm || timeout () then ( + Atomic.set last_gc_metrics now; let l = OT.Metrics.make_resource_metrics ~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ())