aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2024-10-09 15:09:39 +0200
committersonartech <sonartech@sonarsource.com>2024-10-11 20:02:42 +0000
commit3f0a34901c55eb1e204fe4028d2773f8484b513f (patch)
treee79565fd2126baf3711a545619470dd2f44d2598 /sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor
parent8859ed6887ec1e2eb3e86a11b98b73b5b8606710 (diff)
downloadsonarqube-3f0a34901c55eb1e204fe4028d2773f8484b513f.tar.gz
sonarqube-3f0a34901c55eb1e204fe4028d2773f8484b513f.zip
SONAR-23327 Implement addTelemetryProperty in the scanner engine
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java31
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java6
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java34
3 files changed, 53 insertions, 18 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
index d6011d56c74..e0930fc5372 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
@@ -76,6 +76,7 @@ import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.report.ScannerReportUtils;
import org.sonar.scanner.repository.ContextPropertiesCache;
+import org.sonar.scanner.repository.TelemetryCache;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import static java.lang.Math.max;
@@ -115,20 +116,21 @@ public class DefaultSensorStorage implements SensorStorage {
private final ReportPublisher reportPublisher;
private final SonarCpdBlockIndex index;
private final ContextPropertiesCache contextPropertiesCache;
+ private final TelemetryCache telemetryCache;
private final Configuration settings;
private final ScannerMetrics scannerMetrics;
private final BranchConfiguration branchConfiguration;
private final Set<String> alreadyLogged = new HashSet<>();
- public DefaultSensorStorage(MetricFinder metricFinder, IssuePublisher moduleIssues, Configuration settings,
- ReportPublisher reportPublisher, SonarCpdBlockIndex index,
- ContextPropertiesCache contextPropertiesCache, ScannerMetrics scannerMetrics, BranchConfiguration branchConfiguration) {
+ public DefaultSensorStorage(MetricFinder metricFinder, IssuePublisher moduleIssues, Configuration settings, ReportPublisher reportPublisher, SonarCpdBlockIndex index,
+ ContextPropertiesCache contextPropertiesCache, TelemetryCache telemetryCache, ScannerMetrics scannerMetrics, BranchConfiguration branchConfiguration) {
this.metricFinder = metricFinder;
this.moduleIssues = moduleIssues;
this.settings = settings;
this.reportPublisher = reportPublisher;
this.index = index;
this.contextPropertiesCache = contextPropertiesCache;
+ this.telemetryCache = telemetryCache;
this.scannerMetrics = scannerMetrics;
this.branchConfiguration = branchConfiguration;
}
@@ -151,7 +153,8 @@ public class DefaultSensorStorage implements SensorStorage {
}
if (component instanceof InputDir || (component instanceof DefaultInputModule defaultInputModule && defaultInputModule.definition().getParent() != null)) {
- logOnce(measure.metric().key(), "Storing measures on folders or modules is deprecated. Provided value of metric '{}' is ignored.", measure.metric().key());
+ logOnce(measure.metric().key(), "Storing measures on folders or modules is deprecated. Provided value of metric '{}' is ignored.",
+ measure.metric().key());
return;
}
@@ -166,7 +169,8 @@ public class DefaultSensorStorage implements SensorStorage {
}
if (!measure.isFromCore() && NEWLY_CORE_METRICS_KEYS.contains(measure.metric().key())) {
- logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube. Provided value is ignored.", measure.metric().key());
+ logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube. Provided value is ignored.",
+ measure.metric().key());
return;
}
@@ -361,8 +365,10 @@ public class DefaultSensorStorage implements SensorStorage {
SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = reloadExistingCoverage(inputFile);
int lineCount = inputFile.lines();
- mergeLineCoverageValues(lineCount, defaultCoverage.hitsByLine(), coveragePerLine, (value, builder) -> builder.setHits(builder.getHits() || value > 0));
- mergeLineCoverageValues(lineCount, defaultCoverage.conditionsByLine(), coveragePerLine, (value, builder) -> builder.setConditions(max(value, builder.getConditions())));
+ mergeLineCoverageValues(lineCount, defaultCoverage.hitsByLine(), coveragePerLine,
+ (value, builder) -> builder.setHits(builder.getHits() || value > 0));
+ mergeLineCoverageValues(lineCount, defaultCoverage.conditionsByLine(), coveragePerLine,
+ (value, builder) -> builder.setConditions(max(value, builder.getConditions())));
mergeLineCoverageValues(lineCount, defaultCoverage.coveredConditionsByLine(), coveragePerLine,
(value, builder) -> builder.setCoveredConditions(max(value, builder.getCoveredConditions())));
@@ -373,7 +379,8 @@ public class DefaultSensorStorage implements SensorStorage {
private SortedMap<Integer, ScannerReport.LineCoverage.Builder> reloadExistingCoverage(DefaultInputFile inputFile) {
SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = new TreeMap<>();
- try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) {
+ try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator =
+ reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) {
while (lineCoverageCloseableIterator.hasNext()) {
final ScannerReport.LineCoverage lineCoverage = lineCoverageCloseableIterator.next();
coveragePerLine.put(lineCoverage.getLine(), ScannerReport.LineCoverage.newBuilder(lineCoverage));
@@ -386,8 +393,8 @@ public class DefaultSensorStorage implements SensorStorage {
void apply(Integer value, ScannerReport.LineCoverage.Builder builder);
}
- private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine,
- LineCoverageOperation op) {
+ private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer,
+ ScannerReport.LineCoverage.Builder> coveragePerLine, LineCoverageOperation op) {
for (Map.Entry<Integer, Integer> lineMeasure : valueByLine.entrySet()) {
int lineIdx = lineMeasure.getKey();
if (lineIdx <= lineCount) {
@@ -437,6 +444,10 @@ public class DefaultSensorStorage implements SensorStorage {
contextPropertiesCache.put(key, value);
}
+ public void storeTelemetry(String key, String value) {
+ telemetryCache.put(key, value);
+ }
+
@Override
public void store(NewSignificantCode newSignificantCode) {
DefaultSignificantCode significantCode = (DefaultSignificantCode) newSignificantCode;
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java
index 3161da1e96d..6f27d1be09f 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ModuleSensorContext.java
@@ -29,6 +29,7 @@ import org.sonar.api.batch.sensor.cache.ReadCache;
import org.sonar.api.batch.sensor.cache.WriteCache;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.Settings;
+import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cache.AnalysisCacheEnabled;
import org.sonar.scanner.scan.branch.BranchConfiguration;
@@ -39,9 +40,10 @@ public class ModuleSensorContext extends ProjectSensorContext {
public ModuleSensorContext(DefaultInputProject project, InputModule module, Configuration config, Settings mutableModuleSettings, FileSystem fs, ActiveRules activeRules,
DefaultSensorStorage sensorStorage, SonarRuntime sonarRuntime, BranchConfiguration branchConfiguration,
- WriteCache writeCache, ReadCache readCache, AnalysisCacheEnabled analysisCacheEnabled, UnchangedFilesHandler unchangedFilesHandler) {
+ WriteCache writeCache, ReadCache readCache, AnalysisCacheEnabled analysisCacheEnabled, UnchangedFilesHandler unchangedFilesHandler,
+ ExecutingSensorContext executingSensorContext, ScannerPluginRepository pluginRepository) {
super(project, config, mutableModuleSettings, fs, activeRules, sensorStorage, sonarRuntime, branchConfiguration, writeCache, readCache, analysisCacheEnabled,
- unchangedFilesHandler);
+ unchangedFilesHandler, executingSensorContext, pluginRepository);
this.module = module;
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
index ec0b9e59f26..7694b77e8b7 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
@@ -54,6 +54,8 @@ import org.sonar.api.config.Configuration;
import org.sonar.api.config.Settings;
import org.sonar.api.scanner.fs.InputProject;
import org.sonar.api.utils.Version;
+import org.sonar.core.platform.PluginInfo;
+import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cache.AnalysisCacheEnabled;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.sensor.noop.NoOpNewAnalysisError;
@@ -75,10 +77,15 @@ public class ProjectSensorContext implements SensorContext {
private final WriteCache writeCache;
private final ReadCache readCache;
private final AnalysisCacheEnabled analysisCacheEnabled;
-
- public ProjectSensorContext(DefaultInputProject project, Configuration config, Settings mutableSettings, FileSystem fs, ActiveRules activeRules,
- DefaultSensorStorage sensorStorage, SonarRuntime sonarRuntime, BranchConfiguration branchConfiguration, WriteCache writeCache, ReadCache readCache,
- AnalysisCacheEnabled analysisCacheEnabled, UnchangedFilesHandler unchangedFilesHandler) {
+ private final ExecutingSensorContext executingSensorContext;
+ private final ScannerPluginRepository pluginRepo;
+
+ public ProjectSensorContext(DefaultInputProject project, Configuration config, Settings mutableSettings, FileSystem fs,
+ ActiveRules activeRules,
+ DefaultSensorStorage sensorStorage, SonarRuntime sonarRuntime, BranchConfiguration branchConfiguration,
+ WriteCache writeCache, ReadCache readCache,
+ AnalysisCacheEnabled analysisCacheEnabled, UnchangedFilesHandler unchangedFilesHandler,
+ ExecutingSensorContext executingSensorContext, ScannerPluginRepository pluginRepo) {
this.project = project;
this.config = config;
this.mutableSettings = mutableSettings;
@@ -91,6 +98,8 @@ public class ProjectSensorContext implements SensorContext {
this.analysisCacheEnabled = analysisCacheEnabled;
this.skipUnchangedFiles = branchConfiguration.isPullRequest();
this.unchangedFilesHandler = unchangedFilesHandler;
+ this.executingSensorContext = executingSensorContext;
+ this.pluginRepo = pluginRepo;
}
@Override
@@ -215,8 +224,12 @@ public class ProjectSensorContext implements SensorContext {
}
@Override
- public void addTelemetryProperty(String s, String s1) {
- //NOOP
+ public void addTelemetryProperty(String key, String value) {
+ if (isSonarSourcePlugin()) {
+ this.sensorStorage.storeTelemetry(key, value);
+ } else {
+ throw new IllegalStateException("Telemetry properties can only be added by SonarSource plugins");
+ }
}
@Override
@@ -228,4 +241,13 @@ public class ProjectSensorContext implements SensorContext {
public boolean canSkipUnchangedFiles() {
return this.skipUnchangedFiles;
}
+
+ private boolean isSonarSourcePlugin() {
+ SensorId sensorExecuting = executingSensorContext.getSensorExecuting();
+ if (sensorExecuting != null) {
+ PluginInfo pluginInfo = pluginRepo.getPluginInfo(sensorExecuting.getPluginKey());
+ return "sonarsource".equalsIgnoreCase(pluginInfo.getOrganizationName());
+ }
+ return false;
+ }
}