timeout-based GC metrics collection

This commit is contained in:
Simon Cruanes 2022-11-30 17:05:22 -05:00
parent 2f1410cc1a
commit dbd08b7e0a
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 29 additions and 7 deletions

View file

@ -10,14 +10,25 @@ include Common_
let needs_gc_metrics = Atomic.make false 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 [] let gc_metrics = ref []
(* side channel for GC, appended to {!E_metrics}'s data *) (* side channel for GC, appended to {!E_metrics}'s data *)
(* capture current GC metrics if {!needs_gc_metrics} is true, (* capture current GC metrics if {!needs_gc_metrics} is true,
and push them into {!gc_metrics} for later or it has been a long time since the last GC metrics collection,
collection *) and push them into {!gc_metrics} for later collection *)
let sample_gc_metrics_if_needed () = 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 = let l =
OT.Metrics.make_resource_metrics OT.Metrics.make_resource_metrics
~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ()) ~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ())

View file

@ -9,14 +9,25 @@ include Common_
let needs_gc_metrics = Atomic.make false 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 () let gc_metrics = AList.make ()
(* side channel for GC, appended to {!E_metrics}'s data *) (* side channel for GC, appended to {!E_metrics}'s data *)
(* capture current GC metrics if {!needs_gc_metrics} is true, (* capture current GC metrics if {!needs_gc_metrics} is true
and push them into {!gc_metrics} for later or it has been a long time since the last GC metrics collection,
collection *) and push them into {!gc_metrics} for later collection *)
let sample_gc_metrics_if_needed () = 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 = let l =
OT.Metrics.make_resource_metrics OT.Metrics.make_resource_metrics
~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ()) ~attrs:(Opentelemetry.GC_metrics.get_runtime_attributes ())