Skip to content

Commit 3d9327a

Browse files
author
Akila Tennakoon
committed
add telemetry to code actions
1 parent 5a795ff commit 3d9327a

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/handlers/ExecutionHandler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function executionHandler(
3737
});
3838
}
3939
case ANALYZE_DIAGNOSTIC: {
40+
TelemetryService.instance.get('CodeAction').count(`accepted.diagnoseWithAI`, 1);
4041
return executeWithError(components, async () => {
4142
const message = await components.cfnAI.analyzeDiagnostic(
4243
params.arguments?.[0] as string,
@@ -66,6 +67,15 @@ export function executionHandler(
6667
components.diagnosticCoordinator
6768
.handleClearCfnDiagnostic(uri, diagnosticId)
6869
.catch((err) => log.error(err, `Error clearing diagnostic`));
70+
TelemetryService.instance.get('CodeAction').count(`accepted.clearDiagnostic`, 1);
71+
}
72+
break;
73+
}
74+
case TRACK_CODE_ACTION_ACCEPTED: {
75+
const args = params.arguments ?? [];
76+
if (args.length >= 1) {
77+
const actionType = args[0] as string;
78+
TelemetryService.instance.get('CodeAction').count(`accepted.${actionType}`, 1);
6979
}
7080
break;
7181
}
@@ -91,3 +101,4 @@ export const OPTIMIZE_TEMPLATE = '/command/llm/template/optimize';
91101
export const ANALYZE_DIAGNOSTIC = '/command/llm/diagnostic/analyze';
92102
export const RECOMMEND_RELATED_RESOURCES = '/command/llm/template/recommend-related';
93103
export const CLEAR_DIAGNOSTIC = '/command/template/clear-diagnostic';
104+
export const TRACK_CODE_ACTION_ACCEPTED = '/command/codeAction/track';

src/services/CodeActionService.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@ import { SyntaxTreeManager } from '../context/syntaxtree/SyntaxTreeManager';
1515
import { NodeSearch } from '../context/syntaxtree/utils/NodeSearch';
1616
import { NodeType } from '../context/syntaxtree/utils/NodeType';
1717
import { DocumentManager } from '../document/DocumentManager';
18-
import { ANALYZE_DIAGNOSTIC } from '../handlers/ExecutionHandler';
18+
import { ANALYZE_DIAGNOSTIC, TRACK_CODE_ACTION_ACCEPTED } from '../handlers/ExecutionHandler';
1919
import { CfnInfraCore } from '../server/CfnInfraCore';
2020
import { CFN_VALIDATION_SOURCE } from '../stacks/actions/ValidationWorkflow';
2121
import { LoggerFactory } from '../telemetry/LoggerFactory';
22-
import { Track } from '../telemetry/TelemetryDecorator';
22+
import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
23+
import { Telemetry, Track } from '../telemetry/TelemetryDecorator';
2324
import { pointToPosition } from '../utils/TypeConverters';
2425
import { ExtractToParameterProvider } from './extractToParameter/ExtractToParameterProvider';
2526

2627
export interface CodeActionFix {
2728
title: string;
2829
kind: string;
30+
actionType: string;
2931
diagnostic: Diagnostic;
3032
textEdits: TextEdit[];
3133
command?: Command;
3234
}
3335

3436
export class CodeActionService {
37+
@Telemetry() private readonly telemetry!: ScopedTelemetry;
3538
private static readonly REMOVE_ERROR_TITLE = 'Hide validation error';
3639
private readonly log = LoggerFactory.getLogger(CodeActionService);
3740

@@ -44,7 +47,9 @@ export class CodeActionService {
4447
private readonly documentManager: DocumentManager,
4548
private readonly contextManager: ContextManager,
4649
private readonly extractToParameterProvider?: ExtractToParameterProvider,
47-
) {}
50+
) {
51+
this.initializeCounters();
52+
}
4853

4954
/**
5055
* Process diagnostics and generate code actions with fixes
@@ -76,6 +81,13 @@ export class CodeActionService {
7681
return codeActions;
7782
}
7883

84+
private initializeCounters(): void {
85+
this.telemetry.count('quickfix.cfnLintFixOffered', 0);
86+
this.telemetry.count('quickfix.clearDiagnosticOffered', 0);
87+
this.telemetry.count('refactor.extractToParameterOffered', 0);
88+
this.telemetry.count('refactor.extractAllToParameterOffered', 0);
89+
}
90+
7991
/**
8092
* Generate fixes for a specific diagnostic
8193
*/
@@ -112,10 +124,12 @@ export class CodeActionService {
112124
* Generate fixes for CFN Validation diagnostics
113125
*/
114126
private generateCfnValidationFixes(diagnostic: Diagnostic, uri: string): CodeActionFix[] {
127+
this.telemetry.count('quickfix.clearDiagnosticOffered', 1)
115128
return [
116129
{
117130
title: CodeActionService.REMOVE_ERROR_TITLE,
118131
kind: 'quickfix',
132+
actionType: 'clearDiagnostic',
119133
diagnostic,
120134
textEdits: [],
121135
command: {
@@ -165,6 +179,8 @@ export class CodeActionService {
165179
}
166180
}
167181

182+
this.telemetry.count('quickfix.cfnLintFixOffered', fixes.length);
183+
168184
return fixes;
169185
}
170186

@@ -182,6 +198,7 @@ export class CodeActionService {
182198
fixes.push({
183199
title: `Remove invalid property '${propertyName}'`,
184200
kind: 'quickfix',
201+
actionType: 'removeProperty',
185202
diagnostic,
186203
textEdits: [
187204
{
@@ -284,6 +301,7 @@ export class CodeActionService {
284301
fixes.push({
285302
title: `Add required property '${propertyName}'`,
286303
kind: 'quickfix',
304+
actionType: 'addRequiredProperty',
287305
diagnostic,
288306
textEdits: [
289307
{
@@ -324,6 +342,12 @@ export class CodeActionService {
324342

325343
if (fix.command) {
326344
codeAction.command = fix.command;
345+
} else {
346+
codeAction.command = {
347+
title: 'Track code action',
348+
command: TRACK_CODE_ACTION_ACCEPTED,
349+
arguments: [fix.actionType],
350+
};
327351
}
328352

329353
return codeAction;
@@ -520,20 +544,27 @@ export class CodeActionService {
520544
const canExtract = this.extractToParameterProvider.canExtract(context);
521545

522546
if (canExtract) {
523-
const extractAction = this.generateExtractToParameterAction(params, context);
547+
const extractAction = this.telemetry.measure('refactor.extractToParameter', () =>
548+
this.generateExtractToParameterAction(params, context)
549+
);
550+
524551
if (extractAction) {
525552
refactorActions.push(extractAction);
553+
this.telemetry.count('refactor.extractToParameterOffered', 1);
526554
}
527555

528-
const hasMultiple = this.extractToParameterProvider.hasMultipleOccurrences(
529-
context,
530-
params.textDocument.uri,
556+
const hasMultiple = this.telemetry.measure('refactor.hasMultipleOccurrences', () =>
557+
this.extractToParameterProvider!.hasMultipleOccurrences(context, params.textDocument.uri)
531558
);
532559

533560
if (hasMultiple) {
534-
const extractAllAction = this.generateExtractAllOccurrencesToParameterAction(params, context);
561+
const extractAllAction = this.telemetry.measure('refactor.extractAllToParameter', () =>
562+
this.generateExtractAllOccurrencesToParameterAction(params, context)
563+
);
564+
535565
if (extractAllAction) {
536566
refactorActions.push(extractAllAction);
567+
this.telemetry.count('refactor.extractAllToParameterOffered', 1);
537568
}
538569
}
539570
}
@@ -579,7 +610,13 @@ export class CodeActionService {
579610
command: {
580611
title: 'Position cursor in parameter description',
581612
command: 'aws.cloudformation.extractToParameter.positionCursor',
582-
arguments: [params.textDocument.uri, extractionResult.parameterName, context.documentType],
613+
arguments: [
614+
params.textDocument.uri,
615+
extractionResult.parameterName,
616+
context.documentType,
617+
TRACK_CODE_ACTION_ACCEPTED,
618+
'extractToParameter',
619+
],
583620
},
584621
};
585622
} catch (error) {
@@ -625,7 +662,13 @@ export class CodeActionService {
625662
command: {
626663
title: 'Position cursor in parameter description',
627664
command: 'aws.cloudformation.extractToParameter.positionCursor',
628-
arguments: [params.textDocument.uri, extractionResult.parameterName, context.documentType],
665+
arguments: [
666+
params.textDocument.uri,
667+
extractionResult.parameterName,
668+
context.documentType,
669+
TRACK_CODE_ACTION_ACCEPTED,
670+
'extractAllToParameter',
671+
],
629672
},
630673
};
631674
} catch (error) {

0 commit comments

Comments
 (0)