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

[api] Avoid null dimensions for Metric #3246

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
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
Loading