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

refactor code and fix some npe #1201

Merged
merged 5 commits into from
Aug 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ private Statement getConnection(String username, String password, String url,Int
private void queryOneRow(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
statement.setMaxRows(1);
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
if (resultSet.next()) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String column : columns) {
Expand All @@ -213,8 +212,6 @@ private void queryOneRow(Statement statement, String sql, List<String> columns,
}
builder.addValues(valueRowBuilder.build());
}
} finally {
resultSet.close();
}
}

Expand All @@ -231,8 +228,7 @@ private void queryOneRow(Statement statement, String sql, List<String> columns,
*/
private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
HashMap<String, String> values = new HashMap<>(columns.size());
while (resultSet.next()) {
if (resultSet.getString(1) != null) {
Expand All @@ -251,8 +247,6 @@ private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<
}
}
builder.addValues(valueRowBuilder.build());
} finally {
resultSet.close();
}
}

Expand All @@ -268,8 +262,7 @@ private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<
*/
private void queryMultiRow(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String column : columns) {
Expand All @@ -284,8 +277,6 @@ private void queryMultiRow(Statement statement, String sql, List<String> columns
}
builder.addValues(valueRowBuilder.build());
}
} finally {
resultSet.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* ftp protocol collection implementation
Expand Down Expand Up @@ -51,11 +52,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
metrics.getAliasFields().forEach(it -> {
if (valueMap.containsKey(it)) {
String fieldValue = valueMap.get(it);
if (fieldValue == null) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
} else {
valueRowBuilder.addColumns(fieldValue);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(fieldValue, CommonConstants.NULL_VALUE));
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PrometheusMatrixParser extends AbstractPrometheusParse {
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
if(promVectorOrMatrix != null && promVectorOrMatrix.getData() != null) {
return DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType());
}
return false;
} catch (Exception e) {
Expand All @@ -36,6 +36,9 @@ public Boolean checkType(String responseStr) {
@Override
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
if (promVectorOrMatrix == null){
return;
}
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
for (List<Object> value : r.getValues()) {
Expand All @@ -54,7 +57,7 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
if (CommonConstants.PROM_TIME.equals(aliasField)) {
for (Object o : value) {
if (o instanceof Double) {
valueRowBuilder.addColumns(String.valueOf(new BigDecimal((Double) o * 1000)));
valueRowBuilder.addColumns(String.valueOf(BigDecimal.valueOf((Double) o * 1000)));
setTimeFlag = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PrometheusVectorParser extends AbstractPrometheusParse {
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
if (promVectorOrMatrix != null && promVectorOrMatrix.getData() != null) {
return DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType());
}
return false;
} catch (Exception e) {
Expand All @@ -38,6 +38,9 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
boolean setTimeFlag = false;
boolean setValueFlag = false;
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
if (promVectorOrMatrix == null){
return;
}
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
Expand All @@ -53,7 +56,7 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
if (CommonConstants.PROM_TIME.equals(aliasField)) {
for (Object o : r.getValue()) {
if (o instanceof Double) {
valueRowBuilder.addColumns(String.valueOf(new BigDecimal((Double) o * 1000)));
valueRowBuilder.addColumns(String.valueOf(BigDecimal.valueOf((Double) o * 1000)));
setTimeFlag = true;
}
}
Expand Down