Class: A2A::Monitoring::HealthEndpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/monitoring/health_endpoints.rb

Overview

Health check endpoints for A2A applications

Provides standard health check endpoints that can be used by load balancers, monitoring systems, and orchestrators.

Instance Method Summary collapse

Constructor Details

#initialize(health_checker = A2A::Monitoring.health_checks) ⇒ HealthEndpoints

Initialize health endpoints

Parameters:



14
15
16
17
# File 'lib/a2a/monitoring/health_endpoints.rb', line 14

def initialize(health_checker = A2A::Monitoring.health_checks)
  @health_checker = health_checker
  setup_default_checks
end

Instance Method Details

#call(env) ⇒ Array

Handle health check request

Parameters:

  • request (Hash)

    HTTP request data

Returns:

  • (Array)

    Rack response array



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/a2a/monitoring/health_endpoints.rb', line 22

def call(env)
  path = env["PATH_INFO"]

  case path
  when "/health"
    handle_health_check
  when "/health/ready"
    handle_readiness_check
  when "/health/live"
    handle_liveness_check
  when "/metrics"
    handle_metrics_endpoint
  else
    [404, { "Content-Type" => "application/json" }, ['{"error":"Not Found"}']]
  end
end

#check_database_connectionObject (private)



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/a2a/monitoring/health_endpoints.rb', line 106

def check_database_connection
  return true unless A2A.config.rails_integration

  if defined?(ActiveRecord)
    ActiveRecord::Base.connection.active?
  else
    true
  end
rescue StandardError
  false
end

#check_plugins_loadedObject (private)



132
133
134
135
136
137
138
139
# File 'lib/a2a/monitoring/health_endpoints.rb', line 132

def check_plugins_loaded
  # Check if critical plugins are loaded
  critical_plugins = A2A.config.get(:critical_plugins) || []

  critical_plugins.all? do |plugin_name|
    A2A::Plugin.loaded?(plugin_name)
  end
end

#check_readinessObject (private)



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/a2a/monitoring/health_endpoints.rb', line 93

def check_readiness
  # Check database connectivity if using database storage
  return false unless check_database_connection

  # Check Redis connectivity if using Redis
  return false unless check_redis_connection

  # Check plugin system
  return false unless check_plugins_loaded

  true
end

#check_redis_connectionObject (private)



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/a2a/monitoring/health_endpoints.rb', line 118

def check_redis_connection
  redis_config = A2A.config.redis_config
  return true unless redis_config && redis_config[:url]

  if defined?(Redis)
    redis = Redis.new(url: redis_config[:url])
    redis.ping == "PONG"
  else
    true
  end
rescue StandardError
  false
end

#handle_health_checkObject (private)

Handle general health check



42
43
44
45
46
47
# File 'lib/a2a/monitoring/health_endpoints.rb', line 42

def handle_health_check
  health_result = @health_checker.check_health
  status_code = health_result[:status] == :healthy ? 200 : 503

  [status_code, json_headers, [health_result.to_json]]
end

#handle_liveness_checkObject (private)

Handle liveness check (application is running)



64
65
66
67
68
69
70
71
72
73
# File 'lib/a2a/monitoring/health_endpoints.rb', line 64

def handle_liveness_check
  # Simple liveness check - if we can respond, we're alive
  response = {
    status: "alive",
    timestamp: Time.now.iso8601,
    uptime: Process.clock_gettime(Process::CLOCK_MONOTONIC)
  }

  [200, json_headers, [response.to_json]]
end

#handle_metrics_endpointObject (private)

Handle metrics endpoint



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/a2a/monitoring/health_endpoints.rb', line 76

def handle_metrics_endpoint
  if defined?(Prometheus)
    # Return Prometheus metrics format
    metrics = Prometheus::Client.registry.metrics
    prometheus_output = Prometheus::Client::Formats::Text.marshal(metrics)
    [200, { "Content-Type" => "text/plain" }, [prometheus_output]]
  else
    # Return JSON metrics
    metrics = A2A::Monitoring.metrics.current_metrics
    [200, json_headers, [{ metrics: metrics }.to_json]]
  end
end

#handle_readiness_checkObject (private)

Handle readiness check (can serve traffic)



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/a2a/monitoring/health_endpoints.rb', line 50

def handle_readiness_check
  # Check if application is ready to serve requests
  ready = check_readiness
  status_code = ready ? 200 : 503

  response = {
    status: ready ? "ready" : "not_ready",
    timestamp: Time.now.iso8601
  }

  [status_code, json_headers, [response.to_json]]
end

#json_headersObject (private)



89
90
91
# File 'lib/a2a/monitoring/health_endpoints.rb', line 89

def json_headers
  { "Content-Type" => "application/json" }
end

#setup_default_checksObject (private)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/a2a/monitoring/health_endpoints.rb', line 141

def setup_default_checks
  # Register default health checks
  @health_checker.register_check(:configuration) do
    A2A.config.validate!
    { status: :healthy, message: "Configuration is valid" }
  rescue StandardError => e
    { status: :unhealthy, message: "Configuration error: #{e.message}" }
  end

  @health_checker.register_check(:memory_usage) do
    # Check memory usage (basic check)
    if defined?(GC)
      stat = GC.stat
      heap_used = stat[:heap_allocated_pages] * stat[:heap_page_size]

      # Simple threshold check (adjust as needed)
      if heap_used > 500_000_000 # 500MB
        { status: :warning, message: "High memory usage: #{heap_used} bytes" }
      else
        { status: :healthy, message: "Memory usage: #{heap_used} bytes" }
      end
    else
      { status: :healthy, message: "Memory check not available" }
    end
  end

  @health_checker.register_check(:plugin_system) do
    loaded_count = A2A::Plugin.loaded_plugins.size
    registered_count = A2A::Plugin.registry.size

    {
      status: :healthy,
      message: "Plugins: #{loaded_count}/#{registered_count} loaded"
    }
  end
end