Skip to content

Commit

Permalink
[api] Avoid null dimensions for Metric (#3246)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed Jun 7, 2024
1 parent ae8aba2 commit 2acbddc
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions api/src/main/java/ai/djl/metric/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Metric {
Pattern.compile(
"\\s*([\\w\\s]+)\\.([\\w\\s]+):([0-9\\-,.e]+)(?>\\|#([^|]*))?(?>\\|(\\d+))?(?>\\|([cgh]))?");

private static final Dimension HOST = new Dimension("Host", getLocalHostName());
private static final Dimension[] HOST = {new Dimension("Host", getLocalHostName())};

@SerializedName("MetricName")
private String metricName;
Expand Down Expand Up @@ -60,18 +60,6 @@ public Metric(String metricName, Number value) {
this(metricName, value, Unit.COUNT);
}

/**
* Constructs a {@code Metric} instance with the specified {@code metricName}, <code>value
* </code>, and {@code unit}.
*
* @param metricName the metric name
* @param value the metric value
* @param unit the metric unit
*/
public Metric(String metricName, Number value, Unit unit) {
this(metricName, null, value.toString(), unit.getValue(), null, HOST);
}

/**
* Constructs a {@code Metric} instance with the specified {@code metricName}, <code>value
* </code>, and {@code unit}.
Expand Down Expand Up @@ -126,7 +114,7 @@ private Metric(
this.unit = unit;
this.value = value;
this.timestamp = timestamp;
this.dimensions = dimensions;
this.dimensions = dimensions.length == 0 ? HOST : dimensions;
}

/**
Expand Down Expand Up @@ -214,7 +202,7 @@ public static Metric parse(String line) {
String type = matcher.group(6);
MetricType metricType = type == null ? null : MetricType.of(type);

Dimension[] dimensions = null;
Dimension[] dimensions;
if (dimension != null) {
String[] dims = dimension.split(",");
dimensions = new Dimension[dims.length];
Expand All @@ -225,6 +213,8 @@ public static Metric parse(String line) {
dimensions[index++] = new Dimension(pair[0], pair[1]);
}
}
} else {
dimensions = HOST;
}

return new Metric(metricName, metricType, value, unit, timestamp, dimensions);
Expand Down

0 comments on commit 2acbddc

Please sign in to comment.