[APM] Don't fetch dynamic index pattern in setupRequest (#70308)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
c081caa634
commit
429805d1b8
9 changed files with 65 additions and 70 deletions
|
@ -56,7 +56,6 @@ describe('timeseriesFetcher', () => {
|
|||
apmAgentConfigurationIndex: '.apm-agent-configuration',
|
||||
apmCustomLinkIndex: '.apm-custom-link',
|
||||
},
|
||||
dynamicIndexPattern: null as any,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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'>;
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
};
|
||||
}
|
||||
|
@ -123,6 +121,8 @@ interface APMOptions {
|
|||
|
||||
interface ClientCreateOptions {
|
||||
clientAsInternalUser?: boolean;
|
||||
indices: ApmIndicesConfig;
|
||||
includeFrozen: boolean;
|
||||
}
|
||||
|
||||
export type ESClient = ReturnType<typeof getESClient>;
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -39,7 +35,6 @@ export interface Setup {
|
|||
ml?: ReturnType<typeof getMlSetup>;
|
||||
config: APMConfig;
|
||||
indices: ApmIndicesConfig;
|
||||
dynamicIndexPattern?: IIndexPattern;
|
||||
}
|
||||
|
||||
export interface SetupTimeRange {
|
||||
|
@ -75,28 +70,33 @@ export async function setupRequest<TParams extends SetupRequestParams>(
|
|||
const { config } = context;
|
||||
const { query } = context.params;
|
||||
|
||||
const indices = await getApmIndices({
|
||||
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 {
|
||||
|
|
|
@ -44,7 +44,6 @@ describe('timeseriesFetcher', () => {
|
|||
apmAgentConfigurationIndex: 'myIndex',
|
||||
apmCustomLinkIndex: 'myIndex',
|
||||
},
|
||||
dynamicIndexPattern: null as any,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
*/
|
||||
|
||||
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';
|
||||
|
@ -13,18 +12,16 @@ import { getUiFiltersES } from '../../helpers/convert_ui_filters/get_ui_filters_
|
|||
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
|
||||
? {
|
||||
|
|
|
@ -26,7 +26,7 @@ export async function getLocalUIFilters({
|
|||
uiFilters: UIFilters;
|
||||
localFilterNames: LocalUIFilterName[];
|
||||
}) {
|
||||
const { client, dynamicIndexPattern } = setup;
|
||||
const { client } = setup;
|
||||
|
||||
const projectionWithoutAggs = cloneDeep(projection);
|
||||
|
||||
|
@ -35,7 +35,6 @@ export async function getLocalUIFilters({
|
|||
return Promise.all(
|
||||
localFilterNames.map(async (name) => {
|
||||
const query = getLocalFilterQuery({
|
||||
indexPattern: dynamicIndexPattern,
|
||||
uiFilters,
|
||||
projection,
|
||||
localUIFilterName: name,
|
||||
|
|
|
@ -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',
|
||||
|
@ -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 };
|
||||
},
|
||||
}));
|
||||
|
|
|
@ -97,10 +97,7 @@ function createLocalFiltersRoute<
|
|||
query,
|
||||
setup: {
|
||||
...setup,
|
||||
uiFiltersES: getUiFiltersES(
|
||||
setup.dynamicIndexPattern,
|
||||
omit(parsedUiFilters, filterNames)
|
||||
),
|
||||
uiFiltersES: getUiFiltersES(omit(parsedUiFilters, filterNames)),
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue