]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15773 added webuptime metric
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Tue, 14 Dec 2021 09:28:25 +0000 (10:28 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 17 Dec 2021 12:06:46 +0000 (12:06 +0000)
server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/ServerMonitoringMetrics.java
server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/WebUptimeTask.java [new file with mode: 0644]
server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/ServerMonitoringMetricsTest.java
server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/WebUptimeTaskTest.java [new file with mode: 0644]

index 0a7ee64a4576980c4e3bc94037b5b1ff14346dfa..82ed9dbd60aeb31b7a54cd2fed74e8b3dc24ccee 100644 (file)
@@ -44,6 +44,8 @@ public class ServerMonitoringMetrics {
   private final Gauge linesOfCodeRemaining;
   private final Gauge linesOfCodeAnalyzed;
 
+  private final Gauge webUptimeMinutes;
+
   public ServerMonitoringMetrics() {
     githubHealthIntegrationStatus = Gauge.build()
       .name("sonarqube_health_integration_github_status")
@@ -112,6 +114,12 @@ public class ServerMonitoringMetrics {
       .help("Total disk space on the device")
       .labelNames("node_name")
       .register();
+
+    webUptimeMinutes = Gauge.build()
+      .name("sonarqube_web_uptime_minutes")
+      .help("Number of minutes for how long the SonarQube instance is running")
+      .register();
+
   }
 
   public void setGithubStatusToGreen() {
@@ -186,7 +194,11 @@ public class ServerMonitoringMetrics {
     elasticsearchDiskSpaceFreeBytesGauge.labels(name).set(diskAvailableBytes);
   }
 
-  public void setElasticSearchDiskSpaceTotalBytes(String name, long disktotalBytes) {
-    elasticSearchDiskSpaceTotalBytes.labels(name).set(disktotalBytes);
+  public void setElasticSearchDiskSpaceTotalBytes(String name, long diskTotalBytes) {
+    elasticSearchDiskSpaceTotalBytes.labels(name).set(diskTotalBytes);
+  }
+
+  public void setWebUptimeMinutes(long minutes) {
+    webUptimeMinutes.set(minutes);
   }
 }
diff --git a/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/WebUptimeTask.java b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/WebUptimeTask.java
new file mode 100644 (file)
index 0000000..dc74f74
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.monitoring;
+
+import java.lang.management.ManagementFactory;
+import org.sonar.api.config.Configuration;
+
+public class WebUptimeTask implements MonitoringTask {
+
+  private static final String DELAY_IN_MILISECONDS_PROPERTY = "sonar.server.monitoring.webuptime.initial.delay";
+  private static final String PERIOD_IN_MILISECONDS_PROPERTY = "sonar.server.monitoring.webuptime.period";
+
+  private final ServerMonitoringMetrics metrics;
+  private final Configuration config;
+
+  public WebUptimeTask(ServerMonitoringMetrics metrics, Configuration configuration) {
+    this.metrics = metrics;
+    this.config = configuration;
+  }
+
+  @Override
+  public long getDelay() {
+    return config.getLong(DELAY_IN_MILISECONDS_PROPERTY).orElse(10_000L);
+  }
+
+  @Override
+  public long getPeriod() {
+    return config.getLong(PERIOD_IN_MILISECONDS_PROPERTY).orElse(5_000L);
+  }
+
+  @Override
+  public void run() {
+    long javaUptimeInMilliseconds = ManagementFactory.getRuntimeMXBean().getUptime();
+    metrics.setWebUptimeMinutes(javaUptimeInMilliseconds / (1000 * 60));
+  }
+}
index 714180789a58a41eea0962390672286034187fe3..6d862b09dd14654afcdfedde5b3d9b9570665a69 100644 (file)
@@ -109,6 +109,16 @@ public class ServerMonitoringMetricsTest {
       .isEqualTo(30);
   }
 
+  @Test
+  public void setters_setWebUptimeMetric() {
+    ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
+
+    metrics.setWebUptimeMinutes(10);
+
+    assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_web_uptime_minutes"))
+      .isEqualTo(10);
+  }
+
   @Test
   public void observeComputeEngineTaskDurationTest() {
     ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
diff --git a/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/WebUptimeTaskTest.java b/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/WebUptimeTaskTest.java
new file mode 100644 (file)
index 0000000..a975fc4
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.monitoring;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.junit.Test;
+import org.sonar.api.config.Configuration;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class WebUptimeTaskTest {
+
+  private final ServerMonitoringMetrics metrics = mock(ServerMonitoringMetrics.class);
+  private final DumpMapConfiguration config = new DumpMapConfiguration();
+  private final WebUptimeTask underTest = new WebUptimeTask(metrics, config);
+
+  @Test
+  public void run_metricsAreUpdatedAlways() {
+    underTest.run();
+
+    verify(metrics, times(1)).setWebUptimeMinutes(anyLong());
+  }
+
+  @Test
+  public void getDelay_returnNumberIfConfigEmpty() {
+    long delay = underTest.getDelay();
+
+    assertThat(delay).isPositive();
+  }
+
+  @Test
+  public void getDelay_returnNumberFromConfig() {
+    config.put("sonar.server.monitoring.webuptime.initial.delay", "100000");
+
+    long delay = underTest.getDelay();
+
+    assertThat(delay).isEqualTo(100_000L);
+  }
+
+  @Test
+  public void getPeriod_returnNumberIfConfigEmpty() {
+    long delay = underTest.getPeriod();
+
+    assertThat(delay).isPositive();
+  }
+
+  @Test
+  public void getPeriod_returnNumberFromConfig() {
+    config.put("sonar.server.monitoring.webuptime.period", "100000");
+
+    long delay = underTest.getPeriod();
+
+    assertThat(delay).isEqualTo(100_000L);
+  }
+
+  private static class DumpMapConfiguration implements Configuration {
+    private final Map<String, String> keyValues = new HashMap<>();
+
+    public Configuration put(String key, String value) {
+      keyValues.put(key, value.trim());
+      return this;
+    }
+
+    @Override
+    public Optional<String> get(String key) {
+      return Optional.ofNullable(keyValues.get(key));
+    }
+
+    @Override
+    public boolean hasKey(String key) {
+      throw new UnsupportedOperationException("hasKey not implemented");
+    }
+
+    @Override
+    public String[] getStringArray(String key) {
+      throw new UnsupportedOperationException("getStringArray not implemented");
+    }
+  }
+}