Skip to content

Commit ca17e1e

Browse files
committed
[APM] Don't fetch dynamic index pattern in setupRequest
We don't need a dynamic index pattern for parsing the filters from the query bar. Additionally, instead of fetching uiIndices in `getParamsForSearchRequest`, we can use `indices` that we already fetched in `setupRequest`.
1 parent 2b72de5 commit ca17e1e

9 files changed

Lines changed: 65 additions & 70 deletions

File tree

x-pack/plugins/apm/server/lib/errors/distribution/__tests__/get_buckets.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ describe('timeseriesFetcher', () => {
5656
apmAgentConfigurationIndex: '.apm-agent-configuration',
5757
apmCustomLinkIndex: '.apm-custom-link',
5858
},
59-
dynamicIndexPattern: null as any,
6059
},
6160
});
6261
});

x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_ui_filters_es.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ import {
1111
localUIFilters,
1212
localUIFilterNames,
1313
} from '../../ui_filters/local_ui_filters/config';
14-
import {
15-
esKuery,
16-
IIndexPattern,
17-
} from '../../../../../../../src/plugins/data/server';
14+
import { esKuery } from '../../../../../../../src/plugins/data/server';
1815

19-
export function getUiFiltersES(
20-
indexPattern: IIndexPattern | undefined,
21-
uiFilters: UIFilters
22-
) {
16+
export function getUiFiltersES(uiFilters: UIFilters) {
2317
const { kuery, environment, ...localFilterValues } = uiFilters;
2418
const mappedFilters = localUIFilterNames
2519
.filter((name) => name in localFilterValues)
@@ -35,7 +29,7 @@ export function getUiFiltersES(
3529

3630
// remove undefined items from list
3731
const esFilters = [
38-
getKueryUiFilterES(indexPattern, uiFilters.kuery),
32+
getKueryUiFilterES(uiFilters.kuery),
3933
getEnvironmentUiFilterES(uiFilters.environment),
4034
]
4135
.filter((filter) => !!filter)
@@ -44,14 +38,11 @@ export function getUiFiltersES(
4438
return esFilters;
4539
}
4640

47-
function getKueryUiFilterES(
48-
indexPattern: IIndexPattern | undefined,
49-
kuery?: string
50-
) {
51-
if (!kuery || !indexPattern) {
41+
function getKueryUiFilterES(kuery?: string) {
42+
if (!kuery) {
5243
return;
5344
}
5445

5546
const ast = esKuery.fromKueryExpression(kuery);
56-
return esKuery.toElasticsearchQuery(ast, indexPattern) as ESFilter;
47+
return esKuery.toElasticsearchQuery(ast) as ESFilter;
5748
}

x-pack/plugins/apm/server/lib/helpers/es_client.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ import {
1919
ESSearchRequest,
2020
ESSearchResponse,
2121
} from '../../../typings/elasticsearch';
22-
import { UI_SETTINGS } from '../../../../../../src/plugins/data/server';
2322
import { OBSERVER_VERSION_MAJOR } from '../../../common/elasticsearch_fieldnames';
2423
import { pickKeys } from '../../../common/utils/pick_keys';
2524
import { APMRequestHandlerContext } from '../../routes/typings';
26-
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
25+
import { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices';
2726

2827
// `type` was deprecated in 7.0
2928
export type APMIndexDocumentParams<T> = Omit<IndexDocumentParams<T>, 'type'>;
@@ -85,20 +84,19 @@ function addFilterForLegacyData(
8584
}
8685

8786
// add additional params for search (aka: read) requests
88-
async function getParamsForSearchRequest(
89-
context: APMRequestHandlerContext,
90-
params: ESSearchRequest,
91-
apmOptions?: APMOptions
92-
) {
93-
const { uiSettings } = context.core;
94-
const [indices, includeFrozen] = await Promise.all([
95-
getApmIndices({
96-
savedObjectsClient: context.core.savedObjects.client,
97-
config: context.config,
98-
}),
99-
uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
100-
]);
101-
87+
function getParamsForSearchRequest({
88+
context,
89+
params,
90+
indices,
91+
includeFrozen,
92+
includeLegacyData,
93+
}: {
94+
context: APMRequestHandlerContext;
95+
params: ESSearchRequest;
96+
indices: ApmIndicesConfig;
97+
includeFrozen: boolean;
98+
includeLegacyData?: boolean;
99+
}) {
102100
// Get indices for legacy data filter (only those which apply)
103101
const apmIndices = Object.values(
104102
pickKeys(
@@ -112,7 +110,7 @@ async function getParamsForSearchRequest(
112110
)
113111
);
114112
return {
115-
...addFilterForLegacyData(apmIndices, params, apmOptions), // filter out pre-7.0 data
113+
...addFilterForLegacyData(apmIndices, params, { includeLegacyData }), // filter out pre-7.0 data
116114
ignore_throttled: !includeFrozen, // whether to query frozen indices or not
117115
};
118116
}
@@ -123,6 +121,8 @@ interface APMOptions {
123121

124122
interface ClientCreateOptions {
125123
clientAsInternalUser?: boolean;
124+
indices: ApmIndicesConfig;
125+
includeFrozen: boolean;
126126
}
127127

128128
export type ESClient = ReturnType<typeof getESClient>;
@@ -134,7 +134,7 @@ function formatObj(obj: Record<string, any>) {
134134
export function getESClient(
135135
context: APMRequestHandlerContext,
136136
request: KibanaRequest,
137-
{ clientAsInternalUser = false }: ClientCreateOptions = {}
137+
{ clientAsInternalUser = false, indices, includeFrozen }: ClientCreateOptions
138138
) {
139139
const {
140140
callAsCurrentUser,
@@ -194,11 +194,13 @@ export function getESClient(
194194
params: TSearchRequest,
195195
apmOptions?: APMOptions
196196
): Promise<ESSearchResponse<TDocument, TSearchRequest>> => {
197-
const nextParams = await getParamsForSearchRequest(
197+
const nextParams = await getParamsForSearchRequest({
198198
context,
199199
params,
200-
apmOptions
201-
);
200+
indices,
201+
includeFrozen,
202+
...apmOptions,
203+
});
202204

203205
return callEs('search', nextParams);
204206
},

x-pack/plugins/apm/server/lib/helpers/setup_request.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
import moment from 'moment';
8+
import { UI_SETTINGS } from 'src/plugins/data/common';
89
import { KibanaRequest } from '../../../../../../src/core/server';
9-
import { IIndexPattern } from '../../../../../../src/plugins/data/common';
1010
import { APMConfig } from '../..';
1111
import {
1212
getApmIndices,
@@ -18,17 +18,13 @@ import { getUiFiltersES } from './convert_ui_filters/get_ui_filters_es';
1818
import { APMRequestHandlerContext } from '../../routes/typings';
1919
import { getESClient } from './es_client';
2020
import { ProcessorEvent } from '../../../common/processor_event';
21-
import { getDynamicIndexPattern } from '../index_pattern/get_dynamic_index_pattern';
2221

23-
function decodeUiFilters(
24-
indexPattern: IIndexPattern | undefined,
25-
uiFiltersEncoded?: string
26-
) {
27-
if (!uiFiltersEncoded || !indexPattern) {
22+
function decodeUiFilters(uiFiltersEncoded?: string) {
23+
if (!uiFiltersEncoded) {
2824
return [];
2925
}
3026
const uiFilters = JSON.parse(uiFiltersEncoded);
31-
return getUiFiltersES(indexPattern, uiFilters);
27+
return getUiFiltersES(uiFilters);
3228
}
3329
// Explicitly type Setup to prevent TS initialization errors
3430
// https://github.com/microsoft/TypeScript/issues/34933
@@ -39,7 +35,6 @@ export interface Setup {
3935
ml?: ReturnType<typeof getMlSetup>;
4036
config: APMConfig;
4137
indices: ApmIndicesConfig;
42-
dynamicIndexPattern?: IIndexPattern;
4338
}
4439

4540
export interface SetupTimeRange {
@@ -75,28 +70,33 @@ export async function setupRequest<TParams extends SetupRequestParams>(
7570
const { config } = context;
7671
const { query } = context.params;
7772

78-
const indices = await getApmIndices({
79-
savedObjectsClient: context.core.savedObjects.client,
80-
config,
81-
});
73+
const [indices, includeFrozen] = await Promise.all([
74+
getApmIndices({
75+
savedObjectsClient: context.core.savedObjects.client,
76+
config,
77+
}),
78+
context.core.uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
79+
]);
8280

83-
const dynamicIndexPattern = await getDynamicIndexPattern({
84-
context,
81+
const createClientOptions = {
8582
indices,
86-
processorEvent: query.processorEvent,
87-
});
83+
includeFrozen,
84+
};
8885

89-
const uiFiltersES = decodeUiFilters(dynamicIndexPattern, query.uiFilters);
86+
const uiFiltersES = decodeUiFilters(query.uiFilters);
9087

9188
const coreSetupRequest = {
9289
indices,
93-
client: getESClient(context, request, { clientAsInternalUser: false }),
90+
client: getESClient(context, request, {
91+
clientAsInternalUser: false,
92+
...createClientOptions,
93+
}),
9494
internalClient: getESClient(context, request, {
9595
clientAsInternalUser: true,
96+
...createClientOptions,
9697
}),
9798
ml: getMlSetup(context, request),
9899
config,
99-
dynamicIndexPattern,
100100
};
101101

102102
return {

x-pack/plugins/apm/server/lib/transactions/charts/get_timeseries_data/fetcher.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('timeseriesFetcher', () => {
4444
apmAgentConfigurationIndex: 'myIndex',
4545
apmCustomLinkIndex: 'myIndex',
4646
},
47-
dynamicIndexPattern: null as any,
4847
},
4948
});
5049
});

x-pack/plugins/apm/server/lib/ui_filters/local_ui_filters/get_local_filter_query.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@
55
*/
66

77
import { omit } from 'lodash';
8-
import { IIndexPattern } from 'src/plugins/data/server';
98
import { mergeProjection } from '../../../../common/projections/util/merge_projection';
109
import { Projection } from '../../../../common/projections/typings';
1110
import { UIFilters } from '../../../../typings/ui_filters';
1211
import { getUiFiltersES } from '../../helpers/convert_ui_filters/get_ui_filters_es';
1312
import { localUIFilters, LocalUIFilterName } from './config';
1413

1514
export const getLocalFilterQuery = ({
16-
indexPattern,
1715
uiFilters,
1816
projection,
1917
localUIFilterName,
2018
}: {
21-
indexPattern: IIndexPattern | undefined;
2219
uiFilters: UIFilters;
2320
projection: Projection;
2421
localUIFilterName: LocalUIFilterName;
2522
}) => {
2623
const field = localUIFilters[localUIFilterName];
27-
const filter = getUiFiltersES(indexPattern, omit(uiFilters, field.name));
24+
const filter = getUiFiltersES(omit(uiFilters, field.name));
2825

2926
const bucketCountAggregation = projection.body.aggs
3027
? {

x-pack/plugins/apm/server/lib/ui_filters/local_ui_filters/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function getLocalUIFilters({
2626
uiFilters: UIFilters;
2727
localFilterNames: LocalUIFilterName[];
2828
}) {
29-
const { client, dynamicIndexPattern } = setup;
29+
const { client } = setup;
3030

3131
const projectionWithoutAggs = cloneDeep(projection);
3232

@@ -35,7 +35,6 @@ export async function getLocalUIFilters({
3535
return Promise.all(
3636
localFilterNames.map(async (name) => {
3737
const query = getLocalFilterQuery({
38-
indexPattern: dynamicIndexPattern,
3938
uiFilters,
4039
projection,
4140
localUIFilterName: name,

x-pack/plugins/apm/server/routes/index_pattern.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { createRoute } from './create_route';
99
import { setupRequest } from '../lib/helpers/setup_request';
1010
import { getInternalSavedObjectsClient } from '../lib/helpers/get_internal_saved_objects_client';
1111
import { getApmIndexPatternTitle } from '../lib/index_pattern/get_apm_index_pattern_title';
12+
import { getDynamicIndexPattern } from '../lib/index_pattern/get_dynamic_index_pattern';
13+
import { getApmIndices } from '../lib/settings/apm_indices/get_apm_indices';
1214

1315
export const staticIndexPatternRoute = createRoute((core) => ({
1416
method: 'POST',
@@ -34,8 +36,17 @@ export const dynamicIndexPatternRoute = createRoute(() => ({
3436
]),
3537
}),
3638
},
37-
handler: async ({ context, request }) => {
38-
const { dynamicIndexPattern } = await setupRequest(context, request);
39+
handler: async ({ context }) => {
40+
const indices = await getApmIndices({
41+
config: context.config,
42+
savedObjectsClient: context.core.savedObjects.client,
43+
});
44+
45+
const dynamicIndexPattern = await getDynamicIndexPattern({
46+
context,
47+
indices,
48+
});
49+
3950
return { dynamicIndexPattern };
4051
},
4152
}));

x-pack/plugins/apm/server/routes/ui_filters.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ function createLocalFiltersRoute<
9797
query,
9898
setup: {
9999
...setup,
100-
uiFiltersES: getUiFiltersES(
101-
setup.dynamicIndexPattern,
102-
omit(parsedUiFilters, filterNames)
103-
),
100+
uiFiltersES: getUiFiltersES(omit(parsedUiFilters, filterNames)),
104101
},
105102
});
106103

0 commit comments

Comments
 (0)