Skip to content

Commit

Permalink
[Telemetry] Fix telemetry-tools TS parser for packages (#149819)
Browse files Browse the repository at this point in the history
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Alejandro Fernández Haro <[email protected]>
Closes #139389
Closes #100544
  • Loading branch information
Bamieh authored Jan 31, 2023
1 parent dda650f commit e5fa891
Show file tree
Hide file tree
Showing 57 changed files with 232 additions and 423 deletions.
5 changes: 5 additions & 0 deletions .telemetryrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"exclude": [
"src/plugins/kibana_usage_collection/server/collectors/config_usage/register_config_usage_collector.ts"
]
},
{
"output": "src/plugins/telemetry/schema/kbn_packages.json",
"root": "packages/",
"exclude": []
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedEnumCollector: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/enum_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/enum_collector.ts',
{
collectorName: 'my_enum_collector',
fetch: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedUsageCollection } from '../ts_parser';

export const parsedExternallyDefinedCollector: ParsedUsageCollection[] = [
[
'src/fixtures/telemetry_collectors/externally_defined_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/externally_defined_collector.ts',
{
collectorName: 'from_variable_collector',
schema: {
Expand All @@ -33,7 +33,7 @@ export const parsedExternallyDefinedCollector: ParsedUsageCollection[] = [
},
],
[
'src/fixtures/telemetry_collectors/externally_defined_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/externally_defined_collector.ts',
{
collectorName: 'from_fn_collector',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedUsageCollection } from '../ts_parser';

export const parsedImportedInterfaceFromExport: ParsedUsageCollection[] = [
[
'src/fixtures/telemetry_collectors/imported_interface_from_export/index.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/imported_interface_from_export/index.ts',
{
collectorName: 'importing_from_export_collector',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedUsageCollection } from '../ts_parser';

export const parsedImportedSchemaCollector: ParsedUsageCollection[] = [
[
'src/fixtures/telemetry_collectors/imported_schema.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/imported_schema.ts',
{
collectorName: 'with_imported_schema',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedUsageCollection } from '../ts_parser';

export const parsedImportedUsageInterface: ParsedUsageCollection[] = [
[
'src/fixtures/telemetry_collectors/imported_usage_interface.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/imported_usage_interface.ts',
{
collectorName: 'imported_usage_interface_collector',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedIndexedInterfaceWithNoMatchingSchema: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/indexed_interface_with_not_matching_schema.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/indexed_interface_with_not_matching_schema.ts',
{
collectorName: 'indexed_interface_with_not_matching_schema',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedNestedCollector: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/nested_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/nested_collector.ts',
{
collectorName: 'my_nested_collector',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedSchemaDefinedWithSpreadsCollector: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/schema_defined_with_spreads_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/schema_defined_with_spreads_collector.ts',
{
collectorName: 'schema_defined_with_spreads',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedUsageCollection } from '../ts_parser';

export const parsedStatsCollector: ParsedUsageCollection[] = [
[
'src/fixtures/telemetry_collectors/stats_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/stats_collector.ts',
{
collectorName: 'my_stats_collector_with_schema',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedWorkingCollector: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/working_collector.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/working_collector.ts',
{
collectorName: 'my_working_collector',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SyntaxKind } from 'typescript';
import { ParsedUsageCollection } from '../ts_parser';

export const parsedCollectorWithDescription: ParsedUsageCollection = [
'src/fixtures/telemetry_collectors/working_collector_with_description.ts',
'packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/working_collector_with_description.ts',
{
collectorName: 'my_working_collector_with_description',
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import moment, { Moment } from 'moment';
import { MakeSchemaFrom } from '@kbn/usage-collection-plugin/server';
import type { MakeSchemaFrom } from '@kbn/usage-collection-plugin/server';

export interface Usage {
locale: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeUsageCollector } = createUsageCollectionSetupMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* Side Public License, v 1.
*/

import type { UsageCollectorOptions } from '@kbn/usage-collection-plugin/server';
// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import type { UsageCollectorOptions } from '@kbn/usage-collection-plugin/server';

const collectorSet = createUsageCollectionSetupMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { getUsageCollector } from './get_usage_collector';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import type { Usage } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import { externallyDefinedSchema } from './constants';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import { Usage } from './constants';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeUsageCollector } = createUsageCollectionSetupMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* Side Public License, v 1.
*/

import type { Collector } from '@kbn/usage-collection-plugin/server';
// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import type { Collector } from '@kbn/usage-collection-plugin/server';

const collectorSet = createUsageCollectionSetupMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* Side Public License, v 1.
*/

import type { MakeSchemaFrom } from '@kbn/usage-collection-plugin/server';
// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';
import type { MakeSchemaFrom } from '@kbn/usage-collection-plugin/server';

const { makeUsageCollector } = createUsageCollectionSetupMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeStatsCollector } = createUsageCollectionSetupMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeUsageCollector } = createUsageCollectionSetupMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeUsageCollector } = createUsageCollectionSetupMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

// eslint-disable-next-line @kbn/imports/no_boundary_crossing
import { createUsageCollectionSetupMock } from '@kbn/usage-collection-plugin/server/mocks';

const { makeUsageCollector } = createUsageCollectionSetupMock();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
*/

import { cloneDeep } from 'lodash';
import * as ts from 'typescript';
import ts from 'typescript';
import { parsedWorkingCollector } from './__fixture__/parsed_working_collector';
import { parsedIndexedInterfaceWithNoMatchingSchema } from './__fixture__/parsed_indexed_interface_with_not_matching_schema';
import { checkCompatibleTypeDescriptor, checkMatchingMapping } from './check_collector_integrity';
import * as path from 'path';
import { readFile } from 'fs';
import { promisify } from 'util';
const read = promisify(readFile);
import { readFile } from 'fs/promises';

async function parseJsonFile(relativePath: string) {
const schemaPath = path.resolve(__dirname, '__fixture__', relativePath);
const fileContent = await read(schemaPath, 'utf8');
const fileContent = await readFile(schemaPath, 'utf8');
return JSON.parse(fileContent);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/kbn-telemetry-tools/src/tools/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import * as path from 'path';
import { REPO_ROOT } from '@kbn/repo-info';
import { parseTelemetryRC } from './config';

describe('parseTelemetryRC', () => {
Expand All @@ -17,7 +16,7 @@ describe('parseTelemetryRC', () => {
});

it('returns parsed rc file', async () => {
const configRoot = path.resolve(REPO_ROOT, 'src/fixtures/telemetry_collectors');
const configRoot = path.resolve(__dirname, '__fixture__', 'telemetry_collectors');
const config = await parseTelemetryRC(configRoot);
expect(config).toStrictEqual([
{
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-telemetry-tools/src/tools/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import * as path from 'path';
import { readFileAsync } from './utils';
import { readFile } from 'fs/promises';
import { TELEMETRY_RC } from './constants';

export interface TelemetryRC {
Expand All @@ -22,7 +22,7 @@ export async function readRcFile(rcRoot: string) {
}

const rcFile = path.resolve(rcRoot, TELEMETRY_RC);
const configString = await readFileAsync(rcFile, 'utf8');
const configString = await readFile(rcFile, 'utf8');
return JSON.parse(configString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
* Side Public License, v 1.
*/

import * as ts from 'typescript';
import ts from 'typescript';
import * as path from 'path';
import { REPO_ROOT } from '@kbn/repo-info';
import { extractCollectors, getProgramPaths } from './extract_collectors';
import { parseTelemetryRC } from './config';
import { allExtractedCollectors } from './__fixture__/all_extracted_collectors';

describe('extractCollectors', () => {
it('extracts collectors given rc file', async () => {
const configRoot = path.join(REPO_ROOT, 'src/fixtures/telemetry_collectors');
const configRoot = path.resolve(__dirname, '__fixture__', 'telemetry_collectors');
const tsConfig = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
if (!tsConfig) {
throw new Error('Could not find a valid tsconfig.json.');
Expand Down
14 changes: 3 additions & 11 deletions packages/kbn-telemetry-tools/src/tools/extract_collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
*/

import globby from 'globby';
import * as ts from 'typescript';
import * as path from 'path';
import { parseUsageCollection } from './ts_parser';
import { TelemetryRC } from './config';
import { compilerHost } from './compiler_host';
import { createKibanaProgram, getAllSourceFiles } from './ts_program';

export async function getProgramPaths({
root,
Expand Down Expand Up @@ -52,15 +51,8 @@ export async function getProgramPaths({
}

export function* extractCollectors(fullPaths: string[], tsConfig: any) {
const program = ts.createProgram(fullPaths, tsConfig, compilerHost);
program.getTypeChecker();
const sourceFiles = fullPaths.map((fullPath) => {
const sourceFile = program.getSourceFile(fullPath);
if (!sourceFile) {
throw Error(`Unable to get sourceFile ${fullPath}.`);
}
return sourceFile;
});
const program = createKibanaProgram(fullPaths, tsConfig);
const sourceFiles = getAllSourceFiles(fullPaths, program);

for (const sourceFile of sourceFiles) {
yield* parseUsageCollection(sourceFile, program);
Expand Down
6 changes: 2 additions & 4 deletions packages/kbn-telemetry-tools/src/tools/manage_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { generateMapping } from './manage_schema';
import { parsedWorkingCollector } from './__fixture__/parsed_working_collector';
import { parsedCollectorWithDescription } from './__fixture__/parsed_working_collector_with_description';
import * as path from 'path';
import { readFile } from 'fs';
import { promisify } from 'util';
const read = promisify(readFile);
import { readFile } from 'fs/promises';

async function parseJsonFile(relativePath: string) {
const schemaPath = path.resolve(__dirname, '__fixture__', relativePath);
const fileContent = await read(schemaPath, 'utf8');
const fileContent = await readFile(schemaPath, 'utf8');
return JSON.parse(fileContent);
}

Expand Down
Loading

0 comments on commit e5fa891

Please sign in to comment.