public async emitTelemetryOnSuggestion()

in packages/core/src/codewhisperer/tracker/codewhispererTracker.ts [92:181]


    public async emitTelemetryOnSuggestion(suggestion: AcceptedSuggestionEntry | InsertedCode) {
        let percentage = 1.0
        let currString = ''
        const customizationArn = undefinedIfEmpty(getSelectedCustomization().arn)

        try {
            if (suggestion.fileUrl?.scheme !== '') {
                const document = await vscode.workspace.openTextDocument(suggestion.fileUrl)
                if (document) {
                    currString = document.getText(new vscode.Range(suggestion.startPosition, suggestion.endPosition))
                    percentage = this.checkDiff(currString, suggestion.originalString)
                }
            }
        } catch (e) {
            getLogger().verbose(`Exception Thrown from CodeWhispererTracker: ${e}`)
            return
        } finally {
            if ('conversationID' in suggestion) {
                const event: AmazonqModifyCode = {
                    cwsprChatConversationId: suggestion.conversationID,
                    cwsprChatMessageId: suggestion.messageID,
                    cwsprChatModificationPercentage: percentage ? percentage : 0,
                    credentialStartUrl: AuthUtil.instance.startUrl,
                }

                telemetry.amazonq_modifyCode.emit(event)

                codeWhispererClient
                    .sendTelemetryEvent({
                        telemetryEvent: {
                            chatUserModificationEvent: {
                                conversationId: event.cwsprChatConversationId,
                                messageId: event.cwsprChatMessageId,
                                modificationPercentage: event.cwsprChatModificationPercentage,
                                customizationArn: customizationArn,
                            },
                        },
                    })
                    .then()
                    .catch(logSendTelemetryEventFailure)
            } else {
                telemetry.codewhisperer_userModification.emit({
                    codewhispererRequestId: suggestion.requestId ? suggestion.requestId : 'undefined',
                    codewhispererSessionId: suggestion.sessionId ? suggestion.sessionId : undefined,
                    codewhispererTriggerType: suggestion.triggerType,
                    codewhispererSuggestionIndex: suggestion.index ? suggestion.index : 0,
                    codewhispererModificationPercentage: percentage ? percentage : 0,
                    codewhispererCompletionType: suggestion.completionType,
                    codewhispererLanguage: suggestion.language,
                    credentialStartUrl: AuthUtil.instance.startUrl,
                    codewhispererCharactersAccepted: suggestion.originalString.length,
                    codewhispererCharactersModified: 0, // TODO: currently we don't have an accurate number for this field with existing implementation
                })

                codeWhispererClient
                    .sendTelemetryEvent({
                        telemetryEvent: {
                            userModificationEvent: {
                                sessionId: suggestion.sessionId,
                                requestId: suggestion.requestId,
                                programmingLanguage: { languageName: suggestion.language },
                                // deprecated % value and should not be used by service side
                                modificationPercentage: percentage,
                                customizationArn: customizationArn,
                                timestamp: new Date(),
                                acceptedCharacterCount: suggestion.originalString.length,
                                unmodifiedAcceptedCharacterCount: getUnmodifiedAcceptedTokens(
                                    suggestion.originalString,
                                    currString
                                ),
                            },
                        },
                        profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
                    })
                    .then()
                    .catch((error) => {
                        let requestId: string | undefined
                        if (isAwsError(error)) {
                            requestId = error.requestId
                        }

                        getLogger().debug(
                            `Failed to send UserModificationEvent to CodeWhisperer, requestId: ${requestId ?? ''}, message: ${
                                error.message
                            }`
                        )
                    })
            }
        }
    }