Skip to content

Commit fb1f9d2

Browse files
authored
telemetry(amazonq chat): Adding metrics for AB Testing. (#4330)
1 parent 6650a5d commit fb1f9d2

File tree

12 files changed

+106
-20
lines changed

12 files changed

+106
-20
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "feature",
3+
"description" : "Amazon Q Chat: Added additional parameters to onCopyCodeToClipboard and onCodeInsertToCursorPosition events"
4+
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/messenger/ChatPromptHandler.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
3232
private var requestId: String = ""
3333
private var statusCode: Int = 0
3434

35+
companion object {
36+
val CODE_BLOCK_REGEX: Regex = Regex("^```", RegexOption.MULTILINE)
37+
}
38+
39+
private fun countTotalNumberOfCodeBlocks(message: StringBuilder): Int {
40+
if (message.isEmpty()) {
41+
return 0
42+
}
43+
val countOfCodeBlocks = CODE_BLOCK_REGEX.findAll(message)
44+
val numberOfTripleBackTicksInMarkdown = countOfCodeBlocks.count()
45+
return numberOfTripleBackTicksInMarkdown / 2
46+
}
47+
3548
fun handle(
3649
tabId: String,
3750
triggerId: String,
@@ -75,7 +88,7 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
7588
ChatMessage(tabId = tabId, triggerId = triggerId, messageId = requestId, messageType = ChatMessageType.Answer, followUps = followUps)
7689

7790
telemetryHelper.setResponseStreamTotalTime(tabId)
78-
telemetryHelper.recordAddMessage(data, response, responseText.length, statusCode)
91+
telemetryHelper.recordAddMessage(data, response, responseText.length, statusCode, countTotalNumberOfCodeBlocks(responseText))
7992
emit(response)
8093
}
8194
.catch { exception ->

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/telemetry/TelemetryHelper.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
8383
}
8484

8585
// When Chat API responds to a user message (full response streamed)
86-
fun recordAddMessage(data: ChatRequestData, response: ChatMessage, responseLength: Int, statusCode: Int) {
86+
fun recordAddMessage(data: ChatRequestData, response: ChatMessage, responseLength: Int, statusCode: Int, numberOfCodeBlocks: Int) {
8787
AmazonqTelemetry.addMessage(
8888
cwsprChatConversationId = getConversationId(response.tabId).orEmpty(),
8989
cwsprChatMessageId = response.messageId,
@@ -93,7 +93,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
9393
cwsprChatProgrammingLanguage = data.activeFileContext.fileContext?.fileLanguage,
9494
cwsprChatActiveEditorTotalCharacters = data.activeFileContext.focusAreaContext?.codeSelection?.length,
9595
cwsprChatActiveEditorImportCount = data.activeFileContext.focusAreaContext?.codeNames?.fullyQualifiedNames?.used?.size,
96-
cwsprChatResponseCodeSnippetCount = 0,
96+
cwsprChatResponseCodeSnippetCount = numberOfCodeBlocks,
9797
cwsprChatResponseCode = statusCode,
9898
cwsprChatSourceLinkCount = response.relatedSuggestions?.size,
9999
cwsprChatReferencesCount = 0, // TODO
@@ -122,6 +122,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
122122
(responseStreamTotalTime[response.tabId] ?: 0).toDouble(),
123123
data.message.length,
124124
responseLength,
125+
numberOfCodeBlocks
125126
)
126127
}
127128

@@ -190,7 +191,9 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
190191
cwsprChatAcceptedCharactersLength = message.code.length,
191192
cwsprChatInteractionTarget = message.insertionTargetType,
192193
cwsprChatHasReference = null,
193-
credentialStartUrl = getStartUrl(context.project)
194+
credentialStartUrl = getStartUrl(context.project),
195+
cwsprChatCodeBlockIndex = message.codeBlockIndex,
196+
cwsprChatTotalCodeBlocks = message.totalCodeBlocks
194197
)
195198
ChatInteractWithMessageEvent.builder().apply {
196199
conversationId(getConversationId(message.tabId).orEmpty())
@@ -210,7 +213,9 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
210213
cwsprChatAcceptedNumberOfLines = message.code.lines().size,
211214
cwsprChatInteractionTarget = message.insertionTargetType,
212215
cwsprChatHasReference = null,
213-
credentialStartUrl = getStartUrl(context.project)
216+
credentialStartUrl = getStartUrl(context.project),
217+
cwsprChatCodeBlockIndex = message.codeBlockIndex,
218+
cwsprChatTotalCodeBlocks = message.totalCodeBlocks
214219
)
215220
ChatInteractWithMessageEvent.builder().apply {
216221
conversationId(getConversationId(message.tabId).orEmpty())

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/messages/CwcMessage.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ sealed interface IncomingCwcMessage : CwcMessage {
6767
val messageId: String,
6868
val code: String,
6969
val insertionTargetType: String?,
70+
val eventId: String?,
71+
val codeBlockIndex: Int?,
72+
val totalCodeBlocks: Int?
7073
) : IncomingCwcMessage
7174

7275
data class InsertCodeAtCursorPosition(
@@ -75,6 +78,9 @@ sealed interface IncomingCwcMessage : CwcMessage {
7578
val code: String,
7679
val insertionTargetType: String?,
7780
val codeReference: List<CodeReference>?,
81+
val eventId: String?,
82+
val codeBlockIndex: Int?,
83+
val totalCodeBlocks: Int?
7884
) : IncomingCwcMessage
7985

8086
data class TriggerTabIdReceived(

plugins/amazonq/mynah-ui/package-lock.json

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

plugins/amazonq/mynah-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lintfix": "eslint -c .eslintrc.js --fix --ext .ts ."
1313
},
1414
"dependencies": {
15-
"@aws/mynah-ui-chat": "npm:@aws/mynah-ui@4.4.2",
15+
"@aws/mynah-ui-chat": "npm:@aws/mynah-ui@4.5.6",
1616
"@types/node": "^14.18.5",
1717
"fs-extra": "^10.0.1",
1818
"ts-node": "^10.7.0",

plugins/amazonq/mynah-ui/src/mynah-ui/ui/apps/cwChatConnector.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export class Connector {
9292
messageId: string,
9393
code?: string,
9494
type?: 'selection' | 'block',
95-
codeReference?: CodeReference[]
95+
codeReference?: CodeReference[],
96+
eventId?: string,
97+
codeBlockIndex?: number,
98+
totalCodeBlocks?: number
9699
): void => {
97100
this.sendMessageToExtension({
98101
tabID: tabID,
@@ -102,6 +105,9 @@ export class Connector {
102105
tabType: 'cwc',
103106
insertionTargetType: type,
104107
codeReference,
108+
eventId,
109+
codeBlockIndex,
110+
totalCodeBlocks,
105111
})
106112
}
107113

@@ -110,7 +116,10 @@ export class Connector {
110116
messageId: string,
111117
code?: string,
112118
type?: 'selection' | 'block',
113-
codeReference?: CodeReference[]
119+
codeReference?: CodeReference[],
120+
eventId?: string,
121+
codeBlockIndex?: number,
122+
totalCodeBlocks?: number
114123
): void => {
115124
this.sendMessageToExtension({
116125
tabID: tabID,
@@ -120,6 +129,9 @@ export class Connector {
120129
tabType: 'cwc',
121130
insertionTargetType: type,
122131
codeReference,
132+
eventId,
133+
codeBlockIndex,
134+
totalCodeBlocks,
123135
})
124136
}
125137

plugins/amazonq/mynah-ui/src/mynah-ui/ui/connector.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ export class Connector {
212212
messageId: string,
213213
code?: string,
214214
type?: 'selection' | 'block',
215-
codeReference?: CodeReference[]
215+
codeReference?: CodeReference[],
216+
eventId?: string,
217+
codeBlockIndex?: number,
218+
totalCodeBlocks?: number
216219
): void => {
217220
switch (this.tabsStorage.getTab(tabID)?.type) {
218221
case 'cwc':
219-
this.cwChatConnector.onCodeInsertToCursorPosition(tabID, messageId, code, type, codeReference)
222+
this.cwChatConnector.onCodeInsertToCursorPosition(tabID, messageId, code, type, codeReference, eventId, codeBlockIndex, totalCodeBlocks)
220223
break
221224
case 'featuredev':
222225
this.featureDevChatConnector.onCodeInsertToCursorPosition(tabID, code, type, codeReference)
@@ -229,11 +232,14 @@ export class Connector {
229232
messageId: string,
230233
code?: string,
231234
type?: 'selection' | 'block',
232-
codeReference?: CodeReference[]
235+
codeReference?: CodeReference[],
236+
eventId?: string,
237+
codeBlockIndex?: number,
238+
totalCodeBlocks?: number
233239
): void => {
234240
switch (this.tabsStorage.getTab(tabID)?.type) {
235241
case 'cwc':
236-
this.cwChatConnector.onCopyCodeToClipboard(tabID, messageId, code, type, codeReference)
242+
this.cwChatConnector.onCopyCodeToClipboard(tabID, messageId, code, type, codeReference, eventId, codeBlockIndex, totalCodeBlocks)
237243
break
238244
case 'featuredev':
239245
this.featureDevChatConnector.onCopyCodeToClipboard(tabID, code, type, codeReference)

plugins/amazonq/mynah-ui/src/mynah-ui/ui/main.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,26 @@ export const createMynahUI = (ideApi: any, featureDevInitEnabled: boolean, codeT
393393
})
394394
},
395395
onCodeInsertToCursorPosition: connector.onCodeInsertToCursorPosition,
396-
onCopyCodeToClipboard: (tabId, messageId, code, type, referenceTrackerInfo) => {
397-
connector.onCopyCodeToClipboard(tabId, messageId, code, type, referenceTrackerInfo)
396+
onCopyCodeToClipboard: (
397+
tabId,
398+
messageId,
399+
code,
400+
type,
401+
referenceTrackerInfo,
402+
eventId,
403+
codeBlockIndex,
404+
totalCodeBlocks
405+
) => {
406+
connector.onCopyCodeToClipboard(
407+
tabId,
408+
messageId,
409+
code,
410+
type,
411+
referenceTrackerInfo,
412+
eventId,
413+
codeBlockIndex,
414+
totalCodeBlocks
415+
)
398416
mynahUI.notify({
399417
type: NotificationType.SUCCESS,
400418
content: 'Selected code is copied to clipboard',

plugins/core/jetbrains-community/resources/telemetryOverride.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@
165165
"type": "string",
166166
"description": "Identifies the entity within the message that user interacts with."
167167
},
168+
{
169+
"name": "cwsprChatCodeBlockIndex",
170+
"type": "int",
171+
"description": "Index of the code block inside a message in the conversation."
172+
},
173+
{
174+
"name": "cwsprChatTotalCodeBlocks",
175+
"type": "int",
176+
"description": "Total number of code blocks inside a message in the conversation."
177+
},
168178
{
169179
"name": "cwsprChatAcceptedCharactersLength",
170180
"type": "int",
@@ -469,6 +479,14 @@
469479
"type": "cwsprChatInteractionTarget",
470480
"required": false
471481
},
482+
{
483+
"type": "cwsprChatCodeBlockIndex",
484+
"required": false
485+
},
486+
{
487+
"type": "cwsprChatTotalCodeBlocks",
488+
"required": false
489+
},
472490
{
473491
"type": "cwsprChatAcceptedCharactersLength",
474492
"required": false

0 commit comments

Comments
 (0)