3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.monitoring;
22 import io.prometheus.client.Collector;
23 import io.prometheus.client.CollectorRegistry;
24 import java.util.Collections;
25 import java.util.Enumeration;
26 import org.junit.Before;
27 import org.junit.Test;
29 import static org.assertj.core.api.Assertions.assertThat;
31 //@Execution(SAME_THREAD) for JUnit5
32 public class ServerMonitoringMetricsTest {
35 public void before() {
36 CollectorRegistry.defaultRegistry.clear();
40 public void creatingClassShouldAddMetricsToRegistry() {
41 assertThat(sizeOfDefaultRegistry()).isNotPositive();
43 new ServerMonitoringMetrics();
45 assertThat(sizeOfDefaultRegistry()).isPositive();
49 public void setters_setGreenStatusForMetricsInTheMetricsRegistry() {
50 ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
52 metrics.setGithubStatusToGreen();
53 metrics.setGitlabStatusToGreen();
54 metrics.setAzureStatusToGreen();
55 metrics.setBitbucketStatusToGreen();
56 metrics.setComputeEngineStatusToGreen();
57 metrics.setElasticSearchStatusToGreen();
59 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_github_status")).isPositive();
60 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_gitlab_status")).isPositive();
61 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_bitbucket_status")).isPositive();
62 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_azuredevops_status")).isPositive();
63 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_heath_compute_engine_status")).isPositive();
64 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_heath_elasticsearch_status")).isPositive();
68 public void setters_setRedStatusForMetricsInTheMetricsRegistry() {
69 ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
71 metrics.setGithubStatusToRed();
72 metrics.setGitlabStatusToRed();
73 metrics.setAzureStatusToRed();
74 metrics.setBitbucketStatusToRed();
75 metrics.setComputeEngineStatusToRed();
76 metrics.setElasticSearchStatusToRed();
78 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_github_status")).isZero();
79 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_gitlab_status")).isZero();
80 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_bitbucket_status")).isZero();
81 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_health_integration_azuredevops_status")).isZero();
82 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_heath_compute_engine_status")).isZero();
83 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_heath_elasticsearch_status")).isZero();
87 public void setters_setNumberOfPendingTasks() {
88 ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
90 metrics.setNumberOfPendingTasks(10);
92 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_compute_engine_pending_tasks_total"))
97 public void setters_setLicenseRelatedMetrics() {
98 ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
100 metrics.setLicenseDayUntilExpire(10);
101 metrics.setLinesOfCodeRemaining(20);
102 metrics.setLinesOfCodeAnalyzed(30);
104 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_license_days_before_expiration_total"))
106 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_license_number_of_lines_remaining_total"))
108 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_license_number_of_lines_analyzed_total"))
113 public void observeComputeEngineTaskDurationTest() {
114 ServerMonitoringMetrics metrics = new ServerMonitoringMetrics();
115 String[] labelNames = {"task_type", "project_key"};
116 String[] labelValues = {"REPORT", "projectKey"};
118 metrics.observeComputeEngineTaskDuration(10, labelValues[0], labelValues[1]);
120 assertThat(CollectorRegistry.defaultRegistry.getSampleValue("sonarqube_compute_engine_tasks_running_duration_seconds_sum",
121 labelNames, labelValues)).isEqualTo(10);
124 private int sizeOfDefaultRegistry() {
125 Enumeration<Collector.MetricFamilySamples> metrics = CollectorRegistry.defaultRegistry.metricFamilySamples();
126 return Collections.list(metrics).size();