@@ -11,13 +11,21 @@ import { first } from 'rxjs';
1111import { schema } from '@kbn/config-schema' ;
1212import { reportServerError } from '@kbn/kibana-utils-plugin/server' ;
1313import { IncomingMessage } from 'http' ;
14+ import type { KibanaExecutionContext } from '@kbn/core-execution-context-common' ;
15+ import { Logger } from '@kbn/logging' ;
16+ import type { ExecutionContextSetup } from '@kbn/core-execution-context-server' ;
17+ import apm from 'elastic-apm-node' ;
1418import { reportSearchError } from '../report_search_error' ;
1519import { getRequestAbortedSignal } from '../../lib' ;
1620import type { DataPluginRouter } from '../types' ;
1721
1822export const SEARCH_API_BASE_URL = '/internal/search' ;
1923
20- export function registerSearchRoute ( router : DataPluginRouter ) : void {
24+ export function registerSearchRoute (
25+ router : DataPluginRouter ,
26+ logger : Logger ,
27+ executionContextSetup : ExecutionContextSetup
28+ ) : void {
2129 router . versioned
2230 . post ( {
2331 path : `${ SEARCH_API_BASE_URL } /{strategy}/{id?}` ,
@@ -65,39 +73,54 @@ export function registerSearchRoute(router: DataPluginRouter): void {
6573 const { strategy, id } = request . params ;
6674 const abortSignal = getRequestAbortedSignal ( request . events . aborted$ ) ;
6775
76+ let executionContext : KibanaExecutionContext | undefined ;
77+ const contextHeader = request . headers [ 'x-kbn-context' ] ;
6878 try {
69- const search = await context . search ;
70- const response = await search
71- . search (
72- { ...searchRequest , id } ,
73- {
74- abortSignal,
75- strategy,
76- legacyHitsTotal,
77- sessionId,
78- isStored,
79- isRestore,
80- retrieveResults,
81- stream,
82- }
83- )
84- . pipe ( first ( ) )
85- . toPromise ( ) ;
86-
87- if ( response && ( response . rawResponse as unknown as IncomingMessage ) . pipe ) {
88- return res . ok ( {
89- body : response . rawResponse ,
90- headers : {
91- 'kbn-search-is-restored' : response . isRestored ? '?1' : '?0' ,
92- 'kbn-search-request-params' : JSON . stringify ( response . requestParams ) ,
93- } ,
94- } ) ;
95- } else {
96- return res . ok ( { body : response } ) ;
79+ if ( contextHeader != null ) {
80+ executionContext = JSON . parse (
81+ decodeURIComponent ( Array . isArray ( contextHeader ) ? contextHeader [ 0 ] : contextHeader )
82+ ) ;
9783 }
9884 } catch ( err ) {
99- return reportSearchError ( res , err ) ;
85+ logger . error ( `Error parsing search execution context: ${ contextHeader } ` ) ;
10086 }
87+
88+ return executionContextSetup . withContext ( executionContext , async ( ) => {
89+ apm . addLabels ( executionContextSetup . getAsLabels ( ) ) ;
90+ try {
91+ const search = await context . search ;
92+ const response = await search
93+ . search (
94+ { ...searchRequest , id } ,
95+ {
96+ abortSignal,
97+ strategy,
98+ legacyHitsTotal,
99+ sessionId,
100+ isStored,
101+ isRestore,
102+ retrieveResults,
103+ stream,
104+ }
105+ )
106+ . pipe ( first ( ) )
107+ . toPromise ( ) ;
108+
109+ if ( response && ( response . rawResponse as unknown as IncomingMessage ) . pipe ) {
110+ return res . ok ( {
111+ body : response . rawResponse ,
112+ headers : {
113+ 'kbn-search-is-restored' : response . isRestored ? '?1' : '?0' ,
114+ 'kbn-search-request-params' : JSON . stringify ( response . requestParams ) ,
115+ } ,
116+ } ) ;
117+ } else {
118+ return res . ok ( { body : response } ) ;
119+ }
120+ } catch ( err ) {
121+ return reportSearchError ( res , err ) ;
122+ }
123+ } ) ;
101124 }
102125 ) ;
103126
0 commit comments