Class: VCAP::Component
- Inherits:
-
Object
- Object
- VCAP::Component
- Defined in:
- lib/vcap/component.rb
Overview
Common component setup for discovery and monitoring
Defined Under Namespace
Classes: SafeHash
Class Attribute Summary collapse
-
.healthz ⇒ Object
Returns the value of attribute healthz.
Class Method Summary collapse
-
.register(opts) ⇒ Object
Announces the availability of this component to NATS.
- .start_http_server(host, port, auth, logger) ⇒ Object
- .update_discover_uptime ⇒ Object
- .updated_healthz ⇒ Object
- .updated_varz ⇒ Object
- .uuid ⇒ Object
- .varz ⇒ Object
Class Attribute Details
.healthz ⇒ Object
Returns the value of attribute healthz.
90 91 92 |
# File 'lib/vcap/component.rb', line 90 def healthz @healthz end |
Class Method Details
.register(opts) ⇒ Object
Announces the availability of this component to NATS. Returns the published configuration of the component, including the ephemeral port and credentials.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/vcap/component.rb', line 154 def register(opts) disallow_exposing_of_config!(opts) uuid = VCAP.secure_uuid type = opts[:type] index = opts[:index] uuid = "#{index}-#{uuid}" if index host = opts[:host] || VCAP.local_ip port = opts[:port] || VCAP.grab_ephemeral_port nats = opts[:nats] || NATS auth = [opts[:user] || VCAP.secure_uuid, opts[:password] || VCAP.secure_uuid] logger = opts[:logger] || Logger.new(nil) log_counter = opts[:log_counter] # Discover message limited @discover = { :type => type, :index => index, :uuid => uuid, :host => "#{host}:#{port}", :credentials => auth, :start => Time.now } # Varz is customizable varz.synchronize do varz.merge!(@discover.dup) varz[:num_cores] = VCAP.num_cores varz[:log_counts] = log_counter if log_counter end @healthz = "ok\n".freeze # Next steps require EM raise "EventMachine reactor needs to be running" if !EventMachine.reactor_running? # Startup the http endpoint for /varz and /healthz start_http_server(host, port, auth, logger) # Listen for discovery requests nats.subscribe('vcap.component.discover') do |msg, reply| update_discover_uptime nats.publish(reply, @discover.to_json) end # Also announce ourselves on startup.. nats.publish('vcap.component.announce', @discover.to_json) @discover end |
.start_http_server(host, port, auth, logger) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/vcap/component.rb', line 131 def start_http_server(host, port, auth, logger) http_server = Thin::Server.new(host, port, :signals => false) do Thin::Logging.silent = true use Rack::Auth::Basic do |username, password| [username, password] == auth end map '/healthz' do run Healthz.new(logger) end map '/varz' do run Varz.new(logger) end end http_server.start! end |
.update_discover_uptime ⇒ Object
205 206 207 |
# File 'lib/vcap/component.rb', line 205 def update_discover_uptime @discover[:uptime] = VCAP.uptime_string(Time.now - @discover[:start]) end |
.updated_healthz ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/vcap/component.rb', line 121 def updated_healthz @last_healthz_update ||= 0 if Time.now.to_f - @last_healthz_update >= 1 @last_healthz_update = Time.now.to_f end healthz.dup end |
.updated_varz ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vcap/component.rb', line 92 def updated_varz @last_varz_update ||= 0 if Time.now.to_f - @last_varz_update >= 1 rss_bytes, pcpu = Stats.process_memory_bytes_and_cpu # Update varz varz.synchronize do @last_varz_update = Time.now.to_f varz[:uptime] = VCAP.uptime_string(Time.now - varz[:start]) varz[:cpu] = pcpu.to_f varz[:cpu_load_avg] = Stats.cpu_load_average varz[:mem_bytes] = rss_bytes.to_i varz[:mem_used_bytes] = Stats.memory_used_bytes varz[:mem_free_bytes] = Stats.memory_free_bytes # Return duplicate while holding lock return varz.dup end else # Return duplicate while holding lock varz.synchronize do return varz.dup end end end |
.uuid ⇒ Object
147 148 149 |
# File 'lib/vcap/component.rb', line 147 def uuid @discover[:uuid] end |