diff options
author | Javier García Orduña <javier.garcia@sonarsource.com> | 2025-03-03 11:16:48 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2025-03-03 20:03:08 +0000 |
commit | c95c1f9e20ee01d8fcbebce38d4b295bde0c7831 (patch) | |
tree | a3be99b9cf0d84806c40a21173661405b01dd27d /server/sonar-ce-task/src/main/java/org/sonar/ce/task | |
parent | 47edcced9c389e8544a83f232e501f489b2b3735 (diff) | |
download | sonarqube-c95c1f9e20ee01d8fcbebce38d4b295bde0c7831.tar.gz sonarqube-c95c1f9e20ee01d8fcbebce38d4b295bde0c7831.zip |
SQRP-187 Add SCA steps telemetry
Diffstat (limited to 'server/sonar-ce-task/src/main/java/org/sonar/ce/task')
7 files changed, 183 insertions, 10 deletions
diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStep.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStep.java index df382fbeba2..1609fab0332 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStep.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStep.java @@ -28,7 +28,6 @@ public interface ComputationStep { /** * Statistics are displayed in the step ending log. They help * understanding the runtime context and potential performance hotspots. - * * <p/> * Example: * <code> @@ -39,7 +38,6 @@ public interface ComputationStep { * <code> * 2018.07.26 10:22:58 DEBUG ce[AWTVrwb-KZf9YbDx-laU][o.s.s.c.t.s.ComputationStepExecutor] Persist issues | inserts=200 | updates=50 | time=30ms * </code> - * * <p/> * Statistics are logged in the order of insertion. */ @@ -55,6 +53,8 @@ public interface ComputationStep { interface Context { Statistics getStatistics(); + + void addTelemetryMetric(String key, Object value); } void execute(Context context); diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java index 38242aa4b33..cd0a2378e2e 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/step/ComputationStepExecutor.java @@ -19,34 +19,44 @@ */ package org.sonar.ce.task.step; +import static com.google.common.base.Preconditions.checkArgument; +import static java.lang.String.format; +import static java.util.Objects.requireNonNull; + import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.ce.task.CeTaskInterrupter; +import org.sonar.ce.task.telemetry.MutableStepsTelemetryHolder; +import org.sonar.ce.task.telemetry.StepsTelemetryUnavailableHolderImpl; import org.sonar.core.util.logs.Profiler; import org.springframework.beans.factory.annotation.Autowired; -import static com.google.common.base.Preconditions.checkArgument; -import static java.lang.String.format; -import static java.util.Objects.requireNonNull; - public final class ComputationStepExecutor { private static final Logger LOGGER = LoggerFactory.getLogger(ComputationStepExecutor.class); private final ComputationSteps steps; private final CeTaskInterrupter taskInterrupter; + private final MutableStepsTelemetryHolder stepsTelemetryHolder; @CheckForNull private final Listener listener; + /** + * This constructor is used when running Portfolio Refresh, Project Import or App Refresh tasks. + * @param steps computation steps + * @param taskInterrupter task interrupter + */ public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter) { - this(steps, taskInterrupter, null); + this(steps, taskInterrupter, new StepsTelemetryUnavailableHolderImpl(), null); } @Autowired - public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter, @Nullable Listener listener) { + public ComputationStepExecutor(ComputationSteps steps, CeTaskInterrupter taskInterrupter, + MutableStepsTelemetryHolder stepsTelemetryHolder, @Nullable Listener listener) { this.steps = steps; this.taskInterrupter = taskInterrupter; + this.stepsTelemetryHolder = stepsTelemetryHolder; this.listener = listener; } @@ -65,7 +75,7 @@ public final class ComputationStepExecutor { private void executeSteps(Profiler stepProfiler) { StepStatisticsImpl statistics = new StepStatisticsImpl(stepProfiler); - ComputationStep.Context context = new StepContextImpl(statistics); + ComputationStep.Context context = new StepContextImpl(statistics, stepsTelemetryHolder); for (ComputationStep step : steps.instances()) { executeStep(stepProfiler, context, step); } @@ -119,14 +129,21 @@ public final class ComputationStepExecutor { private static class StepContextImpl implements ComputationStep.Context { private final ComputationStep.Statistics statistics; + private final MutableStepsTelemetryHolder stepsTelemetryHolder; - private StepContextImpl(ComputationStep.Statistics statistics) { + private StepContextImpl(ComputationStep.Statistics statistics, MutableStepsTelemetryHolder stepsTelemetryHolder) { this.statistics = statistics; + this.stepsTelemetryHolder = stepsTelemetryHolder; } @Override public ComputationStep.Statistics getStatistics() { return statistics; } + + @Override + public void addTelemetryMetric(String key, Object value) { + stepsTelemetryHolder.add(key, value); + } } } diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/MutableStepsTelemetryHolder.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/MutableStepsTelemetryHolder.java new file mode 100644 index 00000000000..83b75d3335c --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/MutableStepsTelemetryHolder.java @@ -0,0 +1,25 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.ce.task.telemetry; + +public interface MutableStepsTelemetryHolder extends StepsTelemetryHolder { + + MutableStepsTelemetryHolder add(String key, Object value); +} diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolder.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolder.java new file mode 100644 index 00000000000..4d8b1ff6128 --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolder.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.ce.task.telemetry; + +import java.util.Map; + +public interface StepsTelemetryHolder { + + Map<String, Object> getTelemetryMetrics(); +} diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolderImpl.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolderImpl.java new file mode 100644 index 00000000000..1f8c1b496a1 --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryHolderImpl.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.ce.task.telemetry; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + +public class StepsTelemetryHolderImpl implements MutableStepsTelemetryHolder { + + private final Map<String, Object> telemetryMetrics = new LinkedHashMap<>(); + + @Override + public Map<String, Object> getTelemetryMetrics() { + return telemetryMetrics; + } + + @Override + public StepsTelemetryHolderImpl add(String key, Object value) { + requireNonNull(key, "Metric has null key"); + requireNonNull(value, () -> String.format("Metric with key [%s] has null value", key)); + checkArgument(!telemetryMetrics.containsKey(key), "Metric with key [%s] is already present", key); + telemetryMetrics.put(key, value); + return this; + } +} diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryUnavailableHolderImpl.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryUnavailableHolderImpl.java new file mode 100644 index 00000000000..7a54bfea961 --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/StepsTelemetryUnavailableHolderImpl.java @@ -0,0 +1,35 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.ce.task.telemetry; + +import java.util.Map; + +public class StepsTelemetryUnavailableHolderImpl implements MutableStepsTelemetryHolder { + + @Override + public Map<String, Object> getTelemetryMetrics() { + throw new UnsupportedOperationException("Telemetry is not available"); + } + + @Override + public StepsTelemetryUnavailableHolderImpl add(String key, Object value) { + throw new UnsupportedOperationException("Telemetry is not available"); + } +} diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/package-info.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/package-info.java new file mode 100644 index 00000000000..c00b8e386cc --- /dev/null +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/telemetry/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.ce.task.telemetry; + +import javax.annotation.ParametersAreNonnullByDefault; + |