[8.12] [Fleet] Do not write to metrics-fleet_server.usage* if .fleet-agents is not yet created (#173689) (#173725)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[Fleet] Do not write to metrics-fleet_server.usage* if .fleet-agents
is not yet created
(#173689)](https://github.com/elastic/kibana/pull/173689)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"nicolas.chaulet@elastic.co"},"sourceCommit":{"committedDate":"2023-12-20T10:46:12Z","message":"[Fleet]
Do not write to metrics-fleet_server.usage* if .fleet-agents is not yet
created
(#173689)","sha":"50f67699f7a6183ba770e24a7a4575567150ad05","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","backport:prev-minor","v8.13.0"],"number":173689,"url":"https://github.com/elastic/kibana/pull/173689","mergeCommit":{"message":"[Fleet]
Do not write to metrics-fleet_server.usage* if .fleet-agents is not yet
created
(#173689)","sha":"50f67699f7a6183ba770e24a7a4575567150ad05"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173689","number":173689,"mergeCommit":{"message":"[Fleet]
Do not write to metrics-fleet_server.usage* if .fleet-agents is not yet
created (#173689)","sha":"50f67699f7a6183ba770e24a7a4575567150ad05"}}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2023-12-20 07:18:52 -05:00 committed by GitHub
parent 9c93b5a9ed
commit fe1cacecb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View file

@ -29,6 +29,14 @@ describe('fetchAgentMetrics', () => {
esClient = elasticsearch.client.asInternalUser as ElasticsearchClientMock;
});
it('should not fetch agent if .fleet-agents is not created', async () => {
esClient.indices.get.mockRejectedValue({ statusCode: 404 });
const result = await fetchAgentMetrics(mockCore, abortController);
expect(result).toBeUndefined();
});
it('should fetch agent metrics', async () => {
esClient.search.mockResolvedValue({
took: 5,

View file

@ -42,6 +42,23 @@ export const fetchAgentMetrics = async (
if (!soClient || !esClient) {
return;
}
const fleetAgentsIndexExists = await esClient.indices
.get({
index: AGENTS_INDEX,
})
.catch((error) => {
if (error.statusCode === 404) {
return;
}
throw error;
});
if (!fleetAgentsIndexExists) {
return;
}
const usage = {
agents: await getAgentUsage(soClient, esClient),
agents_per_version: await getAgentsPerVersion(esClient, abortController),