diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map_from_trace_ids.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map_from_trace_ids.ts index 6e9225041b199..7e43c8f684377 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map_from_trace_ids.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map_from_trace_ids.ts @@ -30,10 +30,8 @@ export function getConnections({ if (!paths) { return []; } - const isEnvironmentSelected = - environment && environment !== ENVIRONMENT_ALL.value; - if (serviceName || isEnvironmentSelected) { + if (serviceName || environment) { paths = paths.filter((path) => { return ( path @@ -46,7 +44,7 @@ export function getConnections({ return false; } - if (!environment) { + if (!environment || environment === ENVIRONMENT_ALL.value) { return true; } diff --git a/x-pack/test/apm_api_integration/tests/service_maps/service_maps.ts b/x-pack/test/apm_api_integration/tests/service_maps/service_maps.ts index f452514cb5172..a48811ad70249 100644 --- a/x-pack/test/apm_api_integration/tests/service_maps/service_maps.ts +++ b/x-pack/test/apm_api_integration/tests/service_maps/service_maps.ts @@ -6,6 +6,7 @@ */ import querystring from 'querystring'; +import url from 'url'; import expect from '@kbn/expect'; import { isEmpty, uniq } from 'lodash'; import archives_metadata from '../../common/fixtures/es_archiver/archives_metadata'; @@ -131,110 +132,127 @@ export default function serviceMapsApiTests({ getService }: FtrProviderContext) expect(environments.has(ENVIRONMENT_NOT_DEFINED)).to.eql(true); expectSnapshot(body).toMatch(); }); - }); - - describe('/api/apm/service-map with ML data', () => { - describe('with the default apm user', () => { - let response: PromiseReturnType; - before(async () => { - response = await supertest.get(`/api/apm/service-map?start=${start}&end=${end}`); - }); + describe('with ML data', () => { + describe('with the default apm user', () => { + before(async () => { + response = await supertest.get(`/api/apm/service-map?start=${start}&end=${end}`); + }); - it('returns service map elements with anomaly stats', () => { - expect(response.status).to.be(200); - const dataWithAnomalies = response.body.elements.filter( - (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) - ); + it('returns service map elements with anomaly stats', () => { + expect(response.status).to.be(200); + const dataWithAnomalies = response.body.elements.filter( + (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) + ); - expect(dataWithAnomalies).to.not.empty(); + expect(dataWithAnomalies).to.not.empty(); - dataWithAnomalies.forEach(({ data }: any) => { - expect( - Object.values(data.serviceAnomalyStats).filter((value) => isEmpty(value)) - ).to.not.empty(); + dataWithAnomalies.forEach(({ data }: any) => { + expect( + Object.values(data.serviceAnomalyStats).filter((value) => isEmpty(value)) + ).to.not.empty(); + }); }); - }); - it('returns the correct anomaly stats', () => { - const dataWithAnomalies = response.body.elements.filter( - (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) - ); - - expectSnapshot(dataWithAnomalies.length).toMatchInline(`8`); - expectSnapshot(dataWithAnomalies.slice(0, 3)).toMatchInline(` - Array [ - Object { - "data": Object { - "agent.name": "python", - "id": "opbeans-python", - "service.name": "opbeans-python", - "serviceAnomalyStats": Object { - "actualValue": 24282.2352941176, - "anomalyScore": 0, - "healthStatus": "healthy", - "jobId": "apm-environment_not_defined-5626-high_mean_transaction_duration", - "serviceName": "opbeans-python", - "transactionType": "request", + it('returns the correct anomaly stats', () => { + const dataWithAnomalies = response.body.elements.filter( + (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) + ); + + expectSnapshot(dataWithAnomalies.length).toMatchInline(`8`); + expectSnapshot(dataWithAnomalies.slice(0, 3)).toMatchInline(` + Array [ + Object { + "data": Object { + "agent.name": "python", + "id": "opbeans-python", + "service.name": "opbeans-python", + "serviceAnomalyStats": Object { + "actualValue": 24282.2352941176, + "anomalyScore": 0, + "healthStatus": "healthy", + "jobId": "apm-environment_not_defined-5626-high_mean_transaction_duration", + "serviceName": "opbeans-python", + "transactionType": "request", + }, }, }, - }, - Object { - "data": Object { - "agent.name": "nodejs", - "id": "opbeans-node", - "service.environment": "testing", - "service.name": "opbeans-node", - "serviceAnomalyStats": Object { - "actualValue": 29300.5555555556, - "anomalyScore": 0, - "healthStatus": "healthy", - "jobId": "apm-testing-384f-high_mean_transaction_duration", - "serviceName": "opbeans-node", - "transactionType": "request", + Object { + "data": Object { + "agent.name": "nodejs", + "id": "opbeans-node", + "service.environment": "testing", + "service.name": "opbeans-node", + "serviceAnomalyStats": Object { + "actualValue": 29300.5555555556, + "anomalyScore": 0, + "healthStatus": "healthy", + "jobId": "apm-testing-384f-high_mean_transaction_duration", + "serviceName": "opbeans-node", + "transactionType": "request", + }, }, }, - }, - Object { - "data": Object { - "agent.name": "rum-js", - "id": "opbeans-rum", - "service.environment": "testing", - "service.name": "opbeans-rum", - "serviceAnomalyStats": Object { - "actualValue": 2386500, - "anomalyScore": 0, - "healthStatus": "healthy", - "jobId": "apm-testing-384f-high_mean_transaction_duration", - "serviceName": "opbeans-rum", - "transactionType": "page-load", + Object { + "data": Object { + "agent.name": "rum-js", + "id": "opbeans-rum", + "service.environment": "testing", + "service.name": "opbeans-rum", + "serviceAnomalyStats": Object { + "actualValue": 2386500, + "anomalyScore": 0, + "healthStatus": "healthy", + "jobId": "apm-testing-384f-high_mean_transaction_duration", + "serviceName": "opbeans-rum", + "transactionType": "page-load", + }, }, }, - }, - ] - `); + ] + `); - expectSnapshot(response.body).toMatch(); + expectSnapshot(response.body).toMatch(); + }); }); - }); - describe('with a user that does not have access to ML', () => { - let response: PromiseReturnType; + describe('with a user that does not have access to ML', () => { + before(async () => { + response = await supertestAsApmReadUserWithoutMlAccess.get( + `/api/apm/service-map?start=${start}&end=${end}` + ); + }); - before(async () => { - response = await supertestAsApmReadUserWithoutMlAccess.get( - `/api/apm/service-map?start=${start}&end=${end}` - ); - }); + it('returns service map elements without anomaly stats', () => { + expect(response.status).to.be(200); - it('returns service map elements without anomaly stats', () => { - expect(response.status).to.be(200); + const dataWithAnomalies = response.body.elements.filter( + (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) + ); - const dataWithAnomalies = response.body.elements.filter( - (el: { data: { serviceAnomalyStats?: {} } }) => !isEmpty(el.data.serviceAnomalyStats) - ); + expect(dataWithAnomalies).to.be.empty(); + }); + }); + }); + + describe('with a single service', () => { + describe('when ENVIRONMENT_ALL is selected', () => { + it('returns service map elements', async () => { + response = await supertest.get( + url.format({ + pathname: '/api/apm/service-map', + query: { + environment: 'ENVIRONMENT_ALL', + start: metadata.start, + end: metadata.end, + serviceName: 'opbeans-java', + }, + }) + ); - expect(dataWithAnomalies).to.be.empty(); + expect(response.status).to.be(200); + expect(response.body.elements.length).to.be.greaterThan(1); + }); }); }); });