From 181dcc1fa5a1804dd5a378295b6d464368833f22 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Thu, 22 Sep 2022 16:04:26 -0400 Subject: [PATCH 1/6] [APM] Prefer span metrics over span events --- .../get_span_destination_metrics.ts | 1 + .../index.tsx | 26 +++++++++++++++++-- .../get_top_dependency_operations.ts | 21 ++++++++++++--- .../apm/server/routes/dependencies/route.ts | 16 ++++++++++-- .../tests/dependencies/top_operations.spec.ts | 1 + 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts b/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts index 4f04feb841dd4..793f57a1a778c 100644 --- a/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts +++ b/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts @@ -18,6 +18,7 @@ export function getSpanDestinationMetrics(events: ApmFields[]) { 'service.environment', 'service.name', 'span.destination.service.resource', + 'span.name', ]); return metricsets.map((metricset) => { diff --git a/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx b/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx index 84be97ab0ac56..ecef396bb0c5d 100644 --- a/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx +++ b/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n'; import { keyBy } from 'lodash'; import React from 'react'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; +import { useSearchServiceDestinationMetrics } from '../../../../context/time_range_metadata/use_search_service_destination_metrics'; import { useApmParams } from '../../../../hooks/use_apm_params'; import { useBreakpoints } from '../../../../hooks/use_breakpoints'; import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher'; @@ -66,6 +67,9 @@ export function DependencyDetailOperationsList() { urlComparisonEnabled, }); + const { searchServiceDestinationMetrics } = + useSearchServiceDestinationMetrics({ rangeFrom, rangeTo, kuery }); + const primaryStatsFetch = useFetcher( (callApmApi) => { return callApmApi('GET /internal/apm/dependencies/operations', { @@ -76,11 +80,19 @@ export function DependencyDetailOperationsList() { end, environment, kuery, + searchServiceDestinationMetrics, }, }, }); }, - [dependencyName, start, end, environment, kuery] + [ + dependencyName, + start, + end, + environment, + kuery, + searchServiceDestinationMetrics, + ] ); const comparisonStatsFetch = useFetcher( @@ -99,11 +111,21 @@ export function DependencyDetailOperationsList() { offset, environment, kuery, + searchServiceDestinationMetrics, }, }, }); }, - [dependencyName, start, end, offset, environment, kuery, comparisonEnabled] + [ + dependencyName, + start, + end, + offset, + environment, + kuery, + comparisonEnabled, + searchServiceDestinationMetrics, + ] ); const columns: Array> = [ diff --git a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts index 5db5e3c40f997..2396a2b8a16a6 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts @@ -10,7 +10,6 @@ import { rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; -import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { EVENT_OUTCOME, SPAN_DESTINATION_SERVICE_RESOURCE, @@ -28,6 +27,11 @@ import { import { getMetricsDateHistogramParams } from '../../lib/helpers/metrics'; import { Setup } from '../../lib/helpers/setup_request'; import { calculateImpactBuilder } from '../traces/calculate_impact_builder'; +import { + getDocumentTypeFilterForServiceDestinationStatistics, + getLatencyFieldForServiceDestinationStatistics, + getProcessorEventForServiceDestinationStatistics, +} from '../../lib/helpers/spans/get_is_using_service_destination_metrics'; const MAX_NUM_OPERATIONS = 500; @@ -51,6 +55,7 @@ export async function getTopDependencyOperations({ offset, environment, kuery, + searchServiceDestinationMetrics, }: { setup: Setup; dependencyName: string; @@ -59,6 +64,7 @@ export async function getTopDependencyOperations({ offset?: string; environment: Environment; kuery: string; + searchServiceDestinationMetrics: boolean; }) { const { apmEventClient } = setup; @@ -71,7 +77,9 @@ export async function getTopDependencyOperations({ const aggs = { duration: { avg: { - field: SPAN_DURATION, + field: getLatencyFieldForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), }, }, successful: { @@ -94,7 +102,11 @@ export async function getTopDependencyOperations({ 'get_top_dependency_operations', { apm: { - events: [ProcessorEvent.span], + events: [ + getProcessorEventForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), + ], }, body: { size: 0, @@ -105,6 +117,9 @@ export async function getTopDependencyOperations({ ...environmentQuery(environment), ...kqlQuery(kuery), ...termQuery(SPAN_DESTINATION_SERVICE_RESOURCE, dependencyName), + ...getDocumentTypeFilterForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), ], }, }, diff --git a/x-pack/plugins/apm/server/routes/dependencies/route.ts b/x-pack/plugins/apm/server/routes/dependencies/route.ts index 22a8c44e0de5a..1ba2e92eee57a 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/route.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/route.ts @@ -492,7 +492,10 @@ const dependencyOperationsRoute = createApmServerRoute({ environmentRt, kueryRt, offsetRt, - t.type({ dependencyName: t.string }), + t.type({ + dependencyName: t.string, + searchServiceDestinationMetrics: toBooleanRt, + }), ]), }), handler: async ( @@ -501,7 +504,15 @@ const dependencyOperationsRoute = createApmServerRoute({ const setup = await setupRequest(resources); const { - query: { dependencyName, start, end, environment, kuery, offset }, + query: { + dependencyName, + start, + end, + environment, + kuery, + offset, + searchServiceDestinationMetrics, + }, } = resources.params; const operations = await getTopDependencyOperations({ @@ -512,6 +523,7 @@ const dependencyOperationsRoute = createApmServerRoute({ offset, environment, kuery, + searchServiceDestinationMetrics, }); return { operations }; diff --git a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts index 8d8de18eb6e01..b4d2ff86d82f2 100644 --- a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts +++ b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts @@ -52,6 +52,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { environment, kuery, dependencyName, + searchServiceDestinationMetrics: false, }, }, }) From 343b4d70d1fedde41b8a9b5597660eb9bab14bf6 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Thu, 22 Sep 2022 16:04:26 -0400 Subject: [PATCH 2/6] [APM] Prefer span metrics over span events --- .../get_span_destination_metrics.ts | 1 + .../index.tsx | 26 +++++++++++++++++-- .../get_top_dependency_operations.ts | 21 ++++++++++++--- .../apm/server/routes/dependencies/route.ts | 16 ++++++++++-- .../tests/dependencies/top_operations.spec.ts | 1 + 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts b/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts index 4f04feb841dd4..793f57a1a778c 100644 --- a/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts +++ b/packages/kbn-apm-synthtrace/src/lib/apm/processors/get_span_destination_metrics.ts @@ -18,6 +18,7 @@ export function getSpanDestinationMetrics(events: ApmFields[]) { 'service.environment', 'service.name', 'span.destination.service.resource', + 'span.name', ]); return metricsets.map((metricset) => { diff --git a/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx b/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx index 84be97ab0ac56..ecef396bb0c5d 100644 --- a/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx +++ b/x-pack/plugins/apm/public/components/app/dependency_detail_operations/dependency_detail_operations_list/index.tsx @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n'; import { keyBy } from 'lodash'; import React from 'react'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; +import { useSearchServiceDestinationMetrics } from '../../../../context/time_range_metadata/use_search_service_destination_metrics'; import { useApmParams } from '../../../../hooks/use_apm_params'; import { useBreakpoints } from '../../../../hooks/use_breakpoints'; import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher'; @@ -66,6 +67,9 @@ export function DependencyDetailOperationsList() { urlComparisonEnabled, }); + const { searchServiceDestinationMetrics } = + useSearchServiceDestinationMetrics({ rangeFrom, rangeTo, kuery }); + const primaryStatsFetch = useFetcher( (callApmApi) => { return callApmApi('GET /internal/apm/dependencies/operations', { @@ -76,11 +80,19 @@ export function DependencyDetailOperationsList() { end, environment, kuery, + searchServiceDestinationMetrics, }, }, }); }, - [dependencyName, start, end, environment, kuery] + [ + dependencyName, + start, + end, + environment, + kuery, + searchServiceDestinationMetrics, + ] ); const comparisonStatsFetch = useFetcher( @@ -99,11 +111,21 @@ export function DependencyDetailOperationsList() { offset, environment, kuery, + searchServiceDestinationMetrics, }, }, }); }, - [dependencyName, start, end, offset, environment, kuery, comparisonEnabled] + [ + dependencyName, + start, + end, + offset, + environment, + kuery, + comparisonEnabled, + searchServiceDestinationMetrics, + ] ); const columns: Array> = [ diff --git a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts index 1f2e270b7d699..0d70fa37adcf3 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts @@ -10,7 +10,6 @@ import { rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; -import { ProcessorEvent } from '@kbn/observability-plugin/common'; import { EVENT_OUTCOME, SPAN_DESTINATION_SERVICE_RESOURCE, @@ -28,6 +27,11 @@ import { import { getMetricsDateHistogramParams } from '../../lib/helpers/metrics'; import { Setup } from '../../lib/helpers/setup_request'; import { calculateImpactBuilder } from '../traces/calculate_impact_builder'; +import { + getDocumentTypeFilterForServiceDestinationStatistics, + getLatencyFieldForServiceDestinationStatistics, + getProcessorEventForServiceDestinationStatistics, +} from '../../lib/helpers/spans/get_is_using_service_destination_metrics'; const MAX_NUM_OPERATIONS = 500; @@ -51,6 +55,7 @@ export async function getTopDependencyOperations({ offset, environment, kuery, + searchServiceDestinationMetrics, }: { setup: Setup; dependencyName: string; @@ -59,6 +64,7 @@ export async function getTopDependencyOperations({ offset?: string; environment: Environment; kuery: string; + searchServiceDestinationMetrics: boolean; }) { const { apmEventClient } = setup; @@ -71,7 +77,9 @@ export async function getTopDependencyOperations({ const aggs = { duration: { avg: { - field: SPAN_DURATION, + field: getLatencyFieldForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), }, }, successful: { @@ -94,7 +102,11 @@ export async function getTopDependencyOperations({ 'get_top_dependency_operations', { apm: { - events: [ProcessorEvent.span], + events: [ + getProcessorEventForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), + ], }, body: { track_total_hits: false, @@ -106,6 +118,9 @@ export async function getTopDependencyOperations({ ...environmentQuery(environment), ...kqlQuery(kuery), ...termQuery(SPAN_DESTINATION_SERVICE_RESOURCE, dependencyName), + ...getDocumentTypeFilterForServiceDestinationStatistics( + searchServiceDestinationMetrics + ), ], }, }, diff --git a/x-pack/plugins/apm/server/routes/dependencies/route.ts b/x-pack/plugins/apm/server/routes/dependencies/route.ts index 22a8c44e0de5a..1ba2e92eee57a 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/route.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/route.ts @@ -492,7 +492,10 @@ const dependencyOperationsRoute = createApmServerRoute({ environmentRt, kueryRt, offsetRt, - t.type({ dependencyName: t.string }), + t.type({ + dependencyName: t.string, + searchServiceDestinationMetrics: toBooleanRt, + }), ]), }), handler: async ( @@ -501,7 +504,15 @@ const dependencyOperationsRoute = createApmServerRoute({ const setup = await setupRequest(resources); const { - query: { dependencyName, start, end, environment, kuery, offset }, + query: { + dependencyName, + start, + end, + environment, + kuery, + offset, + searchServiceDestinationMetrics, + }, } = resources.params; const operations = await getTopDependencyOperations({ @@ -512,6 +523,7 @@ const dependencyOperationsRoute = createApmServerRoute({ offset, environment, kuery, + searchServiceDestinationMetrics, }); return { operations }; diff --git a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts index 8d8de18eb6e01..b4d2ff86d82f2 100644 --- a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts +++ b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts @@ -52,6 +52,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { environment, kuery, dependencyName, + searchServiceDestinationMetrics: false, }, }, }) From 5c3761d7b3a834879a3ffdf522b0137f32631c4d Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Mon, 26 Sep 2022 15:14:08 -0400 Subject: [PATCH 3/6] using weighted_avg when span metrics --- .../get_top_dependency_operations.ts | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts index 0d70fa37adcf3..7b53721aa48ec 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts @@ -13,7 +13,7 @@ import { import { EVENT_OUTCOME, SPAN_DESTINATION_SERVICE_RESOURCE, - SPAN_DURATION, + SPAN_DESTINATION_SERVICE_RESPONSE_TIME_COUNT, SPAN_NAME, } from '../../../common/elasticsearch_fieldnames'; import { Environment } from '../../../common/environment_rt'; @@ -24,14 +24,14 @@ import { calculateThroughputWithInterval, calculateThroughputWithRange, } from '../../lib/helpers/calculate_throughput'; -import { getMetricsDateHistogramParams } from '../../lib/helpers/metrics'; +import { getBucketSizeForAggregatedTransactions } from '../../lib/helpers/get_bucket_size_for_aggregated_transactions'; import { Setup } from '../../lib/helpers/setup_request'; -import { calculateImpactBuilder } from '../traces/calculate_impact_builder'; import { getDocumentTypeFilterForServiceDestinationStatistics, getLatencyFieldForServiceDestinationStatistics, getProcessorEventForServiceDestinationStatistics, } from '../../lib/helpers/spans/get_is_using_service_destination_metrics'; +import { calculateImpactBuilder } from '../traces/calculate_impact_builder'; const MAX_NUM_OPERATIONS = 500; @@ -74,13 +74,28 @@ export async function getTopDependencyOperations({ offset, }); + const { intervalString } = getBucketSizeForAggregatedTransactions({ + start: startWithOffset, + end: endWithOffset, + searchAggregatedServiceMetrics: searchServiceDestinationMetrics, + }); + + const field = getLatencyFieldForServiceDestinationStatistics( + searchServiceDestinationMetrics + ); + const aggs = { duration: { - avg: { - field: getLatencyFieldForServiceDestinationStatistics( - searchServiceDestinationMetrics - ), - }, + ...(searchServiceDestinationMetrics + ? { + weighted_avg: { + value: { field }, + weight: { + field: SPAN_DESTINATION_SERVICE_RESPONSE_TIME_COUNT, + }, + }, + } + : { avg: { field } }), }, successful: { filter: { @@ -132,18 +147,20 @@ export async function getTopDependencyOperations({ }, aggs: { over_time: { - date_histogram: getMetricsDateHistogramParams({ - start: startWithOffset, - end: endWithOffset, - metricsInterval: 60, - }), + date_histogram: { + field: '@timestamp', + fixed_interval: intervalString, + min_doc_count: 0, + extended_bounds: { + min: startWithOffset, + max: endWithOffset, + }, + }, aggs, }, ...aggs, total_time: { - sum: { - field: SPAN_DURATION, - }, + sum: { field }, }, }, }, From e0da8b5062905cc33bc5dd46f30196c6489f0a37 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Tue, 4 Oct 2022 14:54:29 -0400 Subject: [PATCH 4/6] fixing span metrics --- .../get_top_dependency_operations.ts | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts index 7b53721aa48ec..94b0c2213f1b5 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts @@ -10,20 +10,19 @@ import { rangeQuery, termQuery, } from '@kbn/observability-plugin/server'; +import { isFiniteNumber } from '@kbn/observability-plugin/common/utils/is_finite_number'; import { EVENT_OUTCOME, SPAN_DESTINATION_SERVICE_RESOURCE, SPAN_DESTINATION_SERVICE_RESPONSE_TIME_COUNT, + SPAN_DESTINATION_SERVICE_RESPONSE_TIME_SUM, SPAN_NAME, } from '../../../common/elasticsearch_fieldnames'; import { Environment } from '../../../common/environment_rt'; import { EventOutcome } from '../../../common/event_outcome'; import { environmentQuery } from '../../../common/utils/environment_query'; import { getOffsetInMs } from '../../../common/utils/get_offset_in_ms'; -import { - calculateThroughputWithInterval, - calculateThroughputWithRange, -} from '../../lib/helpers/calculate_throughput'; +import { calculateThroughputWithRange } from '../../lib/helpers/calculate_throughput'; import { getBucketSizeForAggregatedTransactions } from '../../lib/helpers/get_bucket_size_for_aggregated_transactions'; import { Setup } from '../../lib/helpers/setup_request'; import { @@ -85,18 +84,16 @@ export async function getTopDependencyOperations({ ); const aggs = { - duration: { + latency: { ...(searchServiceDestinationMetrics - ? { - weighted_avg: { - value: { field }, - weight: { - field: SPAN_DESTINATION_SERVICE_RESPONSE_TIME_COUNT, - }, - }, - } + ? { sum: { field: SPAN_DESTINATION_SERVICE_RESPONSE_TIME_SUM } } : { avg: { field } }), }, + count: { + sum: { + field: SPAN_DESTINATION_SERVICE_RESPONSE_TIME_COUNT, + }, + }, successful: { filter: { term: { @@ -186,14 +183,26 @@ export async function getTopDependencyOperations({ bucket.over_time.buckets.forEach((dateBucket) => { const x = dateBucket.key + offsetInMs; + const latencyValue = isFiniteNumber(dateBucket.latency.value) + ? dateBucket.latency.value + : 0; + const count = isFiniteNumber(dateBucket.count.value) + ? dateBucket.count.value + : 1; timeseries.throughput.push({ x, - y: calculateThroughputWithInterval({ + y: calculateThroughputWithRange({ + start: startWithOffset, + end: endWithOffset, value: dateBucket.doc_count, - bucketSize: 60, }), }); - timeseries.latency.push({ x, y: dateBucket.duration.value }); + timeseries.latency.push({ + x, + y: searchServiceDestinationMetrics + ? latencyValue / count + : dateBucket.latency.value, + }); timeseries.failureRate.push({ x, y: @@ -206,9 +215,18 @@ export async function getTopDependencyOperations({ }); }); + const latencyValue = isFiniteNumber(bucket.latency.value) + ? bucket.latency.value + : 0; + const count = isFiniteNumber(bucket.count.value) + ? bucket.count.value + : 1; + return { spanName: bucket.key as string, - latency: bucket.duration.value, + latency: searchServiceDestinationMetrics + ? latencyValue / count + : bucket.latency.value, throughput: calculateThroughputWithRange({ start: startWithOffset, end: endWithOffset, From c0d9985ec93c5a0b2e9df5f9ce671d5be24afff3 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Tue, 4 Oct 2022 14:57:22 -0400 Subject: [PATCH 5/6] throughput fix --- .../routes/dependencies/get_top_dependency_operations.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts index 94b0c2213f1b5..3c688f9aaa488 100644 --- a/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts +++ b/x-pack/plugins/apm/server/routes/dependencies/get_top_dependency_operations.ts @@ -194,7 +194,9 @@ export async function getTopDependencyOperations({ y: calculateThroughputWithRange({ start: startWithOffset, end: endWithOffset, - value: dateBucket.doc_count, + value: searchServiceDestinationMetrics + ? dateBucket.count.value || 0 + : dateBucket.doc_count, }), }); timeseries.latency.push({ @@ -230,7 +232,9 @@ export async function getTopDependencyOperations({ throughput: calculateThroughputWithRange({ start: startWithOffset, end: endWithOffset, - value: bucket.doc_count, + value: searchServiceDestinationMetrics + ? bucket.count.value || 0 + : bucket.doc_count, }), failureRate: bucket.failure.doc_count > 0 || bucket.successful.doc_count > 0 From d4df7068797a5999f296fa92d8c2dc29e5945039 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Thu, 6 Oct 2022 12:05:06 -0400 Subject: [PATCH 6/6] adding api test --- .../tests/dependencies/top_operations.spec.ts | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts index b4d2ff86d82f2..41c18a8bf35b3 100644 --- a/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts +++ b/x-pack/test/apm_api_integration/tests/dependencies/top_operations.spec.ts @@ -8,6 +8,8 @@ import expect from '@kbn/expect'; import { APIReturnType } from '@kbn/apm-plugin/public/services/rest/create_call_apm_api'; import { ENVIRONMENT_ALL } from '@kbn/apm-plugin/common/environment_filter_values'; import { ValuesType } from 'utility-types'; +import { DependencyOperation } from '@kbn/apm-plugin/server/routes/dependencies/get_top_dependency_operations'; +import { meanBy } from 'lodash'; import { FtrProviderContext } from '../../common/ftr_provider_context'; import { roundNumber } from '../../utils'; import { generateOperationData, generateOperationDataConfig } from './generate_operation_data'; @@ -37,10 +39,12 @@ export default function ApiTest({ getService }: FtrProviderContext) { dependencyName, environment = ENVIRONMENT_ALL.value, kuery = '', + searchServiceDestinationMetrics = false, }: { dependencyName: string; environment?: string; kuery?: string; + searchServiceDestinationMetrics?: boolean; }) { return await apmApiClient .readUser({ @@ -52,7 +56,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { environment, kuery, dependencyName, - searchServiceDestinationMetrics: false, + searchServiceDestinationMetrics, }, }, }) @@ -211,5 +215,60 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(bulkOperation).to.be.ok(); }); }); + + describe('Compare span metrics and span events', () => { + let bulkOperationSpanEventsResponse: ValuesType; + let bulkOperationSpanMetricsResponse: ValuesType; + + before(async () => { + const [spanEventsResponse, spanMetricsResponse] = await Promise.all([ + callApi({ dependencyName: 'elasticsearch', searchServiceDestinationMetrics: false }), + callApi({ dependencyName: 'elasticsearch', searchServiceDestinationMetrics: true }), + ]); + function findBulkOperation(op: DependencyOperation) { + return op.spanName === '/_bulk'; + } + bulkOperationSpanEventsResponse = spanEventsResponse.find(findBulkOperation)!; + bulkOperationSpanMetricsResponse = spanMetricsResponse.find(findBulkOperation)!; + }); + + it('returns same latency', () => { + expect(bulkOperationSpanEventsResponse.latency).to.eql( + bulkOperationSpanMetricsResponse.latency + ); + + const meanSpanMetrics = meanBy( + bulkOperationSpanEventsResponse.timeseries.latency.filter(({ y }) => y !== null), + 'y' + ); + const meanSpanEvents = meanBy( + bulkOperationSpanMetricsResponse.timeseries.latency.filter(({ y }) => y !== null), + 'y' + ); + expect(meanSpanMetrics).to.eql(meanSpanEvents); + }); + + it('returns same throughput', () => { + expect(bulkOperationSpanEventsResponse.throughput).to.eql( + bulkOperationSpanMetricsResponse.throughput + ); + + const meanSpanMetrics = meanBy( + bulkOperationSpanEventsResponse.timeseries.throughput.filter(({ y }) => y !== 0), + 'y' + ); + const meanSpanEvents = meanBy( + bulkOperationSpanMetricsResponse.timeseries.throughput.filter(({ y }) => y !== 0), + 'y' + ); + expect(meanSpanMetrics).to.eql(meanSpanEvents); + }); + + it('returns same impact', () => { + expect(bulkOperationSpanEventsResponse.impact).to.eql( + bulkOperationSpanMetricsResponse.impact + ); + }); + }); }); }