Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kbn-scout] Add Synthtrace as a fixture #210505

Merged
merged 18 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/kbn-scout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
createPlaywrightConfig,
createLazyPageObject,
ingestTestDataHook,
ingestSynthtraceDataHook,
} from './src/playwright';
export type {
ScoutPlaywrightOptions,
Expand Down
105 changes: 105 additions & 0 deletions packages/kbn-scout/src/common/services/synthtrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import {
ApmSynthtraceEsClient,
ApmSynthtraceKibanaClient,
InfraSynthtraceEsClient,
InfraSynthtraceKibanaClient,
LogLevel,
OtelSynthtraceEsClient,
createLogger,
} from '@kbn/apm-synthtrace';
import { ScoutLogger } from './logger';
import { EsClient } from '../../types';

let apmSynthtraceEsClientInstance: ApmSynthtraceEsClient | undefined;
let infraSynthtraceEsClientInstance: InfraSynthtraceEsClient | undefined;
let otelSynthtraceEsClientInstance: OtelSynthtraceEsClient | undefined;
const logger = createLogger(LogLevel.info);

export async function getApmSynthtraceEsClient(
esClient: EsClient,
target: string,
log: ScoutLogger
) {
if (!apmSynthtraceEsClientInstance) {
const apmSynthtraceKibanaClient = new ApmSynthtraceKibanaClient({
logger,
target,
});

const version = await apmSynthtraceKibanaClient.fetchLatestApmPackageVersion();
await apmSynthtraceKibanaClient.installApmPackage(version);
apmSynthtraceEsClientInstance = new ApmSynthtraceEsClient({
client: esClient,
logger,
refreshAfterIndex: true,
version,
});

apmSynthtraceEsClientInstance.pipeline(
apmSynthtraceEsClientInstance.getDefaultPipeline({ includeSerialization: false })
);

log.serviceLoaded('apmSynthtraceClient');
}

return apmSynthtraceEsClientInstance;
}

export async function getInfraSynthtraceEsClient(
esClient: EsClient,
kbnUrl: string,
auth: { username: string; password: string },
log: ScoutLogger
) {
if (!infraSynthtraceEsClientInstance) {
const infraSynthtraceKibanaClient = new InfraSynthtraceKibanaClient({
logger,
target: kbnUrl,
username: auth.username,
password: auth.password,
});

const version = await infraSynthtraceKibanaClient.fetchLatestSystemPackageVersion();
await infraSynthtraceKibanaClient.installSystemPackage(version);
infraSynthtraceEsClientInstance = new InfraSynthtraceEsClient({
client: esClient,
logger,
refreshAfterIndex: true,
});

infraSynthtraceEsClientInstance.pipeline(
infraSynthtraceEsClientInstance.getDefaultPipeline({ includeSerialization: false })
);

log.serviceLoaded('infraSynthtraceClient');
}

return infraSynthtraceEsClientInstance;
}

export function getOtelSynthtraceEsClient(esClient: EsClient, log: ScoutLogger) {
if (!otelSynthtraceEsClientInstance) {
otelSynthtraceEsClientInstance = new OtelSynthtraceEsClient({
client: esClient,
logger,
refreshAfterIndex: true,
});

otelSynthtraceEsClientInstance.pipeline(
otelSynthtraceEsClientInstance.getDefaultPipeline({ includeSerialization: false })
);

log.serviceLoaded('otelSynthtraceClient');
}

return otelSynthtraceEsClientInstance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
coreWorkerFixtures,
esArchiverFixture,
uiSettingsFixture,
synthtraceFixture,
} from './worker';
import type {
EsArchiverFixture,
Expand All @@ -23,23 +24,23 @@ import type {
ScoutLogger,
ScoutTestConfig,
UiSettingsFixture,
SynthtraceFixture,
} from './worker';
import {
scoutPageFixture,
browserAuthFixture,
pageObjectsFixture,
validateTagsFixture,
BrowserAuthFixture,
ScoutPage,
PageObjects,
} from './test';
export type { PageObjects, ScoutPage } from './test';
import type { BrowserAuthFixture, ScoutPage, PageObjects } from './test';
export type { ScoutPage, PageObjects } from './test';

export const scoutFixtures = mergeTests(
// worker scope fixtures
coreWorkerFixtures,
esArchiverFixture,
uiSettingsFixture,
synthtraceFixture,
// api fixtures
apiFixtures,
// test scope fixtures
Expand All @@ -63,4 +64,7 @@ export interface ScoutWorkerFixtures extends ApiFixtures {
esClient: EsClient;
esArchiver: EsArchiverFixture;
uiSettings: UiSettingsFixture;
apmSynthtraceEsClient: SynthtraceFixture['apmSynthtraceEsClient'];
infraSynthtraceEsClient: SynthtraceFixture['infraSynthtraceEsClient'];
otelSynthtraceEsClient: SynthtraceFixture['otelSynthtraceEsClient'];
}
3 changes: 3 additions & 0 deletions packages/kbn-scout/src/playwright/fixtures/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ export type { ScoutSpaceParallelFixture } from './scout_space';

export { apiFixtures } from './apis';
export type { ApiFixtures, ApiParallelWorkerFixtures } from './apis';

export { synthtraceFixture } from './synthtrace';
export type { SynthtraceFixture } from './synthtrace';
114 changes: 114 additions & 0 deletions packages/kbn-scout/src/playwright/fixtures/worker/synthtrace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type {
ApmSynthtraceEsClient,
OtelSynthtraceEsClient,
InfraSynthtraceEsClient,
} from '@kbn/apm-synthtrace';
import { Readable } from 'stream';
import type { ApmFields, InfraDocument, OtelDocument } from '@kbn/apm-synthtrace-client';
import Url from 'url';
import {
getApmSynthtraceEsClient,
getInfraSynthtraceEsClient,
getOtelSynthtraceEsClient,
} from '../../../common/services/synthtrace';
import { coreWorkerFixtures } from './core_fixtures';
import type { SynthtraceEvents } from '../../global_hooks/synthtrace_ingestion';

export interface SynthtraceFixture {
apmSynthtraceEsClient: {
index: (events: SynthtraceEvents<ApmFields>) => Promise<void>;
clean: ApmSynthtraceEsClient['clean'];
};
infraSynthtraceEsClient: {
index: (events: SynthtraceEvents<InfraDocument>) => Promise<void>;
clean: InfraSynthtraceEsClient['clean'];
};
otelSynthtraceEsClient: {
index: (events: SynthtraceEvents<OtelDocument>) => Promise<void>;
clean: OtelSynthtraceEsClient['clean'];
};
}

export const synthtraceFixture = coreWorkerFixtures.extend<{}, SynthtraceFixture>({
apmSynthtraceEsClient: [
async ({ esClient, config, kbnUrl, log }, use) => {
const { username, password } = config.auth;
const kibanaUrl = new URL(kbnUrl.get());
const kibanaUrlWithAuth = Url.format({
protocol: kibanaUrl.protocol,
hostname: kibanaUrl.hostname,
port: kibanaUrl.port,
auth: `${username}:${password}`,
});

const apmSynthtraceEsClient = await getApmSynthtraceEsClient(
esClient,
kibanaUrlWithAuth,
log
);

const index = async (events: SynthtraceEvents<ApmFields>) =>
await apmSynthtraceEsClient.index(
Readable.from(Array.from(events).flatMap((event) => event.serialize()))
);

const clean = async () => await apmSynthtraceEsClient.clean();

await use({ index, clean });

// cleanup function after all tests have ran
await apmSynthtraceEsClient.clean();
},
{ scope: 'worker' },
],
infraSynthtraceEsClient: [
async ({ esClient, config, kbnUrl, log }, use) => {
const infraSynthtraceEsClient = await getInfraSynthtraceEsClient(
esClient,
kbnUrl.get(),
config.auth,
log
);

const index = async (events: SynthtraceEvents<InfraDocument>) =>
await infraSynthtraceEsClient.index(
Readable.from(Array.from(events).flatMap((event) => event.serialize()))
);

const clean = async () => await infraSynthtraceEsClient.clean();

await use({ index, clean });

// cleanup function after all tests have ran
await infraSynthtraceEsClient.clean();
},
{ scope: 'worker' },
],
otelSynthtraceEsClient: [
async ({ esClient, log }, use) => {
const otelSynthtraceEsClient = await getOtelSynthtraceEsClient(esClient, log);

const index = async (events: SynthtraceEvents<OtelDocument>) =>
await otelSynthtraceEsClient.index(
Readable.from(Array.from(events).flatMap((event) => event.serialize()))
);

const clean = async () => await otelSynthtraceEsClient.clean();

await use({ index, clean });

// cleanup function after all tests have ran
await otelSynthtraceEsClient.clean();
},
{ scope: 'worker' },
],
});
1 change: 1 addition & 0 deletions packages/kbn-scout/src/playwright/global_hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
*/

export { ingestTestDataHook } from './data_ingestion';
export { ingestSynthtraceDataHook } from './synthtrace_ingestion';
Loading