├── .gitignore ├── LICENSE ├── README.md ├── promql_client ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── bdwise │ │ │ └── prometheus │ │ │ └── client │ │ │ ├── builder │ │ │ ├── AlertManagerMetaQueryBuilder.java │ │ │ ├── InstantQueryBuilder.java │ │ │ ├── LabelMetaQueryBuilder.java │ │ │ ├── QueryBuilder.java │ │ │ ├── QueryBuilderType.java │ │ │ ├── RangeQueryBuilder.java │ │ │ ├── SeriesMetaQueryBuilder.java │ │ │ ├── StatusMetaQueryBuilder.java │ │ │ ├── TargetMetaQueryBuilder.java │ │ │ └── Utils.java │ │ │ └── converter │ │ │ ├── ConvertUtil.java │ │ │ ├── Data.java │ │ │ ├── Result.java │ │ │ ├── am │ │ │ ├── AlertManagerResultItem.java │ │ │ └── DefaultAlertManagerResult.java │ │ │ ├── label │ │ │ └── DefaultLabelResult.java │ │ │ ├── query │ │ │ ├── DefaultQueryResult.java │ │ │ ├── MatrixData.java │ │ │ ├── QueryDataType.java │ │ │ ├── QueryResultItemValue.java │ │ │ ├── ScalarData.java │ │ │ └── VectorData.java │ │ │ ├── series │ │ │ ├── DefaultSeriesResult.java │ │ │ └── SeriesResultItem.java │ │ │ ├── status │ │ │ └── DefaultConfigResult.java │ │ │ └── target │ │ │ ├── DefaultTargetResult.java │ │ │ └── TargetResultItem.java │ └── test │ │ └── java │ │ └── com │ │ └── bdwise │ │ └── prometheus │ │ └── client │ │ ├── builder │ │ └── UriBuilderTest.java │ │ └── comverter │ │ └── query │ │ ├── AlertManagerResultTest.java │ │ ├── ConfigResultTest.java │ │ ├── LabelResultTest.java │ │ ├── MatrixResultTest.java │ │ ├── ScalarResultTest.java │ │ ├── SeriesResultTest.java │ │ ├── TargetResultTest.java │ │ └── VectorResultTest.java └── target │ └── classes │ └── com │ └── bdwise │ └── prometheus │ └── client │ └── converter │ └── query │ ├── DefaultQueryResult.class │ └── QueryResultItemValue.class └── promql_client_test ├── pom.xml └── src └── test └── java └── com └── bdwise └── prometheus └── client └── PromqlTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | promql_client/.settings/ 4 | promql_client/target 5 | promql_client_test/.settings/ 6 | promql_client_test/target 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 thomas kang 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prometheus Query API Java Client Usage 2 | *** 3 | ## Build API URL 4 | *** 5 | ### RangeQuery 6 | ``` 7 | RangeQueryBuilder rangeQueryBuilder = QueryBuilderType.RangeQuery.newInstance(TARGET_SERVER); 8 | URI targetUri = rangeQueryBuilder.withQuery("irate(received_api_call_total[60s])") 9 | .withStartEpochTime(System.currentTimeMillis() / 1000 - 60*10) 10 | .withEndEpochTime(System.currentTimeMillis() / 1000) 11 | .withStepTime("60s") 12 | .build(); 13 | ``` 14 | ### InstantQuery 15 | ``` 16 | InstantQueryBuilder iqb = QueryBuilderType.InstantQuery.newInstance(TARGET_SERVER); 17 | URI targetUri = iqb.withQuery("irate(received_api_call_total[60s])").build(); 18 | ``` 19 | ### LabelQuery 20 | ``` 21 | LabelMetaQueryBuilder lmqb = QueryBuilderType.LabelMetadaQuery.newInstance(TARGET_SERVER); 22 | URI targetUri = lmqb.withLabel("pod").build(); 23 | ``` 24 | ### Statuc(Config)Query 25 | ``` 26 | StatusMetaQueryBuilder smqb = QueryBuilderType.StatusMetadaQuery.newInstance(TARGET_SERVER); 27 | URI targetUri = smqb.build(); 28 | ``` 29 | ### TargetQuery 30 | ``` 31 | TargetMetaQueryBuilder tmqb = QueryBuilderType.TargetMetadaQuery.newInstance(TARGET_SERVER); 32 | URI targetUri = tmqb.build(); 33 | ``` 34 | ### AlertManagerQuery 35 | ``` 36 | AlertManagerMetaQueryBuilder ammqb = QueryBuilderType.AlertManagerMetadaQuery.newInstance(TARGET_SERVER); 37 | URI targetUri = ammqb.build(); 38 | ``` 39 | *** 40 | ## Parse Result 41 | *** 42 | ### Parse Matrix Type Query Result 43 | ``` 44 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 45 | ``` 46 | ### Parse Vector Type Query Result 47 | ``` 48 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 49 | ``` 50 | ### Parse Scalar Type Query Result 51 | ``` 52 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 53 | ``` 54 | ### Parse Label API Result 55 | ``` 56 | DefaultLabelResult result = ConvertUtil.convertLabelResultString(rtVal); 57 | ``` 58 | ### Parse Status(Config) API Result 59 | ``` 60 | DefaultConfigResult result = ConvertUtil.convertConfigResultString(rtVal); 61 | ``` 62 | ### Parse Targets API Result 63 | ``` 64 | DefaultTargetResult result = ConvertUtil.convertTargetResultString(rtVal); 65 | ``` 66 | ### Parse AlertManager API Result 67 | ``` 68 | DefaultAlertManagerResult result = ConvertUtil.convertAlertManagerResultString(rtVal); 69 | ``` 70 | 71 | -------------------------------------------------------------------------------- /promql_client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.bdwise.prometheus.client 6 | promql_client 7 | 0.1-SNAPSHOT 8 | jar 9 | 10 | promql_client 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 4.13.1 22 | test 23 | 24 | 25 | com.google.code.gson 26 | gson 27 | 2.8.1 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/AlertManagerMetaQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.net.URI; 4 | 5 | public class AlertManagerMetaQueryBuilder implements QueryBuilder { 6 | 7 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/alertmanagers"; 8 | 9 | private String targetUriPattern; 10 | 11 | public AlertManagerMetaQueryBuilder(String serverUrl) { 12 | this.targetUriPattern = serverUrl + TARGET_URI_PATTERN_SUFFIX; 13 | } 14 | public URI build() { 15 | return URI.create(targetUriPattern); 16 | } 17 | 18 | private boolean validate() { 19 | return true; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/InstantQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.URI; 5 | import java.net.URLEncoder; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | 10 | public class InstantQueryBuilder implements QueryBuilder { 11 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/query?query=#{query}&time=#{time}&timeout=#{timeout}"; 12 | 13 | private static final String TIME_EPOCH_TIME = "time"; 14 | private static final String TIMEOUT = "timeout"; 15 | private static final String QUERY_STRING = "query"; 16 | 17 | 18 | private String targetUriPattern; 19 | private Map params = new HashMap(); 20 | 21 | public InstantQueryBuilder(String serverUrl) { 22 | targetUriPattern = serverUrl+ TARGET_URI_PATTERN_SUFFIX; 23 | params.put(TIMEOUT, ""); 24 | params.put(TIME_EPOCH_TIME, ""); 25 | } 26 | 27 | public InstantQueryBuilder withQuery(String query) { 28 | try { 29 | params.put(QUERY_STRING, URLEncoder.encode(query, "utf-8")); 30 | } catch (UnsupportedEncodingException e) { 31 | e.printStackTrace(); 32 | } 33 | return this; 34 | } 35 | 36 | public InstantQueryBuilder withEpochTime(long time) { 37 | params.put(TIME_EPOCH_TIME, time); 38 | return this; 39 | } 40 | 41 | 42 | public InstantQueryBuilder withTimeout(String timeout) { 43 | params.put(TIMEOUT, timeout); 44 | return this; 45 | } 46 | 47 | public URI build() { 48 | return URI.create(Utils.namedFormat(targetUriPattern, params)); 49 | } 50 | 51 | private boolean validate() { 52 | return true; 53 | } 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/LabelMetaQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.net.URI; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class LabelMetaQueryBuilder implements QueryBuilder{ 8 | 9 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/label/#{label}/values"; 10 | 11 | private static final String LABEL_STRING = "label"; 12 | 13 | private String targetUriPattern; 14 | private Map params = new HashMap(); 15 | 16 | public LabelMetaQueryBuilder(String serverUrl) { 17 | this.targetUriPattern = serverUrl + TARGET_URI_PATTERN_SUFFIX; 18 | } 19 | 20 | public LabelMetaQueryBuilder withLabel(String label) { 21 | params.put(LABEL_STRING, label); 22 | return this; 23 | } 24 | 25 | 26 | public URI build() { 27 | return URI.create(Utils.namedFormat(targetUriPattern, params)); 28 | } 29 | 30 | private boolean validate() { 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/QueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | public interface QueryBuilder { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/QueryBuilderType.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | public enum QueryBuilderType { 4 | RangeQuery{ 5 | 6 | @SuppressWarnings("unchecked") 7 | @Override 8 | public RangeQueryBuilder newInstance(String prometheusUrl) { 9 | return new RangeQueryBuilder(prometheusUrl); 10 | } 11 | 12 | }, 13 | InstantQuery{ 14 | 15 | @SuppressWarnings("unchecked") 16 | @Override 17 | public InstantQueryBuilder newInstance(String prometheusUrl) { 18 | return new InstantQueryBuilder(prometheusUrl); 19 | } 20 | 21 | }, 22 | SeriesMetadaQuery{ 23 | 24 | @SuppressWarnings("unchecked") 25 | @Override 26 | public QueryBuilder newInstance(String prometheusUrl) { 27 | return new SeriesMetaQueryBuilder(prometheusUrl); 28 | } 29 | 30 | }, 31 | LabelMetadaQuery{ 32 | 33 | @SuppressWarnings("unchecked") 34 | @Override 35 | public QueryBuilder newInstance(String prometheusUrl) { 36 | return new LabelMetaQueryBuilder(prometheusUrl); 37 | } 38 | 39 | }, 40 | 41 | TargetMetadaQuery{ 42 | 43 | @SuppressWarnings("unchecked") 44 | @Override 45 | public QueryBuilder newInstance(String prometheusUrl) { 46 | return new TargetMetaQueryBuilder(prometheusUrl); 47 | } 48 | 49 | }, 50 | 51 | AlertManagerMetadaQuery{ 52 | 53 | @SuppressWarnings("unchecked") 54 | @Override 55 | public QueryBuilder newInstance(String prometheusUrl) { 56 | return new AlertManagerMetaQueryBuilder(prometheusUrl); 57 | } 58 | 59 | }, 60 | 61 | StatusMetadaQuery{ 62 | 63 | @SuppressWarnings("unchecked") 64 | @Override 65 | public QueryBuilder newInstance(String prometheusUrl) { 66 | return new StatusMetaQueryBuilder(prometheusUrl); 67 | } 68 | 69 | }; 70 | 71 | 72 | public abstract T newInstance(String prometheusUrl); 73 | } 74 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/RangeQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.URI; 5 | import java.net.URLEncoder; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | 10 | public class RangeQueryBuilder implements QueryBuilder { 11 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/query_range?query=#{query}&start=#{start}&end=#{end}&step=#{step}"; 12 | 13 | private static final String START_TIME_EPOCH_TIME = "start"; 14 | private static final String END_TIME_EPOCH_TIME = "end"; 15 | private static final String STEP_TIME = "step"; 16 | private static final String QUERY_STRING = "query"; 17 | 18 | 19 | private String targetUriPattern; 20 | private Map params = new HashMap(); 21 | 22 | public RangeQueryBuilder(String serverUrl) { 23 | targetUriPattern = serverUrl+ TARGET_URI_PATTERN_SUFFIX; 24 | } 25 | 26 | public RangeQueryBuilder withQuery(String query) { 27 | try { 28 | params.put(QUERY_STRING, URLEncoder.encode(query, "utf-8")); 29 | } catch (UnsupportedEncodingException e) { 30 | e.printStackTrace(); 31 | } 32 | return this; 33 | } 34 | 35 | public RangeQueryBuilder withStartEpochTime(long startTime) { 36 | params.put(START_TIME_EPOCH_TIME, startTime); 37 | return this; 38 | } 39 | 40 | public RangeQueryBuilder withEndEpochTime(long endTime) { 41 | params.put(END_TIME_EPOCH_TIME, endTime); 42 | return this; 43 | } 44 | 45 | public RangeQueryBuilder withStepTime(String step) { 46 | params.put(STEP_TIME, step); 47 | return this; 48 | } 49 | 50 | public URI build() { 51 | return URI.create(Utils.namedFormat(targetUriPattern, params)); 52 | } 53 | 54 | private boolean validate() { 55 | return true; 56 | } 57 | 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/SeriesMetaQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.net.URI; 5 | import java.net.URLEncoder; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | public class SeriesMetaQueryBuilder implements QueryBuilder{ 10 | private static final String TARGET_URI="/api/v1/series?"; 11 | private static final String TARGET_URI_PATTERN_SUFFIX = "#{selector}&start=#{start}&end=#{end}"; 12 | 13 | private static final String START_TIME_EPOCH_TIME = "start"; 14 | private static final String END_TIME_EPOCH_TIME = "end"; 15 | private static final String QUERY_STRING = "selector"; 16 | 17 | private String serverUrl; 18 | private Map params = new HashMap(); 19 | 20 | public SeriesMetaQueryBuilder(String serverUrl) { 21 | this.serverUrl = serverUrl + TARGET_URI; 22 | } 23 | 24 | public SeriesMetaQueryBuilder withSelector(String selector) { 25 | try { 26 | params.put(QUERY_STRING, URLEncoder.encode(selector, "UTF-8").replaceAll("%3D", "=").replaceAll("%26", "&")); 27 | } catch (UnsupportedEncodingException e) { 28 | e.printStackTrace(); 29 | } 30 | return this; 31 | } 32 | 33 | public SeriesMetaQueryBuilder withStartEpochTime(long startTime) { 34 | params.put(START_TIME_EPOCH_TIME, startTime); 35 | return this; 36 | } 37 | 38 | public SeriesMetaQueryBuilder withEndEpochTime(long endTime) { 39 | params.put(END_TIME_EPOCH_TIME, endTime); 40 | return this; 41 | } 42 | 43 | public URI build() { 44 | return URI.create(Utils.namedFormat(serverUrl + Utils.namedFormat(TARGET_URI_PATTERN_SUFFIX, params), params)); 45 | } 46 | 47 | private boolean validate() { 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/StatusMetaQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.net.URI; 4 | 5 | public class StatusMetaQueryBuilder implements QueryBuilder { 6 | 7 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/status/config"; 8 | 9 | private String targetUriPattern; 10 | 11 | public StatusMetaQueryBuilder(String serverUrl) { 12 | this.targetUriPattern = serverUrl + TARGET_URI_PATTERN_SUFFIX; 13 | } 14 | public URI build() { 15 | return URI.create(targetUriPattern); 16 | } 17 | 18 | private boolean validate() { 19 | return true; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/TargetMetaQueryBuilder.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.net.URI; 4 | 5 | public class TargetMetaQueryBuilder implements QueryBuilder { 6 | 7 | private static final String TARGET_URI_PATTERN_SUFFIX = "/api/v1/targets"; 8 | 9 | private String targetUriPattern; 10 | 11 | public TargetMetaQueryBuilder(String serverUrl) { 12 | this.targetUriPattern = serverUrl + TARGET_URI_PATTERN_SUFFIX; 13 | } 14 | 15 | public URI build() { 16 | return URI.create(targetUriPattern); 17 | } 18 | 19 | private boolean validate() { 20 | return true; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/builder/Utils.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | import com.bdwise.prometheus.client.converter.query.QueryDataType; 10 | import com.google.gson.stream.JsonReader; 11 | 12 | public class Utils { 13 | private final static Pattern namedFormatPattern = Pattern.compile("#\\{(?.*?)}"); 14 | public static String namedFormat(final String format, Map kvs) { 15 | final StringBuffer buffer = new StringBuffer(); 16 | final Matcher match = namedFormatPattern.matcher(format); 17 | while (match.find()) { 18 | final String key = match.group("key"); 19 | final Object value = kvs.get(key); 20 | if (value != null) 21 | match.appendReplacement(buffer, value.toString()); 22 | else if (kvs.containsKey(key)) 23 | match.appendReplacement(buffer, "null"); 24 | else 25 | match.appendReplacement(buffer, ""); 26 | } 27 | match.appendTail(buffer); 28 | return buffer.toString(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/ConvertUtil.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import com.bdwise.prometheus.client.converter.am.AlertManagerResultItem; 8 | import com.bdwise.prometheus.client.converter.am.DefaultAlertManagerResult; 9 | import com.bdwise.prometheus.client.converter.label.DefaultLabelResult; 10 | import com.bdwise.prometheus.client.converter.query.DefaultQueryResult; 11 | import com.bdwise.prometheus.client.converter.query.QueryDataType; 12 | import com.bdwise.prometheus.client.converter.series.DefaultSeriesResult; 13 | import com.bdwise.prometheus.client.converter.series.SeriesResultItem; 14 | import com.bdwise.prometheus.client.converter.status.DefaultConfigResult; 15 | import com.bdwise.prometheus.client.converter.target.DefaultTargetResult; 16 | import com.bdwise.prometheus.client.converter.target.TargetResultItem; 17 | import com.google.gson.Gson; 18 | import com.google.gson.GsonBuilder; 19 | import com.google.gson.TypeAdapter; 20 | import com.google.gson.stream.JsonReader; 21 | import com.google.gson.stream.JsonWriter; 22 | 23 | 24 | public abstract class ConvertUtil { 25 | 26 | private static QueryDataType searchDataType(String typeString) { 27 | for (QueryDataType each : QueryDataType.class.getEnumConstants()) { 28 | if (each.name().compareToIgnoreCase(typeString) == 0) { 29 | return each; 30 | } 31 | } 32 | return null; 33 | } 34 | 35 | private static Map convertJsonToMap(JsonReader reader) throws IOException { 36 | Map resultMap = new HashMap(); 37 | reader.beginObject(); 38 | while(reader.hasNext()) { 39 | resultMap.put(reader.nextName(), reader.nextString()); 40 | } 41 | reader.endObject(); 42 | return resultMap; 43 | } 44 | 45 | @SuppressWarnings("unchecked") 46 | public static DefaultAlertManagerResult convertAlertManagerResultString(String jsonString) { 47 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultAlertManagerResult.class, new TypeAdapter() { 48 | 49 | @Override 50 | public void write(JsonWriter out, DefaultAlertManagerResult value) throws IOException { 51 | 52 | } 53 | 54 | @Override 55 | public DefaultAlertManagerResult read(JsonReader reader) throws IOException { 56 | DefaultAlertManagerResult amResult = new DefaultAlertManagerResult(); 57 | String status = null; 58 | reader.beginObject(); 59 | while(reader.hasNext()) { 60 | String propertyName = reader.nextName(); 61 | if("status".equals(propertyName)) { 62 | status = reader.nextString(); 63 | } else if("data".equals(propertyName)) { 64 | reader.beginObject(); 65 | while(reader.hasNext()) { 66 | String _propertyName = reader.nextName(); 67 | if("activeAlertmanagers".equals(_propertyName)) { 68 | reader.beginArray(); 69 | while(reader.hasNext()) { 70 | AlertManagerResultItem amResultItem = new AlertManagerResultItem(); 71 | reader.beginObject(); 72 | while(reader.hasNext()) { 73 | String __propertyName = reader.nextName(); 74 | if("url".equals(__propertyName)) { 75 | amResultItem.setUrl(reader.nextString()); 76 | } else { 77 | reader.nextString(); 78 | } 79 | } 80 | reader.endObject(); 81 | amResult.addActiveManager(amResultItem); 82 | } 83 | reader.endArray(); 84 | 85 | } else if("droppedAlertmanagers".equals(_propertyName)) { 86 | reader.beginArray(); 87 | while(reader.hasNext()) { 88 | AlertManagerResultItem amResultItem = new AlertManagerResultItem(); 89 | reader.beginObject(); 90 | while(reader.hasNext()) { 91 | String __propertyName = reader.nextName(); 92 | if("url".equals(__propertyName)) { 93 | amResultItem.setUrl(reader.nextString()); 94 | } else { 95 | reader.nextString(); 96 | } 97 | } 98 | reader.endObject(); 99 | amResult.addDroppedManager(amResultItem); 100 | } 101 | reader.endArray(); 102 | } 103 | } 104 | reader.endObject(); 105 | } 106 | } 107 | reader.endObject(); 108 | amResult.setStatus(status); 109 | return amResult; 110 | } 111 | 112 | }).create(); 113 | return gson.fromJson(jsonString, DefaultAlertManagerResult.class); 114 | } 115 | 116 | 117 | @SuppressWarnings("unchecked") 118 | public static DefaultTargetResult convertTargetResultString(String jsonString) { 119 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultTargetResult.class, new TypeAdapter() { 120 | 121 | @Override 122 | public void write(JsonWriter out, DefaultTargetResult value) throws IOException { 123 | 124 | } 125 | 126 | @Override 127 | public DefaultTargetResult read(JsonReader reader) throws IOException { 128 | DefaultTargetResult targetResult = new DefaultTargetResult(); 129 | String status = null; 130 | reader.beginObject(); 131 | while(reader.hasNext()) { 132 | String propertyName = reader.nextName(); 133 | if("status".equals(propertyName)) { 134 | status = reader.nextString(); 135 | } else if("data".equals(propertyName)) { 136 | reader.beginObject(); 137 | while(reader.hasNext()) { 138 | String _propertyName = reader.nextName(); 139 | if("activeTargets".equals(_propertyName)) { 140 | reader.beginArray(); 141 | while(reader.hasNext()) { 142 | TargetResultItem targetResultItem = new TargetResultItem(); 143 | reader.beginObject(); 144 | while(reader.hasNext()) { 145 | String __propertyName = reader.nextName(); 146 | if("discoveredLabels".equals(__propertyName)) { 147 | targetResultItem.setDiscoveredLabels(convertJsonToMap(reader)); 148 | } else if("labels".equals(__propertyName)) { 149 | targetResultItem.setLabel(convertJsonToMap(reader)); 150 | } else if("scrapeUrl".equals(__propertyName)) { 151 | targetResultItem.setScrapeUrl(reader.nextString()); 152 | } else if("lastError".equals(__propertyName)) { 153 | targetResultItem.setLastError(reader.nextString()); 154 | } else if("lastScrape".equals(__propertyName)) { 155 | targetResultItem.setLastScrape(reader.nextString()); 156 | } else if("health".equals(__propertyName)) { 157 | targetResultItem.setHealth(reader.nextString()); 158 | } 159 | } 160 | reader.endObject(); 161 | targetResult.addActiveTarget(targetResultItem); 162 | } 163 | reader.endArray(); 164 | 165 | } else if("droppedTargets".equals(_propertyName)) { 166 | reader.beginArray(); 167 | while(reader.hasNext()) { 168 | TargetResultItem targetResultItem = new TargetResultItem(); 169 | reader.beginObject(); 170 | while(reader.hasNext()) { 171 | String __propertyName = reader.nextName(); 172 | if("discoveredLabels".equals(__propertyName)) { 173 | targetResultItem.setDiscoveredLabels(convertJsonToMap(reader)); 174 | } 175 | } 176 | reader.endObject(); 177 | targetResult.addDroppedTarget(targetResultItem); 178 | } 179 | reader.endArray(); 180 | } 181 | } 182 | reader.endObject(); 183 | } 184 | } 185 | reader.endObject(); 186 | targetResult.setStatus(status); 187 | return targetResult; 188 | } 189 | 190 | }).create(); 191 | return gson.fromJson(jsonString, DefaultTargetResult.class); 192 | } 193 | 194 | 195 | @SuppressWarnings("unchecked") 196 | public static DefaultConfigResult convertConfigResultString(String jsonString) { 197 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultConfigResult.class, new TypeAdapter() { 198 | 199 | @Override 200 | public void write(JsonWriter out, DefaultConfigResult value) throws IOException { 201 | 202 | } 203 | 204 | @Override 205 | public DefaultConfigResult read(JsonReader reader) throws IOException { 206 | DefaultConfigResult configResult = new DefaultConfigResult(); 207 | String status = null; 208 | reader.beginObject(); 209 | while(reader.hasNext()) { 210 | String propertyName = reader.nextName(); 211 | if("status".equals(propertyName)) { 212 | status = reader.nextString(); 213 | } else if("data".equals(propertyName)) { 214 | reader.beginObject(); 215 | while(reader.hasNext()) { 216 | String _propertyName = reader.nextName(); 217 | if("yaml".equals(_propertyName)) { 218 | configResult.addData(reader.nextString()); 219 | } else { 220 | reader.nextString(); 221 | } 222 | } 223 | reader.endObject(); 224 | } 225 | } 226 | reader.endObject(); 227 | configResult.setStatus(status); 228 | return configResult; 229 | } 230 | 231 | }).create(); 232 | return gson.fromJson(jsonString, DefaultConfigResult.class); 233 | 234 | } 235 | 236 | @SuppressWarnings("unchecked") 237 | public static DefaultLabelResult convertLabelResultString(String jsonString) { 238 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultLabelResult.class, new TypeAdapter() { 239 | 240 | @Override 241 | public void write(JsonWriter out, DefaultLabelResult value) throws IOException { 242 | 243 | } 244 | 245 | @Override 246 | public DefaultLabelResult read(JsonReader reader) throws IOException { 247 | DefaultLabelResult seriesResult = new DefaultLabelResult(); 248 | String status = null; 249 | reader.beginObject(); 250 | while(reader.hasNext()) { 251 | String propertyName = reader.nextName(); 252 | if("status".equals(propertyName)) { 253 | status = reader.nextString(); 254 | } else if("data".equals(propertyName)) { 255 | reader.beginArray(); 256 | while(reader.hasNext()) { 257 | seriesResult.addData(reader.nextString()); 258 | } 259 | reader.endArray(); 260 | } 261 | } 262 | reader.endObject(); 263 | seriesResult.setStatus(status); 264 | return seriesResult; 265 | } 266 | 267 | }).create(); 268 | return gson.fromJson(jsonString, DefaultLabelResult.class); 269 | 270 | } 271 | 272 | 273 | @SuppressWarnings("unchecked") 274 | public static DefaultSeriesResult convertSeriesResultString(String jsonString) { 275 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultSeriesResult.class, new TypeAdapter() { 276 | 277 | @Override 278 | public void write(JsonWriter out, DefaultSeriesResult value) throws IOException { 279 | 280 | } 281 | 282 | @Override 283 | public DefaultSeriesResult read(JsonReader reader) throws IOException { 284 | DefaultSeriesResult seriesResult = new DefaultSeriesResult(); 285 | String status = null; 286 | reader.beginObject(); 287 | while(reader.hasNext()) { 288 | String propertyName = reader.nextName(); 289 | if("status".equals(propertyName)) { 290 | status = reader.nextString(); 291 | } else if("data".equals(propertyName)) { 292 | reader.beginArray(); 293 | while(reader.hasNext()) { 294 | SeriesResultItem seriesData = new SeriesResultItem(); 295 | reader.beginObject(); 296 | while(reader.hasNext()) { 297 | seriesData.put(reader.nextName(), reader.nextString()); 298 | } 299 | reader.endObject(); 300 | seriesResult.addData(seriesData); 301 | // System.out.println(seriesData); 302 | } 303 | reader.endArray(); 304 | } 305 | } 306 | reader.endObject(); 307 | seriesResult.setStatus(status); 308 | return seriesResult; 309 | } 310 | 311 | }).create(); 312 | return gson.fromJson(jsonString, DefaultSeriesResult.class); 313 | 314 | } 315 | 316 | @SuppressWarnings("unchecked") 317 | public static DefaultQueryResult convertQueryResultString(String jsonString) { 318 | Gson gson = new GsonBuilder().registerTypeAdapter(DefaultQueryResult.class, new TypeAdapter>() { 319 | @Override 320 | public DefaultQueryResult read(JsonReader reader) throws IOException { 321 | DefaultQueryResult queryResult = new DefaultQueryResult(); 322 | String status = null; 323 | String resultType = null; 324 | reader.beginObject(); 325 | while(reader.hasNext()) { 326 | String propertyName = reader.nextName(); 327 | if("status".equals(propertyName)) { 328 | status = reader.nextString(); 329 | } else if("data".equals(propertyName)) { 330 | reader.beginObject(); 331 | while(reader.hasNext()) { 332 | propertyName = reader.nextName(); 333 | if("resultType".equals(propertyName)) { 334 | resultType = reader.nextString(); 335 | } else if("result".equals(propertyName)) { 336 | System.out.println("resultType:"+ resultType); 337 | reader.beginArray(); 338 | while(reader.hasNext()) { 339 | queryResult.addData((T) searchDataType(resultType).convert(reader)); 340 | } 341 | reader.endArray(); 342 | } 343 | } 344 | reader.endObject(); 345 | } 346 | } 347 | reader.endObject(); 348 | queryResult.setStatus(status); 349 | queryResult.setResultType(resultType); 350 | return queryResult; 351 | } 352 | 353 | @Override 354 | public void write(JsonWriter arg0, DefaultQueryResult arg1) throws IOException { 355 | } 356 | }).create(); 357 | return gson.fromJson(jsonString, DefaultQueryResult.class); 358 | } 359 | 360 | } 361 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/Data.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter; 2 | 3 | public interface Data { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/Result.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter; 2 | 3 | import java.util.List; 4 | 5 | public abstract class Result{ 6 | String status = null; 7 | String resultType = null; 8 | public String getStatus() { 9 | return status; 10 | } 11 | public void setStatus(String status) { 12 | this.status = status; 13 | } 14 | public String getResultType() { 15 | return resultType; 16 | } 17 | public void setResultType(String resultType) { 18 | this.resultType = resultType; 19 | } 20 | 21 | public abstract List getResult(); 22 | } 23 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/am/AlertManagerResultItem.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.am; 2 | 3 | 4 | public class AlertManagerResultItem { 5 | 6 | private String url; 7 | 8 | public String getUrl() { 9 | return url; 10 | } 11 | 12 | public void setUrl(String url) { 13 | this.url = url; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "AlertManagerResultItem [url=" + url + "]"; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/am/DefaultAlertManagerResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.am; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Result; 7 | 8 | public class DefaultAlertManagerResult extends Result{ 9 | List activeAlertmanagers = new ArrayList(); 10 | List droppedAlertmanagers = new ArrayList(); 11 | public void addActiveManager(AlertManagerResultItem data) { 12 | activeAlertmanagers.add(data); 13 | } 14 | 15 | public void addDroppedManager(AlertManagerResultItem data) { 16 | droppedAlertmanagers.add(data); 17 | } 18 | 19 | @Override 20 | public List getResult() { 21 | return activeAlertmanagers; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "TargetResultItem [activeAM=" + activeAlertmanagers + ",droppedAM="+droppedAlertmanagers+"]"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/label/DefaultLabelResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.label; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Result; 7 | 8 | public class DefaultLabelResult extends Result{ 9 | List result = new ArrayList(); 10 | public void addData(String data) { 11 | result.add(data); 12 | } 13 | 14 | @Override 15 | public List getResult() { 16 | return result; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "DefaultLabelResult [result=" + result + "]"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/DefaultQueryResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Data; 7 | import com.bdwise.prometheus.client.converter.Result; 8 | 9 | 10 | public class DefaultQueryResult extends Result{ 11 | 12 | List result = new ArrayList(); 13 | public void addData(T data) { 14 | result.add(data); 15 | } 16 | 17 | @Override 18 | public List getResult() { 19 | return result; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "DefaultQueryResult [result=" + result + "]"; 25 | } 26 | 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/MatrixData.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Arrays; 5 | import java.util.Date; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import com.bdwise.prometheus.client.converter.Data; 10 | 11 | 12 | 13 | public class MatrixData implements Data { 14 | 15 | private Map metric = new HashMap(); 16 | 17 | private QueryResultItemValue[] dataValues; 18 | 19 | 20 | public Map getMetric() { 21 | return metric; 22 | } 23 | 24 | public void setMetric(Map metric) { 25 | this.metric = metric; 26 | } 27 | 28 | public QueryResultItemValue[] getDataValues() { 29 | return dataValues; 30 | } 31 | 32 | public void setDataValues(QueryResultItemValue[] values) { 33 | this.dataValues = values; 34 | } 35 | 36 | 37 | public double[] getValues() { 38 | double[] values = new double[dataValues.length]; 39 | int index = 0; 40 | for(QueryResultItemValue dataValue : dataValues) { 41 | values[index++] = dataValue.getValue(); 42 | } 43 | return values; 44 | } 45 | 46 | public double[] getTimestamps() { 47 | double[] timestamps = new double[dataValues.length]; 48 | int index = 0; 49 | for(QueryResultItemValue dataValue : dataValues) { 50 | timestamps[index++] = dataValue.getTimestamp(); 51 | } 52 | return timestamps; 53 | } 54 | 55 | public String[] getFormattedTimestamps(String format) { 56 | SimpleDateFormat formatter = new SimpleDateFormat(format); 57 | String[] timestamps = new String[dataValues.length]; 58 | int index = 0; 59 | for(QueryResultItemValue dataValue : dataValues) { 60 | timestamps[index++] = formatter.format(new Date(Math.round(dataValue.getTimestamp()*1000L))); 61 | } 62 | return timestamps; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "MatrixData [metric=" + metric + ", dataValues=" + Arrays.toString(dataValues) + "]"; 68 | } 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/QueryDataType.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import com.bdwise.prometheus.client.converter.Data; 9 | import com.google.gson.stream.JsonReader; 10 | 11 | public enum QueryDataType { 12 | Matrix{ 13 | 14 | @SuppressWarnings("unchecked") 15 | @Override 16 | public MatrixData convert(JsonReader reader) throws IOException { 17 | MatrixData resultDataItem = new MatrixData(); 18 | reader.beginObject(); 19 | while(reader.hasNext()) { 20 | String name = reader.nextName(); 21 | if("metric".equalsIgnoreCase(name)) { 22 | Map metric = new HashMap(); 23 | reader.beginObject(); 24 | while(reader.hasNext()) { 25 | metric.put(reader.nextName(), reader.nextString()); 26 | } 27 | reader.endObject(); 28 | resultDataItem.setMetric(metric); 29 | } else if("values".equalsIgnoreCase(name)) { 30 | ArrayList resultDataItemValue = new ArrayList(); 31 | reader.beginArray(); 32 | while(reader.hasNext()) { 33 | reader.beginArray(); 34 | resultDataItemValue.add(new QueryResultItemValue(reader.nextDouble(), reader.nextDouble())); 35 | reader.endArray(); 36 | } 37 | reader.endArray(); 38 | resultDataItem.setDataValues(resultDataItemValue.toArray(new QueryResultItemValue[] {})); 39 | } 40 | } 41 | reader.endObject(); 42 | return resultDataItem; 43 | } 44 | 45 | }, 46 | Vector{ 47 | 48 | @SuppressWarnings("unchecked") 49 | @Override 50 | public VectorData convert(JsonReader reader) throws IOException { 51 | VectorData resultDataItem = new VectorData(); 52 | reader.beginObject(); 53 | while(reader.hasNext()) { 54 | String name = reader.nextName(); 55 | if("metric".equalsIgnoreCase(name)) { 56 | Map metric = new HashMap(); 57 | reader.beginObject(); 58 | while(reader.hasNext()) { 59 | metric.put(reader.nextName(), reader.nextString()); 60 | } 61 | reader.endObject(); 62 | resultDataItem.setMetric(metric); 63 | } else if("value".equalsIgnoreCase(name)) { 64 | reader.beginArray(); 65 | resultDataItem.setDataValue(new QueryResultItemValue(reader.nextDouble(), reader.nextDouble())); 66 | reader.endArray(); 67 | } 68 | } 69 | reader.endObject(); 70 | return resultDataItem; 71 | } 72 | 73 | }, 74 | Scalar{ 75 | 76 | @SuppressWarnings("unchecked") 77 | @Override 78 | public ScalarData convert(JsonReader reader) throws IOException { 79 | ScalarData resultDataItem = null; 80 | resultDataItem = new ScalarData(reader.nextDouble(), reader.nextDouble()); 81 | return resultDataItem; 82 | } 83 | 84 | }; 85 | public abstract T convert(JsonReader reader) throws IOException ; 86 | } 87 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/QueryResultItemValue.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | public class QueryResultItemValue { 4 | private double timestamp; 5 | private double value; 6 | public QueryResultItemValue(double timestamp, double value) { 7 | super(); 8 | this.timestamp = timestamp; 9 | this.value = value; 10 | } 11 | public double getTimestamp() { 12 | return timestamp; 13 | } 14 | public void setTimestamp(double timestamp) { 15 | this.timestamp = timestamp; 16 | } 17 | public double getValue() { 18 | return value; 19 | } 20 | public void setValue(double value) { 21 | this.value = value; 22 | } 23 | @Override 24 | public String toString() { 25 | return "QueryResultItemValue [timestamp=" + timestamp + ", value=" + value + "]"; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/ScalarData.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.Data; 4 | 5 | public class ScalarData extends QueryResultItemValue implements Data { 6 | 7 | public ScalarData(double timestamp, double value) { 8 | super(timestamp, value); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/query/VectorData.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.query; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import com.bdwise.prometheus.client.converter.Data; 9 | 10 | 11 | public class VectorData implements Data { 12 | 13 | private Map metric = new HashMap(); 14 | 15 | private QueryResultItemValue dataValue; 16 | 17 | 18 | public Map getMetric() { 19 | return metric; 20 | } 21 | 22 | public void setMetric(Map metric) { 23 | this.metric = metric; 24 | } 25 | 26 | public QueryResultItemValue getDataValue() { 27 | return dataValue; 28 | } 29 | 30 | public void setDataValue(QueryResultItemValue value) { 31 | this.dataValue = value; 32 | } 33 | 34 | 35 | public double getValue() { 36 | return dataValue.getValue(); 37 | } 38 | 39 | public double getTimestamps() { 40 | return dataValue.getTimestamp(); 41 | } 42 | 43 | public String getFormattedTimestamps(String format) { 44 | SimpleDateFormat formatter = new SimpleDateFormat(format); 45 | return formatter.format(new Date(Math.round(dataValue.getTimestamp()*1000))); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "VectorData [metric=" + metric + ", dataValue=" + dataValue + "]"; 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/series/DefaultSeriesResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.series; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Result; 7 | 8 | public class DefaultSeriesResult extends Result{ 9 | List result = new ArrayList(); 10 | public void addData(SeriesResultItem data) { 11 | result.add(data); 12 | } 13 | 14 | @Override 15 | public List getResult() { 16 | return result; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "SeriesResultItem [result=" + result + "]"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/series/SeriesResultItem.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.series; 2 | 3 | import java.util.HashMap; 4 | 5 | import com.bdwise.prometheus.client.converter.Data; 6 | 7 | public class SeriesResultItem extends HashMap implements Data { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/status/DefaultConfigResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.status; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Result; 7 | 8 | public class DefaultConfigResult extends Result{ 9 | List result = new ArrayList(); 10 | public void addData(String data) { 11 | result.add(data); 12 | } 13 | 14 | @Override 15 | public List getResult() { 16 | return result; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "DefaultConfigResult [result=" + result + "]"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/target/DefaultTargetResult.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.target; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.bdwise.prometheus.client.converter.Result; 7 | 8 | public class DefaultTargetResult extends Result{ 9 | List activeTargets = new ArrayList(); 10 | List droppedTargets = new ArrayList(); 11 | public void addActiveTarget(TargetResultItem data) { 12 | activeTargets.add(data); 13 | } 14 | 15 | public void addDroppedTarget(TargetResultItem data) { 16 | droppedTargets.add(data); 17 | } 18 | 19 | @Override 20 | public List getResult() { 21 | return activeTargets; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "TargetResultItem [activeTargets=" + activeTargets + ",droppedTargets="+droppedTargets+"]"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /promql_client/src/main/java/com/bdwise/prometheus/client/converter/target/TargetResultItem.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.converter.target; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class TargetResultItem { 7 | private Map discoveredLabels = new HashMap(); 8 | private Map label = new HashMap(); 9 | private String scrapeUrl; 10 | private String lastError=""; 11 | private String lastScrape; 12 | private String health="UNKNOWN"; 13 | 14 | public Map getDiscoveredLabels() { 15 | return discoveredLabels; 16 | } 17 | public void setDiscoveredLabels(Map discoveredLabels) { 18 | this.discoveredLabels = discoveredLabels; 19 | } 20 | public Map getLabel() { 21 | return label; 22 | } 23 | public void setLabel(Map label) { 24 | this.label = label; 25 | } 26 | public String getScrapeUrl() { 27 | return scrapeUrl; 28 | } 29 | public void setScrapeUrl(String scrapeUrl) { 30 | this.scrapeUrl = scrapeUrl; 31 | } 32 | public String getLastError() { 33 | return lastError; 34 | } 35 | public void setLastError(String lastError) { 36 | this.lastError = lastError; 37 | } 38 | public String getLastScrape() { 39 | return lastScrape; 40 | } 41 | public void setLastScrape(String lastScrape) { 42 | this.lastScrape = lastScrape; 43 | } 44 | public String getHealth() { 45 | return health; 46 | } 47 | public void setHealth(String health) { 48 | this.health = health; 49 | } 50 | @Override 51 | public String toString() { 52 | return "TargetResultItem [discoveredLabels=" + discoveredLabels + ", label=" + label + ", scrapeUrl=" 53 | + scrapeUrl + ", lastError=" + lastError + ", lastScrape=" + lastScrape + ", health=" + health + "]"; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/builder/UriBuilderTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.builder; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URI; 5 | 6 | 7 | import junit.framework.TestCase; 8 | 9 | public class UriBuilderTest extends TestCase { 10 | 11 | public void testRangeQueryBuilder() throws MalformedURLException { 12 | RangeQueryBuilder rangeQueryBuilder = QueryBuilderType.RangeQuery.newInstance("http://52.192.4.59:30900"); 13 | URI targetUri = rangeQueryBuilder.withQuery("irate(received_api_call_total[60s])") 14 | .withStartEpochTime(System.currentTimeMillis() / 1000 - 60*10) 15 | .withEndEpochTime(System.currentTimeMillis() / 1000) 16 | .withStepTime("60s") 17 | .build(); 18 | 19 | System.out.println(targetUri.toURL().toString()); 20 | } 21 | 22 | public void testInstantQueryBuilder() throws MalformedURLException { 23 | InstantQueryBuilder iqb = QueryBuilderType.InstantQuery.newInstance("http://52.192.4.59:30900"); 24 | URI targetUri = iqb.withQuery("irate(received_api_call_total[60s])").build(); 25 | System.out.println(targetUri.toURL().toString()); 26 | } 27 | 28 | public void testSeriesMetaQueryBuilder() throws MalformedURLException { 29 | SeriesMetaQueryBuilder smqb = QueryBuilderType.SeriesMetadaQuery.newInstance("http://52.192.4.59:30900"); 30 | URI targetUri = smqb.withSelector("match[]=up&match[]=process_start_time_seconds{job=\"prometheus\"}").build(); 31 | System.out.println(targetUri.toURL().toString()); 32 | } 33 | 34 | public void testLabelMetaQueryBuilder() throws MalformedURLException { 35 | LabelMetaQueryBuilder lmqb = QueryBuilderType.LabelMetadaQuery.newInstance("http://52.192.4.59:30900"); 36 | URI targetUri = lmqb.withLabel("pod").build(); 37 | System.out.println(targetUri.toURL().toString()); 38 | } 39 | 40 | public void testStatusMetaQueryBuilder() throws MalformedURLException { 41 | StatusMetaQueryBuilder smqb = QueryBuilderType.StatusMetadaQuery.newInstance("http://52.192.4.59:30900"); 42 | URI targetUri = smqb.build(); 43 | System.out.println(targetUri.toURL().toString()); 44 | } 45 | 46 | public void testTargetsMetaQueryBuilder() throws MalformedURLException { 47 | TargetMetaQueryBuilder tmqb = QueryBuilderType.TargetMetadaQuery.newInstance("http://52.192.4.59:30900"); 48 | URI targetUri = tmqb.build(); 49 | System.out.println(targetUri.toURL().toString()); 50 | } 51 | 52 | public void testAlertManagerMetaQueryBuilder() throws MalformedURLException { 53 | AlertManagerMetaQueryBuilder ammqb = QueryBuilderType.AlertManagerMetadaQuery.newInstance("http://52.192.4.59:30900"); 54 | URI targetUri = ammqb.build(); 55 | System.out.println(targetUri.toURL().toString()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/AlertManagerResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.am.AlertManagerResultItem; 5 | import com.bdwise.prometheus.client.converter.am.DefaultAlertManagerResult; 6 | 7 | import junit.framework.TestCase; 8 | 9 | public class AlertManagerResultTest extends TestCase { 10 | private String testAlertManagerData="{\"status\":\"success\",\"data\":{\"activeAlertmanagers\":[]}}"; 11 | 12 | public void testParser() { 13 | DefaultAlertManagerResult result = ConvertUtil.convertAlertManagerResultString(testAlertManagerData); 14 | System.out.println("-----" +result.getResult().size()); 15 | 16 | for(AlertManagerResultItem data : result.getResult()) { 17 | System.out.println("=======>\n" + data); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/ConfigResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.status.DefaultConfigResult; 5 | 6 | import junit.framework.TestCase; 7 | 8 | public class ConfigResultTest extends TestCase { 9 | private String testConfigData="{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 30s\\n scrape_timeout: 10s\\n evaluation_interval: 30s\\nscrape_configs:\\n- job_name: default/person-application-1.5-monitor/0\\n scrape_interval: 10s\\n scrape_timeout: 10s\\n metrics_path: /prometheus\\n scheme: http\\n kubernetes_sd_configs:\\n - api_server: null\\n role: endpoints\\n namespaces:\\n names:\\n - default\\n relabel_configs:\\n - source_labels: [__meta_kubernetes_service_label_run]\\n separator: ;\\n regex: person-application-1.5\\n replacement: $1\\n action: keep\\n - source_labels: [__meta_kubernetes_endpoint_port_name]\\n separator: ;\\n regex: http\\n replacement: $1\\n action: keep\\n - source_labels: [__meta_kubernetes_namespace]\\n separator: ;\\n regex: (.*)\\n target_label: namespace\\n replacement: $1\\n action: replace\\n - source_labels: [__meta_kubernetes_pod_name]\\n separator: ;\\n regex: (.*)\\n target_label: pod\\n replacement: $1\\n action: replace\\n - source_labels: [__meta_kubernetes_service_name]\\n separator: ;\\n regex: (.*)\\n target_label: service\\n replacement: $1\\n action: replace\\n - source_labels: [__meta_kubernetes_service_name]\\n separator: ;\\n regex: (.*)\\n target_label: job\\n replacement: ${1}\\n action: replace\\n - separator: ;\\n regex: (.*)\\n target_label: endpoint\\n replacement: http\\n action: replace\\n\"}}"; 10 | public void testParser() { 11 | DefaultConfigResult result = ConvertUtil.convertConfigResultString(testConfigData); 12 | System.out.println("-----" +result.getResult().size()); 13 | for(String data : result.getResult()) { 14 | System.out.println("=======>\n" + data); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/LabelResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.label.DefaultLabelResult; 5 | 6 | import junit.framework.TestCase; 7 | 8 | public class LabelResultTest extends TestCase { 9 | private String testLabelData="{\"status\":\"success\",\"data\":[\"person-application-1.5-5dcc65c754-7ztnz\",\"person-application-1.5-5dcc65c754-8gb82\",\"person-application-1.5-5dcc65c754-8xh22\"]}"; 10 | public void testParser() { 11 | DefaultLabelResult result = ConvertUtil.convertLabelResultString(testLabelData); 12 | System.out.println("-----" +result.getResult().size()); 13 | for(String data : result.getResult()) { 14 | System.out.println("=======>" + data); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/MatrixResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | 4 | import com.bdwise.prometheus.client.converter.ConvertUtil; 5 | import com.bdwise.prometheus.client.converter.Data; 6 | import com.bdwise.prometheus.client.converter.query.DefaultQueryResult; 7 | import com.bdwise.prometheus.client.converter.query.ScalarData; 8 | 9 | import junit.framework.TestCase; 10 | 11 | public class MatrixResultTest extends TestCase { 12 | 13 | private String testScalarData="{\"status\":\"success\",\"data\":{\"resultType\":\"scalar\",\"result\":[1536200364.286,\"1\"]}}"; 14 | 15 | public void testParser() { 16 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(testScalarData); 17 | System.out.println("-----" +result.getResult().size()); 18 | for(Data data : result.getResult()) { 19 | System.out.println("=======>" + data); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/ScalarResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.Data; 5 | import com.bdwise.prometheus.client.converter.query.DefaultQueryResult; 6 | import com.bdwise.prometheus.client.converter.query.VectorData; 7 | 8 | import junit.framework.TestCase; 9 | 10 | public class ScalarResultTest extends TestCase { 11 | private String testVectorData="{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-7ztnz\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-7ztnz\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8gb82\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8gb82\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8xh22\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8xh22\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]}]}}"; 12 | 13 | public void testParser() { 14 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(testVectorData); 15 | System.out.println("-----" +result.getResult().size()); 16 | for(Data data : result.getResult()) { 17 | System.out.println("=======>" + data); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/SeriesResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.Data; 5 | import com.bdwise.prometheus.client.converter.series.DefaultSeriesResult; 6 | 7 | import junit.framework.TestCase; 8 | 9 | public class SeriesResultTest extends TestCase { 10 | private String testSeriesData="{\"status\":\"success\",\"data\":[{\"__name__\":\"up\",\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\"},{\"__name__\":\"up\",\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\"},{\"__name__\":\"up\",\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\"}]}"; 11 | public void testParser() { 12 | DefaultSeriesResult result = ConvertUtil.convertSeriesResultString(testSeriesData); 13 | System.out.println("-----" +result.getResult().size()); 14 | for(Data data : result.getResult()) { 15 | System.out.println("=======>" + data); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/TargetResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.status.DefaultConfigResult; 5 | import com.bdwise.prometheus.client.converter.target.DefaultTargetResult; 6 | import com.bdwise.prometheus.client.converter.target.TargetResultItem; 7 | 8 | import junit.framework.TestCase; 9 | 10 | public class TargetResultTest extends TestCase { 11 | private String testTargetData="{\"status\":\"success\",\"data\":{\"activeTargets\":[{\"discoveredLabels\":{\"__address__\":\"10.244.2.4:19000\",\"__meta_kubernetes_endpoint_port_name\":\"http\",\"__meta_kubernetes_endpoint_port_protocol\":\"TCP\",\"__meta_kubernetes_endpoint_ready\":\"true\",\"__meta_kubernetes_endpoints_name\":\"person-application-1-5\",\"__meta_kubernetes_namespace\":\"default\",\"__meta_kubernetes_pod_container_name\":\"person-application\",\"__meta_kubernetes_pod_container_port_name\":\"http\",\"__meta_kubernetes_pod_container_port_number\":\"19000\",\"__meta_kubernetes_pod_container_port_protocol\":\"TCP\",\"__meta_kubernetes_pod_host_ip\":\"10.0.0.13\",\"__meta_kubernetes_pod_ip\":\"10.244.2.4\",\"__meta_kubernetes_pod_label_pod_template_hash\":\"1877217310\",\"__meta_kubernetes_pod_label_run\":\"person-application-1.5\",\"__meta_kubernetes_pod_name\":\"person-application-1.5-5dcc65c754-8gb82\",\"__meta_kubernetes_pod_node_name\":\"ip-10-0-0-13\",\"__meta_kubernetes_pod_ready\":\"true\",\"__meta_kubernetes_pod_uid\":\"0e01dd3b-b0f7-11e8-9bc7-0e371c97e2e6\",\"__meta_kubernetes_service_label_run\":\"person-application-1.5\",\"__meta_kubernetes_service_name\":\"person-application-1-5\",\"__metrics_path__\":\"/prometheus\",\"__scheme__\":\"http\",\"job\":\"default/person-application-1.5-monitor/0\"},\"labels\":{\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\"},\"scrapeUrl\":\"http://10.244.2.4:19000/prometheus\",\"lastError\":\"\",\"lastScrape\":\"2018-09-07T07:44:34.004445574Z\",\"health\":\"up\"},{\"discoveredLabels\":{\"__address__\":\"10.244.4.4:19000\",\"__meta_kubernetes_endpoint_port_name\":\"http\",\"__meta_kubernetes_endpoint_port_protocol\":\"TCP\",\"__meta_kubernetes_endpoint_ready\":\"true\",\"__meta_kubernetes_endpoints_name\":\"person-application-1-5\",\"__meta_kubernetes_namespace\":\"default\",\"__meta_kubernetes_pod_container_name\":\"person-application\",\"__meta_kubernetes_pod_container_port_name\":\"http\",\"__meta_kubernetes_pod_container_port_number\":\"19000\",\"__meta_kubernetes_pod_container_port_protocol\":\"TCP\",\"__meta_kubernetes_pod_host_ip\":\"10.0.0.123\",\"__meta_kubernetes_pod_ip\":\"10.244.4.4\",\"__meta_kubernetes_pod_label_pod_template_hash\":\"1877217310\",\"__meta_kubernetes_pod_label_run\":\"person-application-1.5\",\"__meta_kubernetes_pod_name\":\"person-application-1.5-5dcc65c754-7ztnz\",\"__meta_kubernetes_pod_node_name\":\"ip-10-0-0-123\",\"__meta_kubernetes_pod_ready\":\"true\",\"__meta_kubernetes_pod_uid\":\"0e02c3fe-b0f7-11e8-9bc7-0e371c97e2e6\",\"__meta_kubernetes_service_label_run\":\"person-application-1.5\",\"__meta_kubernetes_service_name\":\"person-application-1-5\",\"__metrics_path__\":\"/prometheus\",\"__scheme__\":\"http\",\"job\":\"default/person-application-1.5-monitor/0\"},\"labels\":{\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\"},\"scrapeUrl\":\"http://10.244.4.4:19000/prometheus\",\"lastError\":\"\",\"lastScrape\":\"2018-09-07T07:44:34.287821422Z\",\"health\":\"up\"},{\"discoveredLabels\":{\"__address__\":\"10.244.1.4:19000\",\"__meta_kubernetes_endpoint_port_name\":\"http\",\"__meta_kubernetes_endpoint_port_protocol\":\"TCP\",\"__meta_kubernetes_endpoint_ready\":\"true\",\"__meta_kubernetes_endpoints_name\":\"person-application-1-5\",\"__meta_kubernetes_namespace\":\"default\",\"__meta_kubernetes_pod_container_name\":\"person-application\",\"__meta_kubernetes_pod_container_port_name\":\"http\",\"__meta_kubernetes_pod_container_port_number\":\"19000\",\"__meta_kubernetes_pod_container_port_protocol\":\"TCP\",\"__meta_kubernetes_pod_host_ip\":\"10.0.0.70\",\"__meta_kubernetes_pod_ip\":\"10.244.1.4\",\"__meta_kubernetes_pod_label_pod_template_hash\":\"1877217310\",\"__meta_kubernetes_pod_label_run\":\"person-application-1.5\",\"__meta_kubernetes_pod_name\":\"person-application-1.5-5dcc65c754-8xh22\",\"__meta_kubernetes_pod_node_name\":\"ip-10-0-0-70\",\"__meta_kubernetes_pod_ready\":\"true\",\"__meta_kubernetes_pod_uid\":\"0e02af44-b0f7-11e8-9bc7-0e371c97e2e6\",\"__meta_kubernetes_service_label_run\":\"person-application-1.5\",\"__meta_kubernetes_service_name\":\"person-application-1-5\",\"__metrics_path__\":\"/prometheus\",\"__scheme__\":\"http\",\"job\":\"default/person-application-1.5-monitor/0\"},\"labels\":{\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\"},\"scrapeUrl\":\"http://10.244.1.4:19000/prometheus\",\"lastError\":\"\",\"lastScrape\":\"2018-09-07T07:44:36.886727015Z\",\"health\":\"up\"}]}}"; 12 | public void testParser() { 13 | DefaultTargetResult result = ConvertUtil.convertTargetResultString(testTargetData); 14 | System.out.println("-----" +result.getResult().size()); 15 | for(TargetResultItem data : result.getResult()) { 16 | System.out.println("=======>\n" + data); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /promql_client/src/test/java/com/bdwise/prometheus/client/comverter/query/VectorResultTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client.comverter.query; 2 | 3 | import com.bdwise.prometheus.client.converter.ConvertUtil; 4 | import com.bdwise.prometheus.client.converter.Data; 5 | import com.bdwise.prometheus.client.converter.query.DefaultQueryResult; 6 | import com.bdwise.prometheus.client.converter.query.VectorData; 7 | 8 | import junit.framework.TestCase; 9 | 10 | public class VectorResultTest extends TestCase { 11 | private String testVectorData="{\"status\":\"success\",\"data\":{\"resultType\":\"vector\",\"result\":[{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-7ztnz\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-7ztnz\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.4.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-7ztnz\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8gb82\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8gb82\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.2.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8gb82\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8xh22\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"*\",\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\",\"url\":\"*\"},\"value\":[1536200364.286,\"0\"]},{\"metric\":{\"POD\":\"person-application-1.5-5dcc65c754-8xh22\",\"application\":\"person-1.5\\t\\t\\t\\t\\t\\t\\t\\t\",\"controller\":\"com.satish.monitoring.web.rest.PersonResource\",\"endpoint\":\"http\",\"instance\":\"10.244.1.4:19000\",\"job\":\"person-application-1-5\",\"namespace\":\"default\",\"pod\":\"person-application-1.5-5dcc65c754-8xh22\",\"service\":\"person-application-1-5\",\"url\":\"[GET]/person\"},\"value\":[1536200364.286,\"0\"]}]}}"; 12 | 13 | public void testParser() { 14 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(testVectorData); 15 | System.out.println("-----" +result.getResult().size()); 16 | for(Data data : result.getResult()) { 17 | System.out.println("=======>" + data); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /promql_client/target/classes/com/bdwise/prometheus/client/converter/query/DefaultQueryResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mckang/promql_java_client/950646283470cb1c91fe649d9ce915ec97ec5b60/promql_client/target/classes/com/bdwise/prometheus/client/converter/query/DefaultQueryResult.class -------------------------------------------------------------------------------- /promql_client/target/classes/com/bdwise/prometheus/client/converter/query/QueryResultItemValue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mckang/promql_java_client/950646283470cb1c91fe649d9ce915ec97ec5b60/promql_client/target/classes/com/bdwise/prometheus/client/converter/query/QueryResultItemValue.class -------------------------------------------------------------------------------- /promql_client_test/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.bdwise.prometheus.client 6 | promql_client_test 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | promql_client_test 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | com.bdwise.prometheus.client 20 | promql_client 21 | 0.1-SNAPSHOT 22 | 23 | 24 | org.apache.httpcomponents 25 | httpclient 26 | 4.5.13 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 1.5.6.RELEASE 32 | 33 | 34 | junit 35 | junit 36 | 4.13.1 37 | test 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /promql_client_test/src/test/java/com/bdwise/prometheus/client/PromqlTest.java: -------------------------------------------------------------------------------- 1 | package com.bdwise.prometheus.client; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URI; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | import org.apache.http.client.HttpClient; 9 | import org.apache.http.impl.client.HttpClientBuilder; 10 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 11 | import org.springframework.web.client.RestTemplate; 12 | 13 | import com.bdwise.prometheus.client.builder.AlertManagerMetaQueryBuilder; 14 | import com.bdwise.prometheus.client.builder.InstantQueryBuilder; 15 | import com.bdwise.prometheus.client.builder.LabelMetaQueryBuilder; 16 | import com.bdwise.prometheus.client.builder.QueryBuilderType; 17 | import com.bdwise.prometheus.client.builder.RangeQueryBuilder; 18 | import com.bdwise.prometheus.client.builder.StatusMetaQueryBuilder; 19 | import com.bdwise.prometheus.client.builder.TargetMetaQueryBuilder; 20 | import com.bdwise.prometheus.client.converter.ConvertUtil; 21 | import com.bdwise.prometheus.client.converter.am.DefaultAlertManagerResult; 22 | import com.bdwise.prometheus.client.converter.label.DefaultLabelResult; 23 | import com.bdwise.prometheus.client.converter.query.DefaultQueryResult; 24 | import com.bdwise.prometheus.client.converter.query.MatrixData; 25 | import com.bdwise.prometheus.client.converter.query.QueryResultItemValue; 26 | import com.bdwise.prometheus.client.converter.query.VectorData; 27 | import com.bdwise.prometheus.client.converter.status.DefaultConfigResult; 28 | import com.bdwise.prometheus.client.converter.target.DefaultTargetResult; 29 | 30 | import junit.framework.TestCase; 31 | 32 | public class PromqlTest extends TestCase { 33 | private final static String TARGET_SERVER = "http://192.168.66.60:9090"; 34 | 35 | private RestTemplate template = null; 36 | 37 | @Override 38 | protected void setUp() throws Exception { 39 | HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); 40 | httpRequestFactory.setConnectTimeout(1000); 41 | httpRequestFactory.setReadTimeout(2000); 42 | HttpClient httpClient = HttpClientBuilder.create() 43 | .setMaxConnTotal(100) 44 | .setMaxConnPerRoute(10) 45 | .build(); 46 | httpRequestFactory.setHttpClient(httpClient); 47 | 48 | template = new RestTemplate(httpRequestFactory); 49 | } 50 | 51 | private static String ConvertEpocToFormattedDate(String format, double epocTime) { 52 | SimpleDateFormat formatter = new SimpleDateFormat(format); 53 | return formatter.format(new Date(Math.round(epocTime*1000))); 54 | } 55 | 56 | public void testSimpleRangeQuery() throws MalformedURLException { 57 | RangeQueryBuilder rangeQueryBuilder = QueryBuilderType.RangeQuery.newInstance(TARGET_SERVER); 58 | URI targetUri = rangeQueryBuilder.withQuery("100 - avg(rate(node_cpu{application=\"node_exporter\", mode=\"idle\"}[1m])) by (instance)*100") 59 | .withStartEpochTime(System.currentTimeMillis() / 1000 - 60*10) 60 | .withEndEpochTime(System.currentTimeMillis() / 1000) 61 | .withStepTime("60s") 62 | .build(); 63 | 64 | System.out.println(targetUri.toURL().toString()); 65 | 66 | String rtVal = template.getForObject(targetUri, String.class); 67 | 68 | 69 | 70 | 71 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 72 | 73 | for(MatrixData matrixData : result.getResult()) { 74 | System.out.println(String.format("%s", matrixData.getMetric().get("instance"))); 75 | for(QueryResultItemValue itemValue : matrixData.getDataValues()) { 76 | System.out.println(String.format("%s %10.2f ", 77 | ConvertEpocToFormattedDate("yyyy-MM-dd hh:mm:ss", itemValue.getTimestamp()), 78 | itemValue.getValue() 79 | )); 80 | } 81 | } 82 | 83 | } 84 | 85 | public void testSimpleVectorQuery() throws MalformedURLException { 86 | InstantQueryBuilder iqb = QueryBuilderType.InstantQuery.newInstance(TARGET_SERVER); 87 | URI targetUri = iqb.withQuery("node_cpu{application=\"node_exporter\", mode=\"idle\"}[1m]").build(); 88 | 89 | 90 | 91 | String rtVal = template.getForObject(targetUri, String.class); 92 | 93 | 94 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 95 | 96 | 97 | for(MatrixData matrixData : result.getResult()) { 98 | System.out.println(String.format("%s", matrixData.getMetric().get("instance"))); 99 | for(QueryResultItemValue itemValue : matrixData.getDataValues()) { 100 | System.out.println(String.format("%s %10.2f ", 101 | ConvertEpocToFormattedDate("yyyy-MM-dd hh:mm:ss", itemValue.getTimestamp()), 102 | itemValue.getValue() 103 | )); 104 | } 105 | } 106 | 107 | System.out.println(targetUri.toURL().toString()); 108 | // System.out.println(result); 109 | } 110 | 111 | public void testSimpleInstantQuery() throws MalformedURLException { 112 | InstantQueryBuilder iqb = QueryBuilderType.InstantQuery.newInstance(TARGET_SERVER); 113 | URI targetUri = iqb.withQuery("100 - avg(rate(node_cpu{application=\"node_exporter\", mode=\"idle\"}[1m])) by (instance)*100").build(); 114 | System.out.println(targetUri.toURL().toString()); 115 | 116 | 117 | String rtVal = template.getForObject(targetUri, String.class); 118 | 119 | 120 | DefaultQueryResult result = ConvertUtil.convertQueryResultString(rtVal); 121 | 122 | 123 | for(VectorData vectorData : result.getResult()) { 124 | System.out.println(String.format("%s %s %10.2f", 125 | vectorData.getMetric().get("instance"), 126 | vectorData.getFormattedTimestamps("yyyy-MM-dd hh:mm:ss"), 127 | vectorData.getValue() )); 128 | } 129 | 130 | System.out.println(result); 131 | } 132 | 133 | public void testSimpleLabel() throws MalformedURLException { 134 | LabelMetaQueryBuilder lmqb = QueryBuilderType.LabelMetadaQuery.newInstance(TARGET_SERVER); 135 | URI targetUri = lmqb.withLabel("pod").build(); 136 | System.out.println(targetUri.toURL().toString()); 137 | 138 | 139 | String rtVal = template.getForObject(targetUri, String.class); 140 | 141 | 142 | DefaultLabelResult result = ConvertUtil.convertLabelResultString(rtVal); 143 | 144 | 145 | System.out.println(result); 146 | } 147 | 148 | public void testSimpleConfig() throws MalformedURLException { 149 | StatusMetaQueryBuilder smqb = QueryBuilderType.StatusMetadaQuery.newInstance(TARGET_SERVER); 150 | URI targetUri = smqb.build(); 151 | System.out.println(targetUri.toURL().toString()); 152 | 153 | 154 | String rtVal = template.getForObject(targetUri, String.class); 155 | 156 | 157 | DefaultConfigResult result = ConvertUtil.convertConfigResultString(rtVal); 158 | 159 | 160 | System.out.println(result); 161 | } 162 | 163 | public void testSimpleTargets() throws MalformedURLException { 164 | TargetMetaQueryBuilder tmqb = QueryBuilderType.TargetMetadaQuery.newInstance(TARGET_SERVER); 165 | URI targetUri = tmqb.build(); 166 | System.out.println(targetUri.toURL().toString()); 167 | 168 | 169 | String rtVal = template.getForObject(targetUri, String.class); 170 | 171 | 172 | DefaultTargetResult result = ConvertUtil.convertTargetResultString(rtVal); 173 | 174 | 175 | System.out.println(result); 176 | } 177 | 178 | public void testSimpleAlertManager() throws MalformedURLException { 179 | AlertManagerMetaQueryBuilder ammqb = QueryBuilderType.AlertManagerMetadaQuery.newInstance(TARGET_SERVER); 180 | URI targetUri = ammqb.build(); 181 | System.out.println(targetUri.toURL().toString()); 182 | 183 | 184 | String rtVal = template.getForObject(targetUri, String.class); 185 | 186 | 187 | DefaultAlertManagerResult result = ConvertUtil.convertAlertManagerResultString(rtVal); 188 | 189 | 190 | System.out.println(result); 191 | } 192 | } 193 | --------------------------------------------------------------------------------