-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…143881) * [APM] Support specific fields when creating service groups (#142201) * add support to anomaly rule type to store supported service group fields in alert * address PR feedback and fixes checks * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * add API tests for field validation * fixes linting * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' * fixes multi_terms sort order paths, for each rule type query * adds unit tests and moves some source files * fixed back import path * PR feedback * improvements to kuery validation * fixes selecting 'All' in service.name, transaction.type fields when creating/editing APM Rules (#143861) Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
e82e0a1
commit 796751e
Showing
22 changed files
with
700 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 { | ||
isSupportedField, | ||
validateServiceGroupKuery, | ||
SERVICE_GROUP_SUPPORTED_FIELDS, | ||
} from './service_groups'; | ||
import { | ||
TRANSACTION_TYPE, | ||
TRANSACTION_DURATION, | ||
SERVICE_FRAMEWORK_VERSION, | ||
} from './elasticsearch_fieldnames'; | ||
|
||
describe('service_groups common utils', () => { | ||
describe('isSupportedField', () => { | ||
it('should allow supported fields', () => { | ||
SERVICE_GROUP_SUPPORTED_FIELDS.map((field) => { | ||
expect(isSupportedField(field)).toBe(true); | ||
}); | ||
}); | ||
it('should reject unsupported fields', () => { | ||
const unsupportedFields = [ | ||
TRANSACTION_TYPE, | ||
TRANSACTION_DURATION, | ||
SERVICE_FRAMEWORK_VERSION, | ||
]; | ||
unsupportedFields.map((field) => { | ||
expect(isSupportedField(field)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
describe('validateServiceGroupKuery', () => { | ||
it('should validate supported KQL filter for a service group', () => { | ||
const result = validateServiceGroupKuery( | ||
`service.name: testbeans* or agent.name: "nodejs"` | ||
); | ||
expect(result).toHaveProperty('isValidFields', true); | ||
expect(result).toHaveProperty('isValidSyntax', true); | ||
expect(result).not.toHaveProperty('message'); | ||
}); | ||
it('should return validation error when unsupported fields are used', () => { | ||
const result = validateServiceGroupKuery( | ||
`service.name: testbeans* or agent.name: "nodejs" or transaction.type: request` | ||
); | ||
expect(result).toHaveProperty('isValidFields', false); | ||
expect(result).toHaveProperty('isValidSyntax', true); | ||
expect(result).toHaveProperty( | ||
'message', | ||
'Query filter for service group does not support fields [transaction.type]' | ||
); | ||
}); | ||
it('should return parsing error when KQL is incomplete', () => { | ||
const result = validateServiceGroupKuery( | ||
`service.name: testbeans* or agent.name: "nod` | ||
); | ||
expect(result).toHaveProperty('isValidFields', false); | ||
expect(result).toHaveProperty('isValidSyntax', false); | ||
expect(result).toHaveProperty('message'); | ||
expect(result).not.toBe(''); | ||
}); | ||
}); | ||
}); |
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
File renamed without changes.
File renamed without changes.
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
91 changes: 91 additions & 0 deletions
91
...ugins/apm/server/routes/alerts/rule_types/anomaly/get_service_group_fields_for_anomaly.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,91 @@ | ||
/* | ||
* 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 { firstValueFrom } from 'rxjs'; | ||
import { | ||
IScopedClusterClient, | ||
SavedObjectsClientContract, | ||
} from '@kbn/core/server'; | ||
import { | ||
SERVICE_ENVIRONMENT, | ||
SERVICE_NAME, | ||
TRANSACTION_TYPE, | ||
TRANSACTION_DURATION, | ||
} from '../../../../../common/elasticsearch_fieldnames'; | ||
import { alertingEsClient } from '../../alerting_es_client'; | ||
import { | ||
getServiceGroupFields, | ||
getServiceGroupFieldsAgg, | ||
} from '../get_service_group_fields'; | ||
import { getApmIndices } from '../../../settings/apm_indices/get_apm_indices'; | ||
import { RegisterRuleDependencies } from '../../register_apm_rule_types'; | ||
|
||
export async function getServiceGroupFieldsForAnomaly({ | ||
config$, | ||
scopedClusterClient, | ||
savedObjectsClient, | ||
serviceName, | ||
environment, | ||
transactionType, | ||
timestamp, | ||
bucketSpan, | ||
}: { | ||
config$: RegisterRuleDependencies['config$']; | ||
scopedClusterClient: IScopedClusterClient; | ||
savedObjectsClient: SavedObjectsClientContract; | ||
serviceName: string; | ||
environment: string; | ||
transactionType: string; | ||
timestamp: number; | ||
bucketSpan: number; | ||
}) { | ||
const config = await firstValueFrom(config$); | ||
const indices = await getApmIndices({ | ||
config, | ||
savedObjectsClient, | ||
}); | ||
const { transaction: index } = indices; | ||
|
||
const params = { | ||
index, | ||
body: { | ||
size: 0, | ||
track_total_hits: false, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ term: { [SERVICE_NAME]: serviceName } }, | ||
{ term: { [TRANSACTION_TYPE]: transactionType } }, | ||
{ term: { [SERVICE_ENVIRONMENT]: environment } }, | ||
{ | ||
range: { | ||
'@timestamp': { | ||
gte: timestamp, | ||
lte: timestamp + bucketSpan * 1000, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
...getServiceGroupFieldsAgg({ | ||
sort: [{ [TRANSACTION_DURATION]: { order: 'desc' as const } }], | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
const response = await alertingEsClient({ | ||
scopedClusterClient, | ||
params, | ||
}); | ||
if (!response.aggregations) { | ||
return {}; | ||
} | ||
return getServiceGroupFields(response.aggregations); | ||
} |
Oops, something went wrong.