Skip to content

Commit 0da6a57

Browse files
committed
refactor: update host metrics to support dynamic schema selection and include wait state in CPU idle calculation
1 parent a44e422 commit 0da6a57

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

x-pack/solutions/observability/plugins/infra/public/components/asset_details/components/kpis/host_kpi_charts.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React from 'react';
99
import { EuiFlexItem } from '@elastic/eui';
1010
import type { DataView } from '@kbn/data-views-plugin/public';
1111
import type { Filter, Query, TimeRange } from '@kbn/es-query';
12+
import type { DataSchemaFormat } from '@kbn/metrics-data-access-plugin/common';
1213
import { Kpi } from './kpi';
1314
import { useHostKpiCharts } from '../../hooks/use_host_metrics_charts';
1415

@@ -20,6 +21,8 @@ export interface HostKpiChartsProps {
2021
lastReloadRequestTime?: number;
2122
getSubtitle?: (formulaValue: string) => string;
2223
loading?: boolean;
24+
25+
schema?: DataSchemaFormat | null;
2326
}
2427

2528
export const HostKpiCharts = ({
@@ -30,10 +33,12 @@ export const HostKpiCharts = ({
3033
query,
3134
lastReloadRequestTime,
3235
loading = false,
36+
schema,
3337
}: HostKpiChartsProps) => {
3438
const charts = useHostKpiCharts({
3539
dataViewId: dataView?.id,
3640
getSubtitle,
41+
schema,
3742
});
3843

3944
return (

x-pack/solutions/observability/plugins/infra/public/components/asset_details/hooks/use_host_metrics_charts.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,27 @@
66
*/
77

88
import { i18n } from '@kbn/i18n';
9-
import { findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
9+
import { DataSchemaFormat, findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
1010
import useAsync from 'react-use/lib/useAsync';
11-
import type { SchemaTypes } from '../../../../common/http_api/shared/schema_type';
12-
import { usePluginConfig } from '../../../containers/plugin_config_context';
1311
import type { HostMetricTypes } from '../charts/types';
1412
import { useChartSeriesColor } from './use_chart_series_color';
1513

1614
export const useHostCharts = ({
1715
metric,
1816
dataViewId,
1917
overview,
18+
schema,
2019
}: {
2120
metric: HostMetricTypes;
2221
dataViewId?: string;
2322
overview?: boolean;
23+
schema?: DataSchemaFormat | null;
2424
}) => {
25-
const config = usePluginConfig();
26-
2725
const { value: charts = [], error } = useAsync(async () => {
2826
const hostCharts = await getHostsCharts({
2927
metric,
3028
overview,
31-
schema: config.featureFlags.hostOtelEnabled ? 'semconv' : 'ecs',
29+
schema: schema ?? DataSchemaFormat.ECS,
3230
});
3331

3432
return hostCharts.map((chart) => ({
@@ -39,24 +37,25 @@ export const useHostCharts = ({
3937
},
4038
}),
4139
}));
42-
}, [config.featureFlags.hostOtelEnabled, dataViewId, metric, overview]);
40+
}, [dataViewId, metric, overview, schema]);
4341

4442
return { charts, error };
4543
};
4644

4745
export const useKubernetesCharts = ({
4846
dataViewId,
4947
overview,
48+
schema,
5049
}: {
5150
dataViewId?: string;
5251
overview?: boolean;
52+
schema?: DataSchemaFormat | null;
5353
}) => {
5454
const model = findInventoryModel('host');
55-
const config = usePluginConfig();
5655

5756
const { value: charts = [], error } = useAsync(async () => {
5857
const { kubernetesNode } = await model.metrics.getCharts({
59-
schema: config.featureFlags.hostOtelEnabled ? 'semconv' : 'ecs',
58+
schema: schema ?? DataSchemaFormat.ECS,
6059
});
6160

6261
if (!kubernetesNode) {
@@ -82,7 +81,7 @@ export const useKubernetesCharts = ({
8281
}),
8382
};
8483
});
85-
}, [model.metrics, config.featureFlags.hostOtelEnabled, overview, dataViewId]);
84+
}, [model.metrics, schema, overview, dataViewId]);
8685

8786
return { charts, error };
8887
};
@@ -98,17 +97,19 @@ export const useHostKpiCharts = ({
9897
dataViewId,
9998
seriesColor,
10099
getSubtitle,
100+
schema,
101101
}: {
102102
dataViewId?: string;
103103
seriesColor?: string;
104104
getSubtitle?: (formulaValue: string) => string;
105+
schema?: DataSchemaFormat | null;
105106
}) => {
106107
seriesColor = useChartSeriesColor(seriesColor);
107-
const config = usePluginConfig();
108+
108109
const { value: charts = [] } = useAsync(async () => {
109110
const model = findInventoryModel('host');
110111
const { cpu, memory, disk } = await model.metrics.getCharts({
111-
schema: config.featureFlags.hostOtelEnabled ? 'semconv' : 'ecs',
112+
schema: schema ?? DataSchemaFormat.ECS,
112113
});
113114

114115
return [
@@ -127,7 +128,7 @@ export const useHostKpiCharts = ({
127128
},
128129
}),
129130
}));
130-
}, [dataViewId, seriesColor, getSubtitle, config.featureFlags.hostOtelEnabled]);
131+
}, [dataViewId, seriesColor, getSubtitle, schema]);
131132

132133
return charts;
133134
};
@@ -139,11 +140,13 @@ const getHostsCharts = async ({
139140
}: {
140141
metric: HostMetricTypes;
141142
overview?: boolean;
142-
schema: SchemaTypes;
143+
schema?: DataSchemaFormat | null;
143144
}) => {
144145
const model = findInventoryModel('host');
145146

146-
const { cpu, memory, network, disk, logs } = await model.metrics.getCharts({ schema });
147+
const { cpu, memory, network, disk, logs } = await model.metrics.getCharts({
148+
schema: schema ?? DataSchemaFormat.ECS,
149+
});
147150

148151
switch (metric) {
149152
case 'cpu':

x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/kpis/kpi_charts.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const KpiCharts = () => {
8080
lastReloadRequestTime={afterLoadedState.reloadRequestTime}
8181
getSubtitle={afterLoadedState.getSubtitle}
8282
loading={loading}
83+
schema={searchCriteria.preferredSchema}
8384
/>
8485
);
8586
};

x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/components/tabs/metrics/metrics_grid.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ import { Chart } from './chart';
1212
import { Popover } from '../../common/popover';
1313
import { useMetricsDataViewContext } from '../../../../../../containers/metrics_source';
1414
import { useMetricsCharts } from '../../../hooks/use_metrics_charts';
15+
import { useUnifiedSearchContext } from '../../../hooks/use_unified_search';
1516

1617
export const MetricsGrid = () => {
1718
const { metricsView } = useMetricsDataViewContext();
18-
const charts = useMetricsCharts({ dataViewId: metricsView?.dataViewReference.id });
19+
const { searchCriteria } = useUnifiedSearchContext();
20+
const charts = useMetricsCharts({
21+
dataViewId: metricsView?.dataViewReference.id,
22+
schema: searchCriteria.preferredSchema,
23+
});
1924

2025
return (
2126
<>

x-pack/solutions/observability/plugins/infra/public/pages/metrics/hosts/hooks/use_metrics_charts.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77

88
import useAsync from 'react-use/lib/useAsync';
99
import type { LensBreakdownConfig } from '@kbn/lens-embeddable-utils/config_builder';
10+
import { DataSchemaFormat } from '@kbn/metrics-data-access-plugin/common';
1011
import { findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
11-
import { usePluginConfig } from '../../../../containers/plugin_config_context';
1212
import { PAGE_SIZE_OPTIONS } from '../constants';
1313

14-
export const useMetricsCharts = ({ dataViewId }: { dataViewId?: string }) => {
15-
const config = usePluginConfig();
16-
14+
export const useMetricsCharts = ({
15+
dataViewId,
16+
schema,
17+
}: {
18+
dataViewId?: string;
19+
schema?: DataSchemaFormat | null;
20+
}) => {
1721
const { value: charts = [] } = useAsync(async () => {
1822
const model = findInventoryModel('host');
1923
const { cpu, disk, memory, network } = await model.metrics.getCharts({
20-
schema: config.featureFlags.hostOtelEnabled ? 'semconv' : 'ecs',
24+
schema: schema ?? DataSchemaFormat.ECS,
2125
});
2226

2327
return [
@@ -52,7 +56,7 @@ export const useMetricsCharts = ({ dataViewId }: { dataViewId?: string }) => {
5256
},
5357
}),
5458
}));
55-
}, [dataViewId, config.featureFlags.hostOtelEnabled]);
59+
}, [schema, dataViewId]);
5660

5761
return charts;
5862
};

x-pack/solutions/observability/plugins/metrics_data_access/common/inventory_models/host/metrics/charts/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { init as initMemoryCharts } from './memory';
1111
import { init as initNetworkCharts } from './network';
1212
import { logs } from './logs';
1313
import { charts as kubernetesNodeCharts } from '../../../kubernetes/node/metrics';
14-
import type { ChartsConfigMap, FormulasCatalog } from '../../../shared/metrics/types';
14+
import {
15+
DataSchemaFormat,
16+
type ChartsConfigMap,
17+
type FormulasCatalog,
18+
} from '../../../shared/metrics/types';
1519
import type { HostFormulas } from '../formulas';
1620

1721
export const initCharts = (formulas: FormulasCatalog<HostFormulas>) => {
@@ -21,7 +25,9 @@ export const initCharts = (formulas: FormulasCatalog<HostFormulas>) => {
2125
memory: initMemoryCharts(formulas),
2226
network: initNetworkCharts(formulas),
2327
logs,
24-
...(formulas.schema === 'ecs' ? { kubernetesNode: kubernetesNodeCharts.node } : {}),
28+
...(formulas.schema === DataSchemaFormat.ECS
29+
? { kubernetesNode: kubernetesNodeCharts.node }
30+
: {}),
2531
} satisfies ChartsConfigMap;
2632
};
2733
export type HostCharts = ReturnType<typeof initCharts>;

x-pack/solutions/observability/plugins/metrics_data_access/common/inventory_models/host/metrics/snapshot/cpu_v2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const cpuV2: SchemaBasedAggregations = {
1919
cpu_idle: {
2020
terms: {
2121
field: 'state',
22-
include: ['idle'],
22+
include: ['idle', 'wait'],
2323
},
2424
aggs: {
2525
avg: {

0 commit comments

Comments
 (0)