Skip to content

Commit 7bf55c5

Browse files
authored
Merge pull request github#11251 from igfoo/igfoo/total
Kotlin: Add total number of diagnostics to telemetry
2 parents 3ef7f3f + fab2d30 commit 7bf55c5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

java/kotlin-extractor/src/main/kotlin/utils/Logger.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ open class LoggerBase(val logCounter: LogCounter) {
138138
fullMsgBuilder.append(suffix)
139139

140140
val fullMsg = fullMsgBuilder.toString()
141+
emitDiagnostic(tw, severity, diagnosticLocStr, msg, fullMsg, locationString, mkLocationId)
142+
}
143+
144+
private fun emitDiagnostic(tw: TrapWriter, severity: Severity, diagnosticLocStr: String, msg: String, fullMsg: String, locationString: String? = null, mkLocationId: () -> Label<DbLocation> = { tw.unknownLocation }) {
141145
val locStr = if (locationString == null) "" else "At " + locationString + ": "
142146
val kind = if (severity <= Severity.WarnHigh) "WARN" else "ERROR"
143147
val logMessage = LogMessage(kind, "Diagnostic($diagnosticLocStr): $locStr$fullMsg")
@@ -190,9 +194,10 @@ open class LoggerBase(val logCounter: LogCounter) {
190194
// We don't know if this location relates to an error
191195
// or a warning, so we just declare hitting the limit
192196
// to be an error regardless.
193-
val logMessage = LogMessage("ERROR", "Total of $count diagnostics from $caller.")
194-
tw.writeComment(logMessage.toText())
195-
logStream.write(logMessage.toJsonLine())
197+
val message = "Total of $count diagnostics (reached limit of ${logCounter.diagnosticLimit}) from $caller."
198+
if (verbosity >= 1) {
199+
emitDiagnostic(tw, Severity.Error, "Limit", message, message)
200+
}
196201
}
197202
}
198203
}

java/ql/src/Telemetry/ExtractorInformation.ql

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ predicate extractorDiagnostics(string key, int value) {
6060
)
6161
}
6262

63+
/*
64+
* Just counting the diagnostics doesn't give the full picture, as
65+
* CODEQL_EXTRACTOR_KOTLIN_DIAGNOSTIC_LIMIT means that some diagnostics
66+
* will be suppressed. In that case, we need to look for the
67+
* suppression message, uncount those that did get emitted, uncount the
68+
* suppression message itself, and then add on the full count.
69+
*/
70+
71+
predicate extractorTotalDiagnostics(string key, int value) {
72+
exists(string extractor, string limitRegex |
73+
limitRegex = "Total of ([0-9]+) diagnostics \\(reached limit of ([0-9]+)\\).*" and
74+
key = "Total number of diagnostics from " + extractor and
75+
value =
76+
strictcount(Diagnostic d | d.getGeneratedBy() = extractor) +
77+
sum(Diagnostic d |
78+
d.getGeneratedBy() = extractor
79+
|
80+
d.getMessage().regexpCapture(limitRegex, 1).toInt() -
81+
d.getMessage().regexpCapture(limitRegex, 2).toInt() - 1
82+
)
83+
)
84+
}
85+
6386
from string key, int value
6487
where
6588
compilationInfo(key, value) or
@@ -69,5 +92,6 @@ where
6992
numberOfLinesOfCode(key, value) or
7093
totalNumberOfLinesByExtension(key, value) or
7194
numberOfLinesOfCodeByExtension(key, value) or
72-
extractorDiagnostics(key, value)
95+
extractorDiagnostics(key, value) or
96+
extractorTotalDiagnostics(key, value)
7397
select key, value

0 commit comments

Comments
 (0)