Class: Smplkit::MetricsReporter
- Inherits:
-
Object
- Object
- Smplkit::MetricsReporter
- Defined in:
- lib/smplkit/metrics.rb
Overview
Periodically flushes accumulated SDK telemetry to the platform.
Aggregation runs on a daemon thread. Public methods are thread-safe and block briefly only to enqueue metrics.
When telemetry is disabled in resolved config, Smplkit::Client does not construct a MetricsReporter and the metrics accessor on sub-clients is nil. Sub-clients guard every call with metrics&..
Constant Summary collapse
- DEFAULT_FLUSH_INTERVAL =
60.0
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(http_client:, environment:, service:, flush_interval: DEFAULT_FLUSH_INTERVAL) ⇒ MetricsReporter
constructor
A new instance of MetricsReporter.
- #record(metric, unit:, dimensions: nil) ⇒ Object
- #record_gauge(metric, value, unit:, dimensions: nil) ⇒ Object
Constructor Details
#initialize(http_client:, environment:, service:, flush_interval: DEFAULT_FLUSH_INTERVAL) ⇒ MetricsReporter
Returns a new instance of MetricsReporter.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/smplkit/metrics.rb', line 17 def initialize(http_client:, environment:, service:, flush_interval: DEFAULT_FLUSH_INTERVAL) @http_client = http_client @environment = environment @service = service @flush_interval = flush_interval @counts = {} @gauges = {} @closed = Concurrent::AtomicBoolean.new(false) @lock = Mutex.new schedule_flush end |
Instance Method Details
#close ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/smplkit/metrics.rb', line 56 def close return if @closed.true? @closed.make_true @timer&.shutdown flush end |
#flush ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/smplkit/metrics.rb', line 43 def flush counts_snap, gauges_snap = @lock.synchronize do c = @counts.dup g = @gauges.dup @counts.clear @gauges.clear [c, g] end send_payload(counts_snap, gauges_snap) unless counts_snap.empty? && gauges_snap.empty? rescue StandardError => e Smplkit.debug("metrics", "flush failed: #{e.class}: #{e.}") end |
#record(metric, unit:, dimensions: nil) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/smplkit/metrics.rb', line 29 def record(metric, unit:, dimensions: nil) return if @closed.true? key = [metric, unit, dimensions || {}].freeze @lock.synchronize { @counts[key] = (@counts[key] || 0) + 1 } end |
#record_gauge(metric, value, unit:, dimensions: nil) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/smplkit/metrics.rb', line 36 def record_gauge(metric, value, unit:, dimensions: nil) return if @closed.true? key = [metric, unit, dimensions || {}].freeze @lock.synchronize { @gauges[key] = value } end |