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:) ⇒ 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:) ⇒ LoggersClient
Returns a new instance of LoggersClient.
110 111 112 113 |
# File 'lib/smplkit/logging/client.rb', line 110 def initialize(http_client, buffer:) @api = SmplkitGeneratedClient::Logging::LoggersApi.new(http_client) @buffer = buffer 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.
223 224 225 226 |
# File 'lib/smplkit/logging/client.rb', line 223 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.
217 218 219 220 |
# File 'lib/smplkit/logging/client.rb', line 217 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.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/smplkit/logging/client.rb', line 147 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.
164 165 166 |
# File 'lib/smplkit/logging/client.rb', line 164 def flush_sync flush end |
#get(id) ⇒ SmplLogger
Fetch a single logger by id.
207 208 209 210 |
# File 'lib/smplkit/logging/client.rb', line 207 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.
241 242 243 244 |
# File 'lib/smplkit/logging/client.rb', line 241 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.
194 195 196 197 198 199 200 |
# File 'lib/smplkit/logging/client.rb', line 194 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).
232 233 234 235 |
# File 'lib/smplkit/logging/client.rb', line 232 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.
183 184 185 |
# File 'lib/smplkit/logging/client.rb', line 183 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.
171 172 173 |
# File 'lib/smplkit/logging/client.rb', line 171 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/smplkit/logging/client.rb', line 126 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 Thread.new { threshold_flush } end |