Class: Lescopr::Modes::EmbeddedMode

Inherits:
Object
  • Object
show all
Defined in:
lib/lescopr/modes/embedded.rb

Overview

EmbeddedMode — gRPC-less in-process transport for long-running Ruby apps.

Designed for:

- Docker / Kubernetes containers (no sidecar daemon)
- Gunicorn-style forked multi-worker apps (Puma, Unicorn)
- Any environment where running a separate daemon is impractical

Behaviour:

- Reuses the existing HTTP transport (no gRPC required)
- Background thread flushes queue via HTTPS batch every FLUSH_INTERVAL s
- Exponential backoff on connection errors (up to 12 attempts)
- Exits cleanly via at_exit hook

Constant Summary collapse

FLUSH_INTERVAL =

seconds between normal flushes

30
BATCH_SIZE =
100
MAX_QUEUE =
500
MAX_CONNECT_ATTEMPTS =
12

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ EmbeddedMode

Returns a new instance of EmbeddedMode.



26
27
28
29
30
31
32
33
34
# File 'lib/lescopr/modes/embedded.rb', line 26

def initialize(http_client)
  @http_client   = http_client
  @queue         = []
  @mutex         = Mutex.new
  @stop_event    = false
  @thread        = nil
  @connected     = false
  @logger        = Lescopr::Monitoring::Logger.new
end

Instance Method Details

#add_log(entry) ⇒ Object

── Public API ─────────────────────────────────────────────────────────



54
55
56
57
58
59
60
61
# File 'lib/lescopr/modes/embedded.rb', line 54

def add_log(entry)
  @mutex.synchronize do
    @queue << entry
    if @queue.size > MAX_QUEUE
      @queue = @queue.last(MAX_QUEUE / 2)
    end
  end
end

#flush_syncObject

── Flush ──────────────────────────────────────────────────────────────



65
66
67
68
69
70
71
72
# File 'lib/lescopr/modes/embedded.rb', line 65

def flush_sync
  loop do
    batch = @mutex.synchronize { @queue.shift(BATCH_SIZE) }
    break if batch.empty?

    send_batch(batch)
  end
end

#startObject

── Lifecycle ──────────────────────────────────────────────────────────



38
39
40
41
42
43
44
# File 'lib/lescopr/modes/embedded.rb', line 38

def start
  install_exit_hook!
  start_worker_thread!

  @logger.info("[EMBEDDED] Mode embedded actif (thread HTTP)")
  self
end

#stopObject



46
47
48
49
50
# File 'lib/lescopr/modes/embedded.rb', line 46

def stop
  @stop_event = true
  @thread&.join(5)
  flush_sync
end