-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Related alerts] Add related alerts for all the observability rules #195592
Changes from all commits
806926a
6e100f9
93e1ad7
a6cb408
15c53fc
fa0f792
9db2c4d
577f84f
71f0937
e626130
8e6b4d2
4f1342b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,28 +5,102 @@ | |
* 2.0. | ||
*/ | ||
|
||
import { ALERT_RULE_UUID } from '@kbn/rule-data-utils'; | ||
import type { Group } from '../../typings'; | ||
|
||
export interface Query { | ||
query: string; | ||
language: string; | ||
} | ||
export interface Field { | ||
name: string; | ||
value: string; | ||
} | ||
interface Props { | ||
tags?: string[]; | ||
groups?: Group[]; | ||
ruleId?: string; | ||
sharedFields?: Field[]; | ||
} | ||
|
||
// APM rules | ||
export const SERVICE_NAME = 'service.name'; | ||
// Synthetics rules | ||
export const MONITOR_ID = 'monitor.id'; | ||
// - location | ||
export const OBSERVER_NAME = 'observer.name'; | ||
// Inventory rule | ||
export const HOST = 'host.name'; | ||
export const KUBERNETES_POD = 'kubernetes.pod.uid'; | ||
export const DOCKER_CONTAINER = 'container.id'; | ||
export const EC2_INSTANCE = 'cloud.instance.id'; | ||
export const S3_BUCKETS = 'aws.s3.bucket.name'; | ||
export const RDS_DATABASES = 'aws.rds.db_instance.arn'; | ||
export const SQS_QUEUES = 'aws.sqs.queue.name'; | ||
Comment on lines
+27
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have some of these defined here as well. Could we move it to a common file and use it from there in both places? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sure. I will do it in a follow-up PR. |
||
|
||
const ALL_SHARED_FIELDS = [ | ||
SERVICE_NAME, | ||
MONITOR_ID, | ||
OBSERVER_NAME, | ||
HOST, | ||
KUBERNETES_POD, | ||
DOCKER_CONTAINER, | ||
EC2_INSTANCE, | ||
S3_BUCKETS, | ||
RDS_DATABASES, | ||
SQS_QUEUES, | ||
]; | ||
|
||
interface AlertFields { | ||
[key: string]: any; | ||
} | ||
|
||
export const getSharedFields = (alertFields: AlertFields = {}) => { | ||
const matchedFields: Field[] = []; | ||
ALL_SHARED_FIELDS.forEach((source) => { | ||
Object.keys(alertFields).forEach((field) => { | ||
if (source === field) { | ||
const fieldValue = alertFields[field]; | ||
matchedFields.push({ | ||
name: source, | ||
value: Array.isArray(fieldValue) ? fieldValue[0] : fieldValue, | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
return matchedFields; | ||
}; | ||
|
||
const EXCLUDE_TAGS = ['apm']; | ||
|
||
export const getRelatedAlertKuery = (tags?: string[], groups?: Group[]): string | undefined => { | ||
const tagKueries: string[] = | ||
tags?.map((tag) => { | ||
return `tags: "${tag}"`; | ||
}) ?? []; | ||
export const getRelatedAlertKuery = ({ tags, groups, ruleId, sharedFields }: Props = {}): | ||
| string | ||
| undefined => { | ||
const tagKueries = | ||
tags | ||
?.filter((tag) => !EXCLUDE_TAGS.includes(tag)) | ||
.map((tag) => { | ||
return `tags: "${tag}"`; | ||
}) ?? []; | ||
const groupKueries = | ||
(groups && | ||
groups.map(({ field, value }) => { | ||
return `(${field}: "${value}" or kibana.alert.group.value: "${value}")`; | ||
})) ?? | ||
[]; | ||
const ruleKueries = (ruleId && [`(${ALERT_RULE_UUID}: "${ruleId}")`]) ?? []; | ||
const groupFields = groups?.map((group) => group.field) ?? []; | ||
const sharedFieldsKueries = | ||
sharedFields | ||
?.filter((field) => !groupFields.includes(field.name)) | ||
.map((field) => { | ||
return `(${field.name}: "${field.value}")`; | ||
}) ?? []; | ||
|
||
const tagKueriesStr = tagKueries.length > 0 ? [`(${tagKueries.join(' or ')})`] : []; | ||
const groupKueriesStr = groupKueries.length > 0 ? [`${groupKueries.join(' or ')}`] : []; | ||
const kueries = [...tagKueriesStr, ...groupKueriesStr]; | ||
const kueries = [...tagKueriesStr, ...groupKueriesStr, ...sharedFieldsKueries, ...ruleKueries]; | ||
|
||
return kueries.length ? kueries.join(' or ') : undefined; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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 { ALERT_GROUP, TAGS } from '@kbn/rule-data-utils'; | ||
import { Group } from '../../typings'; | ||
|
||
export interface ObservabilityFields { | ||
[ALERT_GROUP]?: Group[]; | ||
[TAGS]?: string[]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would host.ip also make sense here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, I need to check a bit more to see if this field is useful for most of the scenarios or not. (Added a new tab to this spreadsheet, "other fields" tab, feel free to add more fields that you think might be useful)
I started with a small set of fields, and we can extend it in the future.