]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19425 Init components to add cloud usage to telemetry
authorEric Giffon <eric.giffon@sonarsource.com>
Thu, 1 Jun 2023 08:59:24 +0000 (10:59 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 13 Jun 2023 20:03:39 +0000 (20:03 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java
server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java
server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/CloudUsageDataProvider.java [new file with mode: 0644]
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/CloudUsageDataProviderTest.java [new file with mode: 0644]
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

index c486edd64edc4fd0d20a7217360243b8acc0ef02..44b65e3395c31fe274db48169c87fe6ed8bc26c7 100644 (file)
@@ -46,6 +46,7 @@ public class TelemetryData {
   private final String installationVersion;
   private final boolean inDocker;
   private final ManagedInstanceInformation managedInstanceInformation;
+  private final CloudUsage cloudUsage;
   private final List<UserTelemetryDto> users;
   private final List<Project> projects;
   private final List<ProjectStatistics> projectStatistics;
@@ -76,6 +77,7 @@ public class TelemetryData {
     hasUnanalyzedCpp = builder.hasUnanalyzedCpp;
     customSecurityConfigs = requireNonNullElse(builder.customSecurityConfigs, Set.of());
     managedInstanceInformation = builder.managedInstanceInformation;
+    cloudUsage = builder.cloudUsage;
     ncdId = builder.ncdId;
     branches = builder.branches;
     newCodeDefinitions = builder.newCodeDefinitions;
@@ -125,6 +127,10 @@ public class TelemetryData {
     return managedInstanceInformation;
   }
 
+  public CloudUsage getCloudUsage() {
+    return cloudUsage;
+  }
+
   public Optional<Boolean> hasUnanalyzedC() {
     return Optional.ofNullable(hasUnanalyzedC);
   }
@@ -181,6 +187,7 @@ public class TelemetryData {
     private String installationVersion;
     private boolean inDocker = false;
     private ManagedInstanceInformation managedInstanceInformation;
+    private CloudUsage cloudUsage;
     private Boolean hasUnanalyzedC;
     private Boolean hasUnanalyzedCpp;
     private Set<String> customSecurityConfigs;
@@ -276,6 +283,11 @@ public class TelemetryData {
       return this;
     }
 
+    Builder setCloudUsage(CloudUsage cloudUsage) {
+      this.cloudUsage = cloudUsage;
+      return this;
+    }
+
     TelemetryData build() {
       requireNonNullValues(serverId, version, plugins, database, messageSequenceNumber);
       return new TelemetryData(this);
@@ -340,6 +352,9 @@ public class TelemetryData {
   record ManagedInstanceInformation(boolean isManaged, @Nullable String provider) {
   }
 
+  record CloudUsage(boolean kubernetes) {
+  }
+
   public static class ProjectStatistics {
     private final String projectUuid;
     private final Long branchCount;
index 3bffed11f5eefbebdcfab5c1366a8a134f4ac78f..3712a810f10d258f9b5547e428de0cff28f24a2a 100644 (file)
@@ -37,6 +37,8 @@ public class TelemetryDataJsonWriter {
 
   @VisibleForTesting
   static final String MANAGED_INSTANCE_PROPERTY = "managedInstanceInformation";
+  @VisibleForTesting
+  static final String CLOUD_USAGE_PROPERTY = "cloudUsage";
 
   private static final String LANGUAGE_PROPERTY = "language";
   private static final String VERSION = "version";
@@ -101,6 +103,7 @@ public class TelemetryDataJsonWriter {
     writeNewCodeDefinitions(json, telemetryData);
     writeQualityGates(json, telemetryData);
     writeManagedInstanceInformation(json, telemetryData.getManagedInstanceInformation());
+    writeCloudUsage(json, telemetryData.getCloudUsage());
     extensions.forEach(e -> e.write(json));
 
     json.endObject();
@@ -229,6 +232,13 @@ public class TelemetryDataJsonWriter {
     json.endObject();
   }
 
+  private static void writeCloudUsage(JsonWriter json, TelemetryData.CloudUsage cloudUsage) {
+    json.name(CLOUD_USAGE_PROPERTY);
+    json.beginObject();
+    json.prop("kubernetes", cloudUsage.kubernetes());
+    json.endObject();
+  }
+
   @NotNull
   private static String toUtc(long date) {
     return DateTimeFormatter.ofPattern(DATETIME_FORMAT)
index 512d1a35cc204e499e21e026418034792126b9bc..30144e50ac42c05cc6d3eec252b5cf7c4bbafba8 100644 (file)
@@ -585,6 +585,7 @@ public class TelemetryDataJsonWriterTest {
       .setMessageSequenceNumber(1L)
       .setPlugins(Collections.emptyMap())
       .setManagedInstanceInformation(new TelemetryData.ManagedInstanceInformation(false, null))
+      .setCloudUsage(new TelemetryData.CloudUsage(false))
       .setDatabase(new TelemetryData.Database("H2", "11"))
       .setNcdId(NCD_ID);
   }
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/CloudUsageDataProvider.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/CloudUsageDataProvider.java
new file mode 100644 (file)
index 0000000..67bf2e1
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.sonar.api.server.ServerSide;
+
+@ServerSide
+public class CloudUsageDataProvider {
+
+  public TelemetryData.CloudUsage getCloudUsage() {
+    return new TelemetryData.CloudUsage(isKubernetes());
+  }
+
+  private static boolean isKubernetes() {
+    return true;
+  }
+}
index 1383e027f3a5f85dc30fb9538efd8b3afedc9ff6..384286685ea3064ba11df0309629a75ef7f7968b 100644 (file)
@@ -109,6 +109,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
   private final QualityGateCaycChecker qualityGateCaycChecker;
   private final QualityGateFinder qualityGateFinder;
   private final ManagedInstanceService managedInstanceService;
+  private final CloudUsageDataProvider cloudUsageDataProvider;
   private final Set<NewCodeDefinition> newCodeDefinitions = new HashSet<>();
   private final Map<String, NewCodeDefinition> ncdByProject = new HashMap<>();
   private final Map<String, NewCodeDefinition> ncdByBranch = new HashMap<>();
@@ -118,7 +119,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
   public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository,
     PlatformEditionProvider editionProvider, InternalProperties internalProperties, Configuration configuration,
     DockerSupport dockerSupport, QualityGateCaycChecker qualityGateCaycChecker, QualityGateFinder qualityGateFinder,
-    ManagedInstanceService managedInstanceService) {
+    ManagedInstanceService managedInstanceService, CloudUsageDataProvider cloudUsageDataProvider) {
     this.server = server;
     this.dbClient = dbClient;
     this.pluginRepository = pluginRepository;
@@ -129,6 +130,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
     this.qualityGateCaycChecker = qualityGateCaycChecker;
     this.qualityGateFinder = qualityGateFinder;
     this.managedInstanceService = managedInstanceService;
+    this.cloudUsageDataProvider = cloudUsageDataProvider;
   }
 
   private static Database loadDatabaseMetadata(DbSession dbSession) {
@@ -181,6 +183,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
       .setInstallationVersion(installationVersionProperty.orElse(null))
       .setInDocker(dockerSupport.isRunningInDocker())
       .setManagedInstanceInformation(buildManagedInstanceInformation())
+      .setCloudUsage(buildCloudUsage())
       .build();
   }
 
@@ -443,4 +446,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
     return new TelemetryData.ManagedInstanceInformation(managedInstanceService.isInstanceExternallyManaged(), provider);
   }
 
+  private TelemetryData.CloudUsage buildCloudUsage() {
+    return cloudUsageDataProvider.getCloudUsage();
+  }
 }
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/CloudUsageDataProviderTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/CloudUsageDataProviderTest.java
new file mode 100644 (file)
index 0000000..a7ea230
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CloudUsageDataProviderTest {
+
+  private final CloudUsageDataProvider underTest = new CloudUsageDataProvider();
+
+  @Test
+  public void isKubernetes_shouldReturnValue() {
+    assertThat(underTest.getCloudUsage().kubernetes()).isTrue();
+  }
+}
index 9be832680c6107cdf40b73b987282373a8a318a2..40aa17d9dcf145e3214864798b3a109c990fa711 100644 (file)
@@ -113,11 +113,12 @@ public class TelemetryDataLoaderImplTest {
   private final QualityGateFinder qualityGateFinder = new QualityGateFinder(db.getDbClient());
   private final InternalProperties internalProperties = spy(new MapInternalProperties());
   private final ManagedInstanceService managedInstanceService = mock(ManagedInstanceService.class);
+  private final CloudUsageDataProvider cloudUsageDataProvider = mock(CloudUsageDataProvider.class);
 
   private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, editionProvider,
-    internalProperties, configuration, dockerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService);
+    internalProperties, configuration, dockerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider);
   private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, editionProvider,
-    internalProperties, configuration, dockerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService);
+    internalProperties, configuration, dockerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider);
 
   private QualityGateDto builtInDefaultQualityGate;
   private MetricDto bugsDto;
index 3ffdc677d6b2531a23b1942e5178019d9e2f6c94..719ef992d5f76b67786c08ea3a4e7975382ef19c 100644 (file)
@@ -237,6 +237,7 @@ import org.sonar.server.setting.SettingsChangeNotifier;
 import org.sonar.server.setting.ws.SettingsWsModule;
 import org.sonar.server.source.ws.SourceWsModule;
 import org.sonar.server.startup.LogServerId;
+import org.sonar.server.telemetry.CloudUsageDataProvider;
 import org.sonar.server.telemetry.TelemetryClient;
 import org.sonar.server.telemetry.TelemetryDaemon;
 import org.sonar.server.telemetry.TelemetryDataJsonWriter;
@@ -619,6 +620,7 @@ public class PlatformLevel4 extends PlatformLevel {
       TelemetryDataJsonWriter.class,
       TelemetryDaemon.class,
       TelemetryClient.class,
+      CloudUsageDataProvider.class,
 
       // monitoring
       ServerMonitoringMetrics.class,