-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into use-extends-error
- Loading branch information
Showing
288 changed files
with
10,822 additions
and
2,875 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[ | ||
{ | ||
"output": "src/plugins/telemetry/schema/legacy_oss_plugins.json", | ||
"root": "src/legacy/core_plugins/", | ||
"exclude": [ | ||
"src/legacy/core_plugins/testbed", | ||
"src/legacy/core_plugins/elasticsearch", | ||
"src/legacy/core_plugins/tests_bundle" | ||
] | ||
}, | ||
{ | ||
"output": "src/plugins/telemetry/schema/oss_plugins.json", | ||
"root": "src/plugins/", | ||
"exclude": [ | ||
"src/plugins/kibana_react/", | ||
"src/plugins/testbed/", | ||
"src/plugins/kibana_utils/", | ||
"src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts", | ||
"src/plugins/kibana_usage_collection/server/collectors/application_usage/telemetry_application_usage_collector.ts", | ||
"src/plugins/kibana_usage_collection/server/collectors/management/telemetry_management_collector.ts", | ||
"src/plugins/kibana_usage_collection/server/collectors/ui_metric/telemetry_ui_metric_collector.ts", | ||
"src/plugins/telemetry/server/collectors/usage/telemetry_usage_collector.ts" | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Telemetry Tools | ||
|
||
## Schema extraction tool | ||
|
||
### Description | ||
|
||
The tool is used to extract telemetry collectors schema from all `*.{ts}` files in provided plugins directories to JSON files. The tool looks for `.telemetryrc.json` files in the root of the project and in the `x-pack` dir for its runtime configurations. | ||
|
||
It uses typescript parser to build an AST for each file. The tool is able to validate, extract and match collector schemas. | ||
|
||
### Examples and restrictions | ||
|
||
**Global restrictions**: | ||
|
||
The `id` can be only a string literal, it cannot be a template literals w/o expressions or string-only concatenation expressions or anything else. | ||
|
||
``` | ||
export const myCollector = makeUsageCollector<Usage>({ | ||
type: 'string_literal_only', | ||
... | ||
}); | ||
``` | ||
|
||
### Usage | ||
|
||
```bash | ||
node scripts/telemetry_extract.js | ||
``` | ||
|
||
This command has no additional flags or arguments. The `.telemetryrc.json` files specify the path to the directory where searching should start, output json files, and files to exclude. | ||
|
||
|
||
### Output | ||
|
||
|
||
The generated JSON files contain an ES mapping for each schema. This mapping is used to verify changes in the collectors and as the basis to map those fields into the external telemetry cluster. | ||
|
||
**Example**: | ||
|
||
```json | ||
{ | ||
"properties": { | ||
"cloud": { | ||
"properties": { | ||
"isCloudEnabled": { | ||
"type": "boolean" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Schema validation tool | ||
|
||
### Description | ||
|
||
The tool performs a number of checks on all telemetry collectors and verifies the following: | ||
|
||
1. Verifies the collector structure, fields, and returned values are using the appropriate types. | ||
2. Verifies that the collector `fetch` function Type matches the specified `schema` in the collector. | ||
3. Verifies that the collector `schema` matches the stored json schema . | ||
|
||
### Notes | ||
|
||
We don't catch every possible misuse of the collectors, but only the most common and critical ones. | ||
|
||
What will not be caught by the validator: | ||
|
||
* Mistyped SavedObject/CallCluster return value. Since the hits returned from ES can be typed to anything without any checks. It is advised to add functional tests that grabs the schema json file and checks that the returned usage matches the types exactly. | ||
|
||
* Fields in the schema that are never collected. If you are trying to report a field from ES but that value is never stored in ES, the check will not be able to detect if that field is ever collected in the first palce. It is advised to add unit/functional tests to check that all the fields are being reported as expected. | ||
|
||
The tool looks for `.telemetryrc.json` files in the root of the project and in the `x-pack` dir for its runtime configurations. | ||
|
||
Currently auto-fixer (`--fix`) can automatically fix the json files with the following errors: | ||
|
||
* incompatible schema - this error means that the collector schema was changed but the stored json schema file was not updated. | ||
|
||
* unused schemas - this error means that a collector was removed or its `type` renamed, the json schema file contains a schema that does not have a corrisponding collector. | ||
|
||
### Usage | ||
|
||
```bash | ||
node scripts/telemetry_check --fix | ||
``` | ||
|
||
* `--path` specifies a collector path instead of checking all collectors specified in the `.telemetryrc.json` files. Accepts a `.ts` file. The file must be discoverable by at least one rc file. | ||
* `--fix` tells the tool to try to fix as many violations as possible. All errors that tool won't be able to fix will be reported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
module.exports = { | ||
presets: ['@kbn/babel-preset/node_preset'], | ||
ignore: ['**/*.test.ts', '**/__fixture__/**'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@kbn/telemetry-tools", | ||
"version": "1.0.0", | ||
"license": "Apache-2.0", | ||
"main": "./target/index.js", | ||
"private": true, | ||
"scripts": { | ||
"build": "babel src --out-dir target --delete-dir-on-start --extensions .ts --source-maps=inline", | ||
"kbn:bootstrap": "yarn build", | ||
"kbn:watch": "yarn build --watch" | ||
}, | ||
"devDependencies": { | ||
"lodash": "npm:@elastic/[email protected]", | ||
"@kbn/dev-utils": "1.0.0", | ||
"@kbn/utility-types": "1.0.0", | ||
"@types/normalize-path": "^3.0.0", | ||
"normalize-path": "^3.0.0", | ||
"@types/lodash": "^3.10.1", | ||
"moment": "^2.24.0", | ||
"typescript": "3.9.5" | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
packages/kbn-telemetry-tools/src/cli/run_telemetry_check.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Listr from 'listr'; | ||
import chalk from 'chalk'; | ||
import { createFailError, run } from '@kbn/dev-utils'; | ||
|
||
import { | ||
createTaskContext, | ||
ErrorReporter, | ||
parseConfigsTask, | ||
extractCollectorsTask, | ||
checkMatchingSchemasTask, | ||
generateSchemasTask, | ||
checkCompatibleTypesTask, | ||
writeToFileTask, | ||
TaskContext, | ||
} from '../tools/tasks'; | ||
|
||
export function runTelemetryCheck() { | ||
run( | ||
async ({ flags: { fix = false, path }, log }) => { | ||
if (typeof fix !== 'boolean') { | ||
throw createFailError(`${chalk.white.bgRed(' TELEMETRY ERROR ')} --fix can't have a value`); | ||
} | ||
|
||
if (typeof path === 'boolean') { | ||
throw createFailError(`${chalk.white.bgRed(' TELEMETRY ERROR ')} --path require a value`); | ||
} | ||
|
||
if (fix && typeof path !== 'undefined') { | ||
throw createFailError( | ||
`${chalk.white.bgRed(' TELEMETRY ERROR ')} --fix is incompatible with --path flag.` | ||
); | ||
} | ||
|
||
const list = new Listr([ | ||
{ | ||
title: 'Checking .telemetryrc.json files', | ||
task: () => new Listr(parseConfigsTask(), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Extracting Collectors', | ||
task: (context) => new Listr(extractCollectorsTask(context, path), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Checking Compatible collector.schema with collector.fetch type', | ||
task: (context) => new Listr(checkCompatibleTypesTask(context), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Checking Matching collector.schema against stored json files', | ||
task: (context) => new Listr(checkMatchingSchemasTask(context), { exitOnError: true }), | ||
}, | ||
{ | ||
enabled: (_) => fix, | ||
skip: ({ roots }: TaskContext) => { | ||
return roots.every(({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length); | ||
}, | ||
title: 'Generating new telemetry mappings', | ||
task: (context) => new Listr(generateSchemasTask(context), { exitOnError: true }), | ||
}, | ||
{ | ||
enabled: (_) => fix, | ||
skip: ({ roots }: TaskContext) => { | ||
return roots.every(({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length); | ||
}, | ||
title: 'Updating telemetry mapping files', | ||
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }), | ||
}, | ||
]); | ||
|
||
try { | ||
const context = createTaskContext(); | ||
await list.run(context); | ||
} catch (error) { | ||
process.exitCode = 1; | ||
if (error instanceof ErrorReporter) { | ||
error.errors.forEach((e: string | Error) => log.error(e)); | ||
} else { | ||
log.error('Unhandled exception!'); | ||
log.error(error); | ||
} | ||
} | ||
process.exit(); | ||
}, | ||
{ | ||
flags: { | ||
allowUnexpected: true, | ||
guessTypesForUnexpectedFlags: true, | ||
}, | ||
} | ||
); | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/kbn-telemetry-tools/src/cli/run_telemetry_extract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Listr from 'listr'; | ||
import { run } from '@kbn/dev-utils'; | ||
|
||
import { | ||
createTaskContext, | ||
ErrorReporter, | ||
parseConfigsTask, | ||
extractCollectorsTask, | ||
generateSchemasTask, | ||
writeToFileTask, | ||
} from '../tools/tasks'; | ||
|
||
export function runTelemetryExtract() { | ||
run( | ||
async ({ flags: {}, log }) => { | ||
const list = new Listr([ | ||
{ | ||
title: 'Parsing .telemetryrc.json files', | ||
task: () => new Listr(parseConfigsTask(), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Extracting Telemetry Collectors', | ||
task: (context) => new Listr(extractCollectorsTask(context), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Generating Schema files', | ||
task: (context) => new Listr(generateSchemasTask(context), { exitOnError: true }), | ||
}, | ||
{ | ||
title: 'Writing to file', | ||
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }), | ||
}, | ||
]); | ||
|
||
try { | ||
const context = createTaskContext(); | ||
await list.run(context); | ||
} catch (error) { | ||
process.exitCode = 1; | ||
if (error instanceof ErrorReporter) { | ||
error.errors.forEach((e: string | Error) => log.error(e)); | ||
} else { | ||
log.error('Unhandled exception'); | ||
log.error(error); | ||
} | ||
} | ||
process.exit(); | ||
}, | ||
{ | ||
flags: { | ||
allowUnexpected: true, | ||
guessTypesForUnexpectedFlags: true, | ||
}, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { runTelemetryCheck } from './cli/run_telemetry_check'; | ||
export { runTelemetryExtract } from './cli/run_telemetry_extract'; |
Oops, something went wrong.