-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra UI] Limit Metric Explorer fields (#43322)
* [Infra UI] Limit Metric Explorer fields - Closes #41090 - Closes #39613 - Adds allowed list for ECS, Promehteus, Kubernetes, and Docker fields - Filters "graph pre" fields by selected metrics - Only displays allowed fields for metric selection, graph per, and Kquery bar * Fixing test * Changing all caps to camel case * Fixing logic to be more clear and handle null use cases * Changing to singular
- Loading branch information
1 parent
f818587
commit 70166a0
Showing
15 changed files
with
326 additions
and
4 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
x-pack/legacy/plugins/infra/common/ecs_allowed_list.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,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
getAllowedListForPrefix, | ||
ECS_ALLOWED_LIST, | ||
K8S_ALLOWED_LIST, | ||
PROMETHEUS_ALLOWED_LIST, | ||
DOCKER_ALLOWED_LIST, | ||
} from './ecs_allowed_list'; | ||
describe('getAllowedListForPrefix()', () => { | ||
test('kubernetes', () => { | ||
expect(getAllowedListForPrefix('kubernetes.pod')).toEqual([ | ||
...ECS_ALLOWED_LIST, | ||
'kubernetes.pod', | ||
...K8S_ALLOWED_LIST, | ||
]); | ||
}); | ||
test('docker', () => { | ||
expect(getAllowedListForPrefix('docker.container')).toEqual([ | ||
...ECS_ALLOWED_LIST, | ||
'docker.container', | ||
...DOCKER_ALLOWED_LIST, | ||
]); | ||
}); | ||
test('prometheus', () => { | ||
expect(getAllowedListForPrefix('prometheus.metrics')).toEqual([ | ||
...ECS_ALLOWED_LIST, | ||
'prometheus.metrics', | ||
...PROMETHEUS_ALLOWED_LIST, | ||
]); | ||
}); | ||
test('anything.else', () => { | ||
expect(getAllowedListForPrefix('anything.else')).toEqual([ | ||
...ECS_ALLOWED_LIST, | ||
'anything.else', | ||
]); | ||
}); | ||
}); |
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 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { first } from 'lodash'; | ||
|
||
export const ECS_ALLOWED_LIST = [ | ||
'host', | ||
'cloud', | ||
'event', | ||
'agent', | ||
'fields', | ||
'service', | ||
'ecs', | ||
'metricset', | ||
'tags', | ||
'message', | ||
'labels', | ||
'@timestamp', | ||
'source', | ||
'container', | ||
]; | ||
|
||
export const K8S_ALLOWED_LIST = [ | ||
'kubernetes.pod.name', | ||
'kubernetes.pod.uid', | ||
'kubernetes.namespace', | ||
'kubernetes.node.name', | ||
'kubernetes.labels', | ||
'kubernetes.annotations', | ||
'kubernetes.replicaset.name', | ||
'kubernetes.deployment.name', | ||
'kubernetes.statefulset.name', | ||
'kubernetes.container.name', | ||
'kubernetes.container.image', | ||
]; | ||
|
||
export const PROMETHEUS_ALLOWED_LIST = ['prometheus.labels', 'prometheus.metrics']; | ||
|
||
export const DOCKER_ALLOWED_LIST = [ | ||
'docker.container.id', | ||
'docker.container.image', | ||
'docker.container.name', | ||
'docker.container.labels', | ||
]; | ||
|
||
export const getAllowedListForPrefix = (prefix: string) => { | ||
const firstPart = first(prefix.split(/\./)); | ||
const defaultAllowedList = prefix ? [...ECS_ALLOWED_LIST, prefix] : ECS_ALLOWED_LIST; | ||
switch (firstPart) { | ||
case 'docker': | ||
return [...defaultAllowedList, ...DOCKER_ALLOWED_LIST]; | ||
case 'prometheus': | ||
return [...defaultAllowedList, ...PROMETHEUS_ALLOWED_LIST]; | ||
case 'kubernetes': | ||
return [...defaultAllowedList, ...K8S_ALLOWED_LIST]; | ||
default: | ||
return defaultAllowedList; | ||
} | ||
}; |
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
65 changes: 65 additions & 0 deletions
65
x-pack/legacy/plugins/infra/public/utils/is_displayable.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,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { isDisplayable } from './is_displayable'; | ||
|
||
describe('isDisplayable()', () => { | ||
test('field that is not displayable', () => { | ||
const field = { | ||
name: 'some.field', | ||
type: 'number', | ||
displayable: false, | ||
}; | ||
expect(isDisplayable(field)).toBe(false); | ||
}); | ||
test('field that is displayable', () => { | ||
const field = { | ||
name: 'some.field', | ||
type: 'number', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field)).toBe(true); | ||
}); | ||
test('field that an ecs field', () => { | ||
const field = { | ||
name: '@timestamp', | ||
type: 'date', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field)).toBe(true); | ||
}); | ||
test('field that matches same prefix', () => { | ||
const field = { | ||
name: 'system.network.name', | ||
type: 'string', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field, ['system.network'])).toBe(true); | ||
}); | ||
test('field that does not matches same prefix', () => { | ||
const field = { | ||
name: 'system.load.1', | ||
type: 'number', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field, ['system.network'])).toBe(false); | ||
}); | ||
test('field that is an K8s allowed field but does not match prefix', () => { | ||
const field = { | ||
name: 'kubernetes.namespace', | ||
type: 'string', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field, ['kubernetes.pod'])).toBe(true); | ||
}); | ||
test('field that is a Prometheus allowed field but does not match prefix', () => { | ||
const field = { | ||
name: 'prometheus.labels.foo.bar', | ||
type: 'string', | ||
displayable: true, | ||
}; | ||
expect(isDisplayable(field, ['prometheus.metrics'])).toBe(true); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
x-pack/legacy/plugins/infra/public/utils/is_displayable.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,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { FieldType } from 'ui/index_patterns'; | ||
import { startsWith, uniq } from 'lodash'; | ||
import { getAllowedListForPrefix } from '../../common/ecs_allowed_list'; | ||
|
||
interface DisplayableFieldType extends FieldType { | ||
displayable?: boolean; | ||
} | ||
|
||
const fieldStartsWith = (field: DisplayableFieldType) => (name: string) => | ||
startsWith(field.name, name); | ||
|
||
export const isDisplayable = (field: DisplayableFieldType, additionalPrefixes: string[] = []) => { | ||
// We need to start with at least one prefix, even if it's empty | ||
const prefixes = additionalPrefixes && additionalPrefixes.length ? additionalPrefixes : ['']; | ||
// Create a set of allowed list based on the prefixes | ||
const allowedList = prefixes.reduce( | ||
(acc, prefix) => { | ||
return uniq([...acc, ...getAllowedListForPrefix(prefix)]); | ||
}, | ||
[] as string[] | ||
); | ||
// If the field is displayable and part of the allowed list or covered by the prefix | ||
return ( | ||
(field.displayable && prefixes.some(fieldStartsWith(field))) || | ||
allowedList.some(fieldStartsWith(field)) | ||
); | ||
}; |
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
Oops, something went wrong.