Module: Legion::API::Routes::Stats
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/api/stats.rb
Constant Summary collapse
- EXTENSION_TASK_IVARS =
{ discovered: :@extensions, subscription: :@subscription_tasks, every: :@timer_tasks, poll: :@poll_tasks, once: :@once_tasks, loop: :@loop_tasks, actors: :@running_instances }.freeze
Class Method Summary collapse
- .collect_api ⇒ Object
- .collect_cache ⇒ Object
- .collect_cache_local ⇒ Object
- .collect_data ⇒ Object
- .collect_data_local ⇒ Object
- .collect_extensions ⇒ Object
- .collect_gaia ⇒ Object
- .collect_llm ⇒ Object
- .collect_transport ⇒ Object
- .registered(app) ⇒ Object
Class Method Details
.collect_api ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/legion/api/stats.rb', line 194 def collect_api port = Legion::Settings.dig(:api, :port) || Legion::Settings.dig(:http, :port) || 4567 info = { port: port } # Puma thread pool stats if available puma_server = Puma::Server.current if defined?(Puma::Server) && Puma::Server.respond_to?(:current) if puma_server.respond_to?(:pool_capacity) info[:puma] = { pool_capacity: puma_server.pool_capacity, max_threads: puma_server.max_threads, running: puma_server.running, backlog: puma_server.backlog } end info[:routes] = begin Legion::API.routes.values.flatten.count rescue StandardError nil end info rescue StandardError => e { error: e. } end |
.collect_cache ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/legion/api/stats.rb', line 83 def collect_cache return { connected: false } unless defined?(Legion::Cache) info = { connected: Legion::Cache.connected? } info[:using_local] = Legion::Cache.using_local? if Legion::Cache.respond_to?(:using_local?) info[:using_memory] = Legion::Cache.instance_variable_get(:@using_memory) == true info[:driver] = begin Legion::Settings[:cache][:driver] rescue StandardError nil end if Legion::Cache.connected? && Legion::Cache.respond_to?(:size) info[:pool_size] = begin Legion::Cache.size rescue StandardError nil end info[:pool_available] = begin Legion::Cache.available rescue StandardError nil end end info rescue StandardError => e { error: e. } end |
.collect_cache_local ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/legion/api/stats.rb', line 112 def collect_cache_local return { connected: false } unless defined?(Legion::Cache::Local) info = { connected: Legion::Cache::Local.connected? } if Legion::Cache::Local.connected? info[:pool_size] = begin Legion::Cache::Local.size rescue StandardError nil end info[:pool_available] = begin Legion::Cache::Local.available rescue StandardError nil end end info rescue StandardError => e { error: e. } end |
.collect_data ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/legion/api/stats.rb', line 165 def collect_data return { connected: false } unless defined?(Legion::Data) && Legion::Settings[:data][:connected] if Legion::Data.respond_to?(:stats) stats = Legion::Data.stats stats[:shared] || stats else { connected: true, adapter: begin Legion::Data::Connection.adapter rescue StandardError nil end } end rescue StandardError => e { error: e. } end |
.collect_data_local ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/legion/api/stats.rb', line 182 def collect_data_local return { connected: false } unless defined?(Legion::Data::Local) && Legion::Data::Local.connected? if Legion::Data::Local.respond_to?(:stats) Legion::Data::Local.stats else { connected: true } end rescue StandardError => e { error: e. } end |
.collect_extensions ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/legion/api/stats.rb', line 36 def collect_extensions ext = Legion::Extensions { loaded: ext.extension_handle_registry.loaded.count, running: ext.extension_handle_registry.running.count }.merge(EXTENSION_TASK_IVARS.transform_values { |ivar| ext.instance_variable_get(ivar)&.count || 0 }) rescue StandardError => e { error: e. } end |
.collect_gaia ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/legion/api/stats.rb', line 46 def collect_gaia return { started: false } unless defined?(Legion::Gaia) && Legion::Gaia.respond_to?(:started?) && Legion::Gaia.started? Legion::Gaia.status rescue StandardError => e { error: e. } end |
.collect_llm ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/legion/api/stats.rb', line 133 def collect_llm return { started: false } unless defined?(Legion::LLM) && Legion::LLM.started? info = { started: true } s = Legion::LLM.settings info[:default_model] = s[:default_model] info[:default_provider] = s[:default_provider] info[:pipeline_enabled] = s[:pipeline_enabled] == true if defined?(Legion::LLM::Router) # legit optional-gem guard (standards ยง6): legion-llm may be absent info[:routing_enabled] = true info[:provider_health] = Legion::LLM::Inventory.lanes.each_with_object({}) do |lane, h| key = "#{lane[:provider_family]}/#{lane[:instance_id]}" h[key] = { circuit: lane.dig(:health, :circuit_state)&.to_s } rescue StandardError => e handle_exception(e, level: :warn, operation: 'api.stats.provider_health', lane: lane[:id]) next # skip the malformed lane, but the failure is logged + published, not swallowed end else info[:routing_enabled] = false end if defined?(Legion::LLM::ConversationStore) store = Legion::LLM::ConversationStore info[:conversations] = store.respond_to?(:size) ? store.size : nil end info rescue StandardError => e handle_exception(e, level: :warn, operation: 'api.stats.collect_llm') { error: e. } end |
.collect_transport ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/legion/api/stats.rb', line 54 def collect_transport conn = Legion::Transport::Connection connected = begin Legion::Settings[:transport][:connected] rescue StandardError false end connector = defined?(Legion::Transport::TYPE) ? Legion::Transport::TYPE.to_s : 'unknown' info = { connected: connected, connector: connector } session = conn.session if session.respond_to?(:open?) && session.open? info[:session_open] = true info[:channel_max] = session.channel_max if session.respond_to?(:channel_max) # Bunny tracks open channels in @channels hash channels = session.instance_variable_get(:@channels) info[:channels_open] = channels.is_a?(Hash) ? channels.count : nil else info[:session_open] = false end info[:build_session_open] = conn.build_session_open? info[:lite_mode] = conn.lite_mode? info rescue StandardError => e { error: e. } end |
.registered(app) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/legion/api/stats.rb', line 9 def self.registered(app) app.get '/api/stats' do result = {} result[:extensions] = Routes::Stats.collect_extensions result[:gaia] = Routes::Stats.collect_gaia result[:transport] = Routes::Stats.collect_transport result[:cache] = Routes::Stats.collect_cache result[:cache_local] = Routes::Stats.collect_cache_local result[:llm] = Routes::Stats.collect_llm result[:data] = Routes::Stats.collect_data result[:data_local] = Routes::Stats.collect_data_local result[:api] = Routes::Stats.collect_api json_response(result) end end |