-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNodeDetailsSidePanel.tsx
More file actions
455 lines (430 loc) · 13.1 KB
/
Copy pathNodeDetailsSidePanel.tsx
File metadata and controls
455 lines (430 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import * as React from 'react';
import { StringParam, useQueryParam, withDefault } from 'use-query-params';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { convertDateRangeToGranularityString } from '@hyperdx/common-utils/dist/core/utils';
import { TSource } from '@hyperdx/common-utils/dist/types';
import {
Badge,
Card,
Drawer,
Flex,
Grid,
SegmentedControl,
Text,
} from '@mantine/core';
import {
convertV1ChartConfigToV2,
K8S_CPU_PERCENTAGE_NUMBER_FORMAT,
K8S_MEM_NUMBER_FORMAT,
} from '@/ChartUtils';
import { DBTimeChart } from '@/components/DBTimeChart';
import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils';
import { InfraPodsStatusTable } from '@/KubernetesDashboardPage';
import { getEventBody } from '@/source';
import { parseTimeQuery, useTimeQuery } from '@/timeQuery';
import { formatUptime } from '@/utils';
import { useZIndex, ZIndexContext } from '@/zIndex';
import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar';
import { useQueriedChartConfig } from './hooks/useChartConfig';
import { useGetKeyValues, useTableMetadata } from './hooks/useMetadata';
import styles from '../styles/LogSidePanel.module.scss';
const CHART_HEIGHT = 300;
const defaultTimeRange = parseTimeQuery('Past 1h', false);
const PodDetailsProperty = React.memo(
({ label, value }: { label: string; value?: React.ReactNode }) => {
if (!value) return null;
return (
<div className="pe-4">
<Text size="xs" color="gray">
{label}
</Text>
<Text size="sm">{value}</Text>
</div>
);
},
);
const NodeDetails = ({
name,
dateRange,
metricSource,
}: {
name: string;
dateRange: [Date, Date];
metricSource: TSource;
}) => {
const where = `${metricSource.resourceAttributesExpression}.k8s.node.name:"${name}"`;
const groupBy = ['k8s.node.name'];
const { data, isError, isLoading } = useQueriedChartConfig(
convertV1ChartConfigToV2(
{
series: [
{
table: 'metrics',
field: 'k8s.node.condition_ready - Gauge',
type: 'table',
aggFn: 'last_value',
where,
groupBy,
},
{
table: 'metrics',
field: 'k8s.node.uptime - Sum',
type: 'table',
aggFn: undefined,
where,
groupBy,
},
],
dateRange,
seriesReturnType: 'column',
},
{
metric: metricSource,
},
),
);
const properties = React.useMemo(() => {
if (!data) {
return {};
}
return {
ready: data.data?.[0]?.['last_value(k8s.node.condition_ready)'],
uptime: data.data?.[0]?.['undefined(k8s.node.uptime)'],
};
}, [data]);
return (
<Grid.Col span={12}>
<div className="p-2 gap-2 d-flex flex-wrap">
<PodDetailsProperty label="Node" value={name} />
{properties.ready !== undefined && (
<PodDetailsProperty
label="Status"
value={
properties.ready === 1 ? (
<Badge
variant="light"
color="green"
fw="normal"
tt="none"
size="md"
>
Ready
</Badge>
) : (
<Badge
variant="light"
color="red"
fw="normal"
tt="none"
size="md"
>
Not Ready
</Badge>
)
}
/>
)}
{properties.uptime && (
<PodDetailsProperty
label="Uptime"
value={formatUptime(properties.uptime)}
/>
)}
</div>
</Grid.Col>
);
};
function NodeLogs({
dateRange,
logSource,
where,
}: {
dateRange: [Date, Date];
logSource: TSource;
where: string;
}) {
const [resultType, setResultType] = React.useState<'all' | 'error'>('all');
const _where = where + (resultType === 'error' ? ' Severity:err' : '');
return (
<Card p="md">
<Card.Section p="md" py="xs">
<Flex justify="space-between" align="center">
Latest Node Logs & Spans
<Flex gap="xs" align="center">
<SegmentedControl
size="xs"
value={resultType}
onChange={(value: string) => {
if (value === 'all' || value === 'error') {
setResultType(value);
}
}}
data={[
{ label: 'All', value: 'all' },
{ label: 'Errors', value: 'error' },
]}
/>
{/*
<Link
href={`/search?q=${encodeURIComponent(_where)}`}
passHref
legacyBehavior
>
<Anchor size="xs" color="dimmed">
Search <IconExternalLink size={12} style={{ display: 'inline' }} />
</Anchor>
</Link>
*/}
</Flex>
</Flex>
</Card.Section>
<Card.Section p="md" py="sm" h={CHART_HEIGHT}>
<DBSqlRowTableWithSideBar
sourceId={logSource.id}
isNestedPanel
breadcrumbPath={[{ label: 'Node Details' }]}
config={{
...logSource,
where: _where,
whereLanguage: 'lucene',
select: [
{
valueExpression: logSource.timestampValueExpression,
alias: 'Timestamp',
},
{
valueExpression: `${logSource.severityTextExpression}`,
alias: 'Severity',
},
{
valueExpression: `${logSource.serviceNameExpression}`,
alias: 'Service',
},
{
valueExpression: `${getEventBody(logSource)}`,
alias: 'Message',
},
],
orderBy: [
{
valueExpression: logSource.timestampValueExpression,
ordering: 'DESC',
},
],
limit: { limit: 200, offset: 0 },
dateRange,
}}
isLive={false}
queryKeyPrefix="k8s-dashboard-node-logs"
/>
</Card.Section>
</Card>
);
}
export default function NodeDetailsSidePanel({
metricSource,
logSource,
}: {
metricSource: TSource;
logSource: TSource;
}) {
const [nodeName, setNodeName] = useQueryParam(
'nodeName',
withDefault(StringParam, ''),
{
updateType: 'replaceIn',
},
);
const contextZIndex = useZIndex();
const drawerZIndex = contextZIndex + 10;
const metricsWhere = React.useMemo(() => {
return `${metricSource?.resourceAttributesExpression}.k8s.node.name:"${nodeName}"`;
}, [nodeName, metricSource]);
const { searchedTimeRange: dateRange } = useTimeQuery({
defaultValue: 'Past 1h',
defaultTimeRange: [
defaultTimeRange?.[0]?.getTime() ?? -1,
defaultTimeRange?.[1]?.getTime() ?? -1,
],
});
const { data: logsTableMetadata } = useTableMetadata(tcFromSource(logSource));
let doesPrimaryOrSortingKeysContainServiceExpression = false;
if (
logSource?.serviceNameExpression &&
(logsTableMetadata?.primary_key || logsTableMetadata?.sorting_key)
) {
if (
logsTableMetadata.primary_key &&
logsTableMetadata.primary_key.includes(logSource.serviceNameExpression)
) {
doesPrimaryOrSortingKeysContainServiceExpression = true;
} else if (
logsTableMetadata.sorting_key &&
logsTableMetadata.sorting_key.includes(logSource.serviceNameExpression)
) {
doesPrimaryOrSortingKeysContainServiceExpression = true;
}
}
const { data: logServiceNames } = useGetKeyValues(
{
chartConfig: {
from: logSource.from,
where: `${logSource?.resourceAttributesExpression}.k8s.node.name:"${nodeName}"`,
whereLanguage: 'lucene',
select: '',
timestampValueExpression: logSource.timestampValueExpression ?? '',
connection: logSource.connection,
dateRange,
},
keys: [logSource.serviceNameExpression ?? ''],
limit: 10,
disableRowLimit: false,
},
{
enabled:
!!nodeName &&
!!logSource.serviceNameExpression &&
doesPrimaryOrSortingKeysContainServiceExpression,
},
);
// HACK: craft where clause for logs given the ServiceName is part of the primary key
const logsWhere = React.useMemo(() => {
const _where = `${logSource?.resourceAttributesExpression}.k8s.node.name:"${nodeName}"`;
if (
logServiceNames &&
logServiceNames[0].value.length > 0 &&
doesPrimaryOrSortingKeysContainServiceExpression
) {
const _svs: string[] = logServiceNames[0].value;
const _key = logServiceNames[0].key;
return `(${_svs
.map(sv => `${_key}:"${sv}"`)
.join(' OR ')}) AND ${_where}`;
}
return _where;
}, [
nodeName,
logSource,
doesPrimaryOrSortingKeysContainServiceExpression,
logServiceNames,
]);
const handleClose = React.useCallback(() => {
setNodeName(undefined);
}, [setNodeName]);
if (!nodeName) {
return null;
}
return (
<Drawer
opened={!!nodeName}
onClose={handleClose}
position="right"
size="80vw"
withCloseButton={false}
zIndex={drawerZIndex}
styles={{
body: {
padding: 0,
},
}}
>
<ZIndexContext.Provider value={drawerZIndex}>
<div className={styles.panel} data-testid="k8s-node-details-panel">
<DrawerHeader
header={`Details for ${nodeName}`}
onClose={handleClose}
/>
<DrawerBody>
<Grid>
<NodeDetails
name={nodeName}
dateRange={dateRange}
metricSource={metricSource}
/>
<Grid.Col span={6}>
<Card p="md" data-testid="nodes-details-cpu-usage-chart">
<Card.Section p="md" py="sm" h={CHART_HEIGHT}>
<DBTimeChart
title="CPU Usage by Pod"
config={convertV1ChartConfigToV2(
{
dateRange,
granularity:
convertDateRangeToGranularityString(dateRange),
seriesReturnType: 'column',
series: [
{
type: 'time',
groupBy: ['k8s.pod.name'],
where: metricsWhere,
table: 'metrics',
aggFn: 'avg',
field: 'k8s.pod.cpu.utilization - Gauge',
numberFormat: K8S_CPU_PERCENTAGE_NUMBER_FORMAT,
},
],
},
{
metric: metricSource,
},
)}
/>
</Card.Section>
</Card>
</Grid.Col>
<Grid.Col span={6} data-testid="nodes-details-memory-usage-chart">
<Card p="md">
<Card.Section p="md" py="sm" h={CHART_HEIGHT}>
<DBTimeChart
title="Memory Usage by Pod"
config={convertV1ChartConfigToV2(
{
dateRange,
granularity:
convertDateRangeToGranularityString(dateRange),
seriesReturnType: 'column',
series: [
{
type: 'time',
groupBy: ['k8s.pod.name'],
where: metricsWhere,
table: 'metrics',
aggFn: 'avg',
field: 'k8s.pod.memory.usage - Gauge',
numberFormat: K8S_MEM_NUMBER_FORMAT,
},
],
},
{
metric: metricSource,
},
)}
/>
</Card.Section>
</Card>
</Grid.Col>
<Grid.Col span={12}>
{metricSource && (
<InfraPodsStatusTable
metricSource={metricSource}
dateRange={dateRange}
where={metricsWhere}
/>
)}
</Grid.Col>
<Grid.Col span={12}>
{logSource && (
<NodeLogs
where={logsWhere}
dateRange={dateRange}
logSource={logSource}
/>
)}
</Grid.Col>
</Grid>
</DrawerBody>
</div>
</ZIndexContext.Provider>
</Drawer>
);
}