Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions packages/kbn-telemetry-tools/src/cli/run_telemetry_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,29 @@ export function runTelemetryCheck() {
task: (context) => new Listr(checkCompatibleTypesTask(context), { exitOnError: true }),
},
{
enabled: (_) => !!ignoreStoredJson,
enabled: (_) => fix || !ignoreStoredJson,
title: 'Checking Matching collector.schema against stored json files',
task: (context) =>
new Listr(checkMatchingSchemasTask(context, !fix), { exitOnError: true }),
},
{
enabled: (_) => fix,
skip: ({ roots }: TaskContext) => {
return roots.every(({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length);
const noDiffs = roots.every(
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
);
return noDiffs && 'No changes needed.';
},
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);
const noDiffs = roots.every(
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
);
return noDiffs && 'No changes needed.';
},
title: 'Updating telemetry mapping files',
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }),
Expand Down

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

5 changes: 4 additions & 1 deletion packages/kbn-telemetry-tools/src/tools/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ describe('parseTelemetryRC', () => {
{
root: configRoot,
output: configRoot,
exclude: [path.resolve(configRoot, './unmapped_collector.ts')],
exclude: [
path.resolve(configRoot, './unmapped_collector.ts'),
path.resolve(configRoot, './externally_defined_usage_collector/index.ts'),
],
},
]);
});
Expand Down
25 changes: 16 additions & 9 deletions packages/kbn-telemetry-tools/src/tools/ts_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function loadFixtureProgram(fixtureName: string) {
'src',
'fixtures',
'telemetry_collectors',
`${fixtureName}.ts`
`${fixtureName}`
);
const tsConfig = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
if (!tsConfig) {
Expand All @@ -52,49 +52,56 @@ describe('parseUsageCollection', () => {
it.todo('throws when a function is returned from fetch');
it.todo('throws when an object is not returned from fetch');

it('throws when `makeUsageCollector` argument is a function call', () => {
const { program, sourceFile } = loadFixtureProgram(
'externally_defined_usage_collector/index.ts'
);
expect(() => [...parseUsageCollection(sourceFile, program)]).toThrowErrorMatchingSnapshot();
});

it('throws when mapping fields is not defined', () => {
const { program, sourceFile } = loadFixtureProgram('unmapped_collector');
const { program, sourceFile } = loadFixtureProgram('unmapped_collector.ts');
expect(() => [...parseUsageCollection(sourceFile, program)]).toThrowErrorMatchingSnapshot();
});

it('parses root level defined collector', () => {
const { program, sourceFile } = loadFixtureProgram('working_collector');
const { program, sourceFile } = loadFixtureProgram('working_collector.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual([parsedWorkingCollector]);
});

it('parses collector with schema defined as union of spreads', () => {
const { program, sourceFile } = loadFixtureProgram('schema_defined_with_spreads_collector');
const { program, sourceFile } = loadFixtureProgram('schema_defined_with_spreads_collector.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual([parsedSchemaDefinedWithSpreadsCollector]);
});

it('parses nested collectors', () => {
const { program, sourceFile } = loadFixtureProgram('nested_collector');
const { program, sourceFile } = loadFixtureProgram('nested_collector.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual([parsedNestedCollector]);
});

it('parses imported schema property', () => {
const { program, sourceFile } = loadFixtureProgram('imported_schema');
const { program, sourceFile } = loadFixtureProgram('imported_schema.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual(parsedImportedSchemaCollector);
});

it('parses externally defined collectors', () => {
const { program, sourceFile } = loadFixtureProgram('externally_defined_collector');
const { program, sourceFile } = loadFixtureProgram('externally_defined_collector.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual(parsedExternallyDefinedCollector);
});

it('parses imported Usage interface', () => {
const { program, sourceFile } = loadFixtureProgram('imported_usage_interface');
const { program, sourceFile } = loadFixtureProgram('imported_usage_interface.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual(parsedImportedUsageInterface);
});

it('skips files that do not define a collector', () => {
const { program, sourceFile } = loadFixtureProgram('file_with_no_collector');
const { program, sourceFile } = loadFixtureProgram('file_with_no_collector.ts');
const result = [...parseUsageCollection(sourceFile, program)];
expect(result).toEqual([]);
});
Expand Down
8 changes: 3 additions & 5 deletions packages/kbn-telemetry-tools/src/tools/ts_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,11 @@ export function sourceHasUsageCollector(sourceFile: ts.SourceFile) {
}

const identifiers = (sourceFile as any).identifiers;
if (
(!identifiers.get('makeUsageCollector') && !identifiers.get('type')) ||
!identifiers.get('fetch')
) {
return false;
if (identifiers.get('makeUsageCollector')) {
return true;
}

return false;
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/fixtures/telemetry_collectors/.telemetryrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"root": ".",
"output": ".",
"exclude": [
"./unmapped_collector.ts"
"./unmapped_collector.ts",
"./externally_defined_usage_collector/index.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 interface Usage {
collectorName: string;
}

export function getUsageCollector(collectorName: string) {
return {
type: 'externally_defined_usage_collector',
isReady: () => true,
schema: {
collectorName: {
type: 'keyword' as 'keyword',
},
},
fetch(): Usage {
return {
collectorName,
};
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { getUsageCollector } from './get_usage_collector';

export function registerCollector(collectorSet: UsageCollectionSetup) {
const collectorName = 'some_configs';
const collector = collectorSet.makeUsageCollector(getUsageCollector(collectorName));

collectorSet.registerCollector(collector);
}
Loading