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
Simplification and tests
jason-p-pickering committed Jan 23, 2025

Verified

This commit was signed with the committer’s verified signature.
jason-p-pickering Jason Pickering
commit 169e809af76e4553666c6313a260ccaab61d0a28
Original file line number Diff line number Diff line change
@@ -117,32 +117,18 @@ public DataSummaryController(DataStatisticsService dataStatisticsService) {
metrics.helpLine("data_summary_system_info", "System information");
metrics.typeLine("data_summary_system_info", "gauge");
if (summary.getSystem() != null) {
if (summary.getSystem() != null) {
if (summary.getSystem().getVersion() != null) {
metrics.appendStaticKeyValue(
"data_summary_system_info", "version", summary.getSystem().getVersion());
}
if (summary.getSystem().getRevision() != null) {
metrics.appendStaticKeyValue(
"data_summary_system_info", "revision", summary.getSystem().getRevision());
}
if (summary.getSystem().getBuildTime() != null) {
metrics.appendStaticKeyValue(
"data_summary_system_info",
"build_time",
summary.getSystem().getBuildTime().toString());
}
if (summary.getSystem().getSystemId() != null) {
metrics.appendStaticKeyValue(
"data_summary_system_info", "system_id", summary.getSystem().getSystemId());
}
if (summary.getSystem().getServerDate() != null) {
metrics.appendStaticKeyValue(
"data_summary_system_info",
"server_date",
summary.getSystem().getServerDate().toString());
}
}
metrics.appendStaticKeyValue(
"data_summary_system_info", "version", summary.getSystem().getVersion());
metrics.appendStaticKeyValue(
"data_summary_system_info", "revision", summary.getSystem().getRevision());
metrics.appendStaticKeyValue(
"data_summary_system_info", "build_time", summary.getSystem().getBuildTime().toString());
metrics.appendStaticKeyValue(
"data_summary_system_info", "system_id", summary.getSystem().getSystemId());
metrics.appendStaticKeyValue(
"data_summary_system_info",
"server_date",
summary.getSystem().getServerDate().toString());
}

return metrics.getMetrics();
Original file line number Diff line number Diff line change
@@ -30,9 +30,8 @@
import java.util.Map;

/**
* A simple utility class to build Prometheus text format metrics. Note that there is no validation
* of the input, so the user should make sure that the input is correct. The Prometheus text format
* is documented here: https://prometheus.io/docs/instrumenting/exposition_formats/
* A simple utility class to build Prometheus text format metrics. The Prometheus text format is
* documented here: https://prometheus.io/docs/instrumenting/exposition_formats/
*
* @author Jason P. Pickering
*/
@@ -41,34 +40,59 @@ public class PrometheusTextBuilder {
private StringBuilder metrics = new StringBuilder();

public void helpLine(String metricName, String help) {
metrics.append("# HELP ").append(metricName).append(" ").append(help).append("%n");
metrics.append(String.format("# HELP %s %s%n", metricName, help));
}

/**
* Appends a Prometheus metric type line to the metrics.
*
* @param metricName the name of the metric
* @param type the type of the metric (e.g., counter, gauge)
*/
public void typeLine(String metricName, String type) {
metrics.append("# TYPE ").append(metricName).append(" ").append(type).append("%n");
metrics.append(String.format("# TYPE %s %s%n", metricName, type));
}

/**
* Transform a Map<String, ?> into a Prometheus text format metric. Note that the key is assumed
* to be a string, and the value should be a number which is capable of being converted to a
* string.
*
* @param map the map containing the metrics data
* @param metricName the name of the metric
* @param keyName the name of the key in the metric
* @param help the help text for the metric
* @param type the type of the metric
*/
public void updateMetricsFromMap(
Map<?, ?> map, String metricName, String keyName, String help, String type) {
helpLine(metricName, help);
typeLine(metricName, type);
map.forEach(
(key, value) ->
metrics.append("%s{%s=\"%s\" } %s%n".formatted(metricName, keyName, key, value)));
metrics.append("%s{%s=\"%s\"} %s%n".formatted(metricName, keyName, key, value)));
}

/**
* Appends a static key-value pair to the Prometheus metrics. This is most useful for representing
* labels in the Prometheus text format such as build time, version, etc. This will produce a
* metric with a static value of 1.
*
* @param metricName the name of the metric
* @param key the key for the metric
* @param value the value for the metric
*/
public void appendStaticKeyValue(String metricName, String key, String value) {
if (value != null) {
metrics
.append(metricName)
.append("{key=\"")
.append(key)
.append("\", value=\"")
.append(value)
.append("\"} 1%n");
metrics.append(String.format("%s{key=\"%s\", value=\"%s\"} 1%n", metricName, key, value));
}
}

/**
* Returns the Prometheus metrics as a string.
*
* @return the metrics in Prometheus text format
*/
public String getMetrics() {
return metrics.toString();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Map;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the PrometheusTextBuilder class.
*
* @author Jason P. Pickering
*/
public class PrometheusTextBuilderTest {
@Test
void getMetricsReturnsEmptyStringWhenNoMetrics() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
assertEquals("", builder.getMetrics());
}

@Test
void helpLineAppendsHelpText() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
builder.helpLine("test_metric", "This is a test metric");
assertEquals("# HELP test_metric This is a test metric\n", builder.getMetrics());
}

@Test
void typeLineAppendsTypeText() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
builder.typeLine("test_metric", "counter");
assertEquals("# TYPE test_metric counter\n", builder.getMetrics());
}

@Test
void updateMetricsFromMapAppendsMetrics() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
Map<String, Integer> map = Map.of("key1", 1);
builder.updateMetricsFromMap(map, "test_metric", "key", "Test help", "gauge");
String expected =
"# HELP test_metric Test help\n"
+ "# TYPE test_metric gauge\n"
+ "test_metric{key=\"key1\"} 1\n";
assertEquals(expected, builder.getMetrics());
}

@Test
void appendStaticKeyValueAppendsKeyValue() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
builder.appendStaticKeyValue("test_metric", "key", "value");
assertEquals("test_metric{key=\"key\", value=\"value\"} 1\n", builder.getMetrics());
}

@Test
void appendStaticKeyValueIgnoresNullValue() {
PrometheusTextBuilder builder = new PrometheusTextBuilder();
builder.appendStaticKeyValue("test_metric", "key", null);
assertEquals("", builder.getMetrics());
}
}