Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into split-types-from-co…
Browse files Browse the repository at this point in the history
…de-on-kbn-ui-shared-deps-src
  • Loading branch information
mistic committed Jan 14, 2022
2 parents 4fef419 + 98f531c commit 55ef0e3
Show file tree
Hide file tree
Showing 152 changed files with 2,952 additions and 781 deletions.
2 changes: 1 addition & 1 deletion docs/apm/troubleshooting.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ There are two things you can do to if you'd like to ensure a field is searchable
1. Index your additional data as {apm-guide-ref}/metadata.html[labels] instead.
These are dynamic by default, which means they will be indexed and become searchable and aggregatable.

2. Use the {apm-guide-ref}/configuration-template.html[`append_fields`] feature. As an example,
2. Use the `append_fields` feature. As an example,
adding the following to `apm-server.yml` will enable dynamic indexing for `http.request.cookies`:

[source,yml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ export const isQueryStringFilter = (filter: Filter): filter is QueryStringFilter
*
* @public
*/
export const buildQueryFilter = (query: QueryStringFilter['query'], index: string, alias: string) =>
({
query,
meta: {
index,
alias,
},
} as QueryStringFilter);
export const buildQueryFilter = (
query: QueryStringFilter['query'],
index: string,
alias?: string,
meta: QueryStringFilterMeta = {}
) => ({ query, meta: { index, alias, ...meta } });
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('migration v2', () => {
es: {
license: 'basic',
dataArchive: Path.join(__dirname, 'archives', '7.14.0_xpack_sample_saved_objects.zip'),
esArgs: ['http.max_content_length=1715275b'],
esArgs: ['http.max_content_length=1715329b'],
},
},
}));
Expand All @@ -85,7 +85,7 @@ describe('migration v2', () => {
});

it('completes the migration even when a full batch would exceed ES http.max_content_length', async () => {
root = createRoot({ maxBatchSizeBytes: 1715275 });
root = createRoot({ maxBatchSizeBytes: 1715329 });
esServer = await startES();
await root.preboot();
await root.setup();
Expand All @@ -109,7 +109,7 @@ describe('migration v2', () => {
await root.preboot();
await root.setup();
await expect(root.start()).rejects.toMatchInlineSnapshot(
`[Error: Unable to complete saved object migrations for the [.kibana] index: The document with _id "canvas-workpad-template:workpad-template-061d7868-2b4e-4dc8-8bf7-3772b52926e5" is 1715274 bytes which exceeds the configured maximum batch size of 1015275 bytes. To proceed, please increase the 'migrations.maxBatchSizeBytes' Kibana configuration option and ensure that the Elasticsearch 'http.max_content_length' configuration option is set to an equal or larger value.]`
`[Error: Unable to complete saved object migrations for the [.kibana] index: The document with _id "canvas-workpad-template:workpad-template-061d7868-2b4e-4dc8-8bf7-3772b52926e5" is 1715329 bytes which exceeds the configured maximum batch size of 1015275 bytes. To proceed, please increase the 'migrations.maxBatchSizeBytes' Kibana configuration option and ensure that the Elasticsearch 'http.max_content_length' configuration option is set to an equal or larger value.]`
);

await retryAsync(
Expand All @@ -122,7 +122,7 @@ describe('migration v2', () => {
expect(
records.find((rec) =>
rec.message.startsWith(
`Unable to complete saved object migrations for the [.kibana] index: The document with _id "canvas-workpad-template:workpad-template-061d7868-2b4e-4dc8-8bf7-3772b52926e5" is 1715274 bytes which exceeds the configured maximum batch size of 1015275 bytes. To proceed, please increase the 'migrations.maxBatchSizeBytes' Kibana configuration option and ensure that the Elasticsearch 'http.max_content_length' configuration option is set to an equal or larger value.`
`Unable to complete saved object migrations for the [.kibana] index: The document with _id "canvas-workpad-template:workpad-template-061d7868-2b4e-4dc8-8bf7-3772b52926e5" is 1715329 bytes which exceeds the configured maximum batch size of 1015275 bytes. To proceed, please increase the 'migrations.maxBatchSizeBytes' Kibana configuration option and ensure that the Elasticsearch 'http.max_content_length' configuration option is set to an equal or larger value.`
)
)
).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('migration v2', () => {
});

it('fails with a descriptive message when maxBatchSizeBytes exceeds ES http.max_content_length', async () => {
root = createRoot({ maxBatchSizeBytes: 1715275 });
root = createRoot({ maxBatchSizeBytes: 1715329 });
esServer = await startES();
await root.preboot();
await root.setup();
Expand Down
14 changes: 11 additions & 3 deletions src/plugins/data/common/search/expressions/kibana_context_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ExpressionValueBoxed } from 'src/plugins/expressions/common';
import { Filter } from '../../es_query';
import { Filter } from '@kbn/es-query';
import { ExpressionValueBoxed, ExpressionValueFilter } from 'src/plugins/expressions/common';
import { Query, TimeRange } from '../../query';
import { IndexPatternField } from '../..';
import { adaptToExpressionValueFilter, IndexPatternField } from '../..';

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type ExecutionContextSearch = {
Expand Down Expand Up @@ -45,5 +45,13 @@ export const kibanaContext = {
type: 'null',
};
},
filter: (input: KibanaContext): ExpressionValueFilter => {
const { filters = [] } = input;
return {
type: 'filter',
filterType: 'filter',
and: filters.map(adaptToExpressionValueFilter),
};
},
},
};
22 changes: 20 additions & 2 deletions src/plugins/data/common/search/expressions/select_filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('interpreter/functions#selectFilter', () => {
},
query: {},
},
{
meta: {
group: 'g3',
},
query: {},
},
{
meta: {
group: 'g1',
Expand Down Expand Up @@ -68,6 +74,12 @@ describe('interpreter/functions#selectFilter', () => {
},
"query": Object {},
},
Object {
"meta": Object {
"group": "g3",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i1",
Expand All @@ -94,8 +106,8 @@ describe('interpreter/functions#selectFilter', () => {
`);
});

it('selects filters belonging to certain group', () => {
const actual = fn(kibanaContext, { group: 'g1' }, createMockContext());
it('selects filters belonging to certain groups', () => {
const actual = fn(kibanaContext, { group: ['g1', 'g3'] }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Expand All @@ -105,6 +117,12 @@ describe('interpreter/functions#selectFilter', () => {
},
"query": Object {},
},
Object {
"meta": Object {
"group": "g3",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i1",
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/data/common/search/expressions/select_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { KibanaContext } from './kibana_context_type';

interface Arguments {
group?: string;
group: string[];
from?: string;
ungrouped?: boolean;
}
Expand All @@ -37,6 +37,7 @@ export const selectFilterFunction: ExpressionFunctionSelectFilter = {
help: i18n.translate('data.search.functions.selectFilter.group.help', {
defaultMessage: 'Select only filters belonging to the provided group',
}),
multi: true,
},
from: {
types: ['string'],
Expand All @@ -54,13 +55,15 @@ export const selectFilterFunction: ExpressionFunctionSelectFilter = {
},
},

fn(input, { group, ungrouped, from }) {
fn(input, { group = [], ungrouped, from }) {
return {
...input,
filters:
input.filters?.filter(({ meta }) => {
const isGroupMatching =
(!group && !ungrouped) || group === meta.group || (ungrouped && !meta.group);
(!group.length && !ungrouped) ||
(meta.group && group.length && group.includes(meta.group)) ||
(ungrouped && !meta.group);
const isOriginMatching = !from || from === meta.controlledBy;
return isGroupMatching && isOriginMatching;
}) || [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Filter } from '@kbn/es-query';
import { ExpressionValueFilter } from 'src/plugins/expressions/common';

function getGroupFromFilter(filter: Filter) {
const { meta } = filter;
const { group } = meta ?? {};
return group;
}

function range(filter: Filter): ExpressionValueFilter {
const { query } = filter;
const { range: rangeQuery } = query ?? {};
const column = Object.keys(rangeQuery)[0];
const { gte: from, lte: to } = rangeQuery[column] ?? {};
return {
filterGroup: getGroupFromFilter(filter),
from,
to,
column,
type: 'filter',
filterType: 'time',
and: [],
};
}

function luceneQueryString(filter: Filter): ExpressionValueFilter {
const { query } = filter;
const { query_string: queryString } = query ?? {};
const { query: queryValue } = queryString;

return {
filterGroup: getGroupFromFilter(filter),
query: queryValue,
type: 'filter',
filterType: 'luceneQueryString',
and: [],
};
}

function term(filter: Filter): ExpressionValueFilter {
const { query } = filter;
const { term: termQuery } = query ?? {};
const column = Object.keys(termQuery)[0];
const { value } = termQuery[column] ?? {};

return {
filterGroup: getGroupFromFilter(filter),
column,
value,
type: 'filter',
filterType: 'exactly',
and: [],
};
}

const adapters = { range, term, luceneQueryString };

export function adaptToExpressionValueFilter(filter: Filter): ExpressionValueFilter {
const { query = {} } = filter;
const filterType = Object.keys(query)[0] as keyof typeof adapters;
const adapt = adapters[filterType];
if (!adapt || typeof adapt !== 'function') {
throw new Error(`Unknown filter type: ${filterType}`);
}
return adapt(filter);
}
1 change: 1 addition & 0 deletions src/plugins/data/common/search/expressions/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export * from './function_wrapper';
export { adaptToExpressionValueFilter } from './filters_adapter';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/plugins/expressions/common/executor/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,40 @@ describe('Executor', () => {
});

describe('.migrateToLatest', () => {
const fnMigrateTo = {
name: 'fnMigrateTo',
help: 'test',
args: {
bar: {
types: ['string'],
help: 'test',
},
},
fn: jest.fn(),
};

const fnMigrateFrom = {
name: 'fnMigrateFrom',
help: 'test',
args: {
bar: {
types: ['string'],
help: 'test',
},
},
migrations: {
'8.1.0': ((state: ExpressionAstFunction, version: string) => {
const migrateToAst = parseExpression('fnMigrateTo');
const { arguments: args } = state;
const ast = { ...migrateToAst.chain[0], arguments: args };
return { type: 'expression', chain: [ast, ast] };
}) as unknown as MigrateFunction,
},
fn: jest.fn(),
};
executor.registerFunction(fnMigrateFrom);
executor.registerFunction(fnMigrateTo);

test('calls migrate function for every expression function in expression', () => {
executor.migrateToLatest({
state: parseExpression(
Expand All @@ -255,6 +289,25 @@ describe('Executor', () => {
});
expect(migrateFn).toBeCalledTimes(5);
});

test('migrates expression function to expression function or chain of expression functions', () => {
const plainExpression = 'foo bar={foo bar="baz" | foo bar={foo bar="baz"}}';
const plainExpressionAst = parseExpression(plainExpression);
const migratedExpressionAst = executor.migrateToLatest({
state: parseExpression(`${plainExpression} | fnMigrateFrom bar="baz" | fnMigrateTo`),
version: '8.0.0',
});

expect(migratedExpressionAst).toEqual({
type: 'expression',
chain: [
...plainExpressionAst.chain,
{ type: 'function', function: 'fnMigrateTo', arguments: { bar: ['baz'] } },
{ type: 'function', function: 'fnMigrateTo', arguments: { bar: ['baz'] } },
{ type: 'function', function: 'fnMigrateTo', arguments: {} },
],
});
});
});
});
});
Loading

0 comments on commit 55ef0e3

Please sign in to comment.