Skip to content

Commit

Permalink
APM execution context - app, page, entitiy id (elastic#124996)
Browse files Browse the repository at this point in the history
* Client side execution app level context propagation

* context$ + apm rum integration

* invert the context parent \ child relationship (cc @mikhail)
move more things to top level context

* Pass down context to apm on server

* types

* eslint

* parent <> child

* docs + eslint + jest

* execution context mock

* eslint

* jest

* jest

* server jest

* check

* jest

* storybook

* jest

* report the current space

* fix server side context container

* Remove spaces for now

* docssss

* jest

* lint

* test

* docs

* revert file

* doc

* all context params are optional

* clear on page change

* lint

* ts

* skipped test again

* testing fixes

* oops

* code review #1

* code review #2

* getAsLabels

* maps inherit dashboard context

* docs

* ts

* Give common context to all vis editors

* fix test

* ts \ es \ tests

* labels

* missing types

* docsy docs

* cr #3

* improve jest

* Use editor name

* Update src/plugins/visualizations/public/visualize_app/components/visualize_editor.tsx

Co-authored-by: Marco Liberati <[email protected]>

* fix maps context

* jest tests for maps

* cr

* docs

* Update execution_context.test.ts

* docs

* lint

Co-authored-by: Marco Liberati <[email protected]>
  • Loading branch information
Liza Katz and dej611 authored Mar 3, 2022
1 parent 8e7a792 commit d5416ed
Show file tree
Hide file tree
Showing 84 changed files with 909 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [CoreSetup](./kibana-plugin-core-public.coresetup.md) &gt; [executionContext](./kibana-plugin-core-public.coresetup.executioncontext.md)

## CoreSetup.executionContext property

[ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md)

<b>Signature:</b>

```typescript
executionContext: ExecutionContextSetup;
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CoreSetup<TPluginsStart extends object = object, TStart = unkno
| Property | Type | Description |
| --- | --- | --- |
| [application](./kibana-plugin-core-public.coresetup.application.md) | ApplicationSetup | [ApplicationSetup](./kibana-plugin-core-public.applicationsetup.md) |
| [executionContext](./kibana-plugin-core-public.coresetup.executioncontext.md) | ExecutionContextSetup | [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) |
| [fatalErrors](./kibana-plugin-core-public.coresetup.fatalerrors.md) | FatalErrorsSetup | [FatalErrorsSetup](./kibana-plugin-core-public.fatalerrorssetup.md) |
| [getStartServices](./kibana-plugin-core-public.coresetup.getstartservices.md) | StartServicesAccessor&lt;TPluginsStart, TStart&gt; | [StartServicesAccessor](./kibana-plugin-core-public.startservicesaccessor.md) |
| [http](./kibana-plugin-core-public.coresetup.http.md) | HttpSetup | [HttpSetup](./kibana-plugin-core-public.httpsetup.md) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [CoreStart](./kibana-plugin-core-public.corestart.md) &gt; [executionContext](./kibana-plugin-core-public.corestart.executioncontext.md)

## CoreStart.executionContext property

[ExecutionContextStart](./kibana-plugin-core-public.executioncontextstart.md)

<b>Signature:</b>

```typescript
executionContext: ExecutionContextStart;
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface CoreStart
| [chrome](./kibana-plugin-core-public.corestart.chrome.md) | ChromeStart | [ChromeStart](./kibana-plugin-core-public.chromestart.md) |
| [deprecations](./kibana-plugin-core-public.corestart.deprecations.md) | DeprecationsServiceStart | [DeprecationsServiceStart](./kibana-plugin-core-public.deprecationsservicestart.md) |
| [docLinks](./kibana-plugin-core-public.corestart.doclinks.md) | DocLinksStart | [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) |
| [executionContext](./kibana-plugin-core-public.corestart.executioncontext.md) | ExecutionContextStart | [ExecutionContextStart](./kibana-plugin-core-public.executioncontextstart.md) |
| [fatalErrors](./kibana-plugin-core-public.corestart.fatalerrors.md) | FatalErrorsStart | [FatalErrorsStart](./kibana-plugin-core-public.fatalerrorsstart.md) |
| [http](./kibana-plugin-core-public.corestart.http.md) | HttpStart | [HttpStart](./kibana-plugin-core-public.httpstart.md) |
| [i18n](./kibana-plugin-core-public.corestart.i18n.md) | I18nStart | [I18nStart](./kibana-plugin-core-public.i18nstart.md) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [clear](./kibana-plugin-core-public.executioncontextsetup.clear.md)

## ExecutionContextSetup.clear() method

clears the context

<b>Signature:</b>

```typescript
clear(): void;
```
<b>Returns:</b>

void

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [context$](./kibana-plugin-core-public.executioncontextsetup.context_.md)

## ExecutionContextSetup.context$ property

The current context observable

<b>Signature:</b>

```typescript
context$: Observable<KibanaExecutionContext>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [get](./kibana-plugin-core-public.executioncontextsetup.get.md)

## ExecutionContextSetup.get() method

Get the current top level context

<b>Signature:</b>

```typescript
get(): KibanaExecutionContext;
```
<b>Returns:</b>

KibanaExecutionContext

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [getAsLabels](./kibana-plugin-core-public.executioncontextsetup.getaslabels.md)

## ExecutionContextSetup.getAsLabels() method

returns apm labels

<b>Signature:</b>

```typescript
getAsLabels(): Labels;
```
<b>Returns:</b>

Labels

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md)

## ExecutionContextSetup interface

Kibana execution context. Used to provide execution context to Elasticsearch, reporting, performance monitoring, etc.

<b>Signature:</b>

```typescript
export interface ExecutionContextSetup
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [context$](./kibana-plugin-core-public.executioncontextsetup.context_.md) | Observable&lt;KibanaExecutionContext&gt; | The current context observable |

## Methods

| Method | Description |
| --- | --- |
| [clear()](./kibana-plugin-core-public.executioncontextsetup.clear.md) | clears the context |
| [get()](./kibana-plugin-core-public.executioncontextsetup.get.md) | Get the current top level context |
| [getAsLabels()](./kibana-plugin-core-public.executioncontextsetup.getaslabels.md) | returns apm labels |
| [set(c$)](./kibana-plugin-core-public.executioncontextsetup.set.md) | Set the current top level context |
| [withGlobalContext(context)](./kibana-plugin-core-public.executioncontextsetup.withglobalcontext.md) | merges the current top level context with the specific event context |

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [set](./kibana-plugin-core-public.executioncontextsetup.set.md)

## ExecutionContextSetup.set() method

Set the current top level context

<b>Signature:</b>

```typescript
set(c$: KibanaExecutionContext): void;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| c$ | KibanaExecutionContext | |

<b>Returns:</b>

void

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) &gt; [withGlobalContext](./kibana-plugin-core-public.executioncontextsetup.withglobalcontext.md)

## ExecutionContextSetup.withGlobalContext() method

merges the current top level context with the specific event context

<b>Signature:</b>

```typescript
withGlobalContext(context?: KibanaExecutionContext): KibanaExecutionContext;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| context | KibanaExecutionContext | |

<b>Returns:</b>

KibanaExecutionContext

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [ExecutionContextStart](./kibana-plugin-core-public.executioncontextstart.md)

## ExecutionContextStart type

See [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md)<!-- -->.

<b>Signature:</b>

```typescript
export declare type ExecutionContextStart = ExecutionContextSetup;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Represents a meta-information about a Kibana entity initiating a search request.

```typescript
export declare type KibanaExecutionContext = {
readonly type: string;
readonly name: string;
readonly id: string;
readonly type?: string;
readonly name?: string;
readonly page?: string;
readonly id?: string;
readonly description?: string;
readonly url?: string;
child?: KibanaExecutionContext;
Expand Down
2 changes: 2 additions & 0 deletions docs/development/core/public/kibana-plugin-core-public.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [DeprecationsServiceStart](./kibana-plugin-core-public.deprecationsservicestart.md) | DeprecationsService provides methods to fetch domain deprecation details from the Kibana server. |
| [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) | |
| [ErrorToastOptions](./kibana-plugin-core-public.errortoastoptions.md) | Options available for [IToasts](./kibana-plugin-core-public.itoasts.md) error APIs. |
| [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md) | Kibana execution context. Used to provide execution context to Elasticsearch, reporting, performance monitoring, etc. |
| [FatalErrorInfo](./kibana-plugin-core-public.fatalerrorinfo.md) | Represents the <code>message</code> and <code>stack</code> of a fatal Error |
| [FatalErrorsSetup](./kibana-plugin-core-public.fatalerrorssetup.md) | FatalErrors stop the Kibana Public Core and displays a fatal error screen with details about the Kibana build and the error. |
| [HttpFetchOptions](./kibana-plugin-core-public.httpfetchoptions.md) | All options that may be used with a [HttpHandler](./kibana-plugin-core-public.httphandler.md)<!-- -->. |
Expand Down Expand Up @@ -160,6 +161,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [ChromeBreadcrumb](./kibana-plugin-core-public.chromebreadcrumb.md) | |
| [ChromeHelpExtensionLinkBase](./kibana-plugin-core-public.chromehelpextensionlinkbase.md) | |
| [ChromeHelpExtensionMenuLink](./kibana-plugin-core-public.chromehelpextensionmenulink.md) | |
| [ExecutionContextStart](./kibana-plugin-core-public.executioncontextstart.md) | See [ExecutionContextSetup](./kibana-plugin-core-public.executioncontextsetup.md)<!-- -->. |
| [FatalErrorsStart](./kibana-plugin-core-public.fatalerrorsstart.md) | FatalErrors stop the Kibana Public Core and displays a fatal error screen with details about the Kibana build and the error. |
| [HttpStart](./kibana-plugin-core-public.httpstart.md) | See [HttpSetup](./kibana-plugin-core-public.httpsetup.md) |
| [IToasts](./kibana-plugin-core-public.itoasts.md) | Methods for adding and removing global toast messages. See [ToastsApi](./kibana-plugin-core-public.toastsapi.md)<!-- -->. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [ExecutionContextSetup](./kibana-plugin-core-server.executioncontextsetup.md) &gt; [getAsLabels](./kibana-plugin-core-server.executioncontextsetup.getaslabels.md)

## ExecutionContextSetup.getAsLabels() method

<b>Signature:</b>

```typescript
getAsLabels(): apm.Labels;
```
<b>Returns:</b>

apm.Labels

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export interface ExecutionContextSetup

| Method | Description |
| --- | --- |
| [getAsLabels()](./kibana-plugin-core-server.executioncontextsetup.getaslabels.md) | |
| [withContext(context, fn)](./kibana-plugin-core-server.executioncontextsetup.withcontext.md) | Keeps track of execution context while the passed function is executed. Data are carried over all async operations spawned by the passed function. The nested calls stack the registered context on top of each other. |

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Represents a meta-information about a Kibana entity initiating a search request.

```typescript
export declare type KibanaExecutionContext = {
readonly type: string;
readonly name: string;
readonly id: string;
readonly type?: string;
readonly name?: string;
readonly page?: string;
readonly id?: string;
readonly description?: string;
readonly url?: string;
child?: KibanaExecutionContext;
Expand Down
4 changes: 4 additions & 0 deletions src/core/public/apm_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Transaction } from '@elastic/apm-rum';
import { ApmSystem } from './apm_system';
import { Subject } from 'rxjs';
import { InternalApplicationStart } from './application/types';
import { executionContextServiceMock } from './execution_context/execution_context_service.mock';

const initMock = init as jest.Mocked<typeof init>;
const apmMock = apm as DeeplyMockedKeys<typeof apm>;
Expand Down Expand Up @@ -96,6 +97,7 @@ describe('ApmSystem', () => {
application: {
currentAppId$,
} as any as InternalApplicationStart,
executionContext: executionContextServiceMock.createInternalStartContract(),
});

expect(mark).toHaveBeenCalledWith('apm-start');
Expand All @@ -118,6 +120,7 @@ describe('ApmSystem', () => {
application: {
currentAppId$,
} as any as InternalApplicationStart,
executionContext: executionContextServiceMock.createInternalStartContract(),
});
currentAppId$.next('myapp');

Expand Down Expand Up @@ -145,6 +148,7 @@ describe('ApmSystem', () => {
application: {
currentAppId$,
} as any as InternalApplicationStart,
executionContext: executionContextServiceMock.createInternalStartContract(),
});
currentAppId$.next('myapp');

Expand Down
12 changes: 12 additions & 0 deletions src/core/public/apm_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ApmBase, AgentConfigOptions, Transaction } from '@elastic/apm-rum'
import { modifyUrl } from '@kbn/std';
import { CachedResourceObserver } from './apm_resource_counter';
import type { InternalApplicationStart } from './application';
import { ExecutionContextStart } from './execution_context';

/** "GET protocol://hostname:port/pathname" */
const HTTP_REQUEST_TRANSACTION_NAME_REGEX =
Expand All @@ -27,13 +28,15 @@ interface ApmConfig extends AgentConfigOptions {

interface StartDeps {
application: InternalApplicationStart;
executionContext: ExecutionContextStart;
}

export class ApmSystem {
private readonly enabled: boolean;
private pageLoadTransaction?: Transaction;
private resourceObserver: CachedResourceObserver;
private apm?: ApmBase;

/**
* `apmConfig` would be populated with relevant APM RUM agent
* configuration if server is started with elastic.apm.* config.
Expand Down Expand Up @@ -64,6 +67,15 @@ export class ApmSystem {

this.markPageLoadStart();

start.executionContext.context$.subscribe((c) => {
// We're using labels because we want the context to be indexed
// https://www.elastic.co/guide/en/apm/get-started/current/metadata.html
const apmContext = start.executionContext.getAsLabels();
this.apm?.addLabels(apmContext);
});

// TODO: Start a new transaction every page change instead of every app change.

/**
* Register listeners for navigation changes and capture them as
* route-change transactions after Kibana app is bootstrapped
Expand Down
Loading

0 comments on commit d5416ed

Please sign in to comment.