From f789c234d2212a3bce79b337b9c631799312bd34 Mon Sep 17 00:00:00 2001 From: Bogdana Date: Tue, 13 Aug 2024 14:49:17 +0200 Subject: [PATCH] SONAR-22516 add telemetry for email configuration --- server/sonar-server-common/build.gradle | 1 + .../server/email/EmailSmtpConfiguration.java | 2 +- ...mailConfigAuthMethodTelemetryProvider.java | 66 +++++++++++++++++++ .../EmailConfigHostTelemetryProvider.java | 66 +++++++++++++++++++ ...ConfigAuthMethodTelemetryProviderTest.java | 52 +++++++++++++++ .../EmailConfigHostTelemetryProviderTest.java | 52 +++++++++++++++ .../platformlevel/PlatformLevel4.java | 6 ++ 7 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.java create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java create mode 100644 server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProviderTest.java create mode 100644 server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java diff --git a/server/sonar-server-common/build.gradle b/server/sonar-server-common/build.gradle index bc9f402f590..a2ca516fb12 100644 --- a/server/sonar-server-common/build.gradle +++ b/server/sonar-server-common/build.gradle @@ -24,6 +24,7 @@ dependencies { api project(':server:sonar-db-dao') api project(':server:sonar-db-migration') api project(':server:sonar-process') + api project(':server:sonar-telemetry-core') api project(':sonar-core') api project(':sonar-markdown') api project(':sonar-ws') diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSmtpConfiguration.java b/server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSmtpConfiguration.java index f8ab524b7e2..883b5ce0e49 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSmtpConfiguration.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSmtpConfiguration.java @@ -42,7 +42,7 @@ public class EmailSmtpConfiguration { public static final String EMAIL_CONFIG_PREFIX = "email.prefix"; public static final String EMAIL_CONFIG_PREFIX_DEFAULT = "[SONARQUBE]"; // Auth selection - public static final String EMAIL_CONFIG_SMTP_AUTH_METHOD= "email.smtp.auth.method"; + public static final String EMAIL_CONFIG_SMTP_AUTH_METHOD = "email.smtp.auth.method"; public static final String EMAIL_CONFIG_SMTP_AUTH_METHOD_DEFAULT = "BASIC"; // Basic Auth public static final String EMAIL_CONFIG_SMTP_USERNAME = "email.smtp_username.secured"; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.java new file mode 100644 index 00000000000..c360e9eef1e --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.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.notification.email.telemetry; + +import java.util.Optional; + +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_AUTH_METHOD; + +public class EmailConfigAuthMethodTelemetryProvider implements TelemetryDataProvider { + private final DbClient dbClient; + + public EmailConfigAuthMethodTelemetryProvider(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public String getMetricKey() { + return "email_conf_auth_method"; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + @Override + public Granularity getGranularity() { + return Granularity.WEEKLY; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.STRING; + } + + @Override + public Optional getValue() { + try (DbSession dbSession = dbClient.openSession(false)) { + return dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_AUTH_METHOD); + } + } +} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java new file mode 100644 index 00000000000..13be7e4d786 --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.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.notification.email.telemetry; + +import java.util.Optional; + +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_HOST; + +public class EmailConfigHostTelemetryProvider implements TelemetryDataProvider { + private final DbClient dbClient; + + public EmailConfigHostTelemetryProvider(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public String getMetricKey() { + return "email_conf_host"; + } + + @Override + public Dimension getDimension() { + return Dimension.INSTALLATION; + } + + @Override + public Granularity getGranularity() { + return Granularity.WEEKLY; + } + + @Override + public TelemetryDataType getType() { + return TelemetryDataType.STRING; + } + + @Override + public Optional getValue() { + try (DbSession dbSession = dbClient.openSession(false)) { + return dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST); + } + } +} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProviderTest.java new file mode 100644 index 00000000000..6efc28365e0 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProviderTest.java @@ -0,0 +1,52 @@ +/* + * 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.notification.email.telemetry; + +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_AUTH_METHOD; +import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_AUTH_METHOD_DEFAULT; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; + +class EmailConfigAuthMethodTelemetryProviderTest { + DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS); + private final DbSession dbSession = mock(DbSession.class); + + @Test + void getValue_returnsTheRightEmailConfAuthMethod() { + EmailConfigAuthMethodTelemetryProvider emailConfigAuthMethodTelemetryProvider = new EmailConfigAuthMethodTelemetryProvider(dbClient); + + when(dbClient.openSession(false)).thenReturn(dbSession); + when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_AUTH_METHOD)).thenReturn(Optional.of(EMAIL_CONFIG_SMTP_AUTH_METHOD_DEFAULT)); + + assertThat(emailConfigAuthMethodTelemetryProvider.getMetricKey()).isEqualTo("email_conf_auth_method"); + assertThat(emailConfigAuthMethodTelemetryProvider.getGranularity()).isEqualTo(Granularity.WEEKLY); + assertThat(emailConfigAuthMethodTelemetryProvider.getDimension()).isEqualTo(Dimension.INSTALLATION); + assertThat(emailConfigAuthMethodTelemetryProvider.getValue()).isEqualTo(Optional.of("BASIC")); + } +} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java new file mode 100644 index 00000000000..90a3b51d997 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java @@ -0,0 +1,52 @@ +/* + * 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.notification.email.telemetry; + +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_HOST; + +class EmailConfigHostTelemetryProviderTest { + DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS); + private final DbSession dbSession = mock(DbSession.class); + + @Test + void getValue_returnsTheRightEmailConfHost() { + EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient); + + String host = "smtp.office365.com"; + when(dbClient.openSession(false)).thenReturn(dbSession); + when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.of(host)); + + assertThat(emailConfigHostTelemetryProvider.getMetricKey()).isEqualTo("email_conf_host"); + assertThat(emailConfigHostTelemetryProvider.getGranularity()).isEqualTo(Granularity.WEEKLY); + assertThat(emailConfigHostTelemetryProvider.getDimension()).isEqualTo(Dimension.INSTALLATION); + assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(host)); + } +} 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 da7ff01ea80..8aef512ede0 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 @@ -176,6 +176,8 @@ import org.sonar.server.monitoring.devops.GithubMetricsTask; import org.sonar.server.monitoring.devops.GitlabMetricsTask; import org.sonar.server.newcodeperiod.ws.NewCodePeriodsWsModule; import org.sonar.server.notification.NotificationModule; +import org.sonar.server.notification.email.telemetry.EmailConfigAuthMethodTelemetryProvider; +import org.sonar.server.notification.email.telemetry.EmailConfigHostTelemetryProvider; import org.sonar.server.notification.ws.NotificationWsModule; import org.sonar.server.permission.index.PermissionIndexer; import org.sonar.server.permission.ws.PermissionsWsModule; @@ -682,6 +684,10 @@ public class PlatformLevel4 extends PlatformLevel { // dismiss message new DismissMessageWsModule(), + // Email configuration + EmailConfigHostTelemetryProvider.class, + EmailConfigAuthMethodTelemetryProvider.class, + AzureMetricsTask.class, BitbucketMetricsTask.class, GithubMetricsTask.class, -- 2.39.5