Skip to content

Commit

Permalink
adding comparison to dependencies api
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Apr 21, 2021
1 parent 6532478 commit 8e5d34a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ 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 = ({
setup,
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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ServiceDependencyItem[]> {
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,
}),
]);

Expand Down Expand Up @@ -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,
}),
},
};
}),
};
});
}
16 changes: 11 additions & 5 deletions x-pack/plugins/apm/server/routes/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -601,6 +601,7 @@ export const serviceDependenciesRoute = createApmServerRoute({
}),
environmentRt,
rangeRt,
comparisonRangeRt,
]),
}),
options: {
Expand All @@ -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 };
},
});

Expand Down

0 comments on commit 8e5d34a

Please sign in to comment.