private final Gauge linesOfCodeRemaining;
private final Gauge linesOfCodeAnalyzed;
+ private final Gauge webUptimeMinutes;
+
public ServerMonitoringMetrics() {
githubHealthIntegrationStatus = Gauge.build()
.name("sonarqube_health_integration_github_status")
.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() {
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);
}
}
--- /dev/null
+/*
+ * 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));
+ }
+}
--- /dev/null
+/*
+ * 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");
+ }
+ }
+}