From 9258d5846d5e0947a7ff7813751224af537b20ee Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Wed, 17 Jul 2024 17:09:08 +0200 Subject: [PATCH] SONAR-22479 Added new upgrade related telemetry metrics for the installation dimension --- server/sonar-db-migration/build.gradle | 2 + .../step/MigrationStepsExecutorImpl.java | 22 +++++-- .../TelemetryDbMigrationStepsProvider.java | 65 +++++++++++++++++++ .../TelemetryDbMigrationSuccessProvider.java | 65 +++++++++++++++++++ ...TelemetryDbMigrationTotalTimeProvider.java | 65 +++++++++++++++++++ .../sonar/server/telemetry/package-info.java | 23 +++++++ .../MigrationContainerPopulatorImplTest.java | 6 ++ .../engine/MigrationEngineImplTest.java | 9 +++ .../step/MigrationStepsExecutorImplTest.java | 43 +++++++++--- ...TelemetryDbMigrationStepsProviderTest.java | 43 ++++++++++++ ...lemetryDbMigrationSuccessProviderTest.java | 44 +++++++++++++ ...metryDbMigrationTotalTimeProviderTest.java | 43 ++++++++++++ .../telemetry/core/TelemetryDataProvider.java | 4 ++ .../platformlevel/PlatformLevel2.java | 6 ++ .../platformlevel/PlatformLevel2Test.java | 4 +- 15 files changed, 430 insertions(+), 14 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProvider.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProvider.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProvider.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/package-info.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProviderTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProviderTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProviderTest.java diff --git a/server/sonar-db-migration/build.gradle b/server/sonar-db-migration/build.gradle index 5d46f5b0264..9b5a4032e13 100644 --- a/server/sonar-db-migration/build.gradle +++ b/server/sonar-db-migration/build.gradle @@ -16,6 +16,8 @@ dependencies { api project(':server:sonar-process') api project(':sonar-core') + implementation project(':server:sonar-telemetry-core') + compileOnlyApi 'com.github.spotbugs:spotbugs-annotations' testImplementation 'com.github.spotbugs:spotbugs-annotations' diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java index a98bd05106d..bbccc564367 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java @@ -26,6 +26,9 @@ import org.sonar.core.platform.Container; import org.sonar.core.util.logs.Profiler; import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState; import org.sonar.server.platform.db.migration.history.MigrationHistory; +import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; import static com.google.common.base.Preconditions.checkState; @@ -39,12 +42,19 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor { private final Container migrationContainer; private final MigrationHistory migrationHistory; private final MutableDatabaseMigrationState databaseMigrationState; + private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider; + private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider; + private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider; - public MigrationStepsExecutorImpl( - Container migrationContainer, MigrationHistory migrationHistory, MutableDatabaseMigrationState databaseMigrationState) { + public MigrationStepsExecutorImpl(Container migrationContainer, MigrationHistory migrationHistory, MutableDatabaseMigrationState databaseMigrationState, + TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider, TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider, + TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider) { this.migrationContainer = migrationContainer; this.migrationHistory = migrationHistory; this.databaseMigrationState = databaseMigrationState; + this.telemetryDbMigrationTotalTimeProvider = telemetryDbMigrationTotalTimeProvider; + this.telemetryDbMigrationStepsProvider = telemetryDbMigrationStepsProvider; + this.telemetryDbMigrationSuccessProvider = telemetryDbMigrationSuccessProvider; } @Override @@ -59,17 +69,21 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor { } allStepsExecuted = true; } finally { + long dbMigrationDuration = 0L; if (allStepsExecuted) { - globalProfiler.stopInfo(GLOBAL_END_MESSAGE, + dbMigrationDuration = globalProfiler.stopInfo(GLOBAL_END_MESSAGE, databaseMigrationState.getCompletedMigrations(), databaseMigrationState.getTotalMigrations(), "success"); } else { - globalProfiler.stopError(GLOBAL_END_MESSAGE, + dbMigrationDuration = globalProfiler.stopError(GLOBAL_END_MESSAGE, databaseMigrationState.getCompletedMigrations(), databaseMigrationState.getTotalMigrations(), "failure"); } + telemetryDbMigrationTotalTimeProvider.setDbMigrationTotalTime(dbMigrationDuration); + telemetryDbMigrationStepsProvider.setDbMigrationCompletedSteps(databaseMigrationState.getCompletedMigrations()); + telemetryDbMigrationSuccessProvider.setDbMigrationSuccess(allStepsExecuted); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProvider.java new file mode 100644 index 00000000000..c2e52da08ba --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProvider.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import java.util.Optional; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +public class TelemetryDbMigrationStepsProvider implements TelemetryDataProvider { + + private Integer dbMigrationCompletedSteps = null; + + @Override + public String getMetricKey() { + return "db_migration_completed_steps"; + } + + @Override + public Granularity getGranularity() { + return Granularity.ADHOC; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.INTEGER; + } + + public void setDbMigrationCompletedSteps(Integer dbMigrationCompletedSteps) { + this.dbMigrationCompletedSteps = dbMigrationCompletedSteps; + } + + @Override + public Optional getValue() { + return Optional.ofNullable(dbMigrationCompletedSteps); + } + + @Override + public void destroy() { + dbMigrationCompletedSteps = null; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProvider.java new file mode 100644 index 00000000000..1680a99d770 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProvider.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import java.util.Optional; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +public class TelemetryDbMigrationSuccessProvider implements TelemetryDataProvider { + + private Boolean dbMigrationSuccess = null; + + @Override + public String getMetricKey() { + return "db_migration_success"; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.BOOLEAN; + } + + @Override + public Granularity getGranularity() { + return Granularity.ADHOC; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + public void setDbMigrationSuccess(Boolean dbMigrationSuccess) { + this.dbMigrationSuccess = dbMigrationSuccess; + } + + @Override + public Optional getValue() { + return Optional.ofNullable(dbMigrationSuccess); + } + + @Override + public void destroy() { + dbMigrationSuccess = null; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProvider.java new file mode 100644 index 00000000000..6be779e1f84 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProvider.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import java.util.Optional; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +public class TelemetryDbMigrationTotalTimeProvider implements TelemetryDataProvider { + + private Long dbMigrationTotalTime = null; + + @Override + public String getMetricKey() { + return "db_migration_total_time_ms"; + } + + @Override + public Granularity getGranularity() { + return Granularity.ADHOC; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.INTEGER; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + public void setDbMigrationTotalTime(Long dbMigrationTotalTime) { + this.dbMigrationTotalTime = dbMigrationTotalTime; + } + + @Override + public Optional getValue() { + return Optional.ofNullable(dbMigrationTotalTime); + } + + @Override + public void destroy() { + dbMigrationTotalTime = null; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/package-info.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/package-info.java new file mode 100644 index 00000000000..a1adc893867 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java index 2302f818619..c1af7bacb2f 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java @@ -27,6 +27,9 @@ import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.MigrationStepsExecutorImpl; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; +import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -51,6 +54,9 @@ public class MigrationContainerPopulatorImplTest { // add MigrationStepsExecutorImpl's dependencies migrationContainer.add(mock(MigrationHistory.class)); migrationContainer.add(mock(MutableDatabaseMigrationState.class)); + migrationContainer.add(mock(TelemetryDbMigrationStepsProvider.class)); + migrationContainer.add(mock(TelemetryDbMigrationTotalTimeProvider.class)); + migrationContainer.add(mock(TelemetryDbMigrationSuccessProvider.class)); migrationContainer.startComponents(); underTest.populateContainer(migrationContainer); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java index 42fd3394874..db672f507fa 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java @@ -31,6 +31,9 @@ import org.sonar.server.platform.db.migration.step.MigrationStep; import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.step.NoOpMigrationStatusListener; import org.sonar.server.platform.db.migration.step.RegisteredMigrationStep; +import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -45,10 +48,16 @@ class MigrationEngineImplTest { private final SpringComponentContainer serverContainer = new SpringComponentContainer(); private final MigrationSteps migrationSteps = mock(MigrationSteps.class); private final StepRegistry stepRegistry = new StepRegistry(); + private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider(); + private final TelemetryDbMigrationStepsProvider telemetryUpgradeStepsProvider = new TelemetryDbMigrationStepsProvider(); + private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider(); private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps); @BeforeEach void before() { + serverContainer.add(telemetryDbMigrationTotalTimeProvider); + serverContainer.add(telemetryUpgradeStepsProvider); + serverContainer.add(telemetryDbMigrationSuccessProvider); serverContainer.add(migrationSteps); serverContainer.add(migrationHistory); serverContainer.add(stepRegistry); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java index ce74dd995d4..643bb332373 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java @@ -35,6 +35,9 @@ import org.sonar.server.platform.db.migration.MutableDatabaseMigrationState; import org.sonar.server.platform.db.migration.engine.MigrationContainer; import org.sonar.server.platform.db.migration.engine.SimpleMigrationContainer; import org.sonar.server.platform.db.migration.history.MigrationHistory; +import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; import static com.google.common.base.Preconditions.checkState; import static java.util.Arrays.asList; @@ -51,11 +54,15 @@ class MigrationStepsExecutorImplTest { @RegisterExtension private final LogTesterJUnit5 logTester = new LogTesterJUnit5(); - private MigrationContainer migrationContainer = new SimpleMigrationContainer(); - private MigrationHistory migrationHistory = mock(MigrationHistory.class); - private MutableDatabaseMigrationState databaseMigrationState = mock(); - private MigrationStepsExecutorImpl underTest = new MigrationStepsExecutorImpl(migrationContainer, migrationHistory, databaseMigrationState); - private NoOpMigrationStatusListener migrationStatusListener = mock(); + private final MigrationContainer migrationContainer = new SimpleMigrationContainer(); + private final MigrationHistory migrationHistory = mock(MigrationHistory.class); + private final MutableDatabaseMigrationState databaseMigrationState = mock(); + private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider(); + private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider = new TelemetryDbMigrationStepsProvider(); + private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider(); + private final MigrationStepsExecutorImpl underTest = new MigrationStepsExecutorImpl(migrationContainer, migrationHistory, databaseMigrationState, + telemetryDbMigrationTotalTimeProvider, telemetryDbMigrationStepsProvider, telemetryDbMigrationSuccessProvider); + private final NoOpMigrationStatusListener migrationStatusListener = mock(); @BeforeEach void setUp() { @@ -109,9 +116,9 @@ class MigrationStepsExecutorImplTest { ((SpringComponentContainer) migrationContainer).startComponents(); underTest.execute(asList( - registeredStepOf(1, MigrationStep2.class), - registeredStepOf(2, MigrationStep1.class), - registeredStepOf(3, MigrationStep3.class)), + registeredStepOf(1, MigrationStep2.class), + registeredStepOf(2, MigrationStep1.class), + registeredStepOf(3, MigrationStep3.class)), migrationStatusListener); assertThat(SingleCallCheckerMigrationStep.calledSteps) @@ -183,6 +190,26 @@ class MigrationStepsExecutorImplTest { } } + @Test + void whenExecute_TelemetryDataIsProperlyAdded() { + migrationContainer.add(MigrationStep2.class, MigrationStep1.class, MigrationStep3.class); + when(databaseMigrationState.getCompletedMigrations()).thenReturn(3); + + List steps = asList( + registeredStepOf(1, MigrationStep2.class), + registeredStepOf(2, MigrationStep1.class), + registeredStepOf(3, MigrationStep3.class)); + + ((SpringComponentContainer) migrationContainer).startComponents(); + + underTest.execute(steps, migrationStatusListener); + + assertThat(telemetryDbMigrationTotalTimeProvider.getValue().get()).isPositive(); + assertThat(telemetryDbMigrationStepsProvider.getValue()).hasValue(3); + assertThat(telemetryDbMigrationSuccessProvider.getValue()).hasValue(true); + + } + private static RegisteredMigrationStep registeredStepOf(int migrationNumber, Class migrationStep1Class) { return new RegisteredMigrationStep(migrationNumber, migrationNumber + "-" + migrationStep1Class.getSimpleName(), migrationStep1Class); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProviderTest.java new file mode 100644 index 00000000000..0d3bfe94397 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepsProviderTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.ADHOC; +import static org.sonar.telemetry.core.TelemetryDataType.INTEGER; + +class TelemetryDbMigrationStepsProviderTest { + + @Test + void testGetters() { + TelemetryDbMigrationStepsProvider underTest = new TelemetryDbMigrationStepsProvider(); + underTest.setDbMigrationCompletedSteps(10); + + assertThat(underTest.getMetricKey()).isEqualTo("db_migration_completed_steps"); + assertThat(underTest.getGranularity()).isEqualTo(ADHOC); + assertThat(underTest.getDimension()).isEqualTo(INSTALLATION); + assertThat(underTest.getType()).isEqualTo(INTEGER); + assertThat(underTest.getValue()).contains(10); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProviderTest.java new file mode 100644 index 00000000000..de2e24c9c61 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationSuccessProviderTest.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.ADHOC; +import static org.sonar.telemetry.core.TelemetryDataType.BOOLEAN; + +class TelemetryDbMigrationSuccessProviderTest { + + @Test + void testGetters() { + TelemetryDbMigrationSuccessProvider underTest = new TelemetryDbMigrationSuccessProvider(); + underTest.setDbMigrationSuccess(true); + + assertThat(underTest.getMetricKey()).isEqualTo("db_migration_success"); + assertThat(underTest.getDimension()).isEqualTo(INSTALLATION); + assertThat(underTest.getType()).isEqualTo(BOOLEAN); + assertThat(underTest.getGranularity()).isEqualTo(ADHOC); + assertThat(underTest.getValue()).isPresent(); + assertThat(underTest.getValue().get()).isTrue(); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProviderTest.java new file mode 100644 index 00000000000..349d8146a97 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationTotalTimeProviderTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.server.telemetry; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.ADHOC; +import static org.sonar.telemetry.core.TelemetryDataType.INTEGER; + +class TelemetryDbMigrationTotalTimeProviderTest { + + @Test + void testGetters() { + TelemetryDbMigrationTotalTimeProvider underTest = new TelemetryDbMigrationTotalTimeProvider(); + underTest.setDbMigrationTotalTime(100L); + + assertThat(underTest.getMetricKey()).isEqualTo("db_migration_total_time_ms"); + assertThat(underTest.getDimension()).isEqualTo(INSTALLATION); + assertThat(underTest.getType()).isEqualTo(INTEGER); + assertThat(underTest.getGranularity()).isEqualTo(ADHOC); + assertThat(underTest.getValue()).contains(100L); + } + +} diff --git a/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java b/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java index b29fec4057b..47054aece8e 100644 --- a/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java +++ b/server/sonar-telemetry-core/src/main/java/org/sonar/telemetry/core/TelemetryDataProvider.java @@ -76,4 +76,8 @@ public interface TelemetryDataProvider { default Map getUuidValues() { throw new IllegalStateException("Not implemented"); } + + default void destroy() { + // this method does nothing by default it is used to perform cleanup tasks if needed + } } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java index 6f3d42fe969..18ad5986239 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java @@ -36,6 +36,9 @@ import org.sonar.server.platform.db.migration.DatabaseMigrationStateImpl; import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; import org.sonar.server.platform.db.migration.version.DatabaseVersion; +import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider; +import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider; import org.sonar.server.platform.web.WebPagesCache; import org.sonar.server.plugins.InstalledPluginReferentialFactory; import org.sonar.server.plugins.PluginJarLoader; @@ -84,6 +87,9 @@ public class PlatformLevel2 extends PlatformLevel { // Migration state must be kept at level2 to survive moving in and then out of safe mode // ExecutorService must be kept at level2 because stopping it when stopping safe mode level causes error making SQ fail add( + TelemetryDbMigrationTotalTimeProvider.class, + TelemetryDbMigrationStepsProvider.class, + TelemetryDbMigrationSuccessProvider.class, DatabaseMigrationStateImpl.class, DatabaseMigrationExecutorServiceImpl.class); diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java index b3c5178bfb0..cf7fb74b314 100644 --- a/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java @@ -73,7 +73,7 @@ public class PlatformLevel2Test { verify(container).add(ServerPluginRepository.class); verify(container).add(DatabaseCharsetChecker.class); - verify(container, times(21)).add(any()); + verify(container, times(24)).add(any()); } @Test @@ -94,7 +94,7 @@ public class PlatformLevel2Test { verify(container).add(ServerPluginRepository.class); verify(container, never()).add(DatabaseCharsetChecker.class); - verify(container, times(19)).add(any()); + verify(container, times(22)).add(any()); } -- 2.39.5