Skip to content

Commit

Permalink
[Spotless] Applying Google Code Format for prometheus files #13 (#340) (
Browse files Browse the repository at this point in the history
opensearch-project#1973)

* added ignorefailures

* Spotless apply on Prometheus

---------

Signed-off-by: Mitchell Gale <[email protected]>
Signed-off-by: Mitchell Gale <[email protected]>
  • Loading branch information
MitchellGale committed Aug 22, 2023
1 parent 2ea4b8b commit 4880b25
Show file tree
Hide file tree
Showing 67 changed files with 1,810 additions and 1,704 deletions.
23 changes: 14 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

buildscript {
ext {
opensearch_version = System.getProperty("opensearch.version", "2.10.0-SNAPSHOT")
opensearch_version = System.getProperty("opensearch.version", "3.0.0-SNAPSHOT")
isSnapshot = "true" == System.getProperty("build.snapshot", "true")
buildVersionQualifier = System.getProperty("build.version_qualifier", "")
version_tokens = opensearch_version.tokenize('-')
Expand Down Expand Up @@ -84,14 +84,12 @@ repositories {
spotless {
java {
target fileTree('.') {
include 'core/src/main/java/org/opensearch/sql/DataSourceSchemaName.java',
'core/src/test/java/org/opensearch/sql/data/**/*.java',
'core/src/test/java/org/opensearch/sql/config/**/*.java',
'core/src/test/java/org/opensearch/sql/analysis/**/*.java',
'core/src/main/java/org/opensearch/sql/planner/**/*.java',
'core/src/main/java/org/opensearch/sql/storage/**/*.java',
'core/src/main/java/org/opensearch/sql/utils/**/*.java',
'core/src/main/java/org/opensearch/sql/monitor/**/*.java'
include 'datasources/**/*.java',
'core/**/*.java',
'prometheus/**/*.java',
'sql/**/*.java',
'common/**/*.java',
'ppl/**/*.java'
exclude '**/build/**', '**/build-*/**'
}
importOrder()
Expand Down Expand Up @@ -224,5 +222,12 @@ task updateVersion {
println "Setting version to ${newVersion}."
// String tokenization to support -SNAPSHOT
ant.replaceregexp(file:'build.gradle', match: '"opensearch.version", "\\d.*"', replace: '"opensearch.version", "' + newVersion.tokenize('-')[0] + '-SNAPSHOT"', flags:'g', byline:true)
ant.replaceregexp(match:'"version": "\\d+.\\d+.\\d+.\\d+', replace:'"version": ' + '"' + newVersion.tokenize('-')[0] + '.0', flags:'g', byline:true) {
fileset(dir: projectDir) {
include(name: "workbench/package.json")
include(name: "workbench/opensearch_dashboards.json")
}
}
ant.replaceregexp(file:'workbench/opensearch_dashboards.json', match:'"opensearchDashboardsVersion": "\\d+.\\d+.\\d+', replace:'"opensearchDashboardsVersion": ' + '"' + newVersion.tokenize('-')[0], flags:'g', byline:true)
}
}
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;
}

}
Loading

0 comments on commit 4880b25

Please sign in to comment.