Java tool for querying prometheus using PromQL
It should be noted that currently only Prometheus instant queries (see Prometheus Docs) have been tested!
You can install the package from GitHub with jitpack into your maven project by adding the following to your project's pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.DanielMager</groupId>
<artifactId>PrometheusQueryTool</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
</dependencies>
To get the result JSON for the API request http://localhost:9090/api/v1/query?query=go_gc_duration_seconds_count
:
Run the following code:
PrometheusQueryClient queryClient = new PrometheusQueryClient(new URL("http://localhost:9090/"));
PrometheusQueryResponse response = queryClient.query("go_gc_duration_seconds_count")";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(response));
Which should result in a response as follows:
{
"status": "success",
"data": {
"resultType": "vector",
"result": [
{
"metric": {
"__name__": "go_gc_duration_seconds_count",
"instance": "localhost:9090",
"job": "prometheusMonitoring"
},
"value": [
1.654852972097E9,
"297"
]
}
]
}
}
The tool can be tested with help from the unit test. Start up a simple Prometheus instance e.g. via Docker by running the command:
docker run -p 9090:9090 -rm prom/prometheus:v2.36.1
Let it run for a brief moment so it can gather some data and run PrometheusQueryClientTest#instantQueryTest(). This should result in a response similar to that described in How to use.