Skip to content

Commit 70549c2

Browse files
authored
[Telemetry] Telemetry tools check all makeUsageCollector calls (#79840)
1 parent 4dc6f3b commit 70549c2

File tree

21 files changed

+742
-161
lines changed

21 files changed

+742
-161
lines changed

packages/kbn-telemetry-tools/src/cli/run_telemetry_check.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,29 @@ export function runTelemetryCheck() {
8888
task: (context) => new Listr(checkCompatibleTypesTask(context), { exitOnError: true }),
8989
},
9090
{
91-
enabled: (_) => !!ignoreStoredJson,
91+
enabled: (_) => fix || !ignoreStoredJson,
9292
title: 'Checking Matching collector.schema against stored json files',
9393
task: (context) =>
9494
new Listr(checkMatchingSchemasTask(context, !fix), { exitOnError: true }),
9595
},
9696
{
9797
enabled: (_) => fix,
9898
skip: ({ roots }: TaskContext) => {
99-
return roots.every(({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length);
99+
const noDiffs = roots.every(
100+
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
101+
);
102+
return noDiffs && 'No changes needed.';
100103
},
101104
title: 'Generating new telemetry mappings',
102105
task: (context) => new Listr(generateSchemasTask(context), { exitOnError: true }),
103106
},
104107
{
105108
enabled: (_) => fix,
106109
skip: ({ roots }: TaskContext) => {
107-
return roots.every(({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length);
110+
const noDiffs = roots.every(
111+
({ esMappingDiffs }) => !esMappingDiffs || !esMappingDiffs.length
112+
);
113+
return noDiffs && 'No changes needed.';
108114
},
109115
title: 'Updating telemetry mapping files',
110116
task: (context) => new Listr(writeToFileTask(context), { exitOnError: true }),

packages/kbn-telemetry-tools/src/tools/__snapshots__/ts_parser.test.ts.snap

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-telemetry-tools/src/tools/config.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ describe('parseTelemetryRC', () => {
3333
{
3434
root: configRoot,
3535
output: configRoot,
36-
exclude: [path.resolve(configRoot, './unmapped_collector.ts')],
36+
exclude: [
37+
path.resolve(configRoot, './unmapped_collector.ts'),
38+
path.resolve(configRoot, './externally_defined_usage_collector/index.ts'),
39+
],
3740
},
3841
]);
3942
});

packages/kbn-telemetry-tools/src/tools/ts_parser.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function loadFixtureProgram(fixtureName: string) {
3333
'src',
3434
'fixtures',
3535
'telemetry_collectors',
36-
`${fixtureName}.ts`
36+
`${fixtureName}`
3737
);
3838
const tsConfig = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
3939
if (!tsConfig) {
@@ -52,49 +52,56 @@ describe('parseUsageCollection', () => {
5252
it.todo('throws when a function is returned from fetch');
5353
it.todo('throws when an object is not returned from fetch');
5454

55+
it('throws when `makeUsageCollector` argument is a function call', () => {
56+
const { program, sourceFile } = loadFixtureProgram(
57+
'externally_defined_usage_collector/index.ts'
58+
);
59+
expect(() => [...parseUsageCollection(sourceFile, program)]).toThrowErrorMatchingSnapshot();
60+
});
61+
5562
it('throws when mapping fields is not defined', () => {
56-
const { program, sourceFile } = loadFixtureProgram('unmapped_collector');
63+
const { program, sourceFile } = loadFixtureProgram('unmapped_collector.ts');
5764
expect(() => [...parseUsageCollection(sourceFile, program)]).toThrowErrorMatchingSnapshot();
5865
});
5966

6067
it('parses root level defined collector', () => {
61-
const { program, sourceFile } = loadFixtureProgram('working_collector');
68+
const { program, sourceFile } = loadFixtureProgram('working_collector.ts');
6269
const result = [...parseUsageCollection(sourceFile, program)];
6370
expect(result).toEqual([parsedWorkingCollector]);
6471
});
6572

6673
it('parses collector with schema defined as union of spreads', () => {
67-
const { program, sourceFile } = loadFixtureProgram('schema_defined_with_spreads_collector');
74+
const { program, sourceFile } = loadFixtureProgram('schema_defined_with_spreads_collector.ts');
6875
const result = [...parseUsageCollection(sourceFile, program)];
6976
expect(result).toEqual([parsedSchemaDefinedWithSpreadsCollector]);
7077
});
7178

7279
it('parses nested collectors', () => {
73-
const { program, sourceFile } = loadFixtureProgram('nested_collector');
80+
const { program, sourceFile } = loadFixtureProgram('nested_collector.ts');
7481
const result = [...parseUsageCollection(sourceFile, program)];
7582
expect(result).toEqual([parsedNestedCollector]);
7683
});
7784

7885
it('parses imported schema property', () => {
79-
const { program, sourceFile } = loadFixtureProgram('imported_schema');
86+
const { program, sourceFile } = loadFixtureProgram('imported_schema.ts');
8087
const result = [...parseUsageCollection(sourceFile, program)];
8188
expect(result).toEqual(parsedImportedSchemaCollector);
8289
});
8390

8491
it('parses externally defined collectors', () => {
85-
const { program, sourceFile } = loadFixtureProgram('externally_defined_collector');
92+
const { program, sourceFile } = loadFixtureProgram('externally_defined_collector.ts');
8693
const result = [...parseUsageCollection(sourceFile, program)];
8794
expect(result).toEqual(parsedExternallyDefinedCollector);
8895
});
8996

9097
it('parses imported Usage interface', () => {
91-
const { program, sourceFile } = loadFixtureProgram('imported_usage_interface');
98+
const { program, sourceFile } = loadFixtureProgram('imported_usage_interface.ts');
9299
const result = [...parseUsageCollection(sourceFile, program)];
93100
expect(result).toEqual(parsedImportedUsageInterface);
94101
});
95102

96103
it('skips files that do not define a collector', () => {
97-
const { program, sourceFile } = loadFixtureProgram('file_with_no_collector');
104+
const { program, sourceFile } = loadFixtureProgram('file_with_no_collector.ts');
98105
const result = [...parseUsageCollection(sourceFile, program)];
99106
expect(result).toEqual([]);
100107
});

packages/kbn-telemetry-tools/src/tools/ts_parser.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,11 @@ export function sourceHasUsageCollector(sourceFile: ts.SourceFile) {
178178
}
179179

180180
const identifiers = (sourceFile as any).identifiers;
181-
if (
182-
(!identifiers.get('makeUsageCollector') && !identifiers.get('type')) ||
183-
!identifiers.get('fetch')
184-
) {
185-
return false;
181+
if (identifiers.get('makeUsageCollector')) {
182+
return true;
186183
}
187184

185+
return false;
188186
return true;
189187
}
190188

src/fixtures/telemetry_collectors/.telemetryrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"root": ".",
33
"output": ".",
44
"exclude": [
5-
"./unmapped_collector.ts"
5+
"./unmapped_collector.ts",
6+
"./externally_defined_usage_collector/index.ts"
67
]
78
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export interface Usage {
21+
collectorName: string;
22+
}
23+
24+
export function getUsageCollector(collectorName: string) {
25+
return {
26+
type: 'externally_defined_usage_collector',
27+
isReady: () => true,
28+
schema: {
29+
collectorName: {
30+
type: 'keyword' as 'keyword',
31+
},
32+
},
33+
fetch(): Usage {
34+
return {
35+
collectorName,
36+
};
37+
},
38+
};
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
21+
import { getUsageCollector } from './get_usage_collector';
22+
23+
export function registerCollector(collectorSet: UsageCollectionSetup) {
24+
const collectorName = 'some_configs';
25+
const collector = collectorSet.makeUsageCollector(getUsageCollector(collectorName));
26+
27+
collectorSet.registerCollector(collector);
28+
}

0 commit comments

Comments
 (0)