Skip to content

Commit 5e811f1

Browse files
jesuswrcursoragent
andauthored
Add developer docs for execution context (elastic#253690)
## Summary - Add concise developer docs for Core execution context (browser + server), with usage examples and internal implementation notes. ## Related Addresses elastic#242415. Note: This PR does not close the issue yet because the remaining public/customer documentation lives in a different repository. Will close the issue when update that. ## Test plan - Docs-only change. Made with [Cursor](https://cursor.com) --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a2dd4c5 commit 5e811f1

5 files changed

Lines changed: 131 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# @kbn/core-execution-context-browser-internal
22

33
This package contains the internal types and implementation for Core's browser-side execution context service.
4+
5+
## Pointers
6+
7+
- `x-kbn-context` is attached to `core.http.fetch(...)` requests in `src/core/packages/http/browser-internal/src/fetch.ts`.
8+
- The header value is URI-encoded JSON produced by `src/core/packages/execution-context/browser-internal/src/execution_context_container.ts`.
9+
- `HttpFetchOptions.context` is merged with the current global context via `executionContext.withGlobalContext(...)` before serialization.
Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
11
# @kbn/core-execution-context-browser
22

3-
This package contains the public types for Core's browser-side execution context service.
3+
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).
4+
5+
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`](../common).
6+
7+
## How to set it (recommended)
8+
9+
Use the `useExecutionContext` hook from `@kbn/kibana-react-plugin/public` in your React components.
10+
11+
```ts
12+
import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';
13+
import { useExecutionContext } from '@kbn/kibana-react-plugin/public';
14+
15+
const ctx: KibanaExecutionContext = {
16+
type: 'application',
17+
name: 'discover',
18+
page: 'sessionView',
19+
id: discoverSessionId,
20+
};
21+
22+
useExecutionContext(core.executionContext, ctx);
23+
```
24+
25+
Notes:
26+
- `page` should be a **stable logical unit** (don’t embed ids in `page`; use `id` for identifiers).
27+
- The hook clears on unmount; Core also clears the context when the current app changes.
28+
29+
## Nested context with `child` (embeddables/components)
30+
31+
If you already have a parent context (e.g. from an app), add a child context for a nested component:
32+
33+
```ts
34+
import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';
35+
36+
const parent: KibanaExecutionContext = {
37+
type: 'application',
38+
name: 'dashboard',
39+
page: 'view',
40+
id: dashboardId,
41+
};
42+
43+
const ctx: KibanaExecutionContext = {
44+
...parent,
45+
child: { type: 'visualization', name: embeddableType, id: embeddableId },
46+
};
47+
```
48+
49+
This pattern is used in e.g. ML embeddables: [`use_embeddable_execution_context.ts`](../../../../../x-pack/platform/plugins/shared/ml/public/embeddables/common/use_embeddable_execution_context.ts).
50+
51+
## Per-request context (for a single `http.fetch`)
52+
53+
You can attach a per-request context that will be merged with the current global context:
54+
55+
```ts
56+
await core.http.fetch('/api/my_plugin/do_something', {
57+
context: { type: 'myPlugin', name: 'doSomething', id: entityId },
58+
});
59+
```
60+
61+
## Space id (`space`)
62+
63+
`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.
64+
65+
## Keep it small
66+
67+
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`).
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
# @kbn/core-execution-context-common
22

33
This package contains the common types for Core's execution context.
4+
5+
## `KibanaExecutionContext` (field reference)
6+
7+
`KibanaExecutionContext` is a small object describing where work originates. It is used by the browser to populate the `x-kbn-context` header, and by the server to enrich Elasticsearch `x-opaque-id`.
8+
9+
- **`type`**: high-level category (e.g. `application`, `dashboard`, `visualization`, `task`).
10+
- **`name`**: public name of an app/feature/subsystem (e.g. `discover`, `lens`, `taskManager`).
11+
- **`space`**: current space id (when applicable).
12+
- **`page`**: stable logical unit like a page/tab/route segment (avoid embedding ids; put those in `id`).
13+
- **`id`**: identifier for the current entity (dashboard id, rule id, saved object id, etc.).
14+
- **`description`**: human-readable description (avoid large values and sensitive data).
15+
- **`url`**: browser URL or server endpoint/task URL (avoid unique identifiers if not needed).
16+
- **`meta`**: optional extra structured details (keep small; avoid sensitive data).
17+
- **`child`**: nested context spawned from the current one (embeddables/sub-operations); can be chained.

src/core/packages/execution-context/server-internal/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
This package contains the internal types and implementation for Core's server-side execution context service.
44

5+
## Pointers
6+
7+
- Core HTTP installs request execution context from the `x-kbn-context` header in `src/core/packages/http/server-internal/src/http_server.ts`.
8+
- `AsyncLocalStorage` storage and the `withContext(...)` implementation live in `src/core/packages/execution-context/server-internal/src/execution_context_service.ts`.
9+
- `x-kbn-context` parsing and the compact context string used for Elasticsearch `x-opaque-id` live in `src/core/packages/execution-context/server-internal/src/execution_context_container.ts`.
10+
- Config: `execution_context.enabled` in `src/core/packages/execution-context/server-internal/src/execution_context_config.ts`.
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
# @kbn/core-execution-context-server
22

3-
This package contains the public types for Core's server-side execution context service.
3+
Server-side execution context helps Kibana correlate **server work** with a meaningful “origin” (app/page/entity/background task). Core uses it to enrich Elasticsearch `x-opaque-id`, which is useful for tracing expensive searches in Elasticsearch slow logs.
44

5+
This package contains the **public contract types** for `core.executionContext` on the server. The common data shape is `KibanaExecutionContext` from [`@kbn/core-execution-context-common`](../common).
6+
7+
## Request handling (typical case)
8+
9+
For HTTP requests initiated from the browser, Core’s HTTP layer parses the incoming `x-kbn-context` header and installs it into request-scoped async context. In a normal route handler you generally **do not need to do anything**—Elasticsearch client calls will automatically include the derived `x-opaque-id`.
10+
11+
For more context on how `x-opaque-id` is used to trace slow searches, see Elastic docs: [`Trace an Elasticsearch query in Kibana`](https://www.elastic.co/docs/troubleshoot/kibana/trace-elasticsearch-query-to-the-origin-in-kibana).
12+
13+
## Background/server-only work (use `withContext`)
14+
15+
For work that doesn’t originate from an incoming browser request (task manager, background sync, scheduled jobs, etc.), wrap your work with `core.executionContext.withContext(...)`:
16+
17+
```ts
18+
import type { KibanaExecutionContext } from '@kbn/core-execution-context-common';
19+
20+
const ctx: KibanaExecutionContext = {
21+
type: 'task',
22+
name: 'myPlugin',
23+
id: taskId,
24+
page: 'run',
25+
description: 'sync something',
26+
};
27+
28+
return core.executionContext.withContext(ctx, async () => {
29+
// Any Elasticsearch calls made from here will include the derived x-opaque-id
30+
await esClient.asInternalUser.ping();
31+
});
32+
```
33+
34+
## Debugging
35+
36+
To see stored execution context in Kibana logs, enable debug logging for the `execution_context` logger:
37+
38+
```yml
39+
logging:
40+
loggers:
41+
- name: execution_context
42+
level: debug
43+
```

0 commit comments

Comments
 (0)