diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts index dcbf8edc4a755..640bb90abe711 100644 --- a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/index.ts @@ -32,6 +32,7 @@ export default function apmApiIntegrationTests({ loadTestFile(require.resolve('./service_maps')); loadTestFile(require.resolve('./inspect')); loadTestFile(require.resolve('./service_groups')); + loadTestFile(require.resolve('./time_range_metadata')); loadTestFile(require.resolve('./diagnostics')); loadTestFile(require.resolve('./service_nodes')); loadTestFile(require.resolve('./span_links')); diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/index.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/index.ts new file mode 100644 index 0000000000000..4e3c25936a2db --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/index.ts @@ -0,0 +1,15 @@ +/* + * 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 { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; + +export default function ({ loadTestFile }: DeploymentAgnosticFtrProviderContext) { + describe('time_range_metadata', () => { + loadTestFile(require.resolve('./many_apm_server_versions.spec.ts')); + loadTestFile(require.resolve('./time_range_metadata.spec.ts')); + }); +} diff --git a/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/many_apm_server_versions.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/many_apm_server_versions.spec.ts new file mode 100644 index 0000000000000..31012e6dd6d63 --- /dev/null +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/many_apm_server_versions.spec.ts @@ -0,0 +1,276 @@ +/* + * 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 expect from '@kbn/expect'; +import { apm, timerange } from '@kbn/apm-synthtrace-client'; +import moment from 'moment'; +import { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; +import { + TRANSACTION_DURATION_HISTOGRAM, + TRANSACTION_DURATION_SUMMARY, +} from '@kbn/apm-plugin/common/es_fields/apm'; +import { ApmDocumentType } from '@kbn/apm-plugin/common/document_type'; +import { RollupInterval } from '@kbn/apm-plugin/common/rollup'; +import { LatencyAggregationType } from '@kbn/apm-plugin/common/latency_aggregation_types'; +import { Readable } from 'stream'; +import type { ApmApiClient } from '../../../../services/apm_api'; +import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; + +export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { + const apmApiClient = getService('apmApi'); + const synthtrace = getService('synthtrace'); + const es = getService('es'); + + const baseTime = new Date('2023-10-01T00:00:00.000Z').getTime(); + const startLegacy = moment(baseTime).add(0, 'minutes'); + const start = moment(baseTime).add(5, 'minutes'); + const endLegacy = moment(baseTime).add(10, 'minutes'); + const end = moment(baseTime).add(15, 'minutes'); + + describe('Time range metadata when there are multiple APM Server versions', () => { + describe('when ingesting traces from APM Server with different versions', () => { + let apmSynthtraceEsClient: ApmSynthtraceEsClient; + + before(async () => { + apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(); + await generateTraceDataForService({ + serviceName: 'synth-java-legacy', + start: startLegacy, + end: endLegacy, + isLegacy: true, + synthtrace: apmSynthtraceEsClient, + }); + + await generateTraceDataForService({ + serviceName: 'synth-java', + start, + end, + isLegacy: false, + synthtrace: apmSynthtraceEsClient, + }); + }); + + after(() => { + return apmSynthtraceEsClient.clean(); + }); + + it('ingests transaction metrics with transaction.duration.summary', async () => { + const res = await es.search({ + index: 'metrics-apm*', + body: { + query: { + bool: { + filter: [ + { exists: { field: TRANSACTION_DURATION_HISTOGRAM } }, + { exists: { field: TRANSACTION_DURATION_SUMMARY } }, + ], + }, + }, + }, + }); + + // @ts-expect-error + expect(res.hits.total.value).to.be(20); + }); + + it('ingests transaction metrics without transaction.duration.summary', async () => { + const res = await es.search({ + index: 'metrics-apm*', + body: { + query: { + bool: { + filter: [{ exists: { field: TRANSACTION_DURATION_HISTOGRAM } }], + must_not: [{ exists: { field: TRANSACTION_DURATION_SUMMARY } }], + }, + }, + }, + }); + + // @ts-expect-error + expect(res.hits.total.value).to.be(10); + }); + + it('has transaction.duration.summary field for every document type', async () => { + const response = await apmApiClient.readUser({ + endpoint: 'GET /internal/apm/time_range_metadata', + params: { + query: { + start: endLegacy.toISOString(), + end: end.toISOString(), + enableContinuousRollups: true, + enableServiceTransactionMetrics: true, + useSpanName: false, + kuery: '', + }, + }, + }); + + const allHasSummaryField = response.body.sources + .filter( + (source) => + source.documentType !== ApmDocumentType.TransactionEvent && + source.rollupInterval !== RollupInterval.SixtyMinutes // there is not enough data for 60 minutes + ) + .every((source) => { + return source.hasDurationSummaryField; + }); + + expect(allHasSummaryField).to.eql(true); + }); + + it('does not support transaction.duration.summary when the field is not supported by all APM server versions', async () => { + const response = await apmApiClient.readUser({ + endpoint: 'GET /internal/apm/time_range_metadata', + params: { + query: { + start: startLegacy.toISOString(), + end: endLegacy.toISOString(), + enableContinuousRollups: true, + enableServiceTransactionMetrics: true, + useSpanName: false, + kuery: '', + }, + }, + }); + + const allHasSummaryField = response.body.sources.every((source) => { + return source.hasDurationSummaryField; + }); + + expect(allHasSummaryField).to.eql(false); + }); + + it('does not support transaction.duration.summary for transactionMetric 1m when not all documents within the range support it ', async () => { + const response = await apmApiClient.readUser({ + endpoint: 'GET /internal/apm/time_range_metadata', + params: { + query: { + start: startLegacy.toISOString(), + end: end.toISOString(), + enableContinuousRollups: true, + enableServiceTransactionMetrics: true, + useSpanName: false, + kuery: '', + }, + }, + }); + + const hasDurationSummaryField = response.body.sources.find( + (source) => + source.documentType === ApmDocumentType.TransactionMetric && + source.rollupInterval === RollupInterval.OneMinute // there is not enough data for 60 minutes in the timerange defined for the tests + )?.hasDurationSummaryField; + + expect(hasDurationSummaryField).to.eql(false); + }); + + it('does not have latency data for synth-java-legacy', async () => { + const res = await getLatencyChartForService({ + serviceName: 'synth-java-legacy', + start, + end: endLegacy, + apmApiClient, + useDurationSummary: true, + }); + + expect(res.body.currentPeriod.latencyTimeseries.map(({ y }) => y)).to.eql([ + null, + null, + null, + null, + null, + null, + ]); + }); + + it('has latency data for synth-java service', async () => { + const res = await getLatencyChartForService({ + serviceName: 'synth-java', + start, + end: endLegacy, + apmApiClient, + useDurationSummary: true, + }); + + expect(res.body.currentPeriod.latencyTimeseries.map(({ y }) => y)).to.eql([ + 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, + ]); + }); + }); + }); +} + +// This will retrieve latency data expecting the `transaction.duration.summary` field to be present +function getLatencyChartForService({ + serviceName, + start, + end, + apmApiClient, + useDurationSummary, +}: { + serviceName: string; + start: moment.Moment; + end: moment.Moment; + apmApiClient: ApmApiClient; + useDurationSummary: boolean; +}) { + return apmApiClient.readUser({ + endpoint: `GET /internal/apm/services/{serviceName}/transactions/charts/latency`, + params: { + path: { serviceName }, + query: { + start: start.toISOString(), + end: end.toISOString(), + environment: 'production', + latencyAggregationType: LatencyAggregationType.avg, + transactionType: 'request', + kuery: '', + documentType: ApmDocumentType.TransactionMetric, + rollupInterval: RollupInterval.OneMinute, + bucketSizeInSeconds: 60, + useDurationSummary, + }, + }, + }); +} + +function generateTraceDataForService({ + serviceName, + start, + end, + isLegacy, + synthtrace, +}: { + serviceName: string; + start: moment.Moment; + end: moment.Moment; + isLegacy?: boolean; + synthtrace: ApmSynthtraceEsClient; +}) { + const instance = apm + .service({ + name: serviceName, + environment: 'production', + agentName: 'java', + }) + .instance(`instance`); + + const events = timerange(start, end) + .ratePerMinute(6) + .generator((timestamp) => + instance + .transaction({ transactionName: 'GET /order/{id}' }) + .timestamp(timestamp) + .duration(1000) + .success() + ); + + const apmPipeline = (base: Readable) => { + return synthtrace.getDefaultPipeline({ versionOverride: '8.5.0' })(base); + }; + + return synthtrace.index(events, isLegacy ? apmPipeline : undefined); +} diff --git a/x-pack/test/apm_api_integration/tests/time_range_metadata/time_range_metadata.spec.ts b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/time_range_metadata.spec.ts similarity index 94% rename from x-pack/test/apm_api_integration/tests/time_range_metadata/time_range_metadata.spec.ts rename to x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/time_range_metadata.spec.ts index 6ea90a1b8b1d2..7ec73a692f988 100644 --- a/x-pack/test/apm_api_integration/tests/time_range_metadata/time_range_metadata.spec.ts +++ b/x-pack/test/api_integration/deployment_agnostic/apis/observability/apm/time_range_metadata/time_range_metadata.spec.ts @@ -11,15 +11,14 @@ import { omit, sortBy } from 'lodash'; import moment, { Moment } from 'moment'; import { ApmDocumentType } from '@kbn/apm-plugin/common/document_type'; import { RollupInterval } from '@kbn/apm-plugin/common/rollup'; -import { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; +import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; import { Readable } from 'stream'; import { ToolingLog } from '@kbn/tooling-log'; -import { FtrProviderContext } from '../../common/ftr_provider_context'; +import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context'; -export default function ApiTest({ getService }: FtrProviderContext) { - const registry = getService('registry'); - const apmApiClient = getService('apmApiClient'); - const apmSynthtraceEsClient = getService('apmSynthtraceEsClient'); +export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { + const apmApiClient = getService('apmApi'); + const synthtrace = getService('synthtrace'); const es = getService('es'); const log = getService('log'); @@ -55,29 +54,28 @@ export default function ApiTest({ getService }: FtrProviderContext) { }; } - registry.when('Time range metadata without data', { config: 'basic', archives: [] }, () => { - it('handles empty state', async () => { - const response = await getTimeRangeMedata({ - start, - end, - }); + describe('Time range metadata', () => { + let apmSynthtraceEsClient: ApmSynthtraceEsClient; + describe('without data', () => { + it('handles empty state', async () => { + const response = await getTimeRangeMedata({ + start, + end, + }); - expect(response.isUsingServiceDestinationMetrics).to.eql(false); - expect(response.sources.filter((source) => source.hasDocs)).to.eql([ - { - documentType: ApmDocumentType.TransactionEvent, - rollupInterval: RollupInterval.None, - hasDocs: true, - hasDurationSummaryField: false, - }, - ]); + expect(response.isUsingServiceDestinationMetrics).to.eql(false); + expect(response.sources.filter((source) => source.hasDocs)).to.eql([ + { + documentType: ApmDocumentType.TransactionEvent, + rollupInterval: RollupInterval.None, + hasDocs: true, + hasDurationSummaryField: false, + }, + ]); + }); }); - }); - registry.when( - 'Time range metadata when generating data with multiple APM server versions', - { config: 'basic', archives: [] }, - () => { + describe('when generating data with multiple APM server versions', () => { describe('data loaded with and without summary field', () => { const withoutSummaryFieldStart = moment('2023-04-28T00:00:00.000Z'); const withoutSummaryFieldEnd = moment(withoutSummaryFieldStart).add(2, 'hours'); @@ -86,6 +84,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { const withSummaryFieldEnd = moment(withSummaryFieldStart).add(2, 'hours'); before(async () => { + apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(); await getTransactionEvents({ start: withoutSummaryFieldStart, end: withoutSummaryFieldEnd, @@ -259,15 +258,12 @@ export default function ApiTest({ getService }: FtrProviderContext) { }); }); }); - } - ); - - registry.when( - 'Time range metadata when generating data', - { config: 'basic', archives: [] }, - () => { - before(() => { + }); + + describe('when generating data', () => { + before(async () => { const instance = apm.service('my-service', 'production', 'java').instance('instance'); + apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient(); return apmSynthtraceEsClient.index( timerange(moment(start).subtract(1, 'day'), end) @@ -620,8 +616,8 @@ export default function ApiTest({ getService }: FtrProviderContext) { ]); }); }); - } - ); + }); + }); } function getTransactionEvents({ diff --git a/x-pack/test/apm_api_integration/tests/time_range_metadata/many_apm_server_versions.spec.ts b/x-pack/test/apm_api_integration/tests/time_range_metadata/many_apm_server_versions.spec.ts deleted file mode 100644 index 6031b7dd8de5b..0000000000000 --- a/x-pack/test/apm_api_integration/tests/time_range_metadata/many_apm_server_versions.spec.ts +++ /dev/null @@ -1,278 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import { apm, timerange } from '@kbn/apm-synthtrace-client'; -import moment from 'moment'; -import { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; -import { - TRANSACTION_DURATION_HISTOGRAM, - TRANSACTION_DURATION_SUMMARY, -} from '@kbn/apm-plugin/common/es_fields/apm'; -import { ApmDocumentType } from '@kbn/apm-plugin/common/document_type'; -import { RollupInterval } from '@kbn/apm-plugin/common/rollup'; -import { LatencyAggregationType } from '@kbn/apm-plugin/common/latency_aggregation_types'; -import { Readable } from 'stream'; -import { FtrProviderContext } from '../../common/ftr_provider_context'; -import { ApmApiClient } from '../../common/config'; - -export default function ApiTest({ getService }: FtrProviderContext) { - const registry = getService('registry'); - const apmApiClient = getService('apmApiClient'); - const synthtrace = getService('apmSynthtraceEsClient'); - const es = getService('es'); - - const baseTime = new Date('2023-10-01T00:00:00.000Z').getTime(); - const startLegacy = moment(baseTime).add(0, 'minutes'); - const start = moment(baseTime).add(5, 'minutes'); - const endLegacy = moment(baseTime).add(10, 'minutes'); - const end = moment(baseTime).add(15, 'minutes'); - - registry.when( - 'Time range metadata when there are multiple APM Server versions', - { config: 'basic', archives: [] }, - () => { - describe('when ingesting traces from APM Server with different versions', () => { - before(async () => { - await generateTraceDataForService({ - serviceName: 'synth-java-legacy', - start: startLegacy, - end: endLegacy, - isLegacy: true, - synthtrace, - }); - - await generateTraceDataForService({ - serviceName: 'synth-java', - start, - end, - isLegacy: false, - synthtrace, - }); - }); - - after(() => { - return synthtrace.clean(); - }); - - it('ingests transaction metrics with transaction.duration.summary', async () => { - const res = await es.search({ - index: 'metrics-apm*', - body: { - query: { - bool: { - filter: [ - { exists: { field: TRANSACTION_DURATION_HISTOGRAM } }, - { exists: { field: TRANSACTION_DURATION_SUMMARY } }, - ], - }, - }, - }, - }); - - // @ts-expect-error - expect(res.hits.total.value).to.be(20); - }); - - it('ingests transaction metrics without transaction.duration.summary', async () => { - const res = await es.search({ - index: 'metrics-apm*', - body: { - query: { - bool: { - filter: [{ exists: { field: TRANSACTION_DURATION_HISTOGRAM } }], - must_not: [{ exists: { field: TRANSACTION_DURATION_SUMMARY } }], - }, - }, - }, - }); - - // @ts-expect-error - expect(res.hits.total.value).to.be(10); - }); - - it('has transaction.duration.summary field for every document type', async () => { - const response = await apmApiClient.readUser({ - endpoint: 'GET /internal/apm/time_range_metadata', - params: { - query: { - start: endLegacy.toISOString(), - end: end.toISOString(), - enableContinuousRollups: true, - enableServiceTransactionMetrics: true, - useSpanName: false, - kuery: '', - }, - }, - }); - - const allHasSummaryField = response.body.sources - .filter( - (source) => - source.documentType !== ApmDocumentType.TransactionEvent && - source.rollupInterval !== RollupInterval.SixtyMinutes // there is not enough data for 60 minutes - ) - .every((source) => { - return source.hasDurationSummaryField; - }); - - expect(allHasSummaryField).to.eql(true); - }); - - it('does not support transaction.duration.summary when the field is not supported by all APM server versions', async () => { - const response = await apmApiClient.readUser({ - endpoint: 'GET /internal/apm/time_range_metadata', - params: { - query: { - start: startLegacy.toISOString(), - end: endLegacy.toISOString(), - enableContinuousRollups: true, - enableServiceTransactionMetrics: true, - useSpanName: false, - kuery: '', - }, - }, - }); - - const allHasSummaryField = response.body.sources.every((source) => { - return source.hasDurationSummaryField; - }); - - expect(allHasSummaryField).to.eql(false); - }); - - it('does not support transaction.duration.summary for transactionMetric 1m when not all documents within the range support it ', async () => { - const response = await apmApiClient.readUser({ - endpoint: 'GET /internal/apm/time_range_metadata', - params: { - query: { - start: startLegacy.toISOString(), - end: end.toISOString(), - enableContinuousRollups: true, - enableServiceTransactionMetrics: true, - useSpanName: false, - kuery: '', - }, - }, - }); - - const hasDurationSummaryField = response.body.sources.find( - (source) => - source.documentType === ApmDocumentType.TransactionMetric && - source.rollupInterval === RollupInterval.OneMinute // there is not enough data for 60 minutes in the timerange defined for the tests - )?.hasDurationSummaryField; - - expect(hasDurationSummaryField).to.eql(false); - }); - - it('does not have latency data for synth-java-legacy', async () => { - const res = await getLatencyChartForService({ - serviceName: 'synth-java-legacy', - start, - end: endLegacy, - apmApiClient, - useDurationSummary: true, - }); - - expect(res.body.currentPeriod.latencyTimeseries.map(({ y }) => y)).to.eql([ - null, - null, - null, - null, - null, - null, - ]); - }); - - it('has latency data for synth-java service', async () => { - const res = await getLatencyChartForService({ - serviceName: 'synth-java', - start, - end: endLegacy, - apmApiClient, - useDurationSummary: true, - }); - - expect(res.body.currentPeriod.latencyTimeseries.map(({ y }) => y)).to.eql([ - 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, - ]); - }); - }); - } - ); -} - -// This will retrieve latency data expecting the `transaction.duration.summary` field to be present -function getLatencyChartForService({ - serviceName, - start, - end, - apmApiClient, - useDurationSummary, -}: { - serviceName: string; - start: moment.Moment; - end: moment.Moment; - apmApiClient: ApmApiClient; - useDurationSummary: boolean; -}) { - return apmApiClient.readUser({ - endpoint: `GET /internal/apm/services/{serviceName}/transactions/charts/latency`, - params: { - path: { serviceName }, - query: { - start: start.toISOString(), - end: end.toISOString(), - environment: 'production', - latencyAggregationType: LatencyAggregationType.avg, - transactionType: 'request', - kuery: '', - documentType: ApmDocumentType.TransactionMetric, - rollupInterval: RollupInterval.OneMinute, - bucketSizeInSeconds: 60, - useDurationSummary, - }, - }, - }); -} - -function generateTraceDataForService({ - serviceName, - start, - end, - isLegacy, - synthtrace, -}: { - serviceName: string; - start: moment.Moment; - end: moment.Moment; - isLegacy?: boolean; - synthtrace: ApmSynthtraceEsClient; -}) { - const instance = apm - .service({ - name: serviceName, - environment: 'production', - agentName: 'java', - }) - .instance(`instance`); - - const events = timerange(start, end) - .ratePerMinute(6) - .generator((timestamp) => - instance - .transaction({ transactionName: 'GET /order/{id}' }) - .timestamp(timestamp) - .duration(1000) - .success() - ); - - const apmPipeline = (base: Readable) => { - return synthtrace.getDefaultPipeline({ versionOverride: '8.5.0' })(base); - }; - - return synthtrace.index(events, isLegacy ? apmPipeline : undefined); -}