Class: CloseYourIt::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/closeyourit/client.rb

Overview

Compone Transport + BackgroundWorker: applica before_send e dispatcha l'invio in modo fire-and-forget.

Constant Summary collapse

LOGS_MAX_BATCH =

Tetto di log per singola richiesta a /logs. Il backend rifiuta un batch oltre questo limite (413 R413-LOG-002) scartando l'INTERA richiesta — e il buffer è già stato drenato → log persi. Deve restare ≤ del limite server (LOGS_MAX_BATCH backend = 1000). Vedi #flush_logs.

1000

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
# File 'lib/closeyourit/client.rb', line 12

def initialize(configuration)
  @configuration = configuration
  @transport = Transport.new(configuration)
  @worker = BackgroundWorker.new(
    threads: configuration.async_threads,
    max_queue: configuration.background_worker_max_queue
  )
end

Instance Method Details

#capture_event(event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/closeyourit/client.rb', line 21

def capture_event(event)
  payload = event.to_h
  payload = @configuration.before_send.call(payload) if @configuration.before_send
  return nil if payload.nil?

  path = event.ingest_path(@configuration.project_id)
  accepted = @worker.perform { @transport.send_event(payload, path: path) }
  CloseYourIt.stats.increment(:enqueued) if accepted
  payload
end

#flush_logs(events) ⇒ Object

Invia un batch di log come ARRAY a /logs (l'endpoint accetta singolo o array). before_send è applicato a ciascun payload; quelli scartati (nil) non vengono inviati. I payload oltre LOGS_MAX_BATCH sono spezzati in più POST sequenziali (un chunk = un POST), così un flush grande non viene rigettato in blocco dal backend e perso — vedi R3 / LOGS_MAX_BATCH. Un flush entro il limite resta un singolo POST.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/closeyourit/client.rb', line 37

def flush_logs(events)
  return nil if events.nil? || events.empty?

  payloads = events.map(&:to_h)
  payloads = payloads.filter_map { |payload| @configuration.before_send.call(payload) } if @configuration.before_send
  return nil if payloads.empty?

  path = events.first.ingest_path(@configuration.project_id)
  payloads.each_slice(LOGS_MAX_BATCH) do |chunk|
    @worker.perform { @transport.send_event(chunk, path: path) }
  end
  payloads
end

#shutdownObject



51
52
53
# File 'lib/closeyourit/client.rb', line 51

def shutdown
  @worker.shutdown
end