Collect Hubble traffic continuously; graph from informer caches

The ring buffer on each agent holds 4095 flows, ~10 s at the observed
~380 flows/s, so the 5/15/60 min windows were really showing the last
few seconds. A background follow stream now fills per-minute buckets
for the last hour, and counts are distinct connections (5-tuples)
instead of Hubble events, with opened (SYN) and dropped broken out.
/api/flows reports coveredSec so the UI can say how much of the window
the collector has seen since start.

/api/graph reads informer caches instead of listing every kind per
request (~0.9 s before). Pod-to-workload resolution for flows uses the
same cache. YAML view drops managedFields.

Frontend: traffic refreshes in place every 30 s without relayout; dash
animation rationed (12 fps, only highlighted edges in dense views,
paused after a minute idle) since each step redraws the whole element
layer, ~15 ms on a 200-element view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vitaliy Chefanov
2026-09-23 20:23:20 +03:00
co-authored by Claude Opus 5.5
parent cf84e9976f
commit 6ead0a0d6d
5 changed files with 547 additions and 237 deletions
+86 -14
View File
@@ -532,8 +532,9 @@ function aggregateFlows(flows) {
for (const e of flows.edges) {
const key = e.source + '→' + e.target;
let p = pairs.get(key);
if (!p) { p = { source: e.source, target: e.target, count: 0, dropped: 0, ports: [], last: '' }; pairs.set(key, p); }
p.count += e.count;
if (!p) { p = { source: e.source, target: e.target, count: 0, fresh: 0, dropped: 0, ports: [], last: '' }; pairs.set(key, p); }
p.count += e.connections;
p.fresh += e.new;
p.dropped += e.dropped;
if (!p.ports.includes(e.port)) p.ports.push(e.port);
if (e.last > p.last) p.last = e.last;
@@ -584,14 +585,63 @@ function buildElements() {
}
}
state.trafficEdges.push(e);
const width = 1.6 + Math.min(4.5, Math.log10(e.count + 1) * 1.7);
const label = `${e.ports.slice(0, 3).join(', ')}${e.ports.length > 3 ? '…' : ''} · ${e.count}${e.dropped ? ` · drop ${e.dropped}` : ''}`;
add({ group: 'edges', classes: 'traffic' + (e.dropped ? ' drop' : ''), data: { id: 'tr:' + e.source + '→' + e.target, source: e.source, target: e.target, width, label, flow: e } });
add(trafficEdgeElement(e));
}
}
return els;
}
function trafficEdgeElement(e) {
return {
group: 'edges', classes: 'traffic' + (e.dropped ? ' drop' : ''),
data: { id: 'tr:' + e.source + '→' + e.target, source: e.source, target: e.target, ...trafficEdgeData(e) },
};
}
function trafficEdgeData(e) {
return {
width: 1.6 + Math.min(4.5, Math.log10(e.count + 1) * 1.7),
label: `${e.ports.slice(0, 3).join(', ')}${e.ports.length > 3 ? '…' : ''} · ${e.count} соед.${e.dropped ? ` · drop ${e.dropped}` : ''}`,
flow: e,
};
}
// Refresh traffic on the drawn map without relayout: counts and widths are
// updated, edges between cards already on the map come and go. Peers that
// would need new cards are only counted, so the layout never jumps under
// the cursor; "Обновить" redraws with them.
let hiddenPeers = 0;
async function refreshTraffic() {
if (!state.traffic || state.scope.mode === 'cluster' || document.hidden) return;
const token = renderToken;
let flows;
try { flows = await fetchFlows(scopeNamespaces().slice(0, 100)); } catch { return; }
if (token !== renderToken) return;
state.flows = flows;
state.flowsError = null;
const members = new Set(cy.nodes(':childless').filter((n) => !n.hasClass('ghostcard') && !n.data('external')).map((n) => n.id()));
const next = new Map();
hiddenPeers = 0;
for (const e of aggregateFlows(flows)) {
if (!state.infra && isInfraEdge(e)) continue;
if (!members.has(e.source) && !members.has(e.target)) continue;
if (cy.getElementById(e.source).empty() || cy.getElementById(e.target).empty()) { hiddenPeers++; continue; }
next.set('tr:' + e.source + '→' + e.target, e);
}
cy.batch(() => {
cy.edges('.traffic').forEach((edge) => { if (!next.has(edge.id())) edge.remove(); });
for (const [id, e] of next) {
const edge = cy.getElementById(id);
if (edge.nonempty()) { edge.data(trafficEdgeData(e)); edge.toggleClass('drop', e.dropped > 0); }
else cy.add(trafficEdgeElement(e));
}
cy.edges('.traffic').toggleClass('quiet', next.size > QUIET_AT);
});
state.trafficEdges = [...next.values()];
if (detailsId) renderLinks(detailsId);
updateStatus(cy.elements().map((x) => ({ group: x.group(), data: x.data() })));
}
setInterval(refreshTraffic, 30000);
function buildClusterElements() {
const els = [];
const groups = [
@@ -727,12 +777,14 @@ async function renderMap() {
if (state.traffic && namespaces.length) {
setStatus('загрузка трафика…');
try {
state.flows = await fetchFlows(namespaces.slice(0, 50));
state.flows = await fetchFlows(namespaces.slice(0, 100));
if (!state.flows.connected && !state.flows.edges?.length) state.flowsError = state.flows.error || 'нет связи с Hubble Relay';
} catch (err) {
state.flowsError = err.message;
}
if (token !== renderToken) return;
}
hiddenPeers = 0;
const els = buildElements();
cy.batch(() => { cy.elements().remove(); cy.add(els); });
const quiet = cy.edges('.traffic').length > QUIET_AT;
@@ -754,13 +806,25 @@ function updateStatus(els) {
if (state.flowsError) { setStatus('трафик недоступен: ' + state.flowsError, true); return; }
if (state.flows) {
const total = state.trafficEdges.reduce((s, e) => s + e.count, 0);
const win = { 300: '5 мин', 900: '15 мин', 3600: '1 час' }[state.flows.windowSec] || state.flows.windowSec + ' с';
const f = state.flows;
// The collector only knows what it has seen since the pod started.
const win = f.coveredSec < f.windowSec - 30
? `${fmtDuration(f.coveredSec)} (из ${fmtDuration(f.windowSec)}: сбор идёт с запуска)`
: fmtDuration(f.windowSec);
if (!f.connected) setStatus(`связь с Hubble потеряна (${f.error || 'нет ответа'}), данные могут устареть`, true);
const hint = state.trafficEdges.length > QUIET_AT ? ' — наведите на карточку, чтобы увидеть её трафик' : '';
if (!f.connected) return;
const more = hiddenPeers ? `; ещё ${plural(hiddenPeers, 'новое направление', 'новых направления', 'новых направлений')} — «Обновить»` : '';
setStatus(state.trafficEdges.length
? `трафик за ${win}: ${plural(state.trafficEdges.length, 'направление', 'направления', 'направлений')}, ${plural(total, 'соединение', 'соединения', 'соединений')}${hint}`
: `трафика за ${win} не видно`);
? `трафик за ${win}: ${plural(state.trafficEdges.length, 'направление', 'направления', 'направлений')}, ${plural(total, 'соединение', 'соединения', 'соединений')}${hint}${more}`
: `трафика за ${win} не видно${more}`);
}
}
function fmtDuration(sec) {
if (sec < 90) return plural(Math.max(1, Math.round(sec)), 'секунду', 'секунды', 'секунд');
if (sec < 5400) return Math.round(sec / 60) + ' мин';
return plural(Math.round(sec / 3600), 'час', 'часа', 'часов');
}
function setStatus(text, isError = false) {
const s = $('#stage-status');
s.textContent = text;
@@ -823,16 +887,24 @@ $('#zoom-in').addEventListener('click', () => cy.zoom({ level: cy.zoom() * 1.25,
$('#zoom-out').addEventListener('click', () => cy.zoom({ level: cy.zoom() / 1.25, renderedPosition: { x: cy.width() / 2, y: cy.height() / 2 } }));
$('#zoom-fit').addEventListener('click', fitView);
/* animated dashes on traffic edges */
/* Animated dashes on traffic edges. Any edge style change makes cytoscape
redraw the whole element layer (~15 ms for a 200-element node view), so
the animation is rationed: 12 fps, only highlighted edges in dense views
(no highlight -> no redraw at all), and asleep after a minute without
input. */
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)');
let dash = 0;
let lastTick = 0;
let lastInput = performance.now();
for (const ev of ['pointermove', 'wheel', 'keydown', 'pointerdown']) addEventListener(ev, () => { lastInput = performance.now(); }, { passive: true });
function tick(t) {
requestAnimationFrame(tick);
if (document.hidden || matchMedia('(prefers-reduced-motion: reduce)').matches || t - lastTick < 50) return;
if (document.hidden || reduceMotion.matches || t - lastTick < 80 || t - lastInput > 60000) return;
lastTick = t;
const edges = cy.edges('.traffic');
let edges = cy.edges('.traffic');
if (edges.length > QUIET_AT) edges = edges.filter('.hl');
if (!edges.length) return;
dash = (dash + 1.2) % 120;
dash = (dash + 2) % 120;
edges.style('line-dash-offset', -dash);
}
requestAnimationFrame(tick);
@@ -985,7 +1057,7 @@ function renderLinks(id) {
}
const tIn = [], tOut = [];
for (const e of state.trafficEdges || []) {
const meta = `${e.ports.join(', ')} · ${e.count}${e.dropped ? ` · drop ${e.dropped}` : ''}`;
const meta = `${e.ports.join(', ')} · ${e.count} соед.${e.fresh ? `, новых ${e.fresh}` : ''}${e.dropped ? ` · drop ${e.dropped}` : ''}`;
if (e.target === id) tIn.push(linkButton(e.source, meta, e.dropped));
if (e.source === id) tOut.push(linkButton(e.target, meta, e.dropped));
}