From 5efd87d7a8d6bf62e9729793955a27e351829f75 Mon Sep 17 00:00:00 2001 From: Kate Patticha Date: Thu, 6 Oct 2022 15:51:18 +0200 Subject: [PATCH] [APM] Return service groups stats from all spaces --- .../collect_data_telemetry/tasks.test.ts | 113 +++++++++++++----- .../collect_data_telemetry/tasks.ts | 5 +- 2 files changed, 86 insertions(+), 32 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts index bf03b999f1046..253c8ed9e44d2 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts @@ -450,38 +450,91 @@ describe('data telemetry collection tasks', () => { const task = tasks.find((t) => t.name === 'service_groups'); const savedObjectsClient = savedObjectsClientMock.create(); - savedObjectsClient.find.mockResolvedValue({ - page: 1, - per_page: 500, - total: 2, - saved_objects: [ - { - type: 'apm-service-group', - id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', - namespaces: ['default'], - attributes: { - color: '#5094C4', - kuery: 'service.environment: production', - groupName: 'production', - }, - references: [], - score: 1, - }, - { - type: 'apm-service-group', - id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', - namespaces: ['default'], - attributes: { - color: '#5094C4', - kuery: 'agent.name: go', - groupName: 'agent', - }, - references: [], - score: 0, + it('returns service group stats', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + page: 1, + per_page: 500, + total: 2, + saved_objects: [ + { + type: 'apm-service-group', + id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', + namespaces: ['default'], + attributes: { + color: '#5094C4', + kuery: 'service.environment: production', + groupName: 'production', + }, + references: [], + score: 1, + }, + { + type: 'apm-service-group', + id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', + namespaces: ['space-1'], + attributes: { + color: '#5094C4', + kuery: 'agent.name: go', + groupName: 'agent', + }, + references: [], + score: 0, + }, + ], + }); + + expect(await task?.executor({ savedObjectsClient } as any)).toEqual({ + service_groups: { + kuery_fields: ['service.environment', 'agent.name'], + total: 2, }, - ], + }); }); - it('returns service group stats', async () => { + + it('should return stats from all spaces', () => { + expect(savedObjectsClient.find).toHaveBeenCalledWith({ + type: 'apm-service-group', + page: 1, + perPage: 500, + sortField: 'updated_at', + sortOrder: 'desc', + namespaces: ['*'], + }); + }); + + it('returns unique fields', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + page: 1, + per_page: 500, + total: 2, + saved_objects: [ + { + type: 'apm-service-group', + id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', + namespaces: ['default'], + attributes: { + color: '#5094C4', + kuery: 'service.environment: production', + groupName: 'production', + }, + references: [], + score: 1, + }, + { + type: 'apm-service-group', + id: '0b6157f0-44bd-11ed-bdb7-bffab551cd4d', + namespaces: ['default'], + attributes: { + color: '#5094C4', + kuery: 'service.environment: production and agent.name: go', + groupName: 'agent', + }, + references: [], + score: 0, + }, + ], + }); + expect(await task?.executor({ savedObjectsClient } as any)).toEqual({ service_groups: { kuery_fields: ['service.environment', 'agent.name'], diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts index 12e89ef32832c..2235de6c0be9f 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts @@ -5,7 +5,7 @@ * 2.0. */ import { fromKueryExpression } from '@kbn/es-query'; -import { flatten, merge, sortBy, sum, pickBy } from 'lodash'; +import { flatten, merge, sortBy, sum, pickBy, uniq } from 'lodash'; import { createHash } from 'crypto'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { ProcessorEvent } from '@kbn/observability-plugin/common'; @@ -1138,6 +1138,7 @@ export const tasks: TelemetryTask[] = [ perPage: MAX_NUMBER_OF_SERVICE_GROUPS, sortField: 'updated_at', sortOrder: 'desc', + namespaces: ['*'], }); const kueryNodes = response.saved_objects.map( @@ -1148,7 +1149,7 @@ export const tasks: TelemetryTask[] = [ return { service_groups: { - kuery_fields: kueryFields, + kuery_fields: uniq(kueryFields), total: response.total ?? 0, }, };