-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Display alerts on cluster overview, pods table and pod page
- Loading branch information
1 parent
9e9a5f3
commit 167b72d
Showing
15 changed files
with
472 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from "react"; | ||
import { SceneComponentProps, SceneFlexLayout, SceneObjectBase, SceneObjectState } from "@grafana/scenes"; | ||
import { TableRow } from "./types"; | ||
import { TagList, useStyles2 } from "@grafana/ui"; | ||
import { isString } from "lodash"; | ||
import { GrafanaTheme2 } from "@grafana/data"; | ||
import { css } from "@emotion/css"; | ||
|
||
const getStyles = (theme: GrafanaTheme2) => ({ | ||
justifyStart: css` | ||
justify-content: flex-start; | ||
`, | ||
}); | ||
|
||
interface SceneTagListState extends SceneObjectState { | ||
tags: string[]; | ||
} | ||
|
||
class SceneTagList extends SceneObjectBase<SceneTagListState> { | ||
static Component = (props: SceneComponentProps<SceneTagList>) => { | ||
const styles = useStyles2(getStyles); | ||
const { tags } = props.model.useState(); | ||
return (<TagList className={styles.justifyStart} tags={tags}/>) | ||
} | ||
} | ||
|
||
const KNOWN_LABELS = [ | ||
'alertname', | ||
'severity', | ||
'alertstate', | ||
'cluster', | ||
'namespace', | ||
] | ||
|
||
function isKnownLabelKey(key: string) { | ||
return KNOWN_LABELS.includes(key); | ||
} | ||
|
||
export function expandedRowSceneBuilder(rowIdBuilder: (row: TableRow) => string) { | ||
|
||
return (row: TableRow) => { | ||
|
||
const tags: string[] = [] | ||
Object.entries(row).sort().map(([key, value]) => { | ||
if (isString(value) && value.length > 0 && !isKnownLabelKey(key) && !key.startsWith('__')) { | ||
tags.push(`${key}=${value}`); | ||
} | ||
}); | ||
|
||
return new SceneFlexLayout({ | ||
key: rowIdBuilder(row), | ||
width: '100%', | ||
height: 500, | ||
children: [ | ||
new SceneTagList({ | ||
tags: tags | ||
}), | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import { | ||
EmbeddedScene, | ||
SceneFlexLayout, | ||
SceneFlexItem, | ||
SceneQueryRunner, | ||
TextBoxVariable, | ||
SceneVariableSet, | ||
VariableValueSelectors, | ||
SceneVariables, | ||
} from '@grafana/scenes'; | ||
import { createNamespaceVariable, resolveVariable } from 'common/variableHelpers'; | ||
import { SortingState } from 'common/sortingHelpers'; | ||
import { AsyncTable, Column, ColumnSortingConfig, QueryBuilder } from 'components/AsyncTable'; | ||
import { TextColor } from 'common/types'; | ||
import { TableRow } from './types'; | ||
import { alertLabelValues } from './utils'; | ||
import { expandedRowSceneBuilder } from './AlertExpandedRow'; | ||
import { LabelFilters, serializeLabelFilters } from 'common/queryHelpers'; | ||
|
||
const KNOWN_SEVERITIES = ['critical', 'high', 'warning', 'info']; | ||
|
||
interface SeverityColors { | ||
[key: string]: TextColor; | ||
} | ||
|
||
const KNOWN_SEVERITY_COLORS: SeverityColors = { | ||
'critical': 'error', | ||
'high': 'warning', | ||
'warning': 'warning', | ||
'info': 'primary', | ||
} | ||
|
||
const namespaceVariable = createNamespaceVariable(); | ||
|
||
const searchVariable = new TextBoxVariable({ | ||
name: 'search', | ||
label: 'Search', | ||
value: '', | ||
}); | ||
|
||
const columns: Array<Column<TableRow>> = [ | ||
{ | ||
id: 'alertname', | ||
header: 'ALERT NAME', | ||
accessor: (row: TableRow) => row.alertname, | ||
cellProps: { | ||
color: (row: TableRow) => KNOWN_SEVERITY_COLORS[row.severity], | ||
}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'label', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.alertname.localeCompare(b.alertname) : b.alertname.localeCompare(a.alertname); | ||
} | ||
}, | ||
}, | ||
{ | ||
id: 'alertstate', | ||
header: 'STATE', | ||
accessor: (row: TableRow) => row.alertstate.toLocaleUpperCase(), | ||
cellProps: {}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'label', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.alertstate.localeCompare(b.alertstate) : b.alertstate.localeCompare(a.alertstate); | ||
} | ||
}, | ||
}, | ||
{ | ||
id: 'namespace', | ||
header: 'NAMESPACE', | ||
accessor: (row: TableRow) => row.namespace, | ||
cellType: 'link', | ||
cellProps: {}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'label', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.namespace.localeCompare(b.namespace) : b.namespace.localeCompare(a.namespace); | ||
} | ||
}, | ||
}, | ||
{ | ||
id: 'cluster', | ||
header: 'CLUSTER', | ||
accessor: (row: TableRow) => row.cluster, | ||
cellType: 'link', | ||
cellProps: {}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'label', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.cluster.localeCompare(b.cluster) : b.cluster.localeCompare(a.cluster); | ||
} | ||
}, | ||
}, | ||
{ | ||
id: 'severity', | ||
header: 'SEVERITY', | ||
accessor: (row: TableRow) => row.severity.toLocaleUpperCase(), | ||
cellProps: { | ||
color: (row: TableRow) => KNOWN_SEVERITY_COLORS[row.severity], | ||
}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'label', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.severity.localeCompare(b.severity) : b.severity.localeCompare(a.severity); | ||
} | ||
}, | ||
}, | ||
{ | ||
id: 'Value', | ||
header: 'AGE', | ||
accessor: (row: TableRow) => Date.now()/1000 - row.Value, | ||
cellType: 'formatted', | ||
cellProps: { | ||
format: 'dtdurations' | ||
}, | ||
sortingConfig: { | ||
enabled: true, | ||
type: 'value', | ||
local: true, | ||
compare: (a, b, direction) => { | ||
return direction === 'asc' ? a.Value - b.Value : b.Value - a.Value; | ||
} | ||
} | ||
} | ||
] | ||
|
||
const serieMatcherPredicate = (row: TableRow) => (value: any) => value.alertname === row.alertname; | ||
|
||
function rowMapper(row: TableRow, asyncRowData: any) { | ||
|
||
} | ||
|
||
function createRowId(row: TableRow) { | ||
return alertLabelValues(row).join('/'); | ||
} | ||
|
||
class AlertsQueryBuilder implements QueryBuilder<TableRow> { | ||
|
||
constructor(private labelFilters?: LabelFilters) {} | ||
|
||
rootQueryBuilder(variables: SceneVariableSet | SceneVariables, sorting: SortingState, sortingConfig?: ColumnSortingConfig<TableRow> | undefined) { | ||
|
||
const serializedFilters = this.labelFilters ? serializeLabelFilters(this.labelFilters) : ''; | ||
const hasNamespaceVariable = variables.getByName('namespace') !== undefined; | ||
|
||
const finalQuery = ` | ||
ALERTS{ | ||
cluster="$cluster", | ||
${ hasNamespaceVariable ? `namespace=~"$namespace",` : '' } | ||
alertstate="firing", | ||
${serializedFilters} | ||
} | ||
* ignoring(alertstate) group_right(alertstate) ALERTS_FOR_STATE{ | ||
cluster="$cluster", | ||
${ hasNamespaceVariable ? `namespace=~"$namespace",` : '' } | ||
${serializedFilters} | ||
} | ||
` | ||
|
||
return new SceneQueryRunner({ | ||
datasource: { | ||
uid: 'prometheus', | ||
type: 'prometheus', | ||
}, | ||
queries: [ | ||
{ | ||
refId: 'namespaces', | ||
expr: finalQuery, | ||
instant: true, | ||
format: 'table' | ||
} | ||
], | ||
}); | ||
} | ||
|
||
rowQueryBuilder(rows: TableRow[], variables: SceneVariableSet | SceneVariables) { | ||
return [] | ||
} | ||
} | ||
|
||
export function AlertsTable(labelFilters?: LabelFilters, showVariableControls = true, shouldCreateVariables = true) { | ||
|
||
const variables = new SceneVariableSet({ | ||
variables: shouldCreateVariables ? [ | ||
namespaceVariable, | ||
searchVariable, | ||
]: [] | ||
}) | ||
|
||
const controls = showVariableControls ? [ | ||
new VariableValueSelectors({}) | ||
] : []; | ||
|
||
const defaultSorting: SortingState = { | ||
columnId: 'alertname', | ||
direction: 'asc' | ||
} | ||
|
||
const queryBuilder = new AlertsQueryBuilder(labelFilters); | ||
|
||
return new EmbeddedScene({ | ||
$variables: variables, | ||
controls: controls, | ||
body: new SceneFlexLayout({ | ||
children: [ | ||
new SceneFlexItem({ | ||
width: '100%', | ||
height: '100%', | ||
body: new AsyncTable<TableRow>({ | ||
columns: columns, | ||
createRowId: createRowId, | ||
asyncDataRowMapper: rowMapper, | ||
$data: queryBuilder.rootQueryBuilder(variables, defaultSorting), | ||
queryBuilder: queryBuilder, | ||
expandedRowBuilder: expandedRowSceneBuilder(createRowId) | ||
}), | ||
}), | ||
], | ||
}), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface TableRow { | ||
cluster: string; | ||
namespace: string; | ||
alertname: string; | ||
severity: string; | ||
alertstate: string; | ||
Value: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { isString } from "lodash"; | ||
import { TableRow } from "./types"; | ||
|
||
export function alertLabelValues(row: TableRow) { | ||
const labels: any[] = [] | ||
Object.entries(row).sort().map(([key, value]) => { | ||
if (isString(value) && value.length > 0) { | ||
labels.push(value); | ||
} | ||
}); | ||
return labels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.