]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22516 add telemetry for email configuration
authorBogdana <bogdana.kushnir@sonarsource.com>
Tue, 13 Aug 2024 12:49:17 +0000 (14:49 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 16 Aug 2024 20:02:59 +0000 (20:02 +0000)
server/sonar-server-common/build.gradle
server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSmtpConfiguration.java
server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.java [new file with mode: 0644]
server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java [new file with mode: 0644]
server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProviderTest.java [new file with mode: 0644]
server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java [new file with mode: 0644]
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

index bc9f402f59098c869f3df9792ce4ae087b43a82d..a2ca516fb12bd441857ba1616dcd8b1c0c5b065b 100644 (file)
@@ -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')
index f8ab524b7e22069160d6e0e7466e281a4fe09e46..883b5ce0e492e2829961d35bae2c311d74659355 100644 (file)
@@ -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 (file)
index 0000000..c360e9e
--- /dev/null
@@ -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<String> {
+  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<String> 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 (file)
index 0000000..13be7e4
--- /dev/null
@@ -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<String> {
+  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<String> 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 (file)
index 0000000..6efc283
--- /dev/null
@@ -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 (file)
index 0000000..90a3b51
--- /dev/null
@@ -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));
+  }
+}
index da7ff01ea802d40be05716913ae98882f755f514..8aef512ede097a1a830925562a2699b16ad3d0a6 100644 (file)
@@ -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,