Class: Lens::Rails::RequestsExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lens/rails/requests_exporter.rb

Overview

Subscribes to ActionController, ActionView, ActiveRecord, and ActiveJob notifications and pushes collected records onto a Queue. A dedicated worker thread drains the queue in batches and ships them to POST /v1/requests via a single persistent HTTPS connection.

Constant Summary collapse

SKIP_SCHEMA =
/\A(SCHEMA|ActiveRecord::SchemaMigration|ActiveRecord::InternalMetadata)\z/
SKIP_JOB_CLASS =
/\ASolidQueue::/
BATCH_SIZE =
200

Instance Method Summary collapse

Constructor Details

#initialize(url:, token:, service_name:, sql_threshold_ms: 0.0, sql_ignore: [], max_buffer: 5_000, open_timeout: 2, read_timeout: 2) ⇒ RequestsExporter

Returns a new instance of RequestsExporter.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lens/rails/requests_exporter.rb', line 95

def initialize(url:, token:, service_name:,
  sql_threshold_ms: 0.0, sql_ignore: [],
  max_buffer: 5_000,
  open_timeout: 2, read_timeout: 2)
  @uri = URI("#{url}/v1/requests")
  @token = token
  @service_name = service_name
  @sql_threshold_ms = sql_threshold_ms
  @sql_ignore = sql_ignore
  @max_buffer = max_buffer
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @queue = SizedQueue.new(max_buffer)
  @http = nil
  @fork_mutex = Mutex.new
  subscribe!
  start_worker
  Lens::Rails.register_flushable(self)
end

Instance Method Details

#ensure_worker!Object



121
122
123
124
125
126
127
# File 'lib/lens/rails/requests_exporter.rb', line 121

def ensure_worker!
  return if @worker&.alive?
  @fork_mutex.synchronize do
    return if @worker&.alive?
    restart_flush_thread
  end
end

#restart_flush_threadObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/lens/rails/requests_exporter.rb', line 129

def restart_flush_thread
  begin
    @worker&.kill
  rescue
    nil
  end
  close_http
  @queue = SizedQueue.new(@max_buffer)
  start_worker
end

#shutdown(timeout:) ⇒ Object



115
116
117
118
119
# File 'lib/lens/rails/requests_exporter.rb', line 115

def shutdown(timeout:)
  @queue.close
  @worker&.join(timeout)
rescue
end