Class: BrainzLab::Pulse::Client

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

Defined Under Namespace

Classes: RetryableError

Constant Summary collapse

MAX_RETRIES =
3
RETRY_DELAY =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/brainzlab/pulse/client.rb', line 13

def initialize(config)
  @config = config
  @buffer = []
  @mutex = Mutex.new
  @flush_thread = nil
end

Instance Method Details

#flushObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/brainzlab/pulse/client.rb', line 56

def flush
  traces_to_send = nil

  @mutex.synchronize do
    return if @buffer.empty?

    traces_to_send = @buffer.dup
    @buffer.clear
  end

  send_batch(traces_to_send) if traces_to_send&.any?
end

#send_batch(payloads) ⇒ Object



37
38
39
40
41
42
# File 'lib/brainzlab/pulse/client.rb', line 37

def send_batch(payloads)
  return unless @config.pulse_enabled && @config.pulse_valid?
  return if payloads.empty?

  post('/api/v1/traces/batch', { traces: payloads })
end

#send_metric(payload) ⇒ Object



44
45
46
47
48
# File 'lib/brainzlab/pulse/client.rb', line 44

def send_metric(payload)
  return unless @config.pulse_enabled && @config.pulse_valid?

  post('/api/v1/metrics', payload)
end

#send_span(payload) ⇒ Object



50
51
52
53
54
# File 'lib/brainzlab/pulse/client.rb', line 50

def send_span(payload)
  return unless @config.pulse_enabled && @config.pulse_valid?

  post('/api/v1/spans', payload)
end

#send_trace(payload) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brainzlab/pulse/client.rb', line 20

def send_trace(payload)
  return unless @config.pulse_enabled && @config.pulse_valid?

  # Per-trace sampling — the single chokepoint every trace path funnels
  # through (request middleware, jobs, sidekiq, delayed_job, grape, manual
  # traces). Decided once per complete trace (spans travel inline in the
  # payload, so they never orphan) and BEFORE buffering, so dropped traces
  # never reach the buffer/batch. Error traces are always kept.
  return unless keep_trace?(payload)

  if @config.pulse_buffer_size > 1
    buffer_trace(payload)
  else
    post('/api/v1/traces', payload)
  end
end