-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AO] Create a new rule under Observability plugin based on the metric…
…s threshold rule (#158665) ## Summary ### NOTE: This is a draft PR to MVP the new rule combination (Threshold). More PRs to follow up It fixes #158260 by providing the _new_ **Threshold rule** It fixes #159326 <img width="586" alt="Screenshot 2023-05-30 at 17 55 32" src="https://github.com/elastic/kibana/assets/6838659/0e485266-d93f-442e-81f4-77aa673ed497"> ## ✅ Done - [x] Clone the Metric threshold and update the imports - [x] The rule is listed in the rule creation flyout with its params and preview charts - [x] Working Rule registry - [x] Working Rule executor - [x] Working feature id in the rule registry - [x] Working alerts table and alert summary - [x] Use DataView instead of the Metrics indices under settings - [x] Update the i18n keys - [x] Fix/Update failing checks/tests. Green CI ✅ - [x] Hide it behind a feature flag `xpack.observability.unsafe.thresholdRule.enabled` ## 🏗️ To be done (could be irrelevant, or create a separate issue for it): - [ ] <del> Remove the `metrics` word </del> - [ ] <del> Update file and variable names to match the new rule context.</del> - [ ] <del> Rearrange files, constants, and exports </del> ## 🎯 DoD Having the rule working like the Metric threshold one and seeing its related alerts. ---------
- Loading branch information
Showing
133 changed files
with
13,493 additions
and
50 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
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
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
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
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
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/observability/common/threshold_rule/color_palette.ts
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,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { difference, first, values } from 'lodash'; | ||
import { euiPaletteColorBlind } from '@elastic/eui'; | ||
|
||
export enum Color { | ||
color0 = 'color0', | ||
color1 = 'color1', | ||
color2 = 'color2', | ||
color3 = 'color3', | ||
color4 = 'color4', | ||
color5 = 'color5', | ||
color6 = 'color6', | ||
color7 = 'color7', | ||
color8 = 'color8', | ||
color9 = 'color9', | ||
} | ||
|
||
export type Palette = { | ||
[K in keyof typeof Color]: string; | ||
}; | ||
|
||
const euiPalette = euiPaletteColorBlind(); | ||
|
||
export const defaultPalette: Palette = { | ||
[Color.color0]: euiPalette[1], // (blue) | ||
[Color.color1]: euiPalette[2], // (pink) | ||
[Color.color2]: euiPalette[0], // (green-ish) | ||
[Color.color3]: euiPalette[3], // (purple) | ||
[Color.color4]: euiPalette[4], // (light pink) | ||
[Color.color5]: euiPalette[5], // (yellow) | ||
[Color.color6]: euiPalette[6], // (tan) | ||
[Color.color7]: euiPalette[7], // (orange) | ||
[Color.color8]: euiPalette[8], // (brown) | ||
[Color.color9]: euiPalette[9], // (red) | ||
}; | ||
|
||
export const createPaletteTransformer = (palette: Palette) => (color: Color) => palette[color]; | ||
|
||
export const colorTransformer = createPaletteTransformer(defaultPalette); | ||
|
||
export const sampleColor = (usedColors: Color[] = []): Color => { | ||
const available = difference(values(Color) as Color[], usedColors); | ||
return first(available) || Color.color0; | ||
}; |
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/observability/common/threshold_rule/constants.ts
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,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const SNAPSHOT_CUSTOM_AGGREGATIONS = ['avg', 'max', 'min', 'rate'] as const; | ||
export const TIMESTAMP_FIELD = '@timestamp'; | ||
export const METRIC_EXPLORER_AGGREGATIONS = [ | ||
'avg', | ||
'max', | ||
'min', | ||
'cardinality', | ||
'rate', | ||
'count', | ||
'sum', | ||
'p95', | ||
'p99', | ||
'custom', | ||
] as const; |
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/observability/common/threshold_rule/formatters/bytes.test.ts
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,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { InfraWaffleMapDataFormat } from './types'; | ||
import { createBytesFormatter } from './bytes'; | ||
|
||
describe('createDataFormatter', () => { | ||
it('should format bytes as bytesDecimal', () => { | ||
const formatter = createBytesFormatter(InfraWaffleMapDataFormat.bytesDecimal); | ||
expect(formatter(1000000)).toBe('1 MB'); | ||
}); | ||
it('should format bytes as bitsDecimal', () => { | ||
const formatter = createBytesFormatter(InfraWaffleMapDataFormat.bitsDecimal); | ||
expect(formatter(1000000)).toBe('8 Mbit'); | ||
}); | ||
it('should format bytes as abbreviatedNumber', () => { | ||
const formatter = createBytesFormatter(InfraWaffleMapDataFormat.abbreviatedNumber); | ||
expect(formatter(1000000)).toBe('1 M'); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
x-pack/plugins/observability/common/threshold_rule/formatters/bytes.ts
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,53 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { formatNumber } from './number'; | ||
import { InfraWaffleMapDataFormat } from './types'; | ||
|
||
/** | ||
* The labels are derived from these two Wikipedia articles. | ||
* https://en.wikipedia.org/wiki/Kilobit | ||
* https://en.wikipedia.org/wiki/Kilobyte | ||
*/ | ||
const LABELS = { | ||
[InfraWaffleMapDataFormat.bytesDecimal]: ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], | ||
[InfraWaffleMapDataFormat.bitsDecimal]: [ | ||
'bit', | ||
'kbit', | ||
'Mbit', | ||
'Gbit', | ||
'Tbit', | ||
'Pbit', | ||
'Ebit', | ||
'Zbit', | ||
'Ybit', | ||
], | ||
[InfraWaffleMapDataFormat.abbreviatedNumber]: ['', 'K', 'M', 'B', 'T'], | ||
}; | ||
|
||
const BASES = { | ||
[InfraWaffleMapDataFormat.bytesDecimal]: 1000, | ||
[InfraWaffleMapDataFormat.bitsDecimal]: 1000, | ||
[InfraWaffleMapDataFormat.abbreviatedNumber]: 1000, | ||
}; | ||
|
||
/* | ||
* This formatter always assumes you're input is bytes and the output is a string | ||
* in whatever format you've defined. Bytes in Format Out. | ||
*/ | ||
export const createBytesFormatter = (format: InfraWaffleMapDataFormat) => (bytes: number) => { | ||
const labels = LABELS[format]; | ||
const base = BASES[format]; | ||
const value = format === InfraWaffleMapDataFormat.bitsDecimal ? bytes * 8 : bytes; | ||
// Use an exponential equation to get the power to determine which label to use. If the power | ||
// is greater then the max label then use the max label. | ||
const power = Math.min(Math.floor(Math.log(Math.abs(value)) / Math.log(base)), labels.length - 1); | ||
if (power < 0) { | ||
return `${formatNumber(value)} ${labels[0]}`; | ||
} | ||
return `${formatNumber(value / Math.pow(base, power))} ${labels[power]}`; | ||
}; |
18 changes: 18 additions & 0 deletions
18
x-pack/plugins/observability/common/threshold_rule/formatters/datetime.ts
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,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
export function localizedDate(dateTime: number | Date, locale: string = i18n.getLocale()) { | ||
const formatter = new Intl.DateTimeFormat(locale, { | ||
year: 'numeric', | ||
month: 'short', | ||
day: 'numeric', | ||
}); | ||
|
||
return formatter.format(dateTime); | ||
} |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/observability/common/threshold_rule/formatters/high_precision.ts
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 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const formatHighPrecision = (val: number) => { | ||
return Number(val).toLocaleString('en', { | ||
maximumFractionDigits: 5, | ||
}); | ||
}; |
37 changes: 37 additions & 0 deletions
37
x-pack/plugins/observability/common/threshold_rule/formatters/index.ts
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,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { createBytesFormatter } from './bytes'; | ||
import { formatNumber } from './number'; | ||
import { formatPercent } from './percent'; | ||
import { ThresholdFormatterType } from '../types'; | ||
import { formatHighPrecision } from './high_precision'; | ||
import { InfraWaffleMapDataFormat } from './types'; | ||
|
||
export const FORMATTERS = { | ||
number: formatNumber, | ||
// Because the implimentation for formatting large numbers is the same as formatting | ||
// bytes we are re-using the same code, we just format the number using the abbreviated number format. | ||
abbreviatedNumber: createBytesFormatter(InfraWaffleMapDataFormat.abbreviatedNumber), | ||
// bytes in bytes formatted string out | ||
bytes: createBytesFormatter(InfraWaffleMapDataFormat.bytesDecimal), | ||
// bytes in bits formatted string out | ||
bits: createBytesFormatter(InfraWaffleMapDataFormat.bitsDecimal), | ||
percent: formatPercent, | ||
highPrecision: formatHighPrecision, | ||
}; | ||
|
||
export const createFormatter = | ||
(format: ThresholdFormatterType, template: string = '{{value}}') => | ||
(val: string | number) => { | ||
if (val == null) { | ||
return ''; | ||
} | ||
const fmtFn = FORMATTERS[format]; | ||
const value = fmtFn(Number(val)); | ||
return template.replace(/{{value}}/g, value); | ||
}; |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/observability/common/threshold_rule/formatters/number.ts
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 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const formatNumber = (val: number) => { | ||
return Number(val).toLocaleString('en', { | ||
maximumFractionDigits: 1, | ||
}); | ||
}; |
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/observability/common/threshold_rule/formatters/percent.ts
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,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { formatNumber } from './number'; | ||
export const formatPercent = (val: number) => { | ||
return `${formatNumber(val * 100)}%`; | ||
}; |
Oops, something went wrong.