Class: Lescopr::Core::Client
- Inherits:
-
Object
- Object
- Lescopr::Core::Client
- Defined in:
- lib/lescopr/core/client.rb
Overview
Central SDK client — manages configuration, log queue, daemon lifecycle and the Ruby logger hook.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#direct_mode ⇒ Object
readonly
Returns the value of attribute direct_mode.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#log_queue ⇒ Object
readonly
Returns the value of attribute log_queue.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#sdk_id ⇒ Object
readonly
Returns the value of attribute sdk_id.
-
#sdk_logger ⇒ Object
readonly
Returns the value of attribute sdk_logger.
Instance Method Summary collapse
-
#initialize(configuration) ⇒ Client
constructor
A new instance of Client.
-
#ready? ⇒ Boolean
True if the SDK is fully initialised and ready.
-
#send_log(level, message, metadata = {}) ⇒ Object
Queue a log entry for async delivery.
-
#setup! ⇒ Boolean
Bootstrap: analyse project, register with API, start daemon.
-
#setup_auto_logging! ⇒ Object
Hook into Ruby’s stdlib Logger so existing ‘Rails.logger.info` etc.
-
#shutdown! ⇒ Object
Graceful shutdown — flush queue then stop daemon.
Constructor Details
#initialize(configuration) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lescopr/core/client.rb', line 11 def initialize(configuration) @configuration = configuration @sdk_id = nil @ready = false @mutex = Mutex.new @mode = nil # 'daemon' | 'embedded' | 'direct' @direct_mode = nil # DirectMode or EmbeddedMode instance @sdk_logger = Monitoring::Logger.new(debug: configuration.debug) @log_queue = LogQueue.new @http_client = Transport::HttpClient.new( api_key: configuration.api_key, sdk_key: configuration.sdk_key ) @daemon = DaemonRunner.new(self) @config_mgr = Filesystem::ConfigManager.new load_config end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def configuration @configuration end |
#direct_mode ⇒ Object (readonly)
Returns the value of attribute direct_mode.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def direct_mode @direct_mode end |
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def http_client @http_client end |
#log_queue ⇒ Object (readonly)
Returns the value of attribute log_queue.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def log_queue @log_queue end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def mode @mode end |
#sdk_id ⇒ Object (readonly)
Returns the value of attribute sdk_id.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def sdk_id @sdk_id end |
#sdk_logger ⇒ Object (readonly)
Returns the value of attribute sdk_logger.
8 9 10 |
# File 'lib/lescopr/core/client.rb', line 8 def sdk_logger @sdk_logger end |
Instance Method Details
#ready? ⇒ Boolean
Returns true if the SDK is fully initialised and ready.
32 33 34 |
# File 'lib/lescopr/core/client.rb', line 32 def ready? @ready end |
#send_log(level, message, metadata = {}) ⇒ Object
Queue a log entry for async delivery.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lescopr/core/client.rb', line 95 def send_log(level, , = {}) return unless @ready entry = { timestamp: Time.now.utc.iso8601(3), level: level.to_s.upcase, message: .to_s, sdk_id: @sdk_id, environment: configuration.environment, metadata: } # Route to mode-specific transport if @direct_mode @direct_mode.add_log(entry) else log_queue.push(entry) end end |
#setup! ⇒ Boolean
Bootstrap: analyse project, register with API, start daemon.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 82 83 84 |
# File 'lib/lescopr/core/client.rb', line 38 def setup! unless configuration.api_key && configuration.sdk_key sdk_logger.warn("sdk_key and api_key are required. SDK inactive.") return false end analyzer = Filesystem::ProjectAnalyzer.new payload = analyzer.analyze response = http_client.verify_project(payload) unless response && response[:sdk_id] sdk_logger.warn("API verification failed — check sdk_key / api_key") return false end @sdk_id = response[:sdk_id] config_data = response.merge( sdk_key: configuration.sdk_key, api_key: configuration.api_key, environment: configuration.environment, project_name: payload[:project_name] ) @config_mgr.save(config_data) @daemon.start @ready = true # Determine and start mode after being ready @mode = Modes::Detector.detect sdk_logger.info("SDK initialised — mode: #{@mode}, project: #{payload[:project_name]}, sdk_id: #{@sdk_id}") case @mode when 'direct' @direct_mode = Modes::DirectMode.new(http_client) @direct_mode.start when 'embedded' @direct_mode = Modes::EmbeddedMode.new(http_client) @direct_mode.start end # 'daemon' uses existing DaemonRunner — no extra setup needed true rescue StandardError => e sdk_logger.error("setup! failed: #{e.}") false end |
#setup_auto_logging! ⇒ Object
Hook into Ruby’s stdlib Logger so existing ‘Rails.logger.info` etc. calls are automatically forwarded to Lescopr.
88 89 90 91 92 |
# File 'lib/lescopr/core/client.rb', line 88 def setup_auto_logging! setup! install_global_exception_handler! install_at_exit_hook! end |
#shutdown! ⇒ Object
Graceful shutdown — flush queue then stop daemon.
116 117 118 119 120 121 122 123 124 |
# File 'lib/lescopr/core/client.rb', line 116 def shutdown! if @direct_mode @direct_mode.stop else @daemon.stop end @ready = false sdk_logger.info("SDK shut down") end |