Class: Dispatch::Rails::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch/rails/transport.rb

Overview

Ships events to Dispatch off the request path: a single background worker drains a bounded queue and POSTs each event. Bounded so a flood can never grow memory without limit (excess events are dropped, not blocking).

Defined Under Namespace

Classes: FlushSignal

Constant Summary collapse

QUEUE_LIMIT =
100
OPEN_TIMEOUT =
2
READ_TIMEOUT =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransport

Returns a new instance of Transport.



42
43
44
45
# File 'lib/dispatch/rails/transport.rb', line 42

def initialize
  @queue = Queue.new
  @mutex = Mutex.new
end

Class Method Details

.instanceObject



32
33
34
# File 'lib/dispatch/rails/transport.rb', line 32

def instance
  @instance ||= new
end

.reset!Object

Test seam.



37
38
39
# File 'lib/dispatch/rails/transport.rb', line 37

def reset!
  @instance = nil
end

Instance Method Details

#deliver(event) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/dispatch/rails/transport.rb', line 47

def deliver(event)
  return if @queue.size >= QUEUE_LIMIT

  ensure_worker
  @queue << event
  true
end

#deliver_heartbeat(payload) ⇒ Object

Synchronous send of one flush window of traffic counts. Best-effort: a dropped heartbeat just means the confound guard has one fewer data point.



77
78
79
# File 'lib/dispatch/rails/transport.rb', line 77

def deliver_heartbeat(payload)
  post(Dispatch::Rails.configuration.effective_heartbeat_endpoint, payload)
end

#flush(timeout: 3) ⇒ Object

Block until everything queued (and in flight) has been sent, or the deadline passes. Returns true when fully drained. Called at process exit so a shutdown doesn’t drop already-captured events.



58
59
60
61
62
63
64
65
66
67
# File 'lib/dispatch/rails/transport.rb', line 58

def flush(timeout: 3)
  return true if @queue.empty? && @worker.nil?

  signal = FlushSignal.new
  ensure_worker
  @queue << signal
  signal.wait(timeout)
rescue StandardError
  false
end

#post_ticket(payload) ⇒ Object

Synchronous curated-ticket POST backing Dispatch::Rails.report. Posts to the tickets endpoint and returns the parsed response Hash ({ “id”, “status”, “url” }) on success, or nil on any failure.



84
85
86
87
88
89
90
91
# File 'lib/dispatch/rails/transport.rb', line 84

def post_ticket(payload)
  response = post(Dispatch::Rails.configuration.endpoint, payload)
  return nil unless response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body)
rescue StandardError
  nil
end

#send_now(event) ⇒ Object

Synchronous send of an exception event — used by tests and as the worker’s unit of work. Posts to the Sentry-compatible /store endpoint.



71
72
73
# File 'lib/dispatch/rails/transport.rb', line 71

def send_now(event)
  post(Dispatch::Rails.configuration.effective_error_endpoint, event)
end