Skip to content

Commit

Permalink
[ML] Adding ML execution context to es requests (#148746)
Browse files Browse the repository at this point in the history
Adds the page path as the ID to our existing execution context and adds
an additional context entry to every ml kibana endpoint.
In the search slow log, the id for each slow search from ML will look
like this:


`"a90d5297-fd77-4ea0-ac0d-c302963d7e75;kibana:application:ml:%2Fjobs%2Fnew_job%2Fsingle_metric;application:ml:%2Fapi%2Fml%2Fjobs%2Fnew_job_line_chart`

Separating by semicolon:
`a90d5297-fd77-4ea0-ac0d-c302963d7e75` -> kibana ID

`kibana:application:ml:%2Fjobs%2Fnew_job%2Fsingle_metric` -> default
context items added on the client side to show the source page.

`application:ml:%2Fapi%2Fml%2Fjobs%2Fnew_job_line_chart` -> new items
added by the server to show the source endpoint path.

Note, the paths have been encoded to replace the forward slashes.

Part of #147378
  • Loading branch information
jgowdyelastic committed Jan 24, 2023
1 parent badf9a8 commit afb84dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const useActiveRoute = (routesList: MlRoute[]): MlRoute => {
name: PLUGIN_ID,
type: 'application',
page: activeRoute?.path ?? '/overview',
id: activeRoute?.path,
});

return activeRoute ?? routesMap['/overview'];
Expand Down
57 changes: 41 additions & 16 deletions x-pack/plugins/ml/server/lib/route_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import type {
IScopedClusterClient,
RequestHandler,
SavedObjectsClientContract,
CoreSetup,
CoreStart,
} from '@kbn/core/server';
import type { SpacesPluginSetup } from '@kbn/spaces-plugin/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';

import type { AlertingApiRequestHandlerContext } from '@kbn/alerting-plugin/server';
import type { PluginStart as DataViewsPluginStart } from '@kbn/data-views-plugin/server';
import type { DataViewsService } from '@kbn/data-views-plugin/common';
import { PLUGIN_ID } from '../../common/constants/app';
import { mlSavedObjectServiceFactory, MLSavedObjectService } from '../saved_objects';
import type { MlLicense } from '../../common/license';

Expand Down Expand Up @@ -51,6 +54,7 @@ export class RouteGuard {
private _authorization: SecurityPluginSetup['authz'] | undefined;
private _isMlReady: () => Promise<void>;
private _getDataViews: GetDataViews;
private _getStartServices: CoreSetup['getStartServices'];

constructor(
mlLicense: MlLicense,
Expand All @@ -59,7 +63,8 @@ export class RouteGuard {
spacesPlugin: SpacesPluginSetup | undefined,
authorization: SecurityPluginSetup['authz'] | undefined,
isMlReady: () => Promise<void>,
getDataViews: GetDataViews
getDataViews: GetDataViews,
getStartServices: CoreSetup['getStartServices']
) {
this._mlLicense = mlLicense;
this._getMlSavedObjectClient = getSavedObject;
Expand All @@ -68,6 +73,7 @@ export class RouteGuard {
this._authorization = authorization;
this._isMlReady = isMlReady;
this._getDataViews = getDataViews;
this._getStartServices = getStartServices;
}

public fullLicenseAPIGuard<P, Q, B>(handler: Handler<P, Q, B>) {
Expand All @@ -88,8 +94,11 @@ export class RouteGuard {
return response.forbidden();
}

const { elasticsearch, savedObjects } = await context.core;
const client = elasticsearch.client;
const {
elasticsearch: { client },
savedObjects: { client: savedObjectClient },
} = await context.core;

const mlSavedObjectClient = this._getMlSavedObjectClient(request);
const internalSavedObjectsClient = this._getInternalSavedObjectClient();
if (mlSavedObjectClient === null || internalSavedObjectsClient === null) {
Expand All @@ -107,20 +116,36 @@ export class RouteGuard {
this._isMlReady
);

return handler({
client,
request,
response,
context,
mlSavedObjectService,
mlClient: getMlClient(client, mlSavedObjectService),
getDataViewsService: getDataViewsServiceFactory(
this._getDataViews,
savedObjects.client,
const [coreStart] = await this._getStartServices();
const executionContext = await createExecutionContext(coreStart, request.route.path);

return await coreStart.executionContext.withContext(executionContext, () =>
handler({
client,
request
),
});
request,
response,
context,
mlSavedObjectService,
mlClient: getMlClient(client, mlSavedObjectService),
getDataViewsService: getDataViewsServiceFactory(
this._getDataViews,
savedObjectClient,
client,
request
),
})
);
};
}
}

async function createExecutionContext(coreStart: CoreStart, id?: string) {
const labels = coreStart.executionContext.getAsLabels();
const page = labels.page as string;
return {
type: 'application',
name: PLUGIN_ID,
id: id ?? page,
page,
};
}
3 changes: 2 additions & 1 deletion x-pack/plugins/ml/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ export class MlServerPlugin
plugins.spaces,
plugins.security?.authz,
() => this.isMlReady,
() => this.dataViews
() => this.dataViews,
coreSetup.getStartServices
),
mlLicense: this.mlLicense,
};
Expand Down

0 comments on commit afb84dc

Please sign in to comment.