-
Notifications
You must be signed in to change notification settings - Fork 158
Prometheus Query Exemplars #1782
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
Merged
vamsimanohar
merged 1 commit into
opensearch-project:main
from
vamsimanohar:query_exemplars
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
integ-test/src/test/resources/expectedOutput/ppl/explain_query_exemplars.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"root": { | ||
"name": "QueryExemplarsFunctionTableScanOperator", | ||
"description": { | ||
"request": "query_exemplars(app_ads_ad_requests_total, 1689228292, 1689232299)" | ||
}, | ||
"children": [] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ensearch/sql/prometheus/functions/implementation/QueryExemplarFunctionImplementation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.prometheus.functions.implementation; | ||
|
||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.ENDTIME; | ||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.QUERY; | ||
import static org.opensearch.sql.prometheus.functions.resolver.QueryRangeTableFunctionResolver.STARTTIME; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.exception.ExpressionEvaluationException; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.NamedArgumentExpression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.TableFunctionImplementation; | ||
import org.opensearch.sql.prometheus.client.PrometheusClient; | ||
import org.opensearch.sql.prometheus.request.PrometheusQueryExemplarsRequest; | ||
import org.opensearch.sql.prometheus.storage.QueryExemplarsTable; | ||
import org.opensearch.sql.storage.Table; | ||
|
||
public class QueryExemplarFunctionImplementation extends FunctionExpression implements | ||
TableFunctionImplementation { | ||
|
||
private final FunctionName functionName; | ||
private final List<Expression> arguments; | ||
private final PrometheusClient prometheusClient; | ||
|
||
/** | ||
* Required argument constructor. | ||
* | ||
* @param functionName name of the function | ||
* @param arguments a list of arguments provided | ||
*/ | ||
public QueryExemplarFunctionImplementation(FunctionName functionName, List<Expression> arguments, | ||
PrometheusClient prometheusClient) { | ||
super(functionName, arguments); | ||
this.functionName = functionName; | ||
this.arguments = arguments; | ||
this.prometheusClient = prometheusClient; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
throw new UnsupportedOperationException(String.format( | ||
"Prometheus defined function [%s] is only " | ||
+ "supported in SOURCE clause with prometheus connector catalog", | ||
functionName)); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return ExprCoreType.STRUCT; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
List<String> args = arguments.stream() | ||
.map(arg -> String.format("%s=%s", ((NamedArgumentExpression) arg) | ||
.getArgName(), ((NamedArgumentExpression) arg).getValue().toString())) | ||
.collect(Collectors.toList()); | ||
return String.format("%s(%s)", functionName, String.join(", ", args)); | ||
} | ||
|
||
@Override | ||
public Table applyArguments() { | ||
return new QueryExemplarsTable(prometheusClient, buildExemplarsQueryRequest(arguments)); | ||
} | ||
|
||
private PrometheusQueryExemplarsRequest buildExemplarsQueryRequest(List<Expression> arguments) { | ||
|
||
PrometheusQueryExemplarsRequest request = new PrometheusQueryExemplarsRequest(); | ||
arguments.forEach(arg -> { | ||
String argName = ((NamedArgumentExpression) arg).getArgName(); | ||
Expression argValue = ((NamedArgumentExpression) arg).getValue(); | ||
ExprValue literalValue = argValue.valueOf(); | ||
switch (argName) { | ||
case QUERY: | ||
request | ||
.setQuery((String) literalValue.value()); | ||
break; | ||
case STARTTIME: | ||
request.setStartTime(((Number) literalValue.value()).longValue()); | ||
break; | ||
case ENDTIME: | ||
request.setEndTime(((Number) literalValue.value()).longValue()); | ||
break; | ||
default: | ||
throw new ExpressionEvaluationException( | ||
String.format("Invalid Function Argument:%s", argName)); | ||
} | ||
}); | ||
return request; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...org/opensearch/sql/prometheus/functions/resolver/QueryExemplarsTableFunctionResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.prometheus.functions.resolver; | ||
vamsimanohar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.LONG; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.prometheus.utils.TableFunctionUtils.getNamedArgumentsOfTableFunction; | ||
import static org.opensearch.sql.prometheus.utils.TableFunctionUtils.validatePrometheusTableFunctionArguments; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.function.FunctionBuilder; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.FunctionResolver; | ||
import org.opensearch.sql.expression.function.FunctionSignature; | ||
import org.opensearch.sql.prometheus.client.PrometheusClient; | ||
import org.opensearch.sql.prometheus.functions.implementation.QueryExemplarFunctionImplementation; | ||
|
||
/** | ||
* This class is for query_exemplars table function resolver {@link FunctionResolver}. | ||
* It takes care of validating function arguments and also creating | ||
* required {@link org.opensearch.sql.expression.function.TableFunctionImplementation} Class. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class QueryExemplarsTableFunctionResolver implements FunctionResolver { | ||
vamsimanohar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final PrometheusClient prometheusClient; | ||
public static final String QUERY_EXEMPLARS = "query_exemplars"; | ||
|
||
public static final String QUERY = "query"; | ||
public static final String STARTTIME = "starttime"; | ||
public static final String ENDTIME = "endtime"; | ||
|
||
@Override | ||
public Pair<FunctionSignature, FunctionBuilder> resolve(FunctionSignature unresolvedSignature) { | ||
final FunctionName functionName = FunctionName.of(QUERY_EXEMPLARS); | ||
FunctionSignature functionSignature = | ||
new FunctionSignature(FunctionName.of(QUERY_EXEMPLARS), List.of(STRING, LONG, LONG)); | ||
FunctionBuilder functionBuilder = (functionProperties, arguments) -> { | ||
final List<String> argumentNames = List.of(QUERY, STARTTIME, ENDTIME); | ||
validatePrometheusTableFunctionArguments(arguments, argumentNames); | ||
List<Expression> namedArguments = getNamedArgumentsOfTableFunction(arguments, argumentNames); | ||
return new QueryExemplarFunctionImplementation(functionName, | ||
namedArguments, prometheusClient); | ||
}; | ||
return Pair.of(functionSignature, functionBuilder); | ||
} | ||
|
||
@Override | ||
public FunctionName getFunctionName() { | ||
return FunctionName.of(QUERY_EXEMPLARS); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.