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.Gauge;
23 import io.prometheus.client.Summary;
24 import org.sonar.api.server.ServerSide;
27 public class ServerMonitoringMetrics {
29 public static final int UP_STATUS = 1;
30 public static final int DOWN_STATUS = 0;
31 private final Gauge githubHealthIntegrationStatus;
32 private final Gauge gitlabHealthIntegrationStatus;
33 private final Gauge bitbucketHealthIntegrationStatus;
34 private final Gauge azureHealthIntegrationStatus;
35 private final Gauge computeEngineGauge;
36 private final Gauge elasticsearchGauge;
38 private final Gauge cePendingTasksTotal;
39 private final Summary ceTasksRunningDuration;
40 private final Gauge elasticsearchDiskSpaceFreeBytesGauge;
41 private final Gauge elasticSearchDiskSpaceTotalBytes;
43 private final Gauge licenseDaysBeforeExpiration;
44 private final Gauge linesOfCodeRemaining;
45 private final Gauge linesOfCodeAnalyzed;
47 public ServerMonitoringMetrics() {
48 githubHealthIntegrationStatus = Gauge.build()
49 .name("sonarqube_health_integration_github_status")
50 .help("Tells whether SonarQube instance has configured GitHub integration and its status is green. 1 for green, 0 otherwise .")
53 gitlabHealthIntegrationStatus = Gauge.build()
54 .name("sonarqube_health_integration_gitlab_status")
55 .help("Tells whether SonarQube instance has configured GitLab integration and its status is green. 1 for green, 0 otherwise .")
58 bitbucketHealthIntegrationStatus = Gauge.build()
59 .name("sonarqube_health_integration_bitbucket_status")
60 .help("Tells whether SonarQube instance has configured BitBucket integration and its status is green. 1 for green, 0 otherwise .")
63 azureHealthIntegrationStatus = Gauge.build()
64 .name("sonarqube_health_integration_azuredevops_status")
65 .help("Tells whether SonarQube instance has configured Azure integration and its status is green. 1 for green, 0 otherwise .")
68 cePendingTasksTotal = Gauge.build()
69 .name("sonarqube_compute_engine_pending_tasks_total")
70 .help("Number of tasks at given point of time that were pending in the Compute Engine queue [SHARED, same value for every SonarQube instance]")
73 ceTasksRunningDuration = Summary.build()
74 .name("sonarqube_compute_engine_tasks_running_duration_seconds")
75 .help("Compute engine task running time in seconds")
76 .labelNames("task_type", "project_key")
79 computeEngineGauge = Gauge.build()
80 .name("sonarqube_heath_compute_engine_status")
81 .help("Tells whether Compute Engine is up (healthy, ready to take tasks) or down. 1 for up, 0 for down")
84 elasticsearchGauge = Gauge.build()
85 .name("sonarqube_heath_elasticsearch_status")
86 .help("Tells whether Elasticsearch is up or down. 1 for Up, 0 for down")
89 licenseDaysBeforeExpiration = Gauge.build()
90 .name("sonarqube_license_days_before_expiration_total")
91 .help("Days until the SonarQube license will expire.")
94 linesOfCodeRemaining = Gauge.build()
95 .name("sonarqube_license_number_of_lines_remaining_total")
96 .help("Number of lines remaining until the limit for the current license is hit.")
99 linesOfCodeAnalyzed = Gauge.build()
100 .name("sonarqube_license_number_of_lines_analyzed_total")
101 .help("Number of lines analyzed.")
104 elasticsearchDiskSpaceFreeBytesGauge = Gauge.build()
105 .name("sonarqube_elasticsearch_disk_space_free_bytes")
106 .help("Space left on device")
107 .labelNames("node_name")
110 elasticSearchDiskSpaceTotalBytes = Gauge.build()
111 .name("sonarqube_elasticsearch_disk_space_total_bytes")
112 .help("Total disk space on the device")
113 .labelNames("node_name")
117 public void setGithubStatusToGreen() {
118 githubHealthIntegrationStatus.set(UP_STATUS);
121 public void setGithubStatusToRed() {
122 githubHealthIntegrationStatus.set(DOWN_STATUS);
125 public void setGitlabStatusToGreen() {
126 gitlabHealthIntegrationStatus.set(UP_STATUS);
129 public void setGitlabStatusToRed() {
130 gitlabHealthIntegrationStatus.set(DOWN_STATUS);
133 public void setAzureStatusToGreen() {
134 azureHealthIntegrationStatus.set(UP_STATUS);
137 public void setAzureStatusToRed() {
138 azureHealthIntegrationStatus.set(DOWN_STATUS);
141 public void setBitbucketStatusToGreen() {
142 bitbucketHealthIntegrationStatus.set(UP_STATUS);
145 public void setBitbucketStatusToRed() {
146 bitbucketHealthIntegrationStatus.set(DOWN_STATUS);
149 public void setNumberOfPendingTasks(int numberOfPendingTasks) {
150 cePendingTasksTotal.set(numberOfPendingTasks);
153 public void observeComputeEngineTaskDuration(long durationInSeconds, String taskType, String projectKey) {
154 ceTasksRunningDuration.labels(taskType, projectKey).observe(durationInSeconds);
157 public void setComputeEngineStatusToGreen() {
158 computeEngineGauge.set(UP_STATUS);
161 public void setComputeEngineStatusToRed() {
162 computeEngineGauge.set(DOWN_STATUS);
165 public void setElasticSearchStatusToGreen() {
166 elasticsearchGauge.set(UP_STATUS);
169 public void setElasticSearchStatusToRed() {
170 elasticsearchGauge.set(DOWN_STATUS);
173 public void setLicenseDayUntilExpire(long days) {
174 licenseDaysBeforeExpiration.set(days);
177 public void setLinesOfCodeRemaining(long loc) {
178 linesOfCodeRemaining.set(loc);
181 public void setLinesOfCodeAnalyzed(long loc) {
182 linesOfCodeAnalyzed.set(loc);
185 public void setElasticSearchDiskSpaceFreeBytes(String name, long diskAvailableBytes) {
186 elasticsearchDiskSpaceFreeBytesGauge.labels(name).set(diskAvailableBytes);
189 public void setElasticSearchDiskSpaceTotalBytes(String name, long disktotalBytes) {
190 elasticSearchDiskSpaceTotalBytes.labels(name).set(disktotalBytes);