Skip to content

Commit d68f900

Browse files
authored
Merge pull request #2843 from BPapp-MS/benpapp/AddFlagsToRushTelemetry
[rush-lib] Add flags for parameters to rush telemetry
2 parents 0aaba00 + bd48e11 commit d68f900

File tree

7 files changed

+93
-39
lines changed

7 files changed

+93
-39
lines changed

apps/rush-lib/src/cli/actions/BaseInstallAction.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Stopwatch } from '../../utilities/Stopwatch';
2121
import { VersionMismatchFinder } from '../../logic/versionMismatch/VersionMismatchFinder';
2222
import { Variants } from '../../api/Variants';
2323
import { RushConstants } from '../../logic/RushConstants';
24+
import { SelectionParameterSet } from '../SelectionParameterSet';
2425

2526
const installManagerFactoryModule: typeof import('../../logic/InstallManagerFactory') = Import.lazy(
2627
'../../logic/InstallManagerFactory',
@@ -39,6 +40,11 @@ export abstract class BaseInstallAction extends BaseRushAction {
3940
protected _debugPackageManagerParameter!: CommandLineFlagParameter;
4041
protected _maxInstallAttempts!: CommandLineIntegerParameter;
4142
protected _ignoreHooksParameter!: CommandLineFlagParameter;
43+
/*
44+
* Subclasses can initialize the _selectionParameters property in order for
45+
* the parameters to be written to the telemetry file
46+
*/
47+
protected _selectionParameters?: SelectionParameterSet;
4248

4349
protected onDefineParameters(): void {
4450
this._purgeParameter = this.defineFlagParameter({
@@ -181,15 +187,19 @@ export abstract class BaseInstallAction extends BaseRushAction {
181187
success: boolean
182188
): void {
183189
if (this.parser.telemetry) {
190+
const extraData: Record<string, string> = {
191+
mode: this.actionName,
192+
clean: (!!this._purgeParameter.value).toString(),
193+
debug: installManagerOptions.debug.toString(),
194+
full: installManagerOptions.fullUpgrade.toString(),
195+
...this.getParameterStringMap(),
196+
...this._selectionParameters?.getTelemetry()
197+
};
184198
this.parser.telemetry.log({
185199
name: 'install',
186200
duration: stopwatch.duration,
187201
result: success ? 'Succeeded' : 'Failed',
188-
extraData: {
189-
mode: this.actionName,
190-
clean: (!!this._purgeParameter.value).toString(),
191-
full: installManagerOptions.fullUpgrade.toString()
192-
}
202+
extraData
193203
});
194204
}
195205
}

apps/rush-lib/src/cli/actions/InstallAction.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { RushCommandLineParser } from '../RushCommandLineParser';
77
import { SelectionParameterSet } from '../SelectionParameterSet';
88

99
export class InstallAction extends BaseInstallAction {
10-
protected _selectionParameters!: SelectionParameterSet;
11-
1210
public constructor(parser: RushCommandLineParser) {
1311
super({
1412
actionName: 'install',
@@ -52,7 +50,7 @@ export class InstallAction extends BaseInstallAction {
5250
// it is safe to assume that the value is not null
5351
maxInstallAttempts: this._maxInstallAttempts.value!,
5452
// These are derived independently of the selection for command line brevity
55-
pnpmFilterArguments: this._selectionParameters.getPnpmFilterArguments()
53+
pnpmFilterArguments: this._selectionParameters!.getPnpmFilterArguments()
5654
};
5755
}
5856
}

apps/rush-lib/src/cli/scriptActions/BulkScriptAction.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import * as os from 'os';
55
import colors from 'colors/safe';
66

77
import { AlreadyReportedError, ConsoleTerminalProvider, Terminal } from '@rushstack/node-core-library';
8-
import {
9-
CommandLineFlagParameter,
10-
CommandLineStringParameter,
11-
CommandLineParameterKind
12-
} from '@rushstack/ts-command-line';
8+
import { CommandLineFlagParameter, CommandLineStringParameter } from '@rushstack/ts-command-line';
139

1410
import { Event } from '../../index';
1511
import { SetupChecks } from '../../logic/SetupChecks';
@@ -381,21 +377,10 @@ export class BulkScriptAction extends BaseScriptAction {
381377
}
382378

383379
private _collectTelemetry(stopwatch: Stopwatch, success: boolean): void {
384-
const extraData: { [key: string]: string } = this._selectionParameters.getTelemetry();
385-
386-
for (const customParameter of this.customParameters) {
387-
switch (customParameter.kind) {
388-
case CommandLineParameterKind.Flag:
389-
case CommandLineParameterKind.Choice:
390-
case CommandLineParameterKind.String:
391-
case CommandLineParameterKind.Integer:
392-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
393-
extraData[customParameter.longName] = JSON.stringify((customParameter as any).value);
394-
break;
395-
default:
396-
extraData[customParameter.longName] = '?';
397-
}
398-
}
380+
const extraData: Record<string, string> = {
381+
...this._selectionParameters.getTelemetry(),
382+
...this.getParameterStringMap()
383+
};
399384

400385
if (this.parser.telemetry) {
401386
this.parser.telemetry.log({
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Add properties to the extraData section of the telemetry file for parameter usage in the install commands",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/ts-command-line",
5+
"comment": "Add getParameterStringMap to CommandLineParameterProvider, to easily query parameter usage for telemetry",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/ts-command-line",
10+
"email": "[email protected]"
11+
}

common/reviews/api/ts-command-line.api.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class CommandLineChoiceListParameter extends CommandLineParameter {
3636
// @internal
3737
_setValue(data: any): void;
3838
get values(): ReadonlyArray<string>;
39-
}
39+
}
4040

4141
// @public
4242
export class CommandLineChoiceParameter extends CommandLineParameter {
@@ -53,7 +53,7 @@ export class CommandLineChoiceParameter extends CommandLineParameter {
5353
// @internal
5454
_setValue(data: any): void;
5555
get value(): string | undefined;
56-
}
56+
}
5757

5858
// @public
5959
export const enum CommandLineConstants {
@@ -70,7 +70,7 @@ export class CommandLineFlagParameter extends CommandLineParameter {
7070
// @internal
7171
_setValue(data: any): void;
7272
get value(): boolean;
73-
}
73+
}
7474

7575
// @public
7676
export class CommandLineHelper {
@@ -87,7 +87,7 @@ export class CommandLineIntegerListParameter extends CommandLineParameterWithArg
8787
// @internal
8888
_setValue(data: any): void;
8989
get values(): ReadonlyArray<number>;
90-
}
90+
}
9191

9292
// @public
9393
export class CommandLineIntegerParameter extends CommandLineParameterWithArgument {
@@ -102,7 +102,7 @@ export class CommandLineIntegerParameter extends CommandLineParameterWithArgumen
102102
// @internal
103103
_setValue(data: any): void;
104104
get value(): number | undefined;
105-
}
105+
}
106106

107107
// @public
108108
export abstract class CommandLineParameter {
@@ -157,6 +157,7 @@ export abstract class CommandLineParameterProvider {
157157
getFlagParameter(parameterLongName: string): CommandLineFlagParameter;
158158
getIntegerListParameter(parameterLongName: string): CommandLineIntegerListParameter;
159159
getIntegerParameter(parameterLongName: string): CommandLineIntegerParameter;
160+
getParameterStringMap(): Record<string, string>;
160161
getStringListParameter(parameterLongName: string): CommandLineStringListParameter;
161162
getStringParameter(parameterLongName: string): CommandLineStringParameter;
162163
protected abstract onDefineParameters(): void;
@@ -173,7 +174,7 @@ export abstract class CommandLineParameterWithArgument extends CommandLineParame
173174
constructor(definition: IBaseCommandLineDefinitionWithArgument);
174175
readonly argumentName: string;
175176
readonly completions: (() => Promise<string[]>) | undefined;
176-
}
177+
}
177178

178179
// @public
179180
export abstract class CommandLineParser extends CommandLineParameterProvider {
@@ -188,7 +189,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
188189
protected onExecute(): Promise<void>;
189190
selectedAction: CommandLineAction | undefined;
190191
tryGetAction(actionName: string): CommandLineAction | undefined;
191-
}
192+
}
192193

193194
// @public
194195
export class CommandLineRemainder {
@@ -200,7 +201,7 @@ export class CommandLineRemainder {
200201
// @internal
201202
_setValue(data: any): void;
202203
get values(): ReadonlyArray<string>;
203-
}
204+
}
204205

205206
// @public
206207
export class CommandLineStringListParameter extends CommandLineParameterWithArgument {
@@ -212,7 +213,7 @@ export class CommandLineStringListParameter extends CommandLineParameterWithArgu
212213
// @internal
213214
_setValue(data: any): void;
214215
get values(): ReadonlyArray<string>;
215-
}
216+
}
216217

217218
// @public
218219
export class CommandLineStringParameter extends CommandLineParameterWithArgument {
@@ -227,7 +228,7 @@ export class CommandLineStringParameter extends CommandLineParameterWithArgument
227228
// @internal
228229
_setValue(data: any): void;
229230
get value(): string | undefined;
230-
}
231+
}
231232

232233
// @public (undocumented)
233234
export class DynamicCommandLineAction extends CommandLineAction {
@@ -321,5 +322,4 @@ export interface ICommandLineStringDefinition extends IBaseCommandLineDefinition
321322
export interface ICommandLineStringListDefinition extends IBaseCommandLineDefinitionWithArgument {
322323
}
323324

324-
325325
```

libraries/ts-command-line/src/providers/CommandLineParameterProvider.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,45 @@ export abstract class CommandLineParameterProvider {
289289
return this._getArgumentParser().formatHelp();
290290
}
291291

292+
/**
293+
* Returns a object which maps the long name of each parameter in this.parameters
294+
* to the stringified form of its value. This is useful for logging telemetry, but
295+
* it is not the proper way of accessing parameters or their values.
296+
*/
297+
public getParameterStringMap(): Record<string, string> {
298+
const parameterMap: Record<string, string> = {};
299+
for (const parameter of this.parameters) {
300+
switch (parameter.kind) {
301+
case CommandLineParameterKind.Flag:
302+
case CommandLineParameterKind.Choice:
303+
case CommandLineParameterKind.String:
304+
case CommandLineParameterKind.Integer:
305+
parameterMap[parameter.longName] = JSON.stringify(
306+
(
307+
parameter as
308+
| CommandLineFlagParameter
309+
| CommandLineIntegerParameter
310+
| CommandLineChoiceParameter
311+
| CommandLineStringParameter
312+
).value
313+
);
314+
break;
315+
case CommandLineParameterKind.StringList:
316+
case CommandLineParameterKind.IntegerList:
317+
case CommandLineParameterKind.ChoiceList:
318+
const arrayValue: ReadonlyArray<string | number> | undefined = (
319+
parameter as
320+
| CommandLineIntegerListParameter
321+
| CommandLineStringListParameter
322+
| CommandLineChoiceListParameter
323+
).values;
324+
parameterMap[parameter.longName] = arrayValue ? arrayValue.join(',') : '';
325+
break;
326+
}
327+
}
328+
return parameterMap;
329+
}
330+
292331
/**
293332
* The child class should implement this hook to define its command-line parameters,
294333
* e.g. by calling defineFlagParameter().

0 commit comments

Comments
 (0)