From: Lukasz Jarocki Date: Tue, 14 Dec 2021 09:28:25 +0000 (+0100) Subject: SONAR-15773 added webuptime metric X-Git-Tag: 9.3.0.51899~116 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=57c561eebf9473454138ceb3c412be24db94ece4;p=sonarqube.git SONAR-15773 added webuptime metric --- diff --git a/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/ServerMonitoringMetrics.java b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/ServerMonitoringMetrics.java index 0a7ee64a457..82ed9dbd60a 100644 --- a/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/ServerMonitoringMetrics.java +++ b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/ServerMonitoringMetrics.java @@ -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 index 00000000000..dc74f740789 --- /dev/null +++ b/server/sonar-webserver-monitoring/src/main/java/org/sonar/server/monitoring/WebUptimeTask.java @@ -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)); + } +} diff --git a/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/ServerMonitoringMetricsTest.java b/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/ServerMonitoringMetricsTest.java index 714180789a5..6d862b09dd1 100644 --- a/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/ServerMonitoringMetricsTest.java +++ b/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/ServerMonitoringMetricsTest.java @@ -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 index 00000000000..a975fc4f123 --- /dev/null +++ b/server/sonar-webserver-monitoring/src/test/java/org/sonar/server/monitoring/WebUptimeTaskTest.java @@ -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 keyValues = new HashMap<>(); + + public Configuration put(String key, String value) { + keyValues.put(key, value.trim()); + return this; + } + + @Override + public Optional 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"); + } + } +}