Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Simplifying un-abstracting projections and setup #70157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions x-pack/plugins/apm/common/projections/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import {
Setup,
SetupUIFilters,
SetupTimeRange,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../server/lib/helpers/setup_request';
import { SERVICE_NAME, PROCESSOR_EVENT } from '../elasticsearch_fieldnames';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { rangeFilter } from '../utils/range_filter';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ApmIndicesConfig } from '../../server/lib/settings/apm_indices/get_apm_indices';
import { ESFilter } from '../../typings/elasticsearch';

export function getServicesProjection({
setup,
start,
end,
uiFiltersES,
indices,
}: {
setup: Setup & SetupTimeRange & SetupUIFilters;
start: number;
end: number;
uiFiltersES: ESFilter[];
indices: ApmIndicesConfig;
}) {
const { start, end, uiFiltersES, indices } = setup;

return {
index: [
indices['apm_oss.metricsIndices'],
Expand Down
19 changes: 7 additions & 12 deletions x-pack/plugins/apm/server/lib/service_map/get_service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
SERVICE_NAME,
} from '../../../common/elasticsearch_fieldnames';
import { getServicesProjection } from '../../../common/projections/services';
import { mergeProjection } from '../../../common/projections/util/merge_projection';
import { PromiseReturnType } from '../../../typings/common';
import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { transformServiceMapResponses } from './transform_service_map_responses';
Expand Down Expand Up @@ -76,21 +75,17 @@ async function getServicesData(options: IEnvOptions) {
setup: { ...setup, uiFiltersES: [] },
});

const { filter } = projection.body.query.bool;
const serviceNameFilter = options.serviceName
? [{ term: { [SERVICE_NAME]: options.serviceName } }]
: [];

const params = mergeProjection(projection, {
const params = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving away from mergeProjection will make the code a lot more clear - especially for reviewers when things are changed/moved around

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why we have mergeProjection is two-fold:

  • lodash.merge merges arrays in really weird ways
  • if you change a projection, its consumers do not need to update anything (usually).

The downside of spreading only means that you can miss places where the projection is being used. E.g., you'd have to manually inspect all references. Maybe that's a good thing though. Again not a defense - just want to highlight why it's there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change a projection, its consumers do not need to update anything (usually).

This makes sense. Could we achieve the same with unit tests to ensure that projections are correctly applied?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that depends on the nature of the change. for additive changes, no. for other changes, yes (arguably already covered by snapshot testing of queries)

body: {
index: projection.index,
size: 0,
query: {
bool: {
...projection.body.query.bool,
filter: options.serviceName
? filter.concat({
term: {
[SERVICE_NAME]: options.serviceName,
},
})
: filter,
filter: [...projection.body.query.bool.filter, ...serviceNameFilter],
Copy link
Member Author

@sorenlouv sorenlouv Jun 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of the confusion I get from the projection:

  • we have a higher order function mergeProjection that merges some stuff
  • then we have projection.body.query.bool which is spreaded onto bool (is this needed if we already have mergeProjection?)
  • then we have a specific part of the projection being merged with the full bool projection + some other stuff

What takes precendence? Is ...projection.body.query.bool really needed?

In my rewrite I propose we do away with mergeProjection because it's quite opaque in it's merge behaviour. Instead I suggest we do dead-simple spread concatenations (filter: [...projection.body.query.bool.filter, ...serviceNameFilter]), or even prop assignments (index: projection.index).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mergeProjection = mergePlainObject, e.g. it doesn't merge arrays. If you merge arrays, you can't remove items. Arguably the same applies for merging objects, but that feels more natural. If I speak for myself, in some cases I use explicit spreading so that the shape of the request (ie the result of mergeProjection) is more clear. (Not a argument for or against projections, just an explanation).

},
},
aggs: {
Expand All @@ -109,7 +104,7 @@ async function getServicesData(options: IEnvOptions) {
},
},
},
});
};

const { client } = setup;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,48 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { mergeProjection } from '../../../../common/projections/util/merge_projection';
import { ESFilter } from '../../../../typings/elasticsearch';
import {
PROCESSOR_EVENT,
AGENT_NAME,
SERVICE_ENVIRONMENT,
TRANSACTION_DURATION,
} from '../../../../common/elasticsearch_fieldnames';
import { PromiseReturnType } from '../../../../typings/common';
import {
Setup,
SetupTimeRange,
SetupUIFilters,
} from '../../helpers/setup_request';
import { getServicesProjection } from '../../../../common/projections/services';
import { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices';
import { ESClient } from '../../helpers/es_client';

export type ServiceListAPIResponse = PromiseReturnType<typeof getServicesItems>;
export async function getServicesItems(
setup: Setup & SetupTimeRange & SetupUIFilters
) {
const { start, end, client } = setup;

const projection = getServicesProjection({ setup });
export async function getServicesItems({
start,
end,
uiFiltersES,
indices,
client,
}: {
start: number;
end: number;
uiFiltersES: ESFilter[];
indices: ApmIndicesConfig;
client: ESClient;
}) {
const projection = getServicesProjection({
start,
end,
uiFiltersES,
indices,
});

const params = mergeProjection(projection, {
const params = {
body: {
index: projection.index,
size: 0,
query: projection.body.query,
aggs: {
services: {
terms: {
...projection.body.aggs.services.terms,
field: projection.body.aggs.services.terms.field,
size: 500,
},
aggs: {
Expand All @@ -53,7 +65,7 @@ export async function getServicesItems(
},
},
},
});
};

const resp = await client.search(params);
const aggs = resp.aggregations;
Expand Down