Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument data plugin with ExecutionContextService #8

Draft
wants to merge 3 commits into
base: issue-101587-tracing
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/public/execution_context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ export type {
CapabilitiesSetup,
CapabilitiesStart,
ContextSetup,
ExecutionContextSetup,
HttpResources,
PluginsServiceSetup,
PluginsServiceStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -30,6 +30,7 @@ export interface RequestHandlerParams {
timeFields?: string[];
timeRange?: TimeRange;
getNow?: () => Date;
executionContext?: KibanaExecutionContext;
}

export const handleRequest = async ({
Expand All @@ -45,6 +46,7 @@ export const handleRequest = async ({
timeFields,
timeRange,
getNow,
executionContext,
}: RequestHandlerParams) => {
const forceNow = getNow?.();
const searchSource = await searchSourceService.create();
Expand Down Expand Up @@ -116,6 +118,7 @@ export const handleRequest = async ({
'This request queries Elasticsearch to fetch the data for the visualization.',
}),
},
executionContext,
})
.toPromise();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,6 +17,7 @@ export type ExecutionContextSearch = {
filters?: Filter[];
query?: Query | Query[];
timeRange?: TimeRange;
executionContext?: KibanaExecutionContext;
};

export type ExpressionValueSearchContext = ExpressionValueBoxed<
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/data/common/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -134,6 +134,8 @@ export interface ISearchOptions {
* Inspector integration options
*/
inspector?: IInspectorInfo;

executionContext?: KibanaExecutionContext;
}

/**
Expand All @@ -142,5 +144,5 @@ export interface ISearchOptions {
*/
export type ISearchOptionsSerializable = Pick<
ISearchOptions,
'strategy' | 'legacyHitsTotal' | 'sessionId' | 'isStored' | 'isRestore'
'strategy' | 'legacyHitsTotal' | 'sessionId' | 'isStored' | 'isRestore' | 'executionContext'
>;
7 changes: 6 additions & 1 deletion src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -58,6 +62,7 @@ export function getFunctionDefinition({
timeFields: args.timeFields,
timeRange: get(input, 'timeRange', undefined),
getNow,
executionContext,
});
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/data/server/search/routes/bsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 },
Expand All @@ -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) => {
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {

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) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/common/execution/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/expressions/common/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -62,6 +63,8 @@ export interface ExecutionContext<
* Returns the state (true|false) of the sync colors across panels switch.
*/
isSyncColorsEnabled?: () => boolean;

executionContext?: KibanaExecutionContext;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,6 +78,8 @@ export interface ExpressionExecutionParams {
syncColors?: boolean;

inspectorAdapters?: Adapters;

executionContext?: KibanaExecutionContext;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/expressions/public/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class ExpressionLoader {
searchSessionId: params.searchSessionId,
debug: params.debug,
syncColors: params.syncColors,
executionContext: params.executionContext,
});

const prevDataHandler = this.execution;
Expand Down Expand Up @@ -187,6 +188,8 @@ export class ExpressionLoader {

this.params.inspectorAdapters = (params.inspectorAdapters ||
this.execution?.inspect()) as Adapters;

this.params.executionContext = params.executionContext;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/expressions/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -48,6 +48,7 @@ export interface IExpressionLoaderParams {
renderMode?: RenderMode;
syncColors?: boolean;
hasCompatibleActions?: ExpressionRenderHandlerParams['hasCompatibleActions'];
executionContext?: KibanaExecutionContext;
}

export interface ExpressionRenderError extends Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
<ExpressionWrapper
ExpressionRenderer={this.expressionRenderer}
Expand All @@ -339,6 +346,7 @@ export class Embeddable
hasCompatibleActions={this.hasCompatibleActions}
className={input.className}
style={input.style}
executionContext={executionContext}
canEdit={this.getIsEditable() && input.viewMode === 'edit'}
onRuntimeError={() => {
this.logError('runtime');
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -39,6 +40,7 @@ export interface ExpressionWrapperProps {
className?: string;
canEdit: boolean;
onRuntimeError: () => void;
executionContext?: KibanaExecutionContext;
}

interface VisualizationErrorProps {
Expand Down Expand Up @@ -108,6 +110,7 @@ export function ExpressionWrapper({
errors,
canEdit,
onRuntimeError,
executionContext,
}: ExpressionWrapperProps) {
return (
<I18nProvider>
Expand All @@ -125,6 +128,7 @@ export function ExpressionWrapper({
onData$={onData$}
renderMode={renderMode}
syncColors={syncColors}
executionContext={executionContext}
renderError={(errorMessage, error) => {
onRuntimeError();
return (
Expand Down