aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>2024-09-06 08:47:33 +0200
committersonartech <sonartech@sonarsource.com>2024-09-06 20:02:37 +0000
commitcd40a38f951a1355c086b99017401b93bd203558 (patch)
treeb8c9fe11385b7951a637cf2a5ce6c4ab091f38bc /server/sonar-db-migration
parent0996c6186cfeb8e2c763d2f174b26ab276b232f7 (diff)
downloadsonarqube-cd40a38f951a1355c086b99017401b93bd203558.tar.gz
sonarqube-cd40a38f951a1355c086b99017401b93bd203558.zip
SONAR-22891 added new telemetry metric
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImpl.java8
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java66
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationContainerPopulatorImplTest.java2
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/engine/MigrationEngineImplTest.java5
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/MigrationStepsExecutorImplTest.java4
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java50
6 files changed, 131 insertions, 4 deletions
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 bbccc564367..934b7202285 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,7 @@ 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.TelemetryDbMigrationStepDurationProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
@@ -45,16 +46,18 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor {
private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider;
private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider;
private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider;
+ private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider;
public MigrationStepsExecutorImpl(Container migrationContainer, MigrationHistory migrationHistory, MutableDatabaseMigrationState databaseMigrationState,
TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider, TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider,
- TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider) {
+ TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider, TelemetryDbMigrationStepDurationProvider stepDurationProvider) {
this.migrationContainer = migrationContainer;
this.migrationHistory = migrationHistory;
this.databaseMigrationState = databaseMigrationState;
this.telemetryDbMigrationTotalTimeProvider = telemetryDbMigrationTotalTimeProvider;
this.telemetryDbMigrationStepsProvider = telemetryDbMigrationStepsProvider;
this.telemetryDbMigrationSuccessProvider = telemetryDbMigrationSuccessProvider;
+ this.telemetryDbMigrationStepDurationProvider = stepDurationProvider;
}
@Override
@@ -109,11 +112,12 @@ public class MigrationStepsExecutorImpl implements MigrationStepsExecutor {
throw new MigrationStepExecutionException(step, e);
} finally {
if (done) {
- stepProfiler.stopInfo(STEP_STOP_PATTERN,
+ long durationInMiliseconds = stepProfiler.stopInfo(STEP_STOP_PATTERN,
databaseMigrationState.getCompletedMigrations() + 1,
databaseMigrationState.getTotalMigrations(),
step,
"success");
+ telemetryDbMigrationStepDurationProvider.addCompletedStep(step.getMigrationNumber(), durationInMiliseconds);
} else {
stepProfiler.stopError(STEP_STOP_PATTERN,
databaseMigrationState.getCompletedMigrations() + 1,
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java
new file mode 100644
index 00000000000..06ac499de9c
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProvider.java
@@ -0,0 +1,66 @@
+/*
+ * 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.HashMap;
+import java.util.Map;
+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 TelemetryDbMigrationStepDurationProvider implements TelemetryDataProvider<Long> {
+
+ private final Map<String, Long> dbMigrationStepDurations = new HashMap<>();
+
+ @Override
+ public String getMetricKey() {
+ return "db_migration_step_duration";
+ }
+
+ @Override
+ public Dimension getDimension() {
+ return Dimension.INSTALLATION;
+ }
+
+ @Override
+ public Granularity getGranularity() {
+ return Granularity.ADHOC;
+ }
+
+ @Override
+ public TelemetryDataType getType() {
+ return TelemetryDataType.INTEGER;
+ }
+
+ @Override
+ public Map<String, Long> getValues() {
+ return dbMigrationStepDurations;
+ }
+
+ @Override
+ public void after() {
+ dbMigrationStepDurations.clear();
+ }
+
+ public void addCompletedStep(Long migrationId, Long duration) {
+ dbMigrationStepDurations.put(migrationId.toString(), duration);
+ }
+}
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 c1af7bacb2f..d74b459fa4b 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,7 @@ 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.TelemetryDbMigrationStepDurationProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
@@ -57,6 +58,7 @@ public class MigrationContainerPopulatorImplTest {
migrationContainer.add(mock(TelemetryDbMigrationStepsProvider.class));
migrationContainer.add(mock(TelemetryDbMigrationTotalTimeProvider.class));
migrationContainer.add(mock(TelemetryDbMigrationSuccessProvider.class));
+ migrationContainer.add(mock(TelemetryDbMigrationStepDurationProvider.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 db672f507fa..9462d6b7f49 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,7 @@ 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.TelemetryDbMigrationStepDurationProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
@@ -51,6 +52,7 @@ class MigrationEngineImplTest {
private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider();
private final TelemetryDbMigrationStepsProvider telemetryUpgradeStepsProvider = new TelemetryDbMigrationStepsProvider();
private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider();
+ private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider = new TelemetryDbMigrationStepDurationProvider();
private final MigrationEngineImpl underTest = new MigrationEngineImpl(migrationHistory, serverContainer, migrationSteps);
@BeforeEach
@@ -62,6 +64,7 @@ class MigrationEngineImplTest {
serverContainer.add(migrationHistory);
serverContainer.add(stepRegistry);
serverContainer.add(databaseMigrationState);
+ serverContainer.add(telemetryDbMigrationStepDurationProvider);
serverContainer.startComponents();
}
@@ -97,7 +100,7 @@ class MigrationEngineImplTest {
private record TestMigrationStep(StepRegistry registry) implements MigrationStep {
@Override
- public void execute() throws SQLException {
+ public void execute() {
registry.stepRan = true;
}
}
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 643bb332373..a7a40fb45ce 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,7 @@ 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.TelemetryDbMigrationStepDurationProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationSuccessProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationStepsProvider;
import org.sonar.server.telemetry.TelemetryDbMigrationTotalTimeProvider;
@@ -60,8 +61,9 @@ class MigrationStepsExecutorImplTest {
private final TelemetryDbMigrationTotalTimeProvider telemetryDbMigrationTotalTimeProvider = new TelemetryDbMigrationTotalTimeProvider();
private final TelemetryDbMigrationStepsProvider telemetryDbMigrationStepsProvider = new TelemetryDbMigrationStepsProvider();
private final TelemetryDbMigrationSuccessProvider telemetryDbMigrationSuccessProvider = new TelemetryDbMigrationSuccessProvider();
+ private final TelemetryDbMigrationStepDurationProvider telemetryDbMigrationStepDurationProvider = new TelemetryDbMigrationStepDurationProvider();
private final MigrationStepsExecutorImpl underTest = new MigrationStepsExecutorImpl(migrationContainer, migrationHistory, databaseMigrationState,
- telemetryDbMigrationTotalTimeProvider, telemetryDbMigrationStepsProvider, telemetryDbMigrationSuccessProvider);
+ telemetryDbMigrationTotalTimeProvider, telemetryDbMigrationStepsProvider, telemetryDbMigrationSuccessProvider, telemetryDbMigrationStepDurationProvider);
private final NoOpMigrationStatusListener migrationStatusListener = mock();
@BeforeEach
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java
new file mode 100644
index 00000000000..7ef33356421
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/telemetry/TelemetryDbMigrationStepDurationProviderTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.Map;
+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 TelemetryDbMigrationStepDurationProviderTest {
+
+ @Test
+ void testGetters() {
+ TelemetryDbMigrationStepDurationProvider underTest = new TelemetryDbMigrationStepDurationProvider();
+
+ underTest.addCompletedStep(1L, 10L);
+ underTest.addCompletedStep(9L, 20L);
+
+ assertThat(underTest.getMetricKey()).isEqualTo("db_migration_step_duration");
+ assertThat(underTest.getGranularity()).isEqualTo(ADHOC);
+ assertThat(underTest.getDimension()).isEqualTo(INSTALLATION);
+ assertThat(underTest.getType()).isEqualTo(INTEGER);
+ assertThat(underTest.getValue()).isEmpty();
+ assertThat(underTest.getValues()).containsExactlyInAnyOrderEntriesOf(Map.of("1", 10L, "9", 20L));
+
+ underTest.after();
+
+ assertThat(underTest.getValues()).isEmpty();
+ }
+}