├── .gitignore ├── src ├── main │ └── java │ │ └── com │ │ └── github │ │ └── anhdat │ │ ├── models │ │ ├── KeyValResponse.java │ │ ├── VectorResponse.java │ │ ├── MatrixResponse.java │ │ └── PrometheusResponse.java │ │ ├── PrometheusRest.java │ │ └── PrometheusApiClient.java └── test │ └── java │ └── com │ └── github │ └── anhdat │ └── PrometheusApiClientTest.java ├── README.md ├── LICENSE └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /target 3 | *.iml 4 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/models/KeyValResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat.models; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class KeyValResponse { 7 | String status; 8 | List> data; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/models/VectorResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat.models; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class VectorResponse { 7 | String status; 8 | VectorData data; 9 | 10 | static class VectorResult { 11 | Map metric; 12 | List value; 13 | 14 | @Override 15 | public String toString() { 16 | return String.format( 17 | "metric: %s\nvalue: %s", 18 | metric.toString(), 19 | value == null ? "" : value.toString() 20 | ); 21 | } 22 | } 23 | 24 | static class VectorData { 25 | String resultType; 26 | List result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/models/MatrixResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat.models; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class MatrixResponse { 7 | String status; 8 | MatrixData data; 9 | 10 | static class MatrixData { 11 | String resultType; 12 | List result; 13 | } 14 | 15 | static class MatrixResult { 16 | Map metric; 17 | List> values; 18 | 19 | @Override 20 | public String toString() { 21 | return String.format( 22 | "metric: %s\nvalues: %s", 23 | metric.toString(), 24 | values == null ? "" : values.toString() 25 | ); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prometheu4j 2 | 3 | Prometheus4j is the Prometheus API Client for Java. 4 | It's based on Prometheus API documented [here](https://prometheus.io/docs/prometheus/latest/querying/api/) 5 | 6 | ## Usage 7 | 8 | ```java 9 | // query 10 | PrometheusApiClient client = new PrometheusApiClient("http://localhost:9090/"); 11 | VectorResponse response = client.query("go_gc_duration_seconds"); 12 | System.out.println("response = " + response); 13 | 14 | // range query 15 | // use empty string or null to ommit optional parameters 16 | MatrixResponse response = client.queryRange("go_gc_duration_seconds", tenMinEarlier.toString(), now.toString(), "5m", ""); 17 | 18 | // find series 19 | KeyValResponse response = client.findSeries("up", tenMinEarlier.toString(), now.toString()); 20 | 21 | ``` 22 | 23 | ## Testing 24 | 25 | Run a Prometheus instance on Docker with: 26 | ```jshelllanguage 27 | docker run -p 9090:9090 --rm prom/prometheus:v2.3.1 28 | ``` 29 | 30 | Let it run for 10 min. Then start the tests in `PrometheusApiClientTest.java` file. 31 | 32 | ## License 33 | 34 | This software is distributed under the MIT License. 35 | 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Dat Truong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/PrometheusRest.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat; 2 | 3 | import com.github.anhdat.models.KeyValResponse; 4 | import com.github.anhdat.models.MatrixResponse; 5 | import com.github.anhdat.models.PrometheusResponse; 6 | import com.github.anhdat.models.VectorResponse; 7 | import retrofit2.Call; 8 | import retrofit2.http.GET; 9 | import retrofit2.http.Query; 10 | 11 | public interface PrometheusRest { 12 | @GET("api/v1/query") 13 | Call query( 14 | @Query("query") String query, 15 | @Query("time") String time, 16 | @Query("timeout") String timeout 17 | ); 18 | 19 | @GET("api/v1/query_range") 20 | Call queryRange( 21 | @Query("query") String query, 22 | @Query("start") String start, 23 | @Query("end") String end, 24 | @Query("step") String step, 25 | @Query("timeout") String timeout 26 | ); 27 | 28 | @GET("api/v1/series") 29 | Call findSeries( 30 | @Query("match[]") String match, 31 | @Query("start") String start, 32 | @Query("end") String end 33 | ); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/models/PrometheusResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat.models; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * @author dat.truonganh 9 | */ 10 | public class PrometheusResponse { 11 | String status; 12 | Data data; 13 | 14 | @Override 15 | public String toString() { 16 | return "status: " + status + "\ndata: " + data; 17 | } 18 | 19 | class ResultItem { 20 | Map metric; 21 | List value; 22 | List> values; 23 | 24 | @Override 25 | public String toString() { 26 | return String.format( 27 | "metric: %s\nvalue: %s\nvalues: %s", 28 | metric.toString(), 29 | value == null ? "" : value.toString(), 30 | values == null ? "" : values.toString() 31 | ); 32 | } 33 | } 34 | 35 | class Data { 36 | String resultType; 37 | List result; 38 | 39 | @Override 40 | public String toString() { 41 | String resultString = result.stream().map(ResultItem::toString).collect(Collectors.joining("\n")); 42 | return String.format("type: %s, has %d items: \n%s", resultType, result.size(), resultString); 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.anhdat 8 | prometheus4j 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 8 17 | 8 18 | 19 | 20 | 21 | 22 | 23 | 24 | junit 25 | junit 26 | 4.13.1 27 | test 28 | 29 | 30 | com.squareup.retrofit2 31 | retrofit 32 | 2.5.0 33 | 34 | 35 | com.google.code.gson 36 | gson 37 | 2.8.9 38 | 39 | 40 | com.squareup.retrofit2 41 | converter-gson 42 | 2.4.0 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/anhdat/PrometheusApiClient.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat; 2 | 3 | import com.github.anhdat.models.KeyValResponse; 4 | import com.github.anhdat.models.MatrixResponse; 5 | import com.github.anhdat.models.PrometheusResponse; 6 | import com.github.anhdat.models.VectorResponse; 7 | import retrofit2.Retrofit; 8 | import retrofit2.converter.gson.GsonConverterFactory; 9 | 10 | import java.io.IOException; 11 | 12 | public class PrometheusApiClient { 13 | public String baseUrl; 14 | 15 | private Retrofit retrofit; 16 | private PrometheusRest service; 17 | 18 | public PrometheusApiClient(String baseUrl) { 19 | this.baseUrl = baseUrl; 20 | 21 | this.retrofit = new Retrofit.Builder() 22 | .baseUrl(baseUrl) 23 | .addConverterFactory(GsonConverterFactory.create()) 24 | .build(); 25 | service = retrofit.create(PrometheusRest.class); 26 | } 27 | 28 | public VectorResponse query(String query) throws IOException { 29 | return service.query(query, null, null).execute().body(); 30 | } 31 | 32 | public VectorResponse query(String query, String time) throws IOException { 33 | return service.query(query, time, null).execute().body(); 34 | } 35 | 36 | public VectorResponse query(String query, String time, String timeout) throws IOException { 37 | return service.query(query, time, timeout).execute().body(); 38 | } 39 | 40 | public MatrixResponse queryRange(String query, String start, String end, String step, String timeout) throws IOException { 41 | return service.queryRange(query, start, end, step, timeout).execute().body(); 42 | } 43 | 44 | public KeyValResponse findSeries(String match, String start, String end) throws IOException { 45 | return service.findSeries(match, start, end).execute().body(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/github/anhdat/PrometheusApiClientTest.java: -------------------------------------------------------------------------------- 1 | package com.github.anhdat; 2 | 3 | import com.github.anhdat.models.KeyValResponse; 4 | import com.github.anhdat.models.MatrixResponse; 5 | import com.github.anhdat.models.PrometheusResponse; 6 | import com.github.anhdat.models.VectorResponse; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.io.IOException; 11 | 12 | public class PrometheusApiClientTest { 13 | private PrometheusApiClient client; 14 | Long now; 15 | Long tenMinEarlier; 16 | 17 | @Before 18 | public void initTest() { 19 | client = new PrometheusApiClient("http://localhost:9090/"); 20 | now = System.currentTimeMillis() / 1000; 21 | tenMinEarlier = now - 10 * 60; 22 | } 23 | 24 | @Test 25 | public void testQuery() throws IOException { 26 | VectorResponse response = client.query("go_gc_duration_seconds"); 27 | System.out.println("response = " + response); 28 | } 29 | 30 | @Test 31 | public void testQueryWithTime() throws IOException { 32 | VectorResponse response = client.query("go_gc_duration_seconds", "2018-06-01T20:10:51.781Z"); 33 | System.out.println("response = " + response); 34 | } 35 | 36 | @Test 37 | public void testQueryWithTimestamp() throws IOException { 38 | VectorResponse response = client.query("go_gc_duration_seconds", tenMinEarlier.toString()); 39 | System.out.println("response = " + response); 40 | } 41 | 42 | @Test 43 | public void testQueryWithTimeout() throws IOException { 44 | VectorResponse response = client.query("go_gc_duration_seconds", "", "1ms"); 45 | System.out.println("response = " + response); 46 | } 47 | 48 | @Test 49 | public void testQueryRange() throws IOException { 50 | MatrixResponse response = client.queryRange("go_gc_duration_seconds", tenMinEarlier.toString(), now.toString(), "5m", ""); 51 | System.out.println("response = " + response); 52 | } 53 | 54 | @Test 55 | public void testFindSeries() throws IOException { 56 | KeyValResponse response = client.findSeries("up", tenMinEarlier.toString(), now.toString()); 57 | System.out.println("response = " + response); 58 | } 59 | } --------------------------------------------------------------------------------