Skip to content

Commit

Permalink
[APM] Don't fetch dynamic index pattern in setupRequest (#70308)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
dgieselaar and elasticmachine authored Jul 2, 2020
1 parent c081caa commit 429805d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ describe('timeseriesFetcher', () => {
apmAgentConfigurationIndex: '.apm-agent-configuration',
apmCustomLinkIndex: '.apm-custom-link',
},
dynamicIndexPattern: null as any,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ import {
localUIFilters,
localUIFilterNames,
} from '../../ui_filters/local_ui_filters/config';
import {
esKuery,
IIndexPattern,
} from '../../../../../../../src/plugins/data/server';
import { esKuery } from '../../../../../../../src/plugins/data/server';

export function getUiFiltersES(
indexPattern: IIndexPattern | undefined,
uiFilters: UIFilters
) {
export function getUiFiltersES(uiFilters: UIFilters) {
const { kuery, environment, ...localFilterValues } = uiFilters;
const mappedFilters = localUIFilterNames
.filter((name) => name in localFilterValues)
Expand All @@ -35,7 +29,7 @@ export function getUiFiltersES(

// remove undefined items from list
const esFilters = [
getKueryUiFilterES(indexPattern, uiFilters.kuery),
getKueryUiFilterES(uiFilters.kuery),
getEnvironmentUiFilterES(uiFilters.environment),
]
.filter((filter) => !!filter)
Expand All @@ -44,14 +38,11 @@ export function getUiFiltersES(
return esFilters;
}

function getKueryUiFilterES(
indexPattern: IIndexPattern | undefined,
kuery?: string
) {
if (!kuery || !indexPattern) {
function getKueryUiFilterES(kuery?: string) {
if (!kuery) {
return;
}

const ast = esKuery.fromKueryExpression(kuery);
return esKuery.toElasticsearchQuery(ast, indexPattern) as ESFilter;
return esKuery.toElasticsearchQuery(ast) as ESFilter;
}
44 changes: 23 additions & 21 deletions x-pack/plugins/apm/server/lib/helpers/es_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import {
ESSearchRequest,
ESSearchResponse,
} from '../../../typings/elasticsearch';
import { UI_SETTINGS } from '../../../../../../src/plugins/data/server';
import { OBSERVER_VERSION_MAJOR } from '../../../common/elasticsearch_fieldnames';
import { pickKeys } from '../../../common/utils/pick_keys';
import { APMRequestHandlerContext } from '../../routes/typings';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices';

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

// add additional params for search (aka: read) requests
async function getParamsForSearchRequest(
context: APMRequestHandlerContext,
params: ESSearchRequest,
apmOptions?: APMOptions
) {
const { uiSettings } = context.core;
const [indices, includeFrozen] = await Promise.all([
getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config: context.config,
}),
uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
]);

function getParamsForSearchRequest({
context,
params,
indices,
includeFrozen,
includeLegacyData,
}: {
context: APMRequestHandlerContext;
params: ESSearchRequest;
indices: ApmIndicesConfig;
includeFrozen: boolean;
includeLegacyData?: boolean;
}) {
// Get indices for legacy data filter (only those which apply)
const apmIndices = Object.values(
pickKeys(
Expand All @@ -112,7 +110,7 @@ async function getParamsForSearchRequest(
)
);
return {
...addFilterForLegacyData(apmIndices, params, apmOptions), // filter out pre-7.0 data
...addFilterForLegacyData(apmIndices, params, { includeLegacyData }), // filter out pre-7.0 data
ignore_throttled: !includeFrozen, // whether to query frozen indices or not
};
}
Expand All @@ -123,6 +121,8 @@ interface APMOptions {

interface ClientCreateOptions {
clientAsInternalUser?: boolean;
indices: ApmIndicesConfig;
includeFrozen: boolean;
}

export type ESClient = ReturnType<typeof getESClient>;
Expand All @@ -134,7 +134,7 @@ function formatObj(obj: Record<string, any>) {
export function getESClient(
context: APMRequestHandlerContext,
request: KibanaRequest,
{ clientAsInternalUser = false }: ClientCreateOptions = {}
{ clientAsInternalUser = false, indices, includeFrozen }: ClientCreateOptions
) {
const {
callAsCurrentUser,
Expand Down Expand Up @@ -194,11 +194,13 @@ export function getESClient(
params: TSearchRequest,
apmOptions?: APMOptions
): Promise<ESSearchResponse<TDocument, TSearchRequest>> => {
const nextParams = await getParamsForSearchRequest(
const nextParams = await getParamsForSearchRequest({
context,
params,
apmOptions
);
indices,
includeFrozen,
...apmOptions,
});

return callEs('search', nextParams);
},
Expand Down
40 changes: 20 additions & 20 deletions x-pack/plugins/apm/server/lib/helpers/setup_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import moment from 'moment';
import { UI_SETTINGS } from '../../../../../../src/plugins/data/common';
import { KibanaRequest } from '../../../../../../src/core/server';
import { IIndexPattern } from '../../../../../../src/plugins/data/common';
import { APMConfig } from '../..';
import {
getApmIndices,
Expand All @@ -18,17 +18,13 @@ import { getUiFiltersES } from './convert_ui_filters/get_ui_filters_es';
import { APMRequestHandlerContext } from '../../routes/typings';
import { getESClient } from './es_client';
import { ProcessorEvent } from '../../../common/processor_event';
import { getDynamicIndexPattern } from '../index_pattern/get_dynamic_index_pattern';

function decodeUiFilters(
indexPattern: IIndexPattern | undefined,
uiFiltersEncoded?: string
) {
if (!uiFiltersEncoded || !indexPattern) {
function decodeUiFilters(uiFiltersEncoded?: string) {
if (!uiFiltersEncoded) {
return [];
}
const uiFilters = JSON.parse(uiFiltersEncoded);
return getUiFiltersES(indexPattern, uiFilters);
return getUiFiltersES(uiFilters);
}
// Explicitly type Setup to prevent TS initialization errors
// https://github.com/microsoft/TypeScript/issues/34933
Expand All @@ -39,7 +35,6 @@ export interface Setup {
ml?: ReturnType<typeof getMlSetup>;
config: APMConfig;
indices: ApmIndicesConfig;
dynamicIndexPattern?: IIndexPattern;
}

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

const indices = await getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config,
});
const [indices, includeFrozen] = await Promise.all([
getApmIndices({
savedObjectsClient: context.core.savedObjects.client,
config,
}),
context.core.uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
]);

const dynamicIndexPattern = await getDynamicIndexPattern({
context,
const createClientOptions = {
indices,
processorEvent: query.processorEvent,
});
includeFrozen,
};

const uiFiltersES = decodeUiFilters(dynamicIndexPattern, query.uiFilters);
const uiFiltersES = decodeUiFilters(query.uiFilters);

const coreSetupRequest = {
indices,
client: getESClient(context, request, { clientAsInternalUser: false }),
client: getESClient(context, request, {
clientAsInternalUser: false,
...createClientOptions,
}),
internalClient: getESClient(context, request, {
clientAsInternalUser: true,
...createClientOptions,
}),
ml: getMlSetup(context, request),
config,
dynamicIndexPattern,
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('timeseriesFetcher', () => {
apmAgentConfigurationIndex: 'myIndex',
apmCustomLinkIndex: 'myIndex',
},
dynamicIndexPattern: null as any,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
*/

import { omit } from 'lodash';
import { IIndexPattern } from 'src/plugins/data/server';
import { mergeProjection } from '../../../../common/projections/util/merge_projection';
import { Projection } from '../../../../common/projections/typings';
import { UIFilters } from '../../../../typings/ui_filters';
import { getUiFiltersES } from '../../helpers/convert_ui_filters/get_ui_filters_es';
import { localUIFilters, LocalUIFilterName } from './config';

export const getLocalFilterQuery = ({
indexPattern,
uiFilters,
projection,
localUIFilterName,
}: {
indexPattern: IIndexPattern | undefined;
uiFilters: UIFilters;
projection: Projection;
localUIFilterName: LocalUIFilterName;
}) => {
const field = localUIFilters[localUIFilterName];
const filter = getUiFiltersES(indexPattern, omit(uiFilters, field.name));
const filter = getUiFiltersES(omit(uiFilters, field.name));

const bucketCountAggregation = projection.body.aggs
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getLocalUIFilters({
uiFilters: UIFilters;
localFilterNames: LocalUIFilterName[];
}) {
const { client, dynamicIndexPattern } = setup;
const { client } = setup;

const projectionWithoutAggs = cloneDeep(projection);

Expand All @@ -35,7 +35,6 @@ export async function getLocalUIFilters({
return Promise.all(
localFilterNames.map(async (name) => {
const query = getLocalFilterQuery({
indexPattern: dynamicIndexPattern,
uiFilters,
projection,
localUIFilterName: name,
Expand Down
15 changes: 13 additions & 2 deletions x-pack/plugins/apm/server/routes/index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { createRoute } from './create_route';
import { setupRequest } from '../lib/helpers/setup_request';
import { getInternalSavedObjectsClient } from '../lib/helpers/get_internal_saved_objects_client';
import { getApmIndexPatternTitle } from '../lib/index_pattern/get_apm_index_pattern_title';
import { getDynamicIndexPattern } from '../lib/index_pattern/get_dynamic_index_pattern';
import { getApmIndices } from '../lib/settings/apm_indices/get_apm_indices';

export const staticIndexPatternRoute = createRoute((core) => ({
method: 'POST',
Expand All @@ -34,8 +36,17 @@ export const dynamicIndexPatternRoute = createRoute(() => ({
]),
}),
},
handler: async ({ context, request }) => {
const { dynamicIndexPattern } = await setupRequest(context, request);
handler: async ({ context }) => {
const indices = await getApmIndices({
config: context.config,
savedObjectsClient: context.core.savedObjects.client,
});

const dynamicIndexPattern = await getDynamicIndexPattern({
context,
indices,
});

return { dynamicIndexPattern };
},
}));
Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/apm/server/routes/ui_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ function createLocalFiltersRoute<
query,
setup: {
...setup,
uiFiltersES: getUiFiltersES(
setup.dynamicIndexPattern,
omit(parsedUiFilters, filterNames)
),
uiFiltersES: getUiFiltersES(omit(parsedUiFilters, filterNames)),
},
});

Expand Down

0 comments on commit 429805d

Please sign in to comment.