From c5f7506ca4656b2d686395596d5ee394e704aded Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Mon, 5 Jul 2021 11:34:15 +0300 Subject: [PATCH 1/3] export executionContext types --- src/core/public/execution_context/index.ts | 1 + src/core/public/index.ts | 2 ++ src/core/server/index.ts | 1 + 3 files changed, 4 insertions(+) diff --git a/src/core/public/execution_context/index.ts b/src/core/public/execution_context/index.ts index efb501b61b5f0..b941af597bcc4 100644 --- a/src/core/public/execution_context/index.ts +++ b/src/core/public/execution_context/index.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +export type { KibanaExecutionContext } from '../../types'; export { ExecutionContextService } from './execution_context_service'; export type { ExecutionContextServiceStart } from './execution_context_service'; export type { ExecutionContextContainer } from './execution_context_container'; diff --git a/src/core/public/index.ts b/src/core/public/index.ts index a7fffb5769715..7cb3bd8b4cb5a 100644 --- a/src/core/public/index.ts +++ b/src/core/public/index.ts @@ -190,6 +190,8 @@ export type { MountPoint, UnmountCallback, PublicUiSettingsParams } from './type export { URL_MAX_LENGTH } from './core_app'; +export type { KibanaExecutionContext } from './execution_context'; + /** * Core services exposed to the `Plugin` setup lifecycle * diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 47af421882b1a..a187a0237e83b 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -543,6 +543,7 @@ export type { CapabilitiesSetup, CapabilitiesStart, ContextSetup, + ExecutionContextSetup, HttpResources, PluginsServiceSetup, PluginsServiceStart, From 05c682ecfe94d6bca6f6a3a82ff36541fe435068 Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Mon, 5 Jul 2021 12:09:19 +0300 Subject: [PATCH 2/3] pass execution context for expressions and bsearch --- .../common/search/expressions/esaggs/request_handler.ts | 5 ++++- .../common/search/expressions/kibana_context_type.ts | 2 ++ src/plugins/data/common/search/types.ts | 6 ++++-- src/plugins/data/public/search/expressions/esaggs.ts | 7 ++++++- .../search/search_interceptor/search_interceptor.ts | 3 +++ src/plugins/data/server/search/routes/bsearch.ts | 9 +++++++-- src/plugins/data/server/search/search_service.ts | 6 +++++- src/plugins/expressions/common/execution/execution.ts | 1 + src/plugins/expressions/common/execution/types.ts | 3 +++ .../expressions/common/service/expressions_services.ts | 3 +++ src/plugins/expressions/public/loader.ts | 3 +++ src/plugins/expressions/public/types/index.ts | 3 ++- .../public/embeddable/visualize_embeddable.ts | 6 ++++++ 13 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/plugins/data/common/search/expressions/esaggs/request_handler.ts b/src/plugins/data/common/search/expressions/esaggs/request_handler.ts index 61193c52a5e74..7ac50d4b0eddf 100644 --- a/src/plugins/data/common/search/expressions/esaggs/request_handler.ts +++ b/src/plugins/data/common/search/expressions/esaggs/request_handler.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { KibanaExecutionContext } from 'src/core/public'; import { i18n } from '@kbn/i18n'; import { Adapters } from 'src/plugins/inspector/common'; @@ -30,6 +30,7 @@ export interface RequestHandlerParams { timeFields?: string[]; timeRange?: TimeRange; getNow?: () => Date; + executionContext?: KibanaExecutionContext; } export const handleRequest = async ({ @@ -45,6 +46,7 @@ export const handleRequest = async ({ timeFields, timeRange, getNow, + executionContext, }: RequestHandlerParams) => { const forceNow = getNow?.(); const searchSource = await searchSourceService.create(); @@ -116,6 +118,7 @@ export const handleRequest = async ({ 'This request queries Elasticsearch to fetch the data for the visualization.', }), }, + executionContext, }) .toPromise(); diff --git a/src/plugins/data/common/search/expressions/kibana_context_type.ts b/src/plugins/data/common/search/expressions/kibana_context_type.ts index 0a7c365bb2914..67a2e827884a0 100644 --- a/src/plugins/data/common/search/expressions/kibana_context_type.ts +++ b/src/plugins/data/common/search/expressions/kibana_context_type.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import type { KibanaExecutionContext } from 'src/core/public'; import { ExpressionValueBoxed } from 'src/plugins/expressions/common'; import { Filter } from '../../es_query'; import { Query, TimeRange } from '../../query'; @@ -16,6 +17,7 @@ export type ExecutionContextSearch = { filters?: Filter[]; query?: Query | Query[]; timeRange?: TimeRange; + executionContext?: KibanaExecutionContext; }; export type ExpressionValueSearchContext = ExpressionValueBoxed< diff --git a/src/plugins/data/common/search/types.ts b/src/plugins/data/common/search/types.ts index c5cf3f9f09e6c..cffba6203dfe2 100644 --- a/src/plugins/data/common/search/types.ts +++ b/src/plugins/data/common/search/types.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { KibanaExecutionContext } from 'src/core/public'; import { Observable } from 'rxjs'; import { IEsSearchRequest, IEsSearchResponse, IndexPattern } from '..'; import type { RequestAdapter } from '../../../inspector/common'; @@ -134,6 +134,8 @@ export interface ISearchOptions { * Inspector integration options */ inspector?: IInspectorInfo; + + executionContext?: KibanaExecutionContext; } /** @@ -142,5 +144,5 @@ export interface ISearchOptions { */ export type ISearchOptionsSerializable = Pick< ISearchOptions, - 'strategy' | 'legacyHitsTotal' | 'sessionId' | 'isStored' | 'isRestore' + 'strategy' | 'legacyHitsTotal' | 'sessionId' | 'isStored' | 'isRestore' | 'executionContext' >; diff --git a/src/plugins/data/public/search/expressions/esaggs.ts b/src/plugins/data/public/search/expressions/esaggs.ts index 1e3d56c71e423..a8fd1b747ff23 100644 --- a/src/plugins/data/public/search/expressions/esaggs.ts +++ b/src/plugins/data/public/search/expressions/esaggs.ts @@ -35,7 +35,11 @@ export function getFunctionDefinition({ }) { return (): EsaggsExpressionFunctionDefinition => ({ ...getEsaggsMeta(), - async fn(input, args, { inspectorAdapters, abortSignal, getSearchSessionId }) { + async fn( + input, + args, + { inspectorAdapters, abortSignal, getSearchSessionId, executionContext } + ) { const { aggs, indexPatterns, searchSource, getNow } = await getStartDependencies(); const indexPattern = await indexPatterns.create(args.index.value, true); @@ -58,6 +62,7 @@ export function getFunctionDefinition({ timeFields: args.timeFields, timeRange: get(input, 'timeRange', undefined), getNow, + executionContext, }); }, }); diff --git a/src/plugins/data/public/search/search_interceptor/search_interceptor.ts b/src/plugins/data/public/search/search_interceptor/search_interceptor.ts index e0e1df65101c7..ff3c173fd18cf 100644 --- a/src/plugins/data/public/search/search_interceptor/search_interceptor.ts +++ b/src/plugins/data/public/search/search_interceptor/search_interceptor.ts @@ -188,6 +188,9 @@ export class SearchInterceptor { serializableOptions.legacyHitsTotal = combined.legacyHitsTotal; if (combined.strategy !== undefined) serializableOptions.strategy = combined.strategy; if (combined.isStored !== undefined) serializableOptions.isStored = combined.isStored; + if (combined.executionContext !== undefined) { + serializableOptions.executionContext = combined.executionContext; + } return serializableOptions; } diff --git a/src/plugins/data/server/search/routes/bsearch.ts b/src/plugins/data/server/search/routes/bsearch.ts index 49d469945ce29..e655a19e46f80 100644 --- a/src/plugins/data/server/search/routes/bsearch.ts +++ b/src/plugins/data/server/search/routes/bsearch.ts @@ -8,6 +8,7 @@ import { catchError, first } from 'rxjs/operators'; import { BfetchServerSetup } from 'src/plugins/bfetch/server'; +import type { ExecutionContextSetup } from 'src/core/server'; import { IKibanaSearchRequest, IKibanaSearchResponse, @@ -17,7 +18,8 @@ import { ISearchStart } from '../types'; export function registerBsearchRoute( bfetch: BfetchServerSetup, - getScoped: ISearchStart['asScoped'] + getScoped: ISearchStart['asScoped'], + executionContextService: ExecutionContextSetup ): void { bfetch.addBatchProcessingRoute< { request: IKibanaSearchRequest; options?: ISearchOptionsSerializable }, @@ -30,8 +32,11 @@ export function registerBsearchRoute( */ onBatchItem: async ({ request: requestData, options }) => { const search = getScoped(request); + const { executionContext, ...restOptions } = options || {}; + if (executionContext) executionContextService.set(executionContext); + return search - .search(requestData, options) + .search(requestData, restOptions) .pipe( first(), catchError((err) => { diff --git a/src/plugins/data/server/search/search_service.ts b/src/plugins/data/server/search/search_service.ts index 00dffefa5e3a6..dc0a494aeb187 100644 --- a/src/plugins/data/server/search/search_service.ts +++ b/src/plugins/data/server/search/search_service.ts @@ -158,7 +158,11 @@ export class SearchService implements Plugin { this.registerSearchStrategy(EQL_SEARCH_STRATEGY, eqlSearchStrategyProvider(this.logger)); - registerBsearchRoute(bfetch, (request: KibanaRequest) => this.asScoped(request)); + registerBsearchRoute( + bfetch, + (request: KibanaRequest) => this.asScoped(request), + core.executionContext + ); core.savedObjects.registerType(searchTelemetry); if (usageCollection) { diff --git a/src/plugins/expressions/common/execution/execution.ts b/src/plugins/expressions/common/execution/execution.ts index b70f261ea4b20..828558f0bc3fe 100644 --- a/src/plugins/expressions/common/execution/execution.ts +++ b/src/plugins/expressions/common/execution/execution.ts @@ -198,6 +198,7 @@ export class Execution< }, isSyncColorsEnabled: () => execution.params.syncColors, ...(execution.params as any).extraContext, + executionContext: execution.params.executionContext, }; this.result = this.input$.pipe( diff --git a/src/plugins/expressions/common/execution/types.ts b/src/plugins/expressions/common/execution/types.ts index d9c8682567b30..78ff7b2eb330a 100644 --- a/src/plugins/expressions/common/execution/types.ts +++ b/src/plugins/expressions/common/execution/types.ts @@ -8,6 +8,7 @@ // eslint-disable-next-line @kbn/eslint/no-restricted-paths import type { KibanaRequest } from 'src/core/server'; +import type { KibanaExecutionContext } from 'src/core/public'; import { ExpressionType, SerializableState } from '../expression_types'; import { Adapters, RequestAdapter } from '../../../inspector/common'; @@ -62,6 +63,8 @@ export interface ExecutionContext< * Returns the state (true|false) of the sync colors across panels switch. */ isSyncColorsEnabled?: () => boolean; + + executionContext?: KibanaExecutionContext; } /** diff --git a/src/plugins/expressions/common/service/expressions_services.ts b/src/plugins/expressions/common/service/expressions_services.ts index b3c0167262661..2b271fe84cbfa 100644 --- a/src/plugins/expressions/common/service/expressions_services.ts +++ b/src/plugins/expressions/common/service/expressions_services.ts @@ -9,6 +9,7 @@ import { take } from 'rxjs/operators'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import type { KibanaRequest } from 'src/core/server'; +import type { KibanaExecutionContext } from 'src/core/public'; import { Executor } from '../executor'; import { AnyExpressionRenderDefinition, ExpressionRendererRegistry } from '../expression_renderers'; @@ -77,6 +78,8 @@ export interface ExpressionExecutionParams { syncColors?: boolean; inspectorAdapters?: Adapters; + + executionContext?: KibanaExecutionContext; } /** diff --git a/src/plugins/expressions/public/loader.ts b/src/plugins/expressions/public/loader.ts index 4165b8906a20e..3ad11c2460184 100644 --- a/src/plugins/expressions/public/loader.ts +++ b/src/plugins/expressions/public/loader.ts @@ -147,6 +147,7 @@ export class ExpressionLoader { searchSessionId: params.searchSessionId, debug: params.debug, syncColors: params.syncColors, + executionContext: params.executionContext, }); const prevDataHandler = this.execution; @@ -187,6 +188,8 @@ export class ExpressionLoader { this.params.inspectorAdapters = (params.inspectorAdapters || this.execution?.inspect()) as Adapters; + + this.params.executionContext = params.executionContext; } } diff --git a/src/plugins/expressions/public/types/index.ts b/src/plugins/expressions/public/types/index.ts index 947b84ec152ac..5302b623e77f8 100644 --- a/src/plugins/expressions/public/types/index.ts +++ b/src/plugins/expressions/public/types/index.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { KibanaExecutionContext } from 'src/core/public'; import { Adapters } from '../../../inspector/public'; import { IInterpreterRenderHandlers, @@ -48,6 +48,7 @@ export interface IExpressionLoaderParams { renderMode?: RenderMode; syncColors?: boolean; hasCompatibleActions?: ExpressionRenderHandlerParams['hasCompatibleActions']; + executionContext?: KibanaExecutionContext; } export interface ExpressionRenderError extends Error { diff --git a/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts b/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts index 03fa8d415aca2..9b2dbd2c4c560 100644 --- a/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts +++ b/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts @@ -391,6 +391,12 @@ export class VisualizeEmbeddable syncColors: this.input.syncColors, uiState: this.vis.uiState, inspectorAdapters: this.inspectorAdapters, + executionContext: { + type: this.vis.params.type, + name: this.vis.type.name, + id: this.vis.id ?? 'unknown_id', + description: this.vis.title ?? this.vis.type.title, + }, }; if (this.abortController) { this.abortController.abort(); From 0e2daa6083ecee435ab5654ae65c86d193beb435 Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Mon, 5 Jul 2021 12:10:41 +0300 Subject: [PATCH 3/3] instrument lens --- x-pack/plugins/lens/public/embeddable/embeddable.tsx | 8 ++++++++ .../plugins/lens/public/embeddable/expression_wrapper.tsx | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/x-pack/plugins/lens/public/embeddable/embeddable.tsx b/x-pack/plugins/lens/public/embeddable/embeddable.tsx index c114aca95578a..f0879c2ba22ab 100644 --- a/x-pack/plugins/lens/public/embeddable/embeddable.tsx +++ b/x-pack/plugins/lens/public/embeddable/embeddable.tsx @@ -8,6 +8,7 @@ import { isEqual, uniqBy } from 'lodash'; import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; +import type { KibanaExecutionContext } from 'src/core/public'; import { ExecutionContextSearch, Filter, @@ -324,6 +325,12 @@ export class Embeddable this.input.onLoad(true); } const input = this.getInput(); + const executionContext: KibanaExecutionContext = { + type: this.savedVis.type ?? 'lens', + name: this.savedVis.visualizationType ?? '', + description: this.savedVis.title ?? this.savedVis.description ?? '', + id: this.id, + }; render( { this.logError('runtime'); diff --git a/x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx b/x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx index 34b373b87465d..fc6fcee9428b0 100644 --- a/x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx +++ b/x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx @@ -14,6 +14,7 @@ import { ReactExpressionRendererType, ReactExpressionRendererProps, } from 'src/plugins/expressions/public'; +import type { KibanaExecutionContext } from 'src/core/public'; import { ExecutionContextSearch } from 'src/plugins/data/public'; import { DefaultInspectorAdapters, RenderMode } from 'src/plugins/expressions'; import classNames from 'classnames'; @@ -39,6 +40,7 @@ export interface ExpressionWrapperProps { className?: string; canEdit: boolean; onRuntimeError: () => void; + executionContext?: KibanaExecutionContext; } interface VisualizationErrorProps { @@ -108,6 +110,7 @@ export function ExpressionWrapper({ errors, canEdit, onRuntimeError, + executionContext, }: ExpressionWrapperProps) { return ( @@ -125,6 +128,7 @@ export function ExpressionWrapper({ onData$={onData$} renderMode={renderMode} syncColors={syncColors} + executionContext={executionContext} renderError={(errorMessage, error) => { onRuntimeError(); return (