Skip to content

Commit 78ec1c6

Browse files
committed
only support queries with path provided
1 parent b09ffd3 commit 78ec1c6

File tree

3 files changed

+42
-50
lines changed

3 files changed

+42
-50
lines changed

client/src/components/prometheusModal/prometheusModal.tsx

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ function usePrometheusWebSocket() {
8989
console.log("📥 Received WebSocket message:", message);
9090

9191
if (message.type === "prometheusQuery" && message.data?.requestId) {
92-
console.log("🎯 Processing prometheus response for ID:", message.data.requestId);
92+
console.log(
93+
"🎯 Processing prometheus response for ID:",
94+
message.data.requestId
95+
);
9396
const pending = pendingRequests.current.get(message.data.requestId);
9497
if (pending) {
9598
pendingRequests.current.delete(message.data.requestId);
@@ -101,7 +104,10 @@ function usePrometheusWebSocket() {
101104
pending.resolve(message.data);
102105
}
103106
} else {
104-
console.warn("⚠️ No pending request found for ID:", message.data.requestId);
107+
console.warn(
108+
"⚠️ No pending request found for ID:",
109+
message.data.requestId
110+
);
105111
}
106112
}
107113
} catch (error) {
@@ -175,25 +181,19 @@ function usePrometheusWebSocket() {
175181
}
176182
}, 10000);
177183

178-
// Check if this is a full path (starts with http) or just a query
179-
const isFullPath =
180-
queryOrPath.startsWith("http://") ||
181-
queryOrPath.startsWith("https://");
182-
183-
console.log("🔍 Is full path:", isFullPath);
184-
185184
const message = {
186185
timestamp: new Date(),
187186
type: "prometheusQuery",
188187
data: {
189-
...(isFullPath
190-
? { fullPath: queryOrPath }
191-
: { query: queryOrPath }),
188+
fullPath: queryOrPath,
192189
requestId,
193190
},
194191
};
195192

196-
console.log("📨 Sending WebSocket message:", JSON.stringify(message, null, 2));
193+
console.log(
194+
"📨 Sending WebSocket message:",
195+
JSON.stringify(message, null, 2)
196+
);
197197

198198
try {
199199
ws.send(JSON.stringify(message));
@@ -224,17 +224,17 @@ export function PrometheusQueryBox({
224224

225225
// Hardcoded query definition wrapped in useMemo
226226
const hardcodedQuery = useMemo(() => {
227-
const query = `round((container_memory_usage_bytes{pod=~"${sessionName}.*",container="amalthea-session"} / container_spec_memory_limit_bytes{pod=~"${sessionName}.*",container="amalthea-session"}) * 100, 0.01) > 80`;
227+
const query = `ALERTS`;
228228
return {
229-
label: "Memory Usage",
229+
label: "Alerts for this session",
230230
query,
231231
path: `http://prometheus-server.monitoring.svc.cluster.local/api/v1/query?query=${encodeURIComponent(
232232
query
233233
)}`,
234-
description: "Memory usage percentage for this session",
234+
description: "Alerts for this session",
235235
icon: "memory",
236-
unit: "%",
237-
alertThreshold: 90,
236+
unit: "",
237+
alertThreshold: 0,
238238
};
239239
}, [sessionName]);
240240

@@ -255,20 +255,20 @@ export function PrometheusQueryBox({
255255
async (predefinedQuery: {
256256
label: string;
257257
query: string;
258-
path?: string;
258+
path: string;
259259
description?: string;
260260
icon?: string;
261261
unit: string;
262262
alertThreshold: number;
263263
}) => {
264-
if (!predefinedQuery.query.trim() && !predefinedQuery.path?.trim())
265-
return;
264+
if (!predefinedQuery.path?.trim()) return;
266265

267-
const queryToSend = predefinedQuery.path?.trim() || predefinedQuery.query.trim();
268-
console.log("📤 Sending query:", queryToSend);
266+
console.log("📤 Sending full path:", predefinedQuery.path);
269267

270268
try {
271-
const result = await sendPrometheusQueryRef.current(queryToSend);
269+
const result = await sendPrometheusQueryRef.current(
270+
predefinedQuery.path
271+
);
272272
return result;
273273
} catch (err) {
274274
return null;
@@ -310,6 +310,16 @@ export function PrometheusQueryBox({
310310
}
311311
}, [executeQuery, hardcodedQuery, sessionName]);
312312

313+
const getAlertDetails = useCallback(() => {
314+
if (queryResults.length === 0) return null;
315+
const result = queryResults[0];
316+
if (result.data.result.length === 0) return null;
317+
318+
const alertInfo = result.data.result[0].metric;
319+
const alertName = alertInfo.name;
320+
return alertName;
321+
}, [queryResults]);
322+
313323
const handleCloseButton = useCallback(() => {
314324
onClose();
315325
}, [onClose]);
@@ -320,6 +330,8 @@ export function PrometheusQueryBox({
320330

321331
const interval = setInterval(() => {
322332
getAllQueryResults();
333+
let alertDetails = getAlertDetails();
334+
console.log("🚨 Alert details:", alertDetails);
323335
}, 15000);
324336

325337
return () => {

server/src/websocket/handlers/prometheus.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,12 @@ export function handlerPrometheusQuery(
2929
channel: Channel,
3030
socket: ws
3131
): void {
32-
const { query, fullPath, requestId } = data;
32+
const { fullPath, requestId } = data;
3333

34-
// If fullPath is provided, use it directly
35-
if (fullPath && typeof fullPath === "string") {
36-
executePrometheusFullPath(fullPath as string, requestId as string, socket);
37-
return;
38-
}
39-
40-
// Otherwise, fall back to the original query logic
41-
if (!config.prometheus.url) {
42-
const errorMessage = new WsMessage(
43-
{
44-
error: "Prometheus server not configured",
45-
requestId,
46-
},
47-
"user",
48-
"prometheusQuery"
49-
).toString();
50-
socket.send(errorMessage);
51-
return;
52-
}
53-
54-
if (!query || typeof query !== "string") {
34+
if (!fullPath || typeof fullPath !== "string") {
5535
const errorMessage = new WsMessage(
5636
{
57-
error: "Missing required 'query' or 'fullPath' parameter",
37+
error: "Missing required 'fullPath' parameter",
5838
requestId,
5939
},
6040
"user",
@@ -64,7 +44,7 @@ export function handlerPrometheusQuery(
6444
return;
6545
}
6646

67-
executePrometheusQuery(query as string, requestId as string, socket);
47+
executePrometheusFullPath(fullPath as string, requestId as string, socket);
6848
}
6949

7050
async function executePrometheusFullPath(

server/src/websocket/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const acceptedMessages: Record<string, Array<MessageData>> = {
9090
],
9191
prometheusQuery: [
9292
{
93-
required: ["requestId"],
94-
optional: ["query", "fullPath"],
93+
required: ["fullPath", "requestId"],
94+
optional: null,
9595
handler: handlerPrometheusQuery,
9696
} as MessageData,
9797
],

0 commit comments

Comments
 (0)