Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add direct-query-core module for prometheus integration #3440

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshuali925
Copy link
Member

@joshuali925 joshuali925 commented Mar 17, 2025

Description

This PR adds direct-query-core module to SQL. It implements the following changes

  • move some shared classes from prometheus module to direct-query-core
    • prometheus client
    • client exception
    • metric metadata model
    • test fixtures
  • add direct-query-core module to support ExecuteDirectQuery and GetDirectQueryResources actions
  • implement query handler for executing the actions. currently the only supported data source type is prometheus

This PR does not expose the actions in SQL plugin at runtime. The follow up #3441 for direct-query module will add the rest and transport actions to SQL

Query API owner: @goyamegh
Resources API owner: @joshuali925

It is currently in draft since some tests are not fixed yet

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

return dataSourceService.getDataSourceMetadata(dataSourceName).getConnector();
}

private Object createClientForType(String dataSourceType, DataSourceMetadata metadata)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be better to create a base client interface instead of Object and generics. Currently it's not straightforward to find all data source client implementations

switch (dataSourceType) {
case "PROMETHEUS":
return createPrometheusClient(metadata);
// Add cases for other data source types as needed
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commment


private PrometheusClient createPrometheusClient(DataSourceMetadata metadata) {
try {
// replace this with validate properties in PrometheusStorageFactory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed?


package org.opensearch.sql.directquery.rest.model;

public enum QueryEntityType {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

@Override
public ExecuteDirectQueryResponse executeDirectQuery(ExecuteDirectQueryRequest request) {
// TODO: Replace with the data source query id.
String queryId = UUID.randomUUID().toString();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed?

try {
return handler.executeQuery(client, request);
} catch (IOException e) {
return "{\"error\": \"Error executing query: " + e.getMessage() + "\"}";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if prometheus returns an error, should the SQL request fail with non-200 response?

Comment on lines +94 to +101
String queryString =
params.entrySet().stream()
.map(
entry ->
URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)
+ "="
+ URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String queryString =
params.entrySet().stream()
.map(
entry ->
URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)
+ "="
+ URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
String queryString = this.paramsToQueryString(params);

Response response = this.okHttpClient.newCall(request).execute();

logger.info("Received Prometheus response for instant query: code={}", response);
// Return the full response object, not just the data field
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the comment mean?

URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)
+ "="
+ URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also reuse the paramsToQueryString here

*/

package org.opensearch.sql.prometheus.exceptions;
package org.opensearch.sql.prometheus.exception;

import org.opensearch.sql.datasources.exceptions.DataSourceClientException;
Copy link
Member Author

@joshuali925 joshuali925 Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be package org.opensearch.sql.datasource.client.exceptions.DataSourceClientException;? i think we should remove one of the two DataSourceClientException

any reason we need the datasource package under direct-query-core? or can we move it to datasource module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant