Skip to content

Commit d710c1e

Browse files
authored
Merge pull request #17287 from tamasvajk/message-count-telemetry
C#: Add aggregated compiler and extractor message counts to extractio…
2 parents 20d9fd1 + 6827bed commit d710c1e

File tree

10 files changed

+42
-4
lines changed

10 files changed

+42
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Compilation.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using Microsoft.CodeAnalysis;
6+
using Semmle.Extraction.Entities;
67
using Semmle.Util;
78

89
namespace Semmle.Extraction.CSharp.Entities
@@ -89,13 +90,21 @@ public void PopulatePerformance(PerformanceMetrics p)
8990
trapFile.compilation_finished(this, (float)p.Total.Cpu.TotalSeconds, (float)p.Total.Elapsed.TotalSeconds);
9091
}
9192

93+
public void PopulateAggregatedMessages()
94+
{
95+
ExtractionMessage.groupedMessageCounts.ForEach(pair =>
96+
{
97+
Context.TrapWriter.Writer.compilation_info(this, $"Extractor message count for group '{pair.Key}'", pair.Value.ToString());
98+
});
99+
}
100+
92101
public override void WriteId(EscapingTextWriter trapFile)
93102
{
94103
trapFile.Write(hashCode);
95104
trapFile.Write(";compilation");
96105
}
97106

98-
public override Location ReportingLocation => throw new NotImplementedException();
107+
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();
99108

100109
public override bool NeedsPopulation => Context.IsAssemblyScope;
101110

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Analyser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ private void DoAnalyseCompilation()
250250

251251
public void LogPerformance(Entities.PerformanceMetrics p) => compilationEntity.PopulatePerformance(p);
252252

253+
public void ExtractAggregatedMessages() => compilationEntity.PopulateAggregatedMessages();
254+
253255
#nullable restore warnings
254256

255257
/// <summary>

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ public static ExitCode Analyse(Stopwatch stopwatch, Analyser analyser, CommonOpt
458458

459459
sw.Restart();
460460
analyser.PerformExtraction(options.Threads);
461+
analyser.ExtractAggregatedMessages();
461462
sw.Stop();
462463
var cpuTime2 = currentProcess.TotalProcessorTime;
463464
var userTime2 = currentProcess.UserProcessorTime;

csharp/extractor/Semmle.Extraction/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ codeql_csharp_library(
2626
],
2727
"//conditions:default": [],
2828
}),
29+
internals_visible_to = ["Semmle.Extraction.CSharp"],
2930
visibility = ["//csharp:__subpackages__"],
3031
deps = [
3132
"//csharp/extractor/Semmle.Util",

csharp/extractor/Semmle.Extraction/Entities/ExtractionMessage.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System.Collections.Concurrent;
2+
using System.IO;
23
using System.Threading;
34
using Semmle.Util;
45

@@ -7,6 +8,8 @@ namespace Semmle.Extraction.Entities
78
internal class ExtractionMessage : FreshEntity
89
{
910
private static readonly int limit = EnvironmentVariables.TryGetExtractorNumberOption<int>("MESSAGE_LIMIT") ?? 10000;
11+
12+
internal static readonly ConcurrentDictionary<string, int> groupedMessageCounts = [];
1013
private static int messageCount = 0;
1114

1215
private readonly Message msg;
@@ -25,6 +28,10 @@ private ExtractionMessage(Context cx, Message msg, bool bypassLimit) : base(cx)
2528

2629
protected override void Populate(TextWriter trapFile)
2730
{
31+
// For the time being we're counting the number of messages per severity, we could introduce other groupings in the future
32+
var key = msg.Severity.ToString();
33+
groupedMessageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);
34+
2835
if (!bypassLimit)
2936
{
3037
var val = Interlocked.Increment(ref messageCount);

csharp/extractor/Semmle.Extraction/Semmle.Extraction.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />
8+
9+
<InternalsVisibleTo Include="Semmle.Extraction.CSharp" />
810
</ItemGroup>
911
<Import Project="..\..\.paket\Paket.Restore.targets" />
1012
</Project>

csharp/ql/integration-tests/all-platforms/standalone/Diag.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ extractorMessagesLeachedLimit
77
compilationInfo
88
| Compiler diagnostic count for CS0103 | 3.0 |
99
| Compiler diagnostic count for CS8019 | 7.0 |
10+
| Extractor message count for group 'Error' | 8.0 |
11+
| Extractor message count for group 'Warning' | 1.0 |

csharp/ql/integration-tests/all-platforms/standalone/Diag.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ query predicate extractorMessagesLeachedLimit(ExtractorMessage msg) {
1111

1212
query predicate compilationInfo(string key, float value) {
1313
exists(Compilation c, string infoValue |
14-
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%")
14+
infoValue = c.getInfo(key) and
15+
key.matches(["Compiler diagnostic count for%", "Extractor message count for group%"])
1516
|
1617
value = infoValue.toFloat()
1718
)

csharp/ql/integration-tests/all-platforms/standalone_winforms/CompilationInfo.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import semmle.code.csharp.commons.Diagnostics
44
query predicate compilationInfo(string key, float value) {
55
key != "Resolved references" and
66
key != "Resolved assembly conflicts" and
7-
not key.matches("Compiler diagnostic count for%") and
7+
not key.matches(["Compiler diagnostic count for%", "Extractor message count for group%"]) and
88
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
99
key = infoKey and
1010
value = infoValue.toFloat()

csharp/ql/src/Telemetry/ExtractorInformation.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DatabaseQuality
1212

1313
predicate compilationInfo(string key, float value) {
1414
not key.matches("Compiler diagnostic count for%") and
15+
not key.matches("Extractor message count for group%") and
1516
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
1617
key = infoKey and
1718
value = infoValue.toFloat()
@@ -22,6 +23,16 @@ predicate compilationInfo(string key, float value) {
2223
)
2324
}
2425

26+
predicate compilerDiagnostics(string key, int value) {
27+
key.matches("Compiler diagnostic count for%") and
28+
strictsum(Compilation c | | c.getInfo(key).toInt()) = value
29+
}
30+
31+
predicate extractorMessages(string key, int value) {
32+
key.matches("Extractor message count for group%") and
33+
strictsum(Compilation c | | c.getInfo(key).toInt()) = value
34+
}
35+
2536
predicate fileCount(string key, int value) {
2637
key = "Number of files" and
2738
value = strictcount(File f)
@@ -140,6 +151,8 @@ from string key, float value
140151
where
141152
(
142153
compilationInfo(key, value) or
154+
compilerDiagnostics(key, value) or
155+
extractorMessages(key, value) or
143156
fileCount(key, value) or
144157
fileCountByExtension(key, value) or
145158
totalNumberOfLines(key, value) or

0 commit comments

Comments
 (0)