|
| 1 | +import { display, setInterval, clearInterval } from '@datadog/browser-core' |
| 2 | +import type { LiveDebuggerInitConfiguration } from '../entries/main' |
| 3 | +import { addProbe, removeProbe } from './probes' |
| 4 | +import type { Probe } from './probes' |
| 5 | + |
| 6 | +/** |
| 7 | + * Remote Config Client for Browser Live Debugger |
| 8 | + * |
| 9 | + * Polls the RC proxy periodically to get probe updates and synchronizes |
| 10 | + * the local probe registry. |
| 11 | + * |
| 12 | + * NOTE: The RC proxy can operate in two modes: |
| 13 | + * - agent mode: Polls local Datadog agent (for POC/development, no CORS issues) |
| 14 | + * - backend mode: Polls Datadog RC backend directly (requires backend access) |
| 15 | + */ |
| 16 | + |
| 17 | +interface ProbeState { |
| 18 | + id: string |
| 19 | + version: number |
| 20 | +} |
| 21 | + |
| 22 | +let pollIntervalId: number | undefined |
| 23 | +let currentProbeStates = new Map<string, ProbeState>() // Track probes by ID and version |
| 24 | + |
| 25 | +/** |
| 26 | + * Start polling the Remote Config proxy for probe updates |
| 27 | + * |
| 28 | + * @param config - Live Debugger configuration |
| 29 | + */ |
| 30 | +export function startRemoteConfigPolling(config: LiveDebuggerInitConfiguration): void { |
| 31 | + if (pollIntervalId !== undefined) { |
| 32 | + display.warn('Live Debugger: Remote Config polling already started') |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const proxyUrl = config.remoteConfigProxyUrl! |
| 37 | + const pollInterval = config.remoteConfigPollInterval || 5000 |
| 38 | + |
| 39 | + display.info(`Live Debugger: Starting Remote Config polling (${proxyUrl}, interval: ${pollInterval}ms)`) |
| 40 | + |
| 41 | + // Build query string with service metadata |
| 42 | + const params = new URLSearchParams() |
| 43 | + params.set('service', config.service!) |
| 44 | + if (config.env) { |
| 45 | + params.set('env', config.env) |
| 46 | + } |
| 47 | + if (config.version) { |
| 48 | + params.set('version', config.version) |
| 49 | + } |
| 50 | + |
| 51 | + const pollUrl = `${proxyUrl}/probes?${params.toString()}` |
| 52 | + |
| 53 | + // Polling function |
| 54 | + const poll = async () => { |
| 55 | + try { |
| 56 | + const response = await fetch(pollUrl) |
| 57 | + |
| 58 | + if (!response.ok) { |
| 59 | + display.error(`Live Debugger: RC poll failed with status ${response.status}`) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + const data = await response.json() |
| 64 | + const probes: Probe[] = data.probes || [] |
| 65 | + |
| 66 | + // Synchronize probes |
| 67 | + synchronizeProbes(probes) |
| 68 | + } catch (err) { |
| 69 | + display.error('Live Debugger: RC poll error:', err as Error) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Initial poll |
| 74 | + void poll() |
| 75 | + |
| 76 | + // Start polling interval |
| 77 | + pollIntervalId = setInterval(() => { |
| 78 | + void poll() |
| 79 | + }, pollInterval) |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Stop Remote Config polling |
| 84 | + */ |
| 85 | +export function stopRemoteConfigPolling(): void { |
| 86 | + if (pollIntervalId !== undefined) { |
| 87 | + clearInterval(pollIntervalId) |
| 88 | + pollIntervalId = undefined |
| 89 | + display.info('Live Debugger: Remote Config polling stopped') |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Synchronize local probes with probes from RC proxy |
| 95 | + * |
| 96 | + * - Adds new probes |
| 97 | + * - Removes probes no longer in the response |
| 98 | + * - Updates probes if version changed |
| 99 | + * |
| 100 | + * @param probes - Array of probes from RC proxy |
| 101 | + */ |
| 102 | +function synchronizeProbes(probes: Probe[]): void { |
| 103 | + const newProbeStates = new Map<string, ProbeState>() |
| 104 | + |
| 105 | + // Process probes from RC |
| 106 | + for (const probe of probes) { |
| 107 | + if (!probe.id) { |
| 108 | + display.warn('Live Debugger: Received probe without ID, skipping') |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + const probeState: ProbeState = { |
| 113 | + id: probe.id, |
| 114 | + version: probe.version |
| 115 | + } |
| 116 | + newProbeStates.set(probe.id, probeState) |
| 117 | + |
| 118 | + const currentState = currentProbeStates.get(probe.id) |
| 119 | + |
| 120 | + if (!currentState) { |
| 121 | + // New probe - add it |
| 122 | + try { |
| 123 | + addProbe(probe) |
| 124 | + display.log(`Live Debugger: Added probe ${probe.id} (v${probe.version})`) |
| 125 | + } catch (err) { |
| 126 | + display.error(`Live Debugger: Failed to add probe ${probe.id}:`, err as Error) |
| 127 | + } |
| 128 | + } else if (currentState.version !== probe.version) { |
| 129 | + // Probe version changed - remove old and add new |
| 130 | + try { |
| 131 | + removeProbe(probe.id) |
| 132 | + addProbe(probe) |
| 133 | + display.log(`Live Debugger: Updated probe ${probe.id} (v${currentState.version} -> v${probe.version})`) |
| 134 | + } catch (err) { |
| 135 | + display.error(`Live Debugger: Failed to update probe ${probe.id}:`, err as Error) |
| 136 | + } |
| 137 | + } |
| 138 | + // If version is the same, probe already exists - no action needed |
| 139 | + } |
| 140 | + |
| 141 | + // Remove probes that are no longer in RC response |
| 142 | + for (const [probeId, currentState] of currentProbeStates.entries()) { |
| 143 | + if (!newProbeStates.has(probeId)) { |
| 144 | + try { |
| 145 | + removeProbe(probeId) |
| 146 | + display.log(`Live Debugger: Removed probe ${probeId} (v${currentState.version})`) |
| 147 | + } catch (err) { |
| 148 | + display.error(`Live Debugger: Failed to remove probe ${probeId}:`, err as Error) |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Update current state |
| 154 | + currentProbeStates = newProbeStates |
| 155 | + |
| 156 | + display.log(`Live Debugger: Synchronized ${newProbeStates.size} probe(s)`) |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Get current Remote Config polling status |
| 161 | + * |
| 162 | + * @returns true if polling is active |
| 163 | + */ |
| 164 | +export function isRemoteConfigPolling(): boolean { |
| 165 | + return pollIntervalId !== undefined |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * Clear probe state (useful for testing) |
| 170 | + */ |
| 171 | +export function clearRemoteConfigState(): void { |
| 172 | + currentProbeStates.clear() |
| 173 | + if (pollIntervalId !== undefined) { |
| 174 | + clearInterval(pollIntervalId) |
| 175 | + pollIntervalId = undefined |
| 176 | + } |
| 177 | +} |
| 178 | + |
0 commit comments