Skip to content

Commit

Permalink
[kbn-scout] Custom event-oriented test reporter & persistence (elasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
dolaru authored and CAWilson94 committed Dec 12, 2024
1 parent 2a47a87 commit 987af3f
Show file tree
Hide file tree
Showing 48 changed files with 1,761 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ packages/kbn-safer-lodash-set @elastic/kibana-security
packages/kbn-saved-objects-settings @elastic/appex-sharedux
packages/kbn-saved-search-component @elastic/obs-ux-logs-team
packages/kbn-scout @elastic/appex-qa
packages/kbn-scout-info @elastic/appex-qa
packages/kbn-scout-reporting @elastic/appex-qa
packages/kbn-screenshotting-server @elastic/appex-sharedux
packages/kbn-search-api-keys-components @elastic/search-kibana
packages/kbn-search-api-keys-server @elastic/search-kibana
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,8 @@
"@kbn/repo-source-classifier": "link:packages/kbn-repo-source-classifier",
"@kbn/repo-source-classifier-cli": "link:packages/kbn-repo-source-classifier-cli",
"@kbn/scout": "link:packages/kbn-scout",
"@kbn/scout-info": "link:packages/kbn-scout-info",
"@kbn/scout-reporting": "link:packages/kbn-scout-reporting",
"@kbn/security-api-integration-helpers": "link:x-pack/test/security_api_integration/packages/helpers",
"@kbn/serverless-storybook-config": "link:packages/serverless/storybook/config",
"@kbn/some-dev-log": "link:packages/kbn-some-dev-log",
Expand Down
6 changes: 6 additions & 0 deletions packages/kbn-scout-info/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @kbn/scout-info

This package stores information that's commonly used by packages in the `@kbn/scout*` namespace, and any other
package that wishes to extend Scout functionality.

Check out the `@kbn/scout` package if you want to learn more about Scout.
4 changes: 2 additions & 2 deletions scripts/scout_test.js → packages/kbn-scout-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

require('../src/setup_node_env');
require('@kbn/scout').runTestsCli();
export * from './src/paths';
export * from './src/reporting';
14 changes: 14 additions & 0 deletions packages/kbn-scout-info/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-scout-info'],
};
6 changes: 6 additions & 0 deletions packages/kbn-scout-info/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "shared-common",
"id": "@kbn/scout-info",
"owner": "@elastic/appex-qa",
"devOnly": true
}
6 changes: 6 additions & 0 deletions packages/kbn-scout-info/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/scout-info",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0"
}
19 changes: 19 additions & 0 deletions packages/kbn-scout-info/src/paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 path from 'node:path';
import { REPO_ROOT } from '@kbn/repo-info';

export const SCOUT_OUTPUT_ROOT = path.resolve(REPO_ROOT, '.scout');

// Servers
export const SCOUT_SERVERS_ROOT = path.resolve(SCOUT_OUTPUT_ROOT, 'servers');

// Reporting
export const SCOUT_REPORT_OUTPUT_ROOT = path.resolve(SCOUT_OUTPUT_ROOT, 'reports');
12 changes: 12 additions & 0 deletions packages/kbn-scout-info/src/reporting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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".
*/

export const SCOUT_TEST_EVENTS_TEMPLATE_NAME = 'scout-test-events';
export const SCOUT_TEST_EVENTS_INDEX_PATTERN = `${SCOUT_TEST_EVENTS_TEMPLATE_NAME}-*`;
export const SCOUT_TEST_EVENTS_DATA_STREAM_NAME = `${SCOUT_TEST_EVENTS_TEMPLATE_NAME}-kibana`;
19 changes: 19 additions & 0 deletions packages/kbn-scout-info/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts",
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/repo-info"
]
}
5 changes: 5 additions & 0 deletions packages/kbn-scout-reporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @kbn/scout-reporting

This package contains reporting functionality for Scout.

Check out the `@kbn/scout` package if you want to learn more about Scout.
12 changes: 12 additions & 0 deletions packages/kbn-scout-reporting/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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".
*/

export * as cli from './src/cli';
export * as datasources from './src/datasources';
export * from './src/reporting';
14 changes: 14 additions & 0 deletions packages/kbn-scout-reporting/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-scout-reporting'],
};
6 changes: 6 additions & 0 deletions packages/kbn-scout-reporting/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "shared-common",
"id": "@kbn/scout-reporting",
"owner": "@elastic/appex-qa",
"devOnly": true
}
6 changes: 6 additions & 0 deletions packages/kbn-scout-reporting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/scout-reporting",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0"
}
37 changes: 37 additions & 0 deletions packages/kbn-scout-reporting/src/cli/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { Client as ESClient, ClientOptions as ESClientOptions } from '@elastic/elasticsearch';
import { ToolingLog } from '@kbn/tooling-log';
import { createFailError } from '@kbn/dev-cli-errors';

/**
* Get an Elasticsearch client for which connectivity has been validated
*
* @param options Elasticsearch client options
* @param log Logger instance
* @throws FailError if cluster information cannot be read from the target Elasticsearch instance
*/
export async function getValidatedESClient(
options: ESClientOptions,
log: ToolingLog
): Promise<ESClient> {
const es = new ESClient(options);

await es.info().then(
(esInfo) => {
log.info(`Connected to Elasticsearch node '${esInfo.name}'`);
},
(err) => {
throw createFailError(`Failed to connect to Elasticsearch\n${err}`);
}
);

return es;
}
11 changes: 11 additions & 0 deletions packages/kbn-scout-reporting/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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".
*/

export { initializeReportDatastream } from './initialize_report_datastream';
export { uploadEvents } from './upload_events';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 { Command } from '@kbn/dev-cli-runner';
import { ScoutReportDataStream } from '../reporting/report';
import { getValidatedESClient } from './common';

export const initializeReportDatastream: Command<void> = {
name: 'initialize-report-datastream',
description: 'Initialize a Scout report datastream in Elasticsearch',
flags: {
string: ['esURL', 'esAPIKey'],
boolean: ['verifyTLSCerts'],
default: {
esURL: process.env.ES_URL,
esAPIKey: process.env.ES_API_KEY,
},
help: `
--esURL (required) Elasticsearch URL [env: ES_URL]
--esAPIKey (required) Elasticsearch API Key [env: ES_API_KEY]
--verifyTLSCerts (optional) Verify TLS certificates
`,
},
run: async ({ flagsReader, log }) => {
const esURL = flagsReader.requiredString('esURL');
const esAPIKey = flagsReader.requiredString('esAPIKey');

// ES connection
log.info(`Connecting to Elasticsearch at ${esURL}`);
const es = await getValidatedESClient(
{
node: esURL,
auth: { apiKey: esAPIKey },
tls: {
rejectUnauthorized: flagsReader.boolean('verifyTLSCerts'),
},
},
log
);

// Initialize the report datastream
const reportDataStream = new ScoutReportDataStream(es, log);
await reportDataStream.initialize();

log.success('Scout report data stream initialized');
},
};
61 changes: 61 additions & 0 deletions packages/kbn-scout-reporting/src/cli/upload_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 fs from 'node:fs';
import { Command } from '@kbn/dev-cli-runner';
import { createFlagError } from '@kbn/dev-cli-errors';
import { ScoutReportDataStream } from '../reporting/report';
import { getValidatedESClient } from './common';

export const uploadEvents: Command<void> = {
name: 'upload-events',
description: 'Upload events recorded by the Scout reporter to Elasticsearch',
flags: {
string: ['eventLogPath', 'esURL', 'esAPIKey'],
boolean: ['verifyTLSCerts'],
default: {
esURL: process.env.ES_URL,
esAPIKey: process.env.ES_API_KEY,
},
help: `
--eventLogPath (required) Path to the event log to upload
--esURL (required) Elasticsearch URL [env: ES_URL]
--esAPIKey (required) Elasticsearch API Key [env: ES_API_KEY]
--verifyTLSCerts (optional) Verify TLS certificates
`,
},
run: async ({ flagsReader, log }) => {
// Read & validate CLI options
const eventLogPath = flagsReader.requiredString('eventLogPath');

if (!fs.existsSync(eventLogPath)) {
throw createFlagError(`Event log path '${eventLogPath}' does not exist.`);
}

const esURL = flagsReader.requiredString('esURL');
const esAPIKey = flagsReader.requiredString('esAPIKey');

// ES connection
log.info(`Connecting to Elasticsearch at ${esURL}`);
const es = await getValidatedESClient(
{
node: esURL,
auth: { apiKey: esAPIKey },
tls: {
rejectUnauthorized: flagsReader.boolean('verifyTLSCerts'),
},
},
log
);

// Event log upload
const reportDataStream = new ScoutReportDataStream(es, log);
await reportDataStream.addEventsFromFile(eventLogPath);
},
};
Loading

0 comments on commit 987af3f

Please sign in to comment.