Class: OpenTrace::Client

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

Defined Under Namespace

Classes: UnixSocketResponse

Constant Summary collapse

MAX_QUEUE_SIZE =
1000
PAYLOAD_MAX_BYTES =

256 KB (default; use config.max_payload_bytes to override)

262_144
MAX_RATE_LIMIT_BACKOFF =

Cap Retry-After at 60 seconds

60
API_VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, sampler: nil) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opentrace/client.rb', line 20

def initialize(config, sampler: nil)
  @config = config
  @sampler = sampler
  @queue  = Thread::Queue.new
  @queue_bytes = 0
  @queue_bytes_mutex = Mutex.new
  @mutex  = Mutex.new
  @thread = nil
  @pid    = Process.pid
  @circuit_breaker = CircuitBreaker.new(
    failure_threshold: config.circuit_breaker_threshold,
    recovery_timeout: config.circuit_breaker_timeout
  )
  @rate_limit_until = nil
  @auth_suspended = false
  @auth_failure_warned = false
  @stats = Stats.new
  @compatibility_checked = false
  @server_capabilities = nil
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



18
19
20
# File 'lib/opentrace/client.rb', line 18

def stats
  @stats
end

Instance Method Details

#auth_suspended?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/opentrace/client.rb', line 75

def auth_suspended?
  @auth_suspended
end

#circuit_stateObject



71
72
73
# File 'lib/opentrace/client.rb', line 71

def circuit_state
  @circuit_breaker.state
end

#enqueue(payload) ⇒ Object



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

def enqueue(payload)
  return unless @config.enabled?

  if @auth_suspended
    @stats.increment(:dropped_auth_suspended)
    fire_on_drop(1, :auth_suspended)
    return
  end

  reset_after_fork! if forked?

  byte_size = estimate_payload_bytes(payload)
  unless push_queue_item(payload, byte_size: byte_size)
    @stats.increment(:dropped_queue_full)
    fire_on_drop(1, :queue_full)
    return
  end

  @stats.increment(:enqueued)
  ensure_thread_running
end

#queue_byte_sizeObject



67
68
69
# File 'lib/opentrace/client.rb', line 67

def queue_byte_size
  @queue_bytes_mutex.synchronize { @queue_bytes }
end

#queue_sizeObject



63
64
65
# File 'lib/opentrace/client.rb', line 63

def queue_size
  @queue.size
end

#shutdown(timeout: 5) ⇒ Object



93
94
95
96
# File 'lib/opentrace/client.rb', line 93

def shutdown(timeout: 5)
  @queue.close
  @thread&.join(timeout)
end

#stats_snapshotObject



79
80
81
82
83
84
85
86
87
# File 'lib/opentrace/client.rb', line 79

def stats_snapshot
  @stats.to_h.merge(
    queue_size: queue_size,
    queue_byte_size: queue_byte_size,
    circuit_state: circuit_state,
    auth_suspended: @auth_suspended,
    server_capabilities: @server_capabilities
  )
end

#supports?(capability) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/opentrace/client.rb', line 89

def supports?(capability)
  @server_capabilities&.include?(capability.to_s)
end