Skip to content

Commit

Permalink
[kbn-journeys] add synthtrace support (elastic#178599)
Browse files Browse the repository at this point in the history
## Summary

Moving synthtrace clients init inside kbn-journeys:

esArchiver does not always solve the issue with data generation. We
already have afew journeys using Synthtrace instead and expect more to
come.

In order to simplify the process of creating new journeys, this PR moves
Synthtrace client initialisation into kbn-journey package and exposes a
way to define client type, generator function & its input arguments:

 ```
import { Journey, SynthtraceOptions } from '@kbn/journeys';
import { subj } from '@kbn/test-subj-selector';
import { generateApmData } from '../synthtrace_data/apm_data';

export const journey = new Journey({
  synthtrace: {
    type: 'apm',
    generator: generateApmData,
    options: {
      from: new Date(Date.now() - 1000 * 60 * 15),
      to: new Date(Date.now() + 1000 * 60 * 15),
    },
  },
})
```

PR also needs review from teams who use Synthtrace to understand if the implementation is matching expectations.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
dmlemeshko and kibanamachine authored Mar 18, 2024
1 parent 45dbfae commit aa45c2a
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 171 deletions.
28 changes: 24 additions & 4 deletions dev_docs/tutorials/performance/adding_performance_journey.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tags: ['kibana', 'onboarding', 'setup', 'performance', 'development']
---

## Overview

In order to achieve our goal of creating best user experience in Kibana, it is important to keep track on its features performance.
To make things easier, we introduced performance journeys, that mimics end-user experience with Kibana.

Expand All @@ -19,6 +20,7 @@ Journeys core is [kbn-journeys](packages/kbn-journeys/README.mdx) package. It is
by [Playwright](https://playwright.dev/) end-to-end testing tool.

### Adding a new performance journey

Let's assume we instrumented dashboard with load time metrics and want to track sample data flights dashboard performance.
Journey supports loading test data with esArchiver or kbnArchiver. Similar to functional tests, it might require to implement custom wait
for UI rendering to be completed.
Expand All @@ -42,18 +44,36 @@ export const journey = new Journey({
});
```

Alternative to archives is to use Synthtrace ES client:

```
export const journey = new Journey({
synthtrace: {
type: 'apm',
generator: generateApmData,
options: {
from: new Date(Date.now() - 1000 * 60 * 15),
to: new Date(Date.now() + 1000 * 60 * 15),
},
},
})
```

In oder to get correct and consistent metrics, it is important to design journey properly:
- use archives to generate test data

- use archives or synthtrace to generate test data
- decouple complex scenarios into multiple simple journeys
- use waiting for page loading / UI component rendering
- test locally and check if journey is stable.
- make sure performance metrics are collected on every run.

### Running performance journey locally for troubleshooting purposes

Use the Node script:
`node scripts/run_performance.js --journey-path x-pack/performance/journeys_e2e/$YOUR_JOURNEY_NAME.ts`
`node scripts/run_performance.js --journey-path x-pack/performance/journeys_e2e/$YOUR_JOURNEY_NAME.ts`

Scripts steps include:

- start Elasticsearch
- start Kibana and run journey first time (warmup) only APM metrics being reported
- start Kibana and run journey second time (test): both EBT and APM metrics being reported
Expand All @@ -65,6 +85,7 @@ Since the tests are run on a local machine, there is also realistic throttling a
simulate real life internet connection. This means that all requests have a fixed latency and limited bandwidth.

### Benchmarking performance on CI

In order to keep track on performance metrics stability, journeys are run on main branch with a scheduled interval.
Bare metal machine is used to produce results as stable and reproducible as possible.

Expand All @@ -77,9 +98,8 @@ RAM: 128 GB
SSD: 1.92 TB Data center Gen4 NVMe

#### Track performance results

APM metrics are reported to [kibana-ops-e2e-perf](https://kibana-ops-e2e-perf.kb.us-central1.gcp.cloud.es.io/) cluster.
You can filter transactions using labels, e.g. `labels.journeyName : "flight_dashboard"`

Custom metrics reported with EBT are available in [Telemetry Staging](https://telemetry-v2-staging.elastic.dev/) cluster, `kibana-performance` space.


31 changes: 29 additions & 2 deletions packages/kbn-journeys/journey/journey_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import Path from 'path';

import { REPO_ROOT } from '@kbn/repo-info';

import { SynthtraceGenerator } from '@kbn/apm-synthtrace-client/src/types';
import { Readable } from 'stream';
import { BaseStepCtx } from './journey';
import { SynthtraceClientType } from '../services/synthtrace';

interface JourneySynthtrace<T extends { '@timestamp'?: number | undefined }, O = any> {
type: SynthtraceClientType;
generator: (options: O) => Readable | SynthtraceGenerator<T>;
options: O;
}

export interface RampConcurrentUsersAction {
action: 'rampConcurrentUsers';
Expand Down Expand Up @@ -69,7 +78,7 @@ export interface ScalabilitySetup {
test: ScalabilityAction[];
}

export interface JourneyConfigOptions<CtxExt> {
export interface JourneyConfigOptions<CtxExt extends { '@timestamp'?: number | undefined }> {
/**
* Relative path to FTR config file. Use to override the default ones:
* 'x-pack/test/functional/config.base.js', 'test/functional/config.base.js'
Expand Down Expand Up @@ -116,9 +125,23 @@ export interface JourneyConfigOptions<CtxExt> {
extendContext?: (ctx: BaseStepCtx) => CtxExt;
/**
* Use this to define actions that will be executed after Kibana & ES were started,
* but before archives are loaded. APM traces are not collected for this hook.
* but before archives are loaded or synthtrace is run. APM traces are not collected for this hook.
*/
beforeSteps?: (ctx: BaseStepCtx & CtxExt) => Promise<void>;
/**
* Use to setup ES data ingestion with APM Synthtrace
*
* synthtrace: {
* type: 'infra',
* generator: generateHostsData,
* options: {
* from: new Date(Date.now() - 1000 * 60 * 10),
* to: new Date(),
* count: 1000,
* },
* },
*/
synthtrace?: JourneySynthtrace<CtxExt>;
}

export class JourneyConfig<CtxExt extends object> {
Expand Down Expand Up @@ -192,4 +215,8 @@ export class JourneyConfig<CtxExt extends object> {
new Promise<void>((resolve) => resolve());
}
}

getSynthtraceConfig() {
return this.#opts.synthtrace;
}
}
60 changes: 56 additions & 4 deletions packages/kbn-journeys/journey/journey_ftr_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import type { Step, AnyStep } from './journey';
import type { JourneyConfig } from './journey_config';
import { JourneyScreenshots } from './journey_screenshots';
import { getNewPageObject } from '../services/page';
import { getSynthtraceClient } from '../services/synthtrace';

export class JourneyFtrHarness {
private readonly screenshots: JourneyScreenshots;
private readonly kbnUrl: KibanaUrl;

constructor(
private readonly log: ToolingLog,
Expand All @@ -44,6 +46,15 @@ export class JourneyFtrHarness {
private readonly journeyConfig: JourneyConfig<any>
) {
this.screenshots = new JourneyScreenshots(this.journeyConfig.getName());
this.kbnUrl = new KibanaUrl(
new URL(
Url.format({
protocol: this.config.get('servers.kibana.protocol'),
hostname: this.config.get('servers.kibana.hostname'),
port: this.config.get('servers.kibana.port'),
})
)
);
}

private browser: ChromiumBrowser | undefined;
Expand All @@ -61,6 +72,7 @@ export class JourneyFtrHarness {
// journey can be run to collect EBT/APM metrics or just as a functional test
// TEST_PERFORMANCE_PHASE is defined via scripts/run_perfomance.js run only
private readonly isPerformanceRun = process.env.TEST_PERFORMANCE_PHASE || false;
private readonly isWarmupPhase = process.env.TEST_PERFORMANCE_PHASE === 'WARMUP';

// Update the Telemetry and APM global labels to link traces with journey
private async updateTelemetryAndAPMLabels(labels: { [k: string]: string }) {
Expand Down Expand Up @@ -158,16 +170,54 @@ export class JourneyFtrHarness {
await this.interceptBrowserRequests(this.page);
}

private async runSynthtrace() {
const config = this.journeyConfig.getSynthtraceConfig();
if (config) {
const client = await getSynthtraceClient(config.type, {
log: this.log,
es: this.es,
auth: this.auth,
kbnUrl: this.kbnUrl,
});
const generator = config.generator(config.options);
await client.index(generator);
}
}

/**
* onSetup is part of high level 'before' hook and does the following sequentially:
* 1. Start browser
* 2. Load test data (opt-in)
* 3. Run BeforeSteps (opt-in)
* 4. Setup APM
*/
private async onSetup() {
// We start browser and init page in the first place
await this.setupBrowserAndPage();
// We allow opt-in beforeSteps hook to manage Kibana/ES state

// We allow opt-in beforeSteps hook to manage Kibana/ES after start, install integrations, etc.
await this.journeyConfig.getBeforeStepsFn(this.getCtx());
// Loading test data

/**
* Loading test data, optionally but following the order:
* 1. Synthtrace client
* 2. ES archives
* 3. Kbn archives (Saved objects)
*/

// To insure we ingest data with synthtrace only once during performance run
if (!this.isPerformanceRun || this.isWarmupPhase) {
await this.runSynthtrace();
}

await Promise.all([
asyncForEach(this.journeyConfig.getEsArchives(), async (esArchive) => {
if (this.isPerformanceRun) {
// we start Elasticsearch only once and keep ES data persisitent.
//
/**
* During performance run we ingest data to ES before WARMUP phase, and avoid re-indexing
* before TEST phase by insuring index already exists
*/
await this.esArchiver.loadIfNeeded(esArchive);
} else {
await this.esArchiver.load(esArchive);
Expand Down Expand Up @@ -242,7 +292,9 @@ export class JourneyFtrHarness {
await this.teardownApm();
await Promise.all([
asyncForEach(this.journeyConfig.getEsArchives(), async (esArchive) => {
// Keep ES data when journey is run twice (avoid unload after "Warmup" phase)
/**
* Keep ES data after WARMUP phase to avoid re-indexing
*/
if (!this.isPerformanceRun) {
await this.esArchiver.unload(esArchive);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-journeys/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "shared-common",
"type": "test-helper",
"id": "@kbn/journeys",
"owner": ["@elastic/kibana-operations", "@elastic/appex-qa"],
"devOnly": true
Expand Down
123 changes: 123 additions & 0 deletions packages/kbn-journeys/services/synthtrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import {
ApmSynthtraceEsClient,
ApmSynthtraceKibanaClient,
InfraSynthtraceEsClient,
InfraSynthtraceKibanaClient,
} from '@kbn/apm-synthtrace';
import { ToolingLog } from '@kbn/tooling-log';
import Url from 'url';
import { Logger } from '@kbn/apm-synthtrace/src/lib/utils/create_logger';
import { Auth, Es } from '.';
import { KibanaUrl } from './kibana_url';

export interface SynthtraceClientOptions {
kbnUrl: KibanaUrl;
auth: Auth;
es: Es;
log: ToolingLog;
}
export type SynthtraceClient = InfraSynthtraceEsClient | ApmSynthtraceEsClient;
export type SynthtraceClientType = 'infra' | 'apm';

export async function getSynthtraceClient(
type: SynthtraceClientType,
options: SynthtraceClientOptions
): Promise<SynthtraceClient> {
if (type === 'infra') {
return initInfraSynthtraceClient(options);
} else {
return initApmSynthtraceClient(options);
}
}

// Adapting ToolingLog instance to Logger interface
class LoggerAdapter implements Logger {
private log: ToolingLog;
private joiner = ', ';

constructor(log: ToolingLog) {
this.log = log;
}

debug(...args: any[]): void {
this.log.debug(args.join(this.joiner));
}

info(...args: any[]): void {
this.log.info(args.join(this.joiner));
}

error(arg: string | Error): void {
this.log.error(arg);
}

perf<T>(name: string, cb: () => T): T {
const startTime = Date.now();
const result = cb();
const duration = Date.now() - startTime;
const durationInSeconds = duration / 1000;
const formattedTime = durationInSeconds.toFixed(3) + 's';
this.log.info(`${name} took ${formattedTime}.`);
return result;
}
}

async function initInfraSynthtraceClient(options: SynthtraceClientOptions) {
const { log, es, auth, kbnUrl } = options;
const logger: Logger = new LoggerAdapter(log);

const synthKbnClient = new InfraSynthtraceKibanaClient({
logger,
target: kbnUrl.get(),
username: auth.getUsername(),
password: auth.getPassword(),
});
const pkgVersion = await synthKbnClient.fetchLatestSystemPackageVersion();
await synthKbnClient.installSystemPackage(pkgVersion);

const synthEsClient = new InfraSynthtraceEsClient({
logger,
client: es,
refreshAfterIndex: true,
});

return synthEsClient;
}

async function initApmSynthtraceClient(options: SynthtraceClientOptions) {
const { log, es, auth, kbnUrl } = options;
const logger: Logger = new LoggerAdapter(log);
const kibanaUrl = new URL(kbnUrl.get());
const kibanaUrlWithAuth = Url.format({
protocol: kibanaUrl.protocol,
hostname: kibanaUrl.hostname,
port: kibanaUrl.port,
auth: `${auth.getUsername()}:${auth.getPassword()}`,
});

const synthKbnClient = new ApmSynthtraceKibanaClient({
logger,
target: kibanaUrlWithAuth,
});
const packageVersion = await synthKbnClient.fetchLatestApmPackageVersion();
await synthKbnClient.installApmPackage(packageVersion);

const synthEsClient = new ApmSynthtraceEsClient({
client: es,
logger,
refreshAfterIndex: true,
version: packageVersion,
});

synthEsClient.pipeline(synthEsClient.getDefaultPipeline(false));

return synthEsClient;
}
2 changes: 2 additions & 0 deletions packages/kbn-journeys/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@kbn/std",
"@kbn/test-subj-selector",
"@kbn/core-http-common",
"@kbn/apm-synthtrace-client",
"@kbn/apm-synthtrace",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "shared-common",
"type": "test-helper",
"id": "@kbn/performance-testing-dataset-extractor",
"devOnly": true,
"owner": "@elastic/kibana-performance-testing"
Expand Down
Loading

0 comments on commit aa45c2a

Please sign in to comment.