Skip to content

Commit a5f89df

Browse files
authored
Update code scan telemetry reporting. (#3240)
1 parent 69f1229 commit a5f89df

File tree

8 files changed

+192
-122
lines changed

8 files changed

+192
-122
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ kotlin = "1.6.21"
1818
kotlinCoroutines = "1.5.0"
1919
mockito = "4.6.1"
2020
mockitoKotlin = "4.0.0"
21-
telemetryGenerator = "1.0.58"
21+
telemetryGenerator = "1.0.61"
2222
testLogger = "3.1.0"
2323
testRetry = "1.2.1"
2424
slf4j = "1.7.36"

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/CodeWhispererCodeScanManager.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.listeners
4545
import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.sessionconfig.CodeScanSessionConfig
4646
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.overlaps
4747
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
48-
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CodeScanResponseContext
4948
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CodeScanTelemetryEvent
5049
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeWhispererTelemetryService
5150
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererColorUtil.INACTIVE_TEXT_COLOR
@@ -109,7 +108,7 @@ internal class CodeWhispererCodeScanManager(val project: Project) {
109108
private fun launchCodeScanCoroutine() = projectCoroutineScope(project).launch {
110109
var codeScanStatus: Result = Result.Failed
111110
val startTime = Instant.now().toEpochMilli()
112-
var codeScanResponseContext = CodeScanResponseContext()
111+
var codeScanResponseContext = defaultCodeScanResponseContext()
113112
try {
114113
val file = FileEditorManager.getInstance(project).selectedEditor?.file
115114
?: noFileOpenError()
@@ -119,11 +118,18 @@ internal class CodeWhispererCodeScanManager(val project: Project) {
119118
LOG.debug { "Creating context truncation for file ${file.path}" }
120119
val sessionContext = CodeScanSessionContext(project, codeScanSessionConfig)
121120
val session = CodeWhispererCodeScanSession(sessionContext)
122-
val (issues, responseContext) = session.run()
123-
codeScanResponseContext = responseContext
124-
renderResponseOnUIThread(issues)
125-
codeScanResponseContext = codeScanResponseContext.copy(reason = "Succeeded")
126-
codeScanStatus = Result.Succeeded
121+
val codeScanResponse = session.run()
122+
codeScanResponseContext = codeScanResponse.responseContext
123+
when (codeScanResponse) {
124+
is CodeScanResponse.Success -> {
125+
val issues = codeScanResponse.issues
126+
renderResponseOnUIThread(issues)
127+
codeScanStatus = Result.Succeeded
128+
}
129+
is CodeScanResponse.Failure -> {
130+
throw codeScanResponse.failureReason
131+
}
132+
}
127133
LOG.info { "Security scan completed." }
128134
}
129135
} catch (e: Exception) {
@@ -140,7 +146,7 @@ internal class CodeWhispererCodeScanManager(val project: Project) {
140146
}
141147
}
142148

143-
private fun handleException(e: Exception): String? {
149+
private fun handleException(e: Exception): String {
144150
val errorMessage = when (e) {
145151
is CodeWhispererException -> e.awsErrorDetails().errorMessage() ?: message("codewhisperer.codescan.service_error")
146152
is CodeWhispererCodeScanException -> e.message

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/CodeWhispererCodeScanSession.kt

Lines changed: 127 additions & 88 deletions
Large diffs are not rendered by default.

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/sessionconfig/CodeScanSessionConfig.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ data class Payload(
6565

6666
data class PayloadContext(
6767
val language: CodewhispererLanguage,
68-
val payloadSize: Long,
6968
val totalLines: Long,
7069
val totalFiles: Int,
71-
val totalTimeInMilliseconds: Long
70+
val totalTimeInMilliseconds: Long,
71+
val srcPayloadSize: Long,
72+
val srcZipFileSize: Long,
73+
val buildPayloadSize: Long? = null,
74+
val buildZipFileSize: Long? = null
7275
)

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/sessionconfig/JavaCodeScanSessionConfig.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,21 @@ internal class JavaCodeScanSessionConfig(
5858
LOG.debug { "Creating payload. File selected as root for the context truncation: ${selectedFile.path}" }
5959

6060
// Include all the dependencies using BFS
61-
val (sourceFiles, buildPaths, payloadSize, totalLines) = includeDependencies()
61+
val (sourceFiles, buildPaths, srcPayloadSize, totalLines) = includeDependencies()
6262

6363
// Copy all the included source files to the source zip
6464
val srcZip = zipFiles(sourceFiles.map { it.toNioPath() })
6565

6666
var noClassFilesFound = true
6767
val outputPaths = CompilerPaths.getOutputPaths(ModuleManager.getInstance(project).modules)
68+
var totalBuildPayloadSize = 0L
6869
val buildFiles = buildPaths.filterNotNull().mapNotNull { relativePath ->
6970
val classFile = findClassFile(relativePath, outputPaths)
7071
if (classFile == null) {
7172
LOG.debug { "Cannot find class file for $relativePath" }
7273
} else {
7374
noClassFilesFound = false
75+
totalBuildPayloadSize += classFile.toFile().length()
7476
}
7577
classFile
7678
}
@@ -81,10 +83,13 @@ internal class JavaCodeScanSessionConfig(
8183

8284
val payloadContext = PayloadContext(
8385
CodewhispererLanguage.Java,
84-
payloadSize,
8586
totalLines,
8687
sourceFiles.size,
87-
Instant.now().toEpochMilli() - start
88+
Instant.now().toEpochMilli() - start,
89+
srcPayloadSize,
90+
srcZip.length(),
91+
totalBuildPayloadSize,
92+
buildZip.length()
8893
)
8994
return Payload(payloadContext, srcZip, buildZip)
9095
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/sessionconfig/PythonCodeScanSessionConfig.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ internal class PythonCodeScanSessionConfig(
5151
val srcZip = zipFiles(includedSourceFiles.map { Path.of(it) })
5252
val payloadContext = PayloadContext(
5353
CodewhispererLanguage.Python,
54-
payloadSize,
5554
totalLines,
5655
includedSourceFiles.size,
57-
Instant.now().toEpochMilli() - start
56+
Instant.now().toEpochMilli() - start,
57+
payloadSize,
58+
srcZip.length()
5859
)
5960

6061
return Payload(payloadContext, srcZip)

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import com.intellij.openapi.editor.VisualPosition
88
import com.intellij.openapi.ui.popup.JBPopup
99
import software.amazon.awssdk.services.codewhisperer.model.ListRecommendationsResponse
1010
import software.amazon.awssdk.services.codewhisperer.model.Recommendation
11+
import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.sessionconfig.PayloadContext
1112
import software.aws.toolkits.jetbrains.services.codewhisperer.service.RequestContext
1213
import software.aws.toolkits.jetbrains.services.codewhisperer.service.ResponseContext
1314
import software.aws.toolkits.telemetry.CodewhispererAutomatedTriggerType
14-
import software.aws.toolkits.telemetry.CodewhispererLanguage
1515
import software.aws.toolkits.telemetry.CodewhispererTriggerType
1616
import software.aws.toolkits.telemetry.Result
1717

@@ -78,11 +78,15 @@ data class CodeScanTelemetryEvent(
7878
val result: Result
7979
)
8080

81+
data class CodeScanServiceInvocationContext(
82+
val artifactsUploadDuration: Long,
83+
val serviceInvocationDuration: Long
84+
)
85+
8186
data class CodeScanResponseContext(
87+
val payloadContext: PayloadContext,
88+
val serviceInvocationContext: CodeScanServiceInvocationContext,
8289
val codeScanJobId: String? = null,
83-
val codewhispererLanguage: CodewhispererLanguage = CodewhispererLanguage.Unknown,
84-
val payloadSizeInBytes: Long = 0L,
85-
val codeScanLines: Long = 0L,
8690
val codeScanTotalIssues: Int = 0,
8791
val reason: String? = null
8892
)

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/telemetry/CodeWhispererTelemetryService.kt

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,38 @@ class CodeWhispererTelemetryService {
110110
}
111111

112112
fun sendSecurityScanEvent(codeScanEvent: CodeScanTelemetryEvent, project: Project? = null) {
113-
val (jobId, language, payloadSize, codeScanLines, codeScanIssues, reason) = codeScanEvent.codeScanResponseContext
113+
val (payloadContext, serviceInvocationContext, codeScanJobId, totalIssues, reason) = codeScanEvent.codeScanResponseContext
114114

115115
LOG.debug {
116-
"Recording code security scan event. " +
117-
"Security scan job id: $jobId, " +
118-
"Language: $language, " +
119-
"Payload size: $payloadSize, " +
120-
"Total number of lines scanned: $codeScanLines, " +
121-
"Total number of security scan issues found: $codeScanIssues " +
122-
"Total duration of the security scan job: ${codeScanEvent.duration} " +
123-
"Reason: $reason"
116+
"Recording code security scan event. \n" +
117+
"Security scan job id: $codeScanJobId, \n" +
118+
"Total number of security scan issues found: $totalIssues, \n" +
119+
"Language: ${payloadContext.language}, \n" +
120+
"Uncompressed source payload size in bytes: ${payloadContext.srcPayloadSize}, \n" +
121+
"Uncompressed build payload size in bytes: ${payloadContext.buildPayloadSize}, \n" +
122+
"Compressed source zip file size in bytes: ${payloadContext.srcZipFileSize}, \n" +
123+
"Compressed build zip file size in bytes: ${payloadContext.buildZipFileSize}, \n" +
124+
"Total duration of the security scan job in milliseconds: ${codeScanEvent.duration}, \n" +
125+
"Context truncation duration in milliseconds: ${payloadContext.totalTimeInMilliseconds}, \n" +
126+
"Artifacts upload duration in milliseconds: ${serviceInvocationContext.artifactsUploadDuration}, \n" +
127+
"Service invocation duration in milliseconds: ${serviceInvocationContext.serviceInvocationDuration}, \n" +
128+
"Total number of lines scanned: ${payloadContext.totalLines}, \n" +
129+
"Reason: $reason \n"
124130
}
125131
CodewhispererTelemetry.securityScan(
126132
project = project,
127-
codewhispererCodeScanLines = codeScanLines.toInt(),
128-
codewhispererCodeScanJobId = jobId,
129-
codewhispererCodeScanPayloadBytes = payloadSize.toInt(),
130-
codewhispererCodeScanTotalIssues = codeScanIssues,
131-
codewhispererLanguage = language,
133+
codewhispererCodeScanLines = payloadContext.totalLines.toInt(),
134+
codewhispererCodeScanJobId = codeScanJobId,
135+
codewhispererCodeScanSrcPayloadBytes = payloadContext.srcPayloadSize.toInt(),
136+
codewhispererCodeScanBuildPayloadBytes = payloadContext.buildPayloadSize?.toInt(),
137+
codewhispererCodeScanSrcZipFileBytes = payloadContext.srcZipFileSize.toInt(),
138+
codewhispererCodeScanBuildZipFileBytes = payloadContext.buildZipFileSize?.toInt(),
139+
codewhispererCodeScanTotalIssues = totalIssues,
140+
codewhispererLanguage = payloadContext.language,
132141
duration = codeScanEvent.duration,
142+
contextTruncationDuration = payloadContext.totalTimeInMilliseconds.toInt(),
143+
artifactsUploadDuration = serviceInvocationContext.artifactsUploadDuration.toInt(),
144+
codeScanServiceInvocationsDuration = serviceInvocationContext.serviceInvocationDuration.toInt(),
133145
reason = reason,
134146
result = codeScanEvent.result
135147
)

0 commit comments

Comments
 (0)