3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import java.util.HashSet;
24 import org.sonar.api.config.Configuration;
25 import org.sonar.api.platform.Server;
26 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
27 import org.sonar.ce.task.step.ComputationStep;
28 import org.sonar.core.util.CloseableIterator;
29 import org.sonar.core.util.UuidFactory;
30 import org.sonar.scanner.protocol.output.ScannerReport;
31 import org.sonar.telemetry.core.Dimension;
32 import org.sonar.telemetry.core.MessageSerializer;
33 import org.sonar.telemetry.core.TelemetryClient;
34 import org.sonar.telemetry.core.schema.AnalysisMetric;
35 import org.sonar.telemetry.core.schema.BaseMessage;
36 import org.sonar.telemetry.core.schema.Metric;
38 import static org.sonar.process.ProcessProperties.Property.SONAR_TELEMETRY_ENABLE;
40 public class SendAnalysisTelemetryStep implements ComputationStep {
42 private final TelemetryClient telemetryClient;
43 private final BatchReportReader batchReportReader;
44 private final Server server;
45 private final UuidFactory uuidFactory;
46 private final Configuration config;
48 public SendAnalysisTelemetryStep(TelemetryClient telemetryClient, BatchReportReader batchReportReader,
49 UuidFactory uuidFactory, Server server, Configuration configuration) {
50 this.telemetryClient = telemetryClient;
51 this.batchReportReader = batchReportReader;
53 this.uuidFactory = uuidFactory;
54 this.config = configuration;
58 public void execute(Context context) {
59 if (!config.getBoolean(SONAR_TELEMETRY_ENABLE.getKey()).orElse(false)) {
62 try (CloseableIterator<ScannerReport.TelemetryEntry> it = batchReportReader.readTelemetryEntries()) {
63 Set<Metric> metrics = new HashSet<>();
64 // it was agreed to limit the number of telemetry entries to 1000 per one analysis
65 final int limit = 1000;
67 while (it.hasNext() && count++ < limit) {
68 ScannerReport.TelemetryEntry telemetryEntry = it.next();
69 metrics.add(new AnalysisMetric(telemetryEntry.getKey(), telemetryEntry.getValue()));
72 if (metrics.isEmpty()) {
75 BaseMessage baseMessage = new BaseMessage.Builder()
76 .setMessageUuid(uuidFactory.create())
77 .setInstallationId(server.getId())
78 .setDimension(Dimension.ANALYSIS)
82 String jsonString = MessageSerializer.serialize(baseMessage);
83 telemetryClient.uploadMetricAsync(jsonString);
89 public String getDescription() {
90 return "This step pushes telemetry data from the Sonar analyzers to Telemetry V2 server in case telemetry is enabled.";