From af66f272ea91ce1a132dc40260247ec720741270 Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Fri, 19 Jul 2024 16:01:17 +0200 Subject: [PATCH] SONAR-22479 Implement after() method for providers --- .../telemetry/TelemetryDbMigrationStepsProvider.java | 2 +- .../telemetry/TelemetryDbMigrationSuccessProvider.java | 2 +- .../telemetry/TelemetryDbMigrationTotalTimeProvider.java | 2 +- .../org/sonar/telemetry/core/TelemetryDataProvider.java | 7 ++++++- .../src/main/java/org/sonar/telemetry/TelemetryDaemon.java | 3 +++ .../sonar/telemetry/metrics/TelemetryMetricsLoader.java | 4 ++++ 6 files changed, 16 insertions(+), 4 deletions(-) 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 index c2e52da08ba..ea400c355ec 100644 --- 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 @@ -59,7 +59,7 @@ public class TelemetryDbMigrationStepsProvider implements TelemetryDataProvider< } @Override - public void destroy() { + public void after() { 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 index 1680a99d770..b50d241f8a5 100644 --- 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 @@ -59,7 +59,7 @@ public class TelemetryDbMigrationSuccessProvider implements TelemetryDataProvide } @Override - public void destroy() { + public void after() { 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 index 6be779e1f84..7fe57f03934 100644 --- 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 @@ -59,7 +59,7 @@ public class TelemetryDbMigrationTotalTimeProvider implements TelemetryDataProvi } @Override - public void destroy() { + public void after() { dbMigrationTotalTime = null; } } 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 47054aece8e..b91453f3aa3 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 @@ -77,7 +77,12 @@ public interface TelemetryDataProvider { throw new IllegalStateException("Not implemented"); } - default void destroy() { + /** + * This method will be executed for every telemetry provider after telemetry metrics are sent. This is important for some telemetry + * providers, more specifically, those adhoc metrics which are usually to be sent as once off values as these values are typically + * stored in memory. The most common use case would be to clear the data. + */ + default void after() { // this method does nothing by default it is used to perform cleanup tasks if needed } } diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java index a528094695a..f920ff8956a 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/TelemetryDaemon.java @@ -169,6 +169,9 @@ public class TelemetryDaemon extends AbstractStoppableScheduledExecutorServiceIm context.getMetricsToUpdate().forEach(toUpdate -> dbClient.telemetryMetricsSentDao().upsert(dbSession, toUpdate)); dbSession.commit(); } + + // Finally, run after tasks for all providers + telemetryMetricsLoader.runProviderAfterTasks(); } private void uploadLegacyTelemetry() throws IOException { diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsLoader.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsLoader.java index f232fe82fce..aeb5f3c3074 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsLoader.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/metrics/TelemetryMetricsLoader.java @@ -90,6 +90,10 @@ public class TelemetryMetricsLoader { } } + public void runProviderAfterTasks() { + this.providers.forEach(TelemetryDataProvider::after); + } + private Set retrieveBaseMessages(Map> metrics) { return metrics.entrySet().stream() // we do not want to send payloads with zero metrics -- 2.39.5