From: Eric Giffon Date: Thu, 1 Jun 2023 08:59:24 +0000 (+0200) Subject: SONAR-19425 Init components to add cloud usage to telemetry X-Git-Tag: 10.1.0.73491~60 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a8395f79ad0269bb0f211d615f186b4111c93275;p=sonarqube.git SONAR-19425 Init components to add cloud usage to telemetry --- diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java index c486edd64ed..44b65e3395c 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java @@ -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 users; private final List projects; private final List 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 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 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; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java index 3bffed11f5e..3712a810f10 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java @@ -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) diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java index 512d1a35cc2..30144e50ac4 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java @@ -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 index 00000000000..67bf2e1cd9a --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/CloudUsageDataProvider.java @@ -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; + } +} diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index 1383e027f3a..384286685ea 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -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 newCodeDefinitions = new HashSet<>(); private final Map ncdByProject = new HashMap<>(); private final Map 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 index 00000000000..a7ea23037d8 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/CloudUsageDataProviderTest.java @@ -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(); + } +} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index 9be832680c6..40aa17d9dcf 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -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; diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 3ffdc677d6b..719ef992d5f 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -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,