Skip to content

DanielMager/PrometheusQueries4J

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PrometheusQueries4J

Java tool for querying prometheus using PromQL

Disclaimer

It should be noted that currently only Prometheus instant queries (see Prometheus Docs) have been tested!

How to install

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>

How to use

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"
				]
			}
		]
	}
}

Testing

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.

About

Tools for querying a prometheus instance using PromQL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages