Class: Apmlens::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/apmlens/client.rb

Overview

The Heavy Lifter. Handles buffering, threading, and network transmission.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Client

Returns a new instance of Client.



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

def initialize
  @queue = Queue.new
  @metric_buffer = Hash.new { |h, k| h[k] = [] }
  @metric_lock = Mutex.new
  @metric_count = 0
end

Instance Method Details

#capture_error(exception, context = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/apmlens/client.rb', line 25

def capture_error(exception, context = {})
  param_hash = {
    type: "errors",
    body: {
      service: Apmlens.configuration.service,
      error: {
        class: exception.class.name,
        message: exception.message,
        stack_trace: exception.backtrace&.first(20),
        occurred_at: Time.now.utc.iso8601,
        metadata: context
      }
    }
  }
  enqueue(param_hash)
end

#capture_trace(name, value, tags = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apmlens/client.rb', line 42

def capture_trace(name, value, tags = {})
  key = { name: name, tags: tags }
  config = Apmlens.configuration

  @metric_lock.synchronize do
    # 1. Series Limit: Check if this is a new series and if we've reached the limit
    if !@metric_buffer.key?(key) && @metric_buffer.size >= config.max_series_limit
      return
    end

    # 2. Samples Limit: Check if we've reached the total samples limit
    if @metric_count >= config.max_samples_limit
      return
    end

    @metric_buffer[key] << value
    @metric_count += 1
  end
end

#start! ⇒ Object



21
22
23
# File 'lib/apmlens/client.rb', line 21

def start!
  start_worker_thread!
end