Class: Tracekit::Snapshots::Client

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

Overview

Client for code monitoring - polls breakpoints and captures snapshots

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, base_url, service_name, poll_interval_seconds = 30, **opts) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tracekit/snapshots/client.rb', line 17

def initialize(api_key, base_url, service_name, poll_interval_seconds = 30, **opts)
  @api_key = api_key
  @base_url = base_url
  @service_name = service_name
  @security_detector = Security::Detector.new
  @breakpoints_cache = Concurrent::Hash.new
  @registration_cache = Concurrent::Hash.new

  # Opt-in capture limits
  @capture_depth = opts[:capture_depth]
  @max_payload = opts[:max_payload]
  @capture_timeout = opts[:capture_timeout]

  # Kill switch: server-initiated monitoring disable
  @kill_switch_active = false
  @normal_poll_interval = poll_interval_seconds

  # SSE (Server-Sent Events) real-time updates
  @sse_endpoint = nil
  @sse_active = false
  @sse_thread = nil

  # Circuit breaker state (Mutex-protected for thread safety)
  cb_config = opts[:circuit_breaker] || {}
  @cb_mutex = Mutex.new
  @cb_failure_timestamps = []
  @cb_state = "closed"
  @cb_opened_at = nil
  @cb_max_failures = cb_config[:max_failures] || 3
  @cb_window_seconds = cb_config[:window_seconds] || 60
  @cb_cooldown_seconds = cb_config[:cooldown_seconds] || 300
  @pending_events = []

  # Start polling timer
  @poll_task = Concurrent::TimerTask.new(execution_interval: poll_interval_seconds) do
    fetch_active_breakpoints
  end
  @poll_task.execute

  # Initial fetch
  fetch_active_breakpoints
end

Instance Attribute Details

#capture_depthObject

Opt-in capture limits (all disabled by default: nil = unlimited)



13
14
15
# File 'lib/tracekit/snapshots/client.rb', line 13

def capture_depth
  @capture_depth
end

#capture_timeoutObject

nil = no timeout seconds (default)



15
16
17
# File 'lib/tracekit/snapshots/client.rb', line 15

def capture_timeout
  @capture_timeout
end

#max_payloadObject

nil = unlimited payload bytes (default)



14
15
16
# File 'lib/tracekit/snapshots/client.rb', line 14

def max_payload
  @max_payload
end

Instance Method Details

#capture_snapshot(label, variables, caller_location = nil) ⇒ Object

Captures a snapshot at the caller’s location. Crash isolation: rescues all exceptions so TraceKit never crashes the host app.



62
63
64
65
66
67
68
# File 'lib/tracekit/snapshots/client.rb', line 62

def capture_snapshot(label, variables, caller_location = nil)
  begin
    do_capture_snapshot(label, variables, caller_location)
  rescue => e
    warn "TraceKit: error in capture_snapshot: #{e.message}" if ENV["DEBUG"]
  end
end

#shutdownObject

Shuts down the client



181
182
183
184
# File 'lib/tracekit/snapshots/client.rb', line 181

def shutdown
  @poll_task&.shutdown
  close_sse
end