Class: Neetodeploy::Rails::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/neetodeploy/autoscale/rails/metrics.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, config = Config.instance) ⇒ Metrics

Returns a new instance of Metrics.



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
# File 'lib/neetodeploy/autoscale/rails/metrics.rb', line 9

def initialize(env, config = Config.instance)
  @config = config
  @env = env
  @request_start_header_raw = env["HTTP_X_REQUEST_START"]
  @request_start_header = @request_start_header_raw.to_i
  @network_time = env["puma.request_body_wait"].to_i
  
  # Debug: Log all HTTP headers to help diagnose (only for non-health-check requests)
  if (@request_start_header_raw.nil? || @request_start_header_raw.empty?) && !health_check_request?(env)
    # Get all HTTP_* headers (these are the request headers)
    http_headers = env.keys.grep(/^HTTP_/)
    
    if http_headers.any?
      NeetoDeploy::Logger.logger.debug("All HTTP headers found: #{http_headers.sort.join(', ')}")
    else
      NeetoDeploy::Logger.logger.debug("No HTTP headers found in env. Available env keys: #{env.keys.grep(/^HTTP|^REQUEST|^SERVER/).sort.join(', ')}")
    end
    
    # Also check for X-Request-Start with different casing
    x_request_variants = env.select { |k, _| k.to_s.upcase.include?("REQUEST_START") || k.to_s.upcase.include?("X_REQUEST") }
    if x_request_variants.any?
      NeetoDeploy::Logger.logger.debug("Found X-Request-Start variants: #{x_request_variants.keys.join(', ')}")
    end
  end
end

Instance Method Details

#health_check_request?(env = @env) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/neetodeploy/autoscale/rails/metrics.rb', line 39

def health_check_request?(env = @env)
  # Ignore health check endpoints that don't go through Traefik
  # These typically come from Kubernetes liveness/readiness probes
  path = env["PATH_INFO"] || env["REQUEST_PATH"] || ""
  path.match?(%r{/health_check|/health|/up|/ready|/live})
end

#ignore?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/neetodeploy/autoscale/rails/metrics.rb', line 35

def ignore?
  @config.gem_disabled? || health_check_request?(@env)
end

#queue_timeObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/neetodeploy/autoscale/rails/metrics.rb', line 46

def queue_time
  # Debug: Log if header is missing or invalid
  if @request_start_header_raw.nil? || @request_start_header_raw.empty?
    NeetoDeploy::Logger.logger.debug("X-Request-Start header missing or empty")
    return nil
  end

  return nil if @request_start_header.zero?

  time_now = Time.now.to_f * 1000

  queue_time = (time_now - @request_start_header).round
  queue_time -= @network_time

  # Log both values for debugging
  NeetoDeploy::Logger.logger.debug(
    "Queue time calculation: X-Request-Start=#{@request_start_header_raw} (#{@request_start_header}ms), " \
    "puma.request_body_wait=#{@network_time}ms, time_now=#{time_now.round}ms, " \
    "calculated_queue_time=#{queue_time}ms"
  )

  queue_time.positive? ? queue_time : nil
end