Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prometheus export of data summary stats #19742

Merged
merged 29 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0a99a36
Add Prometheus export of data summary stats
jason-p-pickering Jan 22, 2025
07d99bc
Remove dependency
jason-p-pickering Jan 22, 2025
6df66a9
Minor refactor
jason-p-pickering Jan 22, 2025
e0d6608
Fix(?) flaky test
jason-p-pickering Jan 22, 2025
3600adb
Linting
jason-p-pickering Jan 22, 2025
b58adf2
Fix code smells
jason-p-pickering Jan 22, 2025
fe0b9b1
Minor
jason-p-pickering Jan 22, 2025
a794fc8
Merge branch 'master' of github.com:dhis2/dhis2-core into DHIS2-18872
jason-p-pickering Jan 22, 2025
47d6ee5
Revert change in unrelated test
jason-p-pickering Jan 22, 2025
378bfe1
Fix test
jason-p-pickering Jan 22, 2025
7938943
Linting
jason-p-pickering Jan 22, 2025
aba1fe2
Add test for data summary JSON response
jason-p-pickering Jan 22, 2025
c2b7bbc
Refactor to use StringBuilder
jason-p-pickering Jan 22, 2025
739f8dc
Rename some methods
jason-p-pickering Jan 22, 2025
ba2c355
Null fix
jason-p-pickering Jan 22, 2025
0a0cede
Linting
jason-p-pickering Jan 22, 2025
169e809
Simplification and tests
jason-p-pickering Jan 23, 2025
474d0fc
Remove code smells
jason-p-pickering Jan 23, 2025
e396fa9
Linting
jason-p-pickering Jan 23, 2025
344e3e2
More fixes
jason-p-pickering Jan 23, 2025
ddb3764
Fix test
jason-p-pickering Jan 23, 2025
7b74a1a
Merge branch 'master' of github.com:dhis2/dhis2-core into DHIS2-18872
jason-p-pickering Jan 23, 2025
aa25ca5
Remove server date from metrics as this is not particularly useful to…
jason-p-pickering Jan 23, 2025
deadf96
Improve system info output
jason-p-pickering Jan 23, 2025
932e552
Merge branch 'master' of github.com:dhis2/dhis2-core into DHIS2-18872
jason-p-pickering Jan 23, 2025
b3db952
Linting
jason-p-pickering Jan 23, 2025
3922b23
Simplification
jason-p-pickering Jan 23, 2025
3e9c828
Minor
jason-p-pickering Jan 23, 2025
562bdbc
Move method into controller
jason-p-pickering Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor to use StringBuilder
jason-p-pickering committed Jan 22, 2025
commit c2b7bbca915a48f8a2dfc5caec7e8618cbaa12d5
Original file line number Diff line number Diff line change
@@ -31,24 +31,18 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.OpenApi;
import org.hisp.dhis.datastatistics.DataStatisticsService;
import org.hisp.dhis.datasummary.DataSummary;
import org.hisp.dhis.security.RequiresAuthority;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.utils.PrometheusTextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;

/**
* dataSummary endpoint to access System Statistics
@@ -74,15 +68,57 @@ public class DataSummaryController {
@GetMapping(value = "/metrics", produces = TEXT_PLAIN_VALUE)
@RequiresAuthority(anyOf = F_PERFORM_MAINTENANCE)
public @ResponseBody String getPrometheusMetrics() {
try {
DataSummary summary = dataStatisticsService.getSystemStatisticsSummary();
DataSummaryPrometheusMetrics.updateMetrics(summary);
Writer writer = new StringWriter();
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
return writer.toString();
} catch (IOException e) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Unable to produce metrics", e);
DataSummary summary = dataStatisticsService.getSystemStatisticsSummary();
final String PROMETHEUS_GAUGE_NAME = "gauge";
PrometheusTextBuilder metrics = new PrometheusTextBuilder();

metrics.updateMetricsFromMap(
summary.getObjectCounts(),
"data_summary_object_counts",
"type",
"Count of metadata objects",
PROMETHEUS_GAUGE_NAME);

metrics.updateMetricsFromMap(
summary.getActiveUsers(),
"data_summary_active_users",
"days",
"Count of active users",
PROMETHEUS_GAUGE_NAME);

metrics.updateMetricsFromMap(
summary.getUserInvitations(),
"data_summary_user_invitations",
"type",
"Count of user invitations",
PROMETHEUS_GAUGE_NAME);

metrics.updateMetricsFromMap(
summary.getDataValueCount(),
"data_summary_data_value_count",
"days",
"Count of updated data values by day",
PROMETHEUS_GAUGE_NAME);

metrics.updateMetricsFromMap(
summary.getEventCount(),
"data_summary_event_count",
"days",
"Count of updated events by day",
PROMETHEUS_GAUGE_NAME);

// The system information is presented just as a static gauge with value 1.0
// The key is the field name and the value is the field value
metrics.createPrometheusHelpLine("data_summary_system_info", "System information");
metrics.createPrometheusTypeLine("data_summary_system_info", "gauge");
if (summary.getSystem() != null) {
metrics.appendSystemInfo("version", summary.getSystem().getVersion());
metrics.appendSystemInfo("revision", summary.getSystem().getRevision());
metrics.appendSystemInfo("build_time", summary.getSystem().getBuildTime().toString());
metrics.appendSystemInfo("system_id", summary.getSystem().getSystemId());
metrics.appendSystemInfo("server_date", summary.getSystem().getServerDate().toString());
}

return metrics.getMetrics();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2004-2025, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.webapi.utils;

import java.util.Map;

public class PrometheusTextBuilder {

private StringBuilder metrics = new StringBuilder();

public void createPrometheusHelpLine(String metricName, String help) {
metrics.append("# HELP ").append(metricName).append(" ").append(help).append("\n");
}

public void createPrometheusTypeLine(String metricName, String type) {
metrics.append("# TYPE ").append(metricName).append(" ").append(type).append("\n");
}

public void updateMetricsFromMap(
Map<?, ?> map, String metricName, String keyName, String help, String type) {
createPrometheusHelpLine(metricName, help);
createPrometheusTypeLine(metricName, type);
map.forEach(
(key, value) ->
metrics.append("%s{%s=\"%s\", } %s.0 \n".formatted(metricName, keyName, key, value)));
}

public void appendSystemInfo(String key, String value) {
if (value != null) {
metrics
.append("data_summary_system_info{key=\"")
.append(key)
.append("\", value=\"")
.append(value)
.append("\"} 1.0\n");
}
}

public String getMetrics() {
return metrics.toString();
}
}