File: backpressure_monitor.rb

package info (click to toggle)
ruby-sentry-ruby 5.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: ruby: 4,701; makefile: 8; sh: 4
file content (45 lines) | stat: -rw-r--r-- 1,125 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Sentry
  class BackpressureMonitor < ThreadedPeriodicWorker
    DEFAULT_INTERVAL = 10
    MAX_DOWNSAMPLE_FACTOR = 10

    def initialize(configuration, client, interval: DEFAULT_INTERVAL)
      super(configuration.logger, interval)
      @client = client

      @healthy = true
      @downsample_factor = 0
    end

    def healthy?
      ensure_thread
      @healthy
    end

    def downsample_factor
      ensure_thread
      @downsample_factor
    end

    def run
      check_health
      set_downsample_factor
    end

    def check_health
      @healthy = !(@client.transport.any_rate_limited? || Sentry.background_worker&.full?)
    end

    def set_downsample_factor
      if @healthy
        log_debug("[BackpressureMonitor] health check positive, reverting to normal sampling") if @downsample_factor.positive?
        @downsample_factor = 0
      else
        @downsample_factor += 1 if @downsample_factor < MAX_DOWNSAMPLE_FACTOR
        log_debug("[BackpressureMonitor] health check negative, downsampling with a factor of #{@downsample_factor}")
      end
    end
  end
end