From 8e5d34ab146821454765bdafafd4392e1b85d462 Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Wed, 21 Apr 2021 11:48:27 -0400 Subject: [PATCH] adding comparison to dependencies api --- .../get_destination_map.ts | 10 +- .../get_service_dependencies/get_metrics.ts | 10 +- .../get_service_dependencies/index.ts | 108 ++++++++++++++++-- x-pack/plugins/apm/server/routes/services.ts | 16 ++- 4 files changed, 124 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts index db491012c986b..730b07a779bff 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts @@ -23,20 +23,24 @@ import { import { ProcessorEvent } from '../../../../common/processor_event'; import { environmentQuery, rangeQuery } from '../../../../server/utils/queries'; import { joinByKey } from '../../../../common/utils/join_by_key'; -import { Setup, SetupTimeRange } from '../../helpers/setup_request'; +import { Setup } from '../../helpers/setup_request'; import { withApmSpan } from '../../../utils/with_apm_span'; export const getDestinationMap = ({ setup, serviceName, environment, + start, + end, }: { - setup: Setup & SetupTimeRange; + setup: Setup; serviceName: string; environment?: string; + start: number; + end: number; }) => { return withApmSpan('get_service_destination_map', async () => { - const { start, end, apmEventClient } = setup; + const { apmEventClient } = setup; const response = await withApmSpan('get_exit_span_samples', async () => apmEventClient.search({ diff --git a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_metrics.ts b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_metrics.ts index c8642c6272b5f..f9a4844243f15 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_metrics.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_metrics.ts @@ -17,7 +17,7 @@ import { ProcessorEvent } from '../../../../common/processor_event'; import { environmentQuery, rangeQuery } from '../../../../server/utils/queries'; import { getBucketSize } from '../../helpers/get_bucket_size'; import { EventOutcome } from '../../../../common/event_outcome'; -import { Setup, SetupTimeRange } from '../../helpers/setup_request'; +import { Setup } from '../../helpers/setup_request'; import { withApmSpan } from '../../../utils/with_apm_span'; export const getMetrics = ({ @@ -25,14 +25,18 @@ export const getMetrics = ({ serviceName, environment, numBuckets, + start, + end, }: { - setup: Setup & SetupTimeRange; + setup: Setup; serviceName: string; environment?: string; numBuckets: number; + start: number; + end: number; }) => { return withApmSpan('get_service_destination_metrics', async () => { - const { start, end, apmEventClient } = setup; + const { apmEventClient } = setup; const response = await apmEventClient.search({ apm: { diff --git a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/index.ts b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/index.ts index 724b5278d7edf..d88313e54b67a 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/index.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/index.ts @@ -5,18 +5,19 @@ * 2.0. */ -import { ValuesType } from 'utility-types'; import { merge } from 'lodash'; +import { ValuesType } from 'utility-types'; import { SPAN_DESTINATION_SERVICE_RESOURCE } from '../../../../common/elasticsearch_fieldnames'; -import { maybe } from '../../../../common/utils/maybe'; import { isFiniteNumber } from '../../../../common/utils/is_finite_number'; -import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; import { joinByKey } from '../../../../common/utils/join_by_key'; +import { maybe } from '../../../../common/utils/maybe'; +import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; +import { offsetPreviousPeriodCoordinates } from '../../../utils/offset_previous_period_coordinate'; +import { withApmSpan } from '../../../utils/with_apm_span'; +import { calculateThroughput } from '../../helpers/calculate_throughput'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; -import { getMetrics } from './get_metrics'; import { getDestinationMap } from './get_destination_map'; -import { calculateThroughput } from '../../helpers/calculate_throughput'; -import { withApmSpan } from '../../../utils/with_apm_span'; +import { getMetrics } from './get_metrics'; export type ServiceDependencyItem = { name: string; @@ -43,30 +44,37 @@ export type ServiceDependencyItem = { | { type: 'external'; spanType?: string; spanSubtype?: string } ); -export function getServiceDependencies({ +function getServiceDependencies({ setup, serviceName, environment, numBuckets, + start, + end, }: { serviceName: string; - setup: Setup & SetupTimeRange; + setup: Setup; environment?: string; numBuckets: number; + start: number; + end: number; }): Promise { return withApmSpan('get_service_dependencies', async () => { - const { start, end } = setup; const [allMetrics, destinationMap] = await Promise.all([ getMetrics({ setup, serviceName, environment, numBuckets, + start, + end, }), getDestinationMap({ setup, serviceName, environment, + start, + end, }), ]); @@ -228,3 +236,85 @@ export function getServiceDependencies({ }); }); } + +export async function getServiceDependenciesPeriods({ + setup, + serviceName, + environment, + numBuckets, + comparisonStart, + comparisonEnd, +}: { + serviceName: string; + setup: Setup & SetupTimeRange; + environment?: string; + numBuckets: number; + comparisonStart?: number; + comparisonEnd?: number; +}) { + return withApmSpan('get_service_dependencies_periods', async () => { + const { start, end } = setup; + + const commonProps = { + serviceName, + environment, + setup, + numBuckets, + }; + + const [currentPeriod, previousPeriod] = await Promise.all([ + getServiceDependencies({ + ...commonProps, + start, + end, + }), + ...(comparisonStart && comparisonEnd + ? [ + getServiceDependencies({ + ...commonProps, + start: comparisonStart, + end: comparisonEnd, + }), + ] + : []), + ]); + + return { + serviceDependencies: currentPeriod.map((currentDependency) => { + const previousDependency = previousPeriod?.find( + (item) => item.name === currentDependency.name + ); + if (!previousDependency) { + return currentDependency; + } + + return { + ...currentDependency, + latency: { + ...currentDependency.latency, + previousPeriodTimeseries: offsetPreviousPeriodCoordinates({ + currentPeriodTimeseries: currentDependency.latency.timeseries, + previousPeriodTimeseries: previousDependency.latency?.timeseries, + }), + }, + throughput: { + ...currentDependency.throughput, + previousPeriodTimeseries: offsetPreviousPeriodCoordinates({ + currentPeriodTimeseries: currentDependency.throughput.timeseries, + previousPeriodTimeseries: + previousDependency.throughput?.timeseries, + }), + }, + errorRate: { + ...currentDependency.errorRate, + previousPeriodTimeseries: offsetPreviousPeriodCoordinates({ + currentPeriodTimeseries: currentDependency.errorRate.timeseries, + previousPeriodTimeseries: + previousDependency.errorRate?.timeseries, + }), + }, + }; + }), + }; + }); +} diff --git a/x-pack/plugins/apm/server/routes/services.ts b/x-pack/plugins/apm/server/routes/services.ts index a27c7d5ba38d2..db6bfe5a472d5 100644 --- a/x-pack/plugins/apm/server/routes/services.ts +++ b/x-pack/plugins/apm/server/routes/services.ts @@ -17,7 +17,7 @@ import { getServiceAnnotations } from '../lib/services/annotations'; import { getServices } from '../lib/services/get_services'; import { getServiceAgentName } from '../lib/services/get_service_agent_name'; import { getServiceAlerts } from '../lib/services/get_service_alerts'; -import { getServiceDependencies } from '../lib/services/get_service_dependencies'; +import { getServiceDependenciesPeriods } from '../lib/services/get_service_dependencies'; import { getServiceInstanceMetadataDetails } from '../lib/services/get_service_instance_metadata_details'; import { getServiceErrorGroupPeriods } from '../lib/services/get_service_error_groups/get_service_error_group_detailed_statistics'; import { getServiceErrorGroupMainStatistics } from '../lib/services/get_service_error_groups/get_service_error_group_main_statistics'; @@ -601,6 +601,7 @@ export const serviceDependenciesRoute = createApmServerRoute({ }), environmentRt, rangeRt, + comparisonRangeRt, ]), }), options: { @@ -610,16 +611,21 @@ export const serviceDependenciesRoute = createApmServerRoute({ const setup = await setupRequest(resources); const { params } = resources; const { serviceName } = params.path; - const { environment, numBuckets } = params.query; + const { + environment, + numBuckets, + comparisonStart, + comparisonEnd, + } = params.query; - const serviceDependencies = await getServiceDependencies({ + return getServiceDependenciesPeriods({ serviceName, environment, setup, numBuckets, + comparisonStart, + comparisonEnd, }); - - return { serviceDependencies }; }, });