Class: Smplkit::Logging::LoggersClient
- Inherits:
-
Object
- Object
- Smplkit::Logging::LoggersClient
- Defined in:
- lib/smplkit/logging/client.rb
Overview
Surface for client.logging.loggers.* (sync).
Logger CRUD plus the discovery buffer. The buffer is owned by the fused
LoggingClient and shared here so discovery (driven by
LoggingClient#install) and explicit register drain through one queue.
Instance Method Summary collapse
- #_update_logger(logger) ⇒ Object private
-
#delete(id) ⇒ void
Delete a logger by id.
-
#flush ⇒ void
Drain the buffer and POST pending logger sources to the bulk endpoint.
-
#flush_sync ⇒ void
Synchronous flush — alias of
flushfor the periodic-flush path. -
#get(id) ⇒ SmplLogger
Fetch a single logger by id.
-
#get_logger_entry(id) ⇒ Object
private
Fetch one logger as a resolution-cache entry.
-
#initialize(http_client, buffer:, streaming: true) ⇒ LoggersClient
constructor
A new instance of LoggersClient.
-
#list(page_number: nil, page_size: nil) ⇒ Array<SmplLogger>
List loggers for the authenticated account.
-
#list_logger_entries ⇒ Object
private
Runtime entry — walks every page and returns an id-keyed Hash of resolution-cache entries (+level+,
group,managed,environments). -
#new(id, managed: true) ⇒ SmplLogger
Build a new unsaved logger.
-
#pending_count ⇒ Integer
Number of sources queued and awaiting flush.
-
#register(items, flush: false) ⇒ void
Queue one or more logger sources for registration with the server.
Constructor Details
#initialize(http_client, buffer:, streaming: true) ⇒ LoggersClient
Returns a new instance of LoggersClient.
118 119 120 121 122 |
# File 'lib/smplkit/logging/client.rb', line 118 def initialize(http_client, buffer:, streaming: true) @api = SmplkitGeneratedClient::Logging::LoggersApi.new(http_client) @buffer = buffer @streaming = streaming ? true : false end |
Instance Method Details
#_update_logger(logger) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
238 239 240 241 |
# File 'lib/smplkit/logging/client.rb', line 238 def _update_logger(logger) response = ApiSupport::ErrorMapping.call { @api.update_logger(logger.id || logger.name, logger_body(logger)) } Helpers.logger_resource_to_model(self, ApiSupport::ResourceShim.from_model(response.data)) end |
#delete(id) ⇒ void
This method returns an undefined value.
Delete a logger by id.
232 233 234 235 |
# File 'lib/smplkit/logging/client.rb', line 232 def delete(id) ApiSupport::ErrorMapping.call { @api.delete_logger(id) } nil end |
#flush ⇒ void
This method returns an undefined value.
Drain the buffer and POST pending logger sources to the bulk endpoint.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/smplkit/logging/client.rb', line 162 def flush batch = @buffer.drain return if batch.empty? items = batch.map do |entry| SmplkitGeneratedClient::Logging::LoggerBulkItem.new( id: entry["id"], resolved_level: entry["resolved_level"], level: entry["level"], service: entry["service"], environment: entry["environment"] ) end body = SmplkitGeneratedClient::Logging::LoggerBulkRequest.new(loggers: items) ApiSupport::ErrorMapping.call { @api.bulk_register_loggers(body) } end |
#flush_sync ⇒ void
This method returns an undefined value.
Synchronous flush — alias of flush for the periodic-flush path.
179 180 181 |
# File 'lib/smplkit/logging/client.rb', line 179 def flush_sync flush end |
#get(id) ⇒ SmplLogger
Fetch a single logger by id.
222 223 224 225 |
# File 'lib/smplkit/logging/client.rb', line 222 def get(id) response = ApiSupport::ErrorMapping.call { @api.get_logger(id) } Helpers.logger_resource_to_model(self, ApiSupport::ResourceShim.from_model(response.data)) end |
#get_logger_entry(id) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetch one logger as a resolution-cache entry. Used by the logger_changed
WS handler.
256 257 258 259 |
# File 'lib/smplkit/logging/client.rb', line 256 def get_logger_entry(id) response = ApiSupport::ErrorMapping.call { @api.get_logger(id) } logger_entry_from_resource(ApiSupport::ResourceShim.from_model(response.data)) end |
#list(page_number: nil, page_size: nil) ⇒ Array<SmplLogger>
List loggers for the authenticated account.
209 210 211 212 213 214 215 |
# File 'lib/smplkit/logging/client.rb', line 209 def list(page_number: nil, page_size: nil) opts = {} opts[:page_number] = page_number unless page_number.nil? opts[:page_size] = page_size unless page_size.nil? response = ApiSupport::ErrorMapping.call { @api.list_loggers(opts) } (response.data || []).map { |r| Helpers.logger_resource_to_model(self, ApiSupport::ResourceShim.from_model(r)) } end |
#list_logger_entries ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runtime entry — walks every page and returns an id-keyed Hash of
resolution-cache entries (+level+, group, managed, environments).
247 248 249 250 |
# File 'lib/smplkit/logging/client.rb', line 247 def list_logger_entries rows = ApiSupport::PaginatedFetch.collect { |opts| @api.list_loggers(opts) } rows.to_h { |r| logger_entry_from_resource(ApiSupport::ResourceShim.from_model(r)) } end |
#new(id, managed: true) ⇒ SmplLogger
Build a new unsaved logger. The returned SmplLogger is local only;
call its SmplLogger#save to persist it.
198 199 200 |
# File 'lib/smplkit/logging/client.rb', line 198 def new(id, managed: true) SmplLogger.new(self, id: id, name: id, resolved_level: nil, managed: managed) end |
#pending_count ⇒ Integer
Number of sources queued and awaiting flush.
186 187 188 |
# File 'lib/smplkit/logging/client.rb', line 186 def pending_count @buffer.pending_count end |
#register(items, flush: false) ⇒ void
This method returns an undefined value.
Queue one or more logger sources for registration with the server.
Sources are buffered locally and sent in a batch. The batch is sent
automatically once enough sources accumulate; pass flush: true to send
the current batch right away instead of waiting.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/smplkit/logging/client.rb', line 135 def register(items, flush: false) batch = items.is_a?(Array) ? items : [items] batch.each do |src| @buffer.add(LoggerSource.new( name: Normalize.normalize_logger_name(src.name), resolved_level: src.resolved_level, level: src.level, service: src.service, environment: src.environment )) end if flush self.flush return end return unless @buffer.pending_count >= LOGGER_BATCH_FLUSH_SIZE # Stateless mode (+streaming: false+) never spawns background threads — # the threshold flush runs inline (blocking) instead. if @streaming Thread.new { threshold_flush } else threshold_flush end end |