Skip to content

Commit 87067f1

Browse files
authored
Support creating/updating prometheus rules (#5228)
* Support creating/updating prometheus rules Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Merge delete resources to write resources Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Add error handling improvements Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Fix bugs in the code and support a different ruler uri Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Add support to delete silences and fetch alert manager status Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Fix instant query and address resource leak issue Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Resolve test issue Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> * Additional fixes Signed-off-by: Ashish Agrawal <ashisagr@amazon.com> --------- Signed-off-by: Ashish Agrawal <ashisagr@amazon.com>
1 parent e7e1d98 commit 87067f1

19 files changed

Lines changed: 1728 additions & 137 deletions

direct-query-core/src/main/java/org/opensearch/sql/datasource/query/QueryHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* Interface for handling queries for specific data source types.
1919
*
2020
* @param <T> The client type this handler works with, extending DataSourceClient
21-
*
2221
* @opensearch.experimental
2322
*/
2423
public interface QueryHandler<T extends DataSourceClient> {
@@ -52,15 +51,15 @@ GetDirectQueryResourcesResponse<?> getResources(T client, GetDirectQueryResource
5251
throws IOException;
5352

5453
/**
55-
* Writes resources to the data source.
54+
* Writes resources to the data source. Supports create, update, and delete operations.
5655
*
5756
* @param client The client instance to use
5857
* @param request The resources request
59-
* @return Response containing the requested resources
60-
* @throws IOException If resource retrieval fails
58+
* @return Response containing the result
59+
* @throws IOException If the operation fails
6160
*/
62-
WriteDirectQueryResourcesResponse<?> writeResources(T client, WriteDirectQueryResourcesRequest request)
63-
throws IOException;
61+
WriteDirectQueryResourcesResponse<?> writeResources(
62+
T client, WriteDirectQueryResourcesRequest request) throws IOException;
6463

6564
/**
6665
* Checks if this handler can handle the given client type.

direct-query-core/src/main/java/org/opensearch/sql/directquery/DirectQueryExecutorService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ GetDirectQueryResourcesResponse<?> getDirectQueryResources(
3535
GetDirectQueryResourcesRequest request);
3636

3737
/**
38-
* Write resources to a data source.
38+
* Write resources to a data source. Supports create, update, and delete operations.
3939
*
4040
* @param request The resources request
41-
* @return A response containing the resources to create
41+
* @return A response containing the result
4242
*/
4343
WriteDirectQueryResourcesResponse<?> writeDirectQueryResources(
44-
WriteDirectQueryResourcesRequest request);
44+
WriteDirectQueryResourcesRequest request);
4545
}

direct-query-core/src/main/java/org/opensearch/sql/directquery/DirectQueryExecutorServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,5 @@ public WriteDirectQueryResourcesResponse<?> writeDirectQueryResources(
118118
"Unsupported data source type: " + request.getDataSource())
119119
);
120120
}
121+
121122
}

direct-query-core/src/main/java/org/opensearch/sql/directquery/rest/model/DirectQueryResourceType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public enum DirectQueryResourceType {
2121
ALERTMANAGER_ALERTS,
2222
ALERTMANAGER_ALERT_GROUPS,
2323
ALERTMANAGER_RECEIVERS,
24-
ALERTMANAGER_SILENCES;
24+
ALERTMANAGER_SILENCES,
25+
ALERTMANAGER_STATUS;
2526

2627
/**
2728
* Convert a string to the corresponding enum value, case-insensitive.

direct-query-core/src/main/java/org/opensearch/sql/directquery/rest/model/WriteDirectQueryResourcesRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class WriteDirectQueryResourcesRequest {
2121
private String resourceName;
2222
private String request;
2323
private Map<String, String> RequestOptions;
24+
private String groupName;
25+
private boolean delete;
2426

2527
/**
2628
* Sets the resource type from a string value.

direct-query-core/src/main/java/org/opensearch/sql/prometheus/client/PrometheusClient.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ Map<String, List<MetricMetadata>> getAllMetrics(Map<String, String> queryParams)
5858
JSONObject getAlerts() throws IOException;
5959

6060
/**
61-
* Get all recording and alerting rules.
61+
* Get all recording and alerting rules, normalized to a consistent JSON format. Handles
62+
* Prometheus JSON, Cortex/Thanos YAML, and AMP JSON responses, returning them all as a
63+
* {"groups":[...]} structure.
6264
*
6365
* @param queryParams Map of query parameters to include in the request
64-
* @return JSONObject containing all rules
66+
* @return JSONObject with {"groups":[...]} structure
6567
* @throws IOException If there is an issue with the request
6668
*/
6769
JSONObject getRules(Map<String, String> queryParams) throws IOException;
@@ -107,4 +109,62 @@ Map<String, List<MetricMetadata>> getAllMetrics(Map<String, String> queryParams)
107109
* @throws IOException If there is an issue with the request
108110
*/
109111
String createAlertmanagerSilences(String silenceJson) throws IOException;
112+
113+
/**
114+
* Expire (delete) a silence in Alertmanager.
115+
*
116+
* @param silenceId The ID of the silence to expire
117+
* @return String containing the response
118+
* @throws IOException If there is an issue with the request
119+
*/
120+
String deleteAlertmanagerSilence(String silenceId) throws IOException;
121+
122+
/**
123+
* Get Alertmanager status including configuration, version, and cluster info.
124+
*
125+
* @return JSONObject containing the Alertmanager status
126+
* @throws IOException If there is an issue with the request
127+
*/
128+
JSONObject getAlertmanagerStatus() throws IOException;
129+
130+
/**
131+
* Get rules for a specific namespace, normalized to a consistent JSON format. Handles
132+
* Cortex/Thanos YAML and AMP JSON responses, returning them all as a {"groups":[...]} structure.
133+
*
134+
* @param namespace The rules namespace
135+
* @param queryParams Map of query parameters to include in the request
136+
* @return JSONObject with {"groups":[...]} structure
137+
* @throws IOException If there is an issue with the request
138+
*/
139+
JSONObject getRulesByNamespace(String namespace, Map<String, String> queryParams)
140+
throws IOException;
141+
142+
/**
143+
* Create or update a rule group in a namespace via the Cortex/Thanos Ruler API.
144+
*
145+
* @param namespace The rules namespace
146+
* @param yamlBody The rule group definition in YAML format
147+
* @return String containing the response
148+
* @throws IOException If there is an issue with the request
149+
*/
150+
String createOrUpdateRuleGroup(String namespace, String yamlBody) throws IOException;
151+
152+
/**
153+
* Delete all rules in a namespace via the Cortex/Thanos Ruler API.
154+
*
155+
* @param namespace The rules namespace to delete
156+
* @return String containing the response
157+
* @throws IOException If there is an issue with the request
158+
*/
159+
String deleteRuleNamespace(String namespace) throws IOException;
160+
161+
/**
162+
* Delete a specific rule group in a namespace via the Cortex/Thanos Ruler API.
163+
*
164+
* @param namespace The rules namespace
165+
* @param groupName The specific rule group name to delete
166+
* @return String containing the response
167+
* @throws IOException If there is an issue with the request
168+
*/
169+
String deleteRuleGroup(String namespace, String groupName) throws IOException;
110170
}

0 commit comments

Comments
 (0)