Skip to content

Commit b20f8fc

Browse files
committed
Kotlin: Add total number of diagnostics to telemetry
1 parent ecd8921 commit b20f8fc

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-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+
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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ predicate extractorDiagnostics(string key, int value) {
5353
)
5454
}
5555

56+
/*
57+
* Just counting the diagnostics doesn't give the full picture, as
58+
* CODEQL_EXTRACTOR_KOTLIN_DIAGNOSTIC_LIMIT means that some diagnostics
59+
* will be suppressed. In that case, we need to look for the
60+
* suppression message, uncount those that did get emitted, uncount the
61+
* suppression message itself, and then add on the full count.
62+
*/
63+
64+
predicate extractorTotalDiagnostics(string key, int value) {
65+
exists(string extractor |
66+
key = "Total number of diagnostics from " + extractor and
67+
value =
68+
strictcount(Diagnostic d | d.getGeneratedBy() = extractor) +
69+
sum(Diagnostic d |
70+
d.getGeneratedBy() = extractor
71+
|
72+
d.getMessage()
73+
.regexpCapture("Total of ([0-9]+) diagnostics \\(reached limit of ([0-9]+)\\).*",
74+
1)
75+
.toInt() -
76+
d.getMessage()
77+
.regexpCapture("Total of ([0-9]+) diagnostics \\(reached limit of ([0-9]+)\\).*",
78+
2)
79+
.toInt() - 1
80+
)
81+
)
82+
}
83+
5684
from string key, int value
5785
where
5886
fileCount(key, value) or
@@ -61,5 +89,6 @@ where
6189
numberOfLinesOfCode(key, value) or
6290
totalNumberOfLinesByExtension(key, value) or
6391
numberOfLinesOfCodeByExtension(key, value) or
64-
extractorDiagnostics(key, value)
92+
extractorDiagnostics(key, value) or
93+
extractorTotalDiagnostics(key, value)
6594
select key, value

0 commit comments

Comments
 (0)