src/core/packages/execution-context/browser/README.md
Browser-side execution context helps Kibana associate HTTP requests with a meaningful “where did this come from?” context (app/page/entity). Core includes this context in the x-kbn-context header on core.http.fetch(...) requests so the server can propagate it (for example into Elasticsearch x-opaque-id for slow log tracing).
This package contains the public contract types for core.executionContext in the browser. The common data shape is KibanaExecutionContext from @kbn/core-execution-context-common.
Use the useExecutionContext hook from @kbn/kibana-react-plugin/public in your React components.
import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';
import { useExecutionContext } from '@kbn/kibana-react-plugin/public';
const ctx: KibanaExecutionContext = {
type: 'application',
name: 'discover',
page: 'sessionView',
id: discoverSessionId,
};
useExecutionContext(core.executionContext, ctx);
Notes:
page should be a stable logical unit (don’t embed ids in page; use id for identifiers).child (embeddables/components)If you already have a parent context (e.g. from an app), add a child context for a nested component:
import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';
const parent: KibanaExecutionContext = {
type: 'application',
name: 'dashboard',
page: 'view',
id: dashboardId,
};
const ctx: KibanaExecutionContext = {
...parent,
child: { type: 'visualization', name: embeddableType, id: embeddableId },
};
This pattern is used in e.g. ML embeddables: use_embeddable_execution_context.ts.
http.fetch)You can attach a per-request context that will be merged with the current global context:
await core.http.fetch('/api/my_plugin/do_something', {
context: { type: 'myPlugin', name: 'doSomething', id: entityId },
});
space)KibanaExecutionContext.space is the active space id. In the browser, the Spaces plugin keeps it in sync by calling core.executionContext.set({ space }), so most plugins shouldn’t need to set it themselves.
The x-kbn-context header is URI-encoded JSON and is size-limited. Keep context values small and avoid sensitive data (especially in description and meta).