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

[Spotless] Applying Google Code Format for prometheus files #13 #1973

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ spotless {
target fileTree('.') {
include 'datasources/**/*.java',
'core/**/*.java',
'prometheus/**/*.java',
'sql/**/*.java',
'common/**/*.java',
'ppl/**/*.java'
Expand Down
3 changes: 3 additions & 0 deletions prometheus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ repositories {
mavenCentral()
}

checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

dependencies {
api project(':core')
implementation project(':datasources')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,60 +38,60 @@ public PrometheusClientImpl(OkHttpClient okHttpClient, URI uri) {
this.uri = uri;
}


@Override
public JSONObject queryRange(String query, Long start, Long end, String step) throws IOException {
String queryUrl = String.format("%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s",
uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8),
start, end, step);
String queryUrl =
String.format(
"%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s",
uri.toString().replaceAll("/$", ""),
URLEncoder.encode(query, StandardCharsets.UTF_8),
start,
end,
step);
logger.debug("queryUrl: " + queryUrl);
Request request = new Request.Builder()
.url(queryUrl)
.build();
Request request = new Request.Builder().url(queryUrl).build();
Response response = this.okHttpClient.newCall(request).execute();
JSONObject jsonObject = readResponse(response);
return jsonObject.getJSONObject("data");
}

@Override
public List<String> getLabels(String metricName) throws IOException {
String queryUrl = String.format("%s/api/v1/labels?%s=%s",
uri.toString().replaceAll("/$", ""),
URLEncoder.encode("match[]", StandardCharsets.UTF_8),
URLEncoder.encode(metricName, StandardCharsets.UTF_8));
String queryUrl =
String.format(
"%s/api/v1/labels?%s=%s",
uri.toString().replaceAll("/$", ""),
URLEncoder.encode("match[]", StandardCharsets.UTF_8),
URLEncoder.encode(metricName, StandardCharsets.UTF_8));
logger.debug("queryUrl: " + queryUrl);
Request request = new Request.Builder()
.url(queryUrl)
.build();
Request request = new Request.Builder().url(queryUrl).build();
Response response = this.okHttpClient.newCall(request).execute();
JSONObject jsonObject = readResponse(response);
return toListOfLabels(jsonObject.getJSONArray("data"));
}

@Override
public Map<String, List<MetricMetadata>> getAllMetrics() throws IOException {
String queryUrl = String.format("%s/api/v1/metadata",
uri.toString().replaceAll("/$", ""));
String queryUrl = String.format("%s/api/v1/metadata", uri.toString().replaceAll("/$", ""));
logger.debug("queryUrl: " + queryUrl);
Request request = new Request.Builder()
.url(queryUrl)
.build();
Request request = new Request.Builder().url(queryUrl).build();
Response response = this.okHttpClient.newCall(request).execute();
JSONObject jsonObject = readResponse(response);
TypeReference<HashMap<String, List<MetricMetadata>>> typeRef
= new TypeReference<>() {};
TypeReference<HashMap<String, List<MetricMetadata>>> typeRef = new TypeReference<>() {};
return new ObjectMapper().readValue(jsonObject.getJSONObject("data").toString(), typeRef);
}

@Override
public JSONArray queryExemplars(String query, Long start, Long end) throws IOException {
String queryUrl = String.format("%s/api/v1/query_exemplars?query=%s&start=%s&end=%s",
uri.toString().replaceAll("/$", ""), URLEncoder.encode(query, StandardCharsets.UTF_8),
start, end);
String queryUrl =
String.format(
"%s/api/v1/query_exemplars?query=%s&start=%s&end=%s",
uri.toString().replaceAll("/$", ""),
URLEncoder.encode(query, StandardCharsets.UTF_8),
start,
end);
logger.debug("queryUrl: " + queryUrl);
Request request = new Request.Builder()
.url(queryUrl)
.build();
Request request = new Request.Builder().url(queryUrl).build();
Response response = this.okHttpClient.newCall(request).execute();
JSONObject jsonObject = readResponse(response);
return jsonObject.getJSONArray("data");
Expand All @@ -100,16 +100,15 @@ public JSONArray queryExemplars(String query, Long start, Long end) throws IOExc
private List<String> toListOfLabels(JSONArray array) {
List<String> result = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
//__name__ is internal label in prometheus representing the metric name.
//Exempting this from labels list as it is not required in any of the operations.
// __name__ is internal label in prometheus representing the metric name.
// Exempting this from labels list as it is not required in any of the operations.
if (!"__name__".equals(array.optString(i))) {
result.add(array.optString(i));
}
}
return result;
}


private JSONObject readResponse(Response response) throws IOException {
if (response.isSuccessful()) {
JSONObject jsonObject = new JSONObject(Objects.requireNonNull(response.body()).string());
Expand All @@ -120,10 +119,9 @@ private JSONObject readResponse(Response response) throws IOException {
}
} else {
throw new RuntimeException(
String.format("Request to Prometheus is Unsuccessful with : %s", Objects.requireNonNull(
response.body(), "Response body can't be null").string()));
String.format(
"Request to Prometheus is Unsuccessful with : %s",
Objects.requireNonNull(response.body(), "Response body can't be null").string()));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public class PrometheusFieldConstants {
public static final String EXEMPLARS_KEY = "exemplars";
public static final String TRACE_ID_KEY = "traceID";
public static final String LABELS_KEY = "labels";
public static final String TIMESTAMP_KEY = "timestamp";
public static final String VALUE_KEY = "value";
public static final String TIMESTAMP_KEY = "timestamp";
public static final String VALUE_KEY = "value";
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.opensearch.sql.prometheus.storage.QueryExemplarsTable;
import org.opensearch.sql.storage.Table;

public class QueryExemplarFunctionImplementation extends FunctionExpression implements
TableFunctionImplementation {
public class QueryExemplarFunctionImplementation extends FunctionExpression
implements TableFunctionImplementation {

private final FunctionName functionName;
private final List<Expression> arguments;
Expand All @@ -39,10 +39,10 @@ public class QueryExemplarFunctionImplementation extends FunctionExpression impl
* Required argument constructor.
*
* @param functionName name of the function
* @param arguments a list of arguments provided
* @param arguments a list of arguments provided
*/
public QueryExemplarFunctionImplementation(FunctionName functionName, List<Expression> arguments,
PrometheusClient prometheusClient) {
public QueryExemplarFunctionImplementation(
FunctionName functionName, List<Expression> arguments, PrometheusClient prometheusClient) {
super(functionName, arguments);
this.functionName = functionName;
this.arguments = arguments;
Expand All @@ -51,10 +51,11 @@ public QueryExemplarFunctionImplementation(FunctionName functionName, List<Expre

@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));
throw new UnsupportedOperationException(
String.format(
"Prometheus defined function [%s] is only "
+ "supported in SOURCE clause with prometheus connector catalog",
functionName));
}

@Override
Expand All @@ -64,10 +65,15 @@ public ExprType type() {

@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());
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));
}

Expand All @@ -79,27 +85,26 @@ public Table applyArguments() {
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));
}
});
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.opensearch.sql.prometheus.storage.PrometheusMetricTable;
import org.opensearch.sql.storage.Table;

public class QueryRangeFunctionImplementation extends FunctionExpression implements
TableFunctionImplementation {
public class QueryRangeFunctionImplementation extends FunctionExpression
implements TableFunctionImplementation {

private final FunctionName functionName;
private final List<Expression> arguments;
Expand All @@ -40,10 +40,10 @@ public class QueryRangeFunctionImplementation extends FunctionExpression impleme
* Required argument constructor.
*
* @param functionName name of the function
* @param arguments a list of expressions
* @param arguments a list of expressions
*/
public QueryRangeFunctionImplementation(FunctionName functionName, List<Expression> arguments,
PrometheusClient prometheusClient) {
public QueryRangeFunctionImplementation(
FunctionName functionName, List<Expression> arguments, PrometheusClient prometheusClient) {
super(functionName, arguments);
this.functionName = functionName;
this.arguments = arguments;
Expand All @@ -52,10 +52,11 @@ public QueryRangeFunctionImplementation(FunctionName functionName, List<Expressi

@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));
throw new UnsupportedOperationException(
String.format(
"Prometheus defined function [%s] is only "
+ "supported in SOURCE clause with prometheus connector catalog",
functionName));
}

@Override
Expand All @@ -65,10 +66,15 @@ public ExprType type() {

@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());
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));
}

Expand All @@ -80,30 +86,29 @@ public Table applyArguments() {
private PrometheusQueryRequest buildQueryFromQueryRangeFunction(List<Expression> arguments) {

PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest();
arguments.forEach(arg -> {
String argName = ((NamedArgumentExpression) arg).getArgName();
Expression argValue = ((NamedArgumentExpression) arg).getValue();
ExprValue literalValue = argValue.valueOf();
switch (argName) {
case QUERY:
prometheusQueryRequest
.setPromQl((String) literalValue.value());
break;
case STARTTIME:
prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue());
break;
case ENDTIME:
prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue());
break;
case STEP:
prometheusQueryRequest.setStep(literalValue.value().toString());
break;
default:
throw new ExpressionEvaluationException(
String.format("Invalid Function Argument:%s", argName));
}
});
arguments.forEach(
arg -> {
String argName = ((NamedArgumentExpression) arg).getArgName();
Expression argValue = ((NamedArgumentExpression) arg).getValue();
ExprValue literalValue = argValue.valueOf();
switch (argName) {
case QUERY:
prometheusQueryRequest.setPromQl((String) literalValue.value());
break;
case STARTTIME:
prometheusQueryRequest.setStartTime(((Number) literalValue.value()).longValue());
break;
case ENDTIME:
prometheusQueryRequest.setEndTime(((Number) literalValue.value()).longValue());
break;
case STEP:
prometheusQueryRequest.setStep(literalValue.value().toString());
break;
default:
throw new ExpressionEvaluationException(
String.format("Invalid Function Argument:%s", argName));
}
});
return prometheusQueryRequest;
}

}
Loading
Loading