Class: Apirelio::Client
- Inherits:
-
Object
- Object
- Apirelio::Client
- Defined in:
- lib/apirelio/client.rb
Instance Attribute Summary collapse
-
#dropped_count ⇒ Object
readonly
Returns the value of attribute dropped_count.
Instance Method Summary collapse
- #capture(context) ⇒ Object
- #flush ⇒ Object
-
#initialize(api_key:, service:, endpoint: "https://apirelio.com", environment: "production", release: nil, enabled: true, batch_size: 100, flush_interval: 5.0, max_queue_size: 10_000, timeout: 2.0, max_retries: 2, metadata_keys: [], transport: nil, on_error: nil, on_dropped: nil) ⇒ Client
constructor
A new instance of Client.
- #pending_count ⇒ Object
- #shutdown(timeout: 5.0) ⇒ Object
Constructor Details
#initialize(api_key:, service:, endpoint: "https://apirelio.com", environment: "production", release: nil, enabled: true, batch_size: 100, flush_interval: 5.0, max_queue_size: 10_000, timeout: 2.0, max_retries: 2, metadata_keys: [], transport: nil, on_error: nil, on_dropped: nil) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/apirelio/client.rb', line 7 def initialize(api_key:, service:, endpoint: "https://apirelio.com", environment: "production", release: nil, enabled: true, batch_size: 100, flush_interval: 5.0, max_queue_size: 10_000, timeout: 2.0, max_retries: 2, metadata_keys: [], transport: nil, on_error: nil, on_dropped: nil) @api_key = api_key.to_s @service = service.to_s @environment = environment.to_s @release = release @enabled = enabled @batch_size = [[batch_size.to_i, 1].max, 500].min @flush_interval = [flush_interval.to_f, 0.1].max @max_queue_size = [max_queue_size.to_i, 1].max @metadata_keys = .map(&:to_s).freeze @transport = transport || HttpBatchTransport.new(endpoint: endpoint, api_key: @api_key, timeout: timeout, max_retries: max_retries) @on_error = on_error @on_dropped = on_dropped @queue = [] @mutex = Mutex.new @flush_mutex = Mutex.new @condition = ConditionVariable.new @closed = false @dropped_count = 0 @worker = Thread.new { worker_loop } @worker.name = "apirelio" if @worker.respond_to?(:name=) end |
Instance Attribute Details
#dropped_count ⇒ Object (readonly)
Returns the value of attribute dropped_count.
5 6 7 |
# File 'lib/apirelio/client.rb', line 5 def dropped_count @dropped_count end |
Instance Method Details
#capture(context) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/apirelio/client.rb', line 33 def capture(context) return false if @closed || !@enabled || @api_key.empty? event = Event.build(context, service: @service, environment: @environment, release: @release, metadata_keys: @metadata_keys) dropped = false @mutex.synchronize do if @queue.length >= @max_queue_size @dropped_count += 1 dropped = true else @queue << event @condition.signal if @queue.length >= @batch_size end end if dropped report_dropped(event) return false end true rescue StandardError => error report(error) false end |
#flush ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/apirelio/client.rb', line 61 def flush @flush_mutex.synchronize do loop do batch = @mutex.synchronize { @queue.shift(@batch_size) } return true if batch.empty? begin @transport.send(batch) rescue StandardError dropped_events = [] @mutex.synchronize do @queue.unshift(*batch) dropped_events = trim_queue end dropped_events.each { |event| report_dropped(event) } raise end end end end |
#pending_count ⇒ Object
57 58 59 |
# File 'lib/apirelio/client.rb', line 57 def pending_count @mutex.synchronize { @queue.length } end |
#shutdown(timeout: 5.0) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/apirelio/client.rb', line 82 def shutdown(timeout: 5.0) @mutex.synchronize do @closed = true @condition.broadcast end @worker.join([timeout.to_f, 0].max) flush rescue StandardError => error report(error) false end |