Skip to content

Commit 0a1f627

Browse files
committed
Fix NPE when using CGroup metrics with no cpu limit
1 parent ad5bb2a commit 0a1f627

19 files changed

+61
-29
lines changed

metrics/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<version>1.0</version>
2121
</dependency>
2222

23+
<dependency>
24+
<groupId>org.jspecify</groupId>
25+
<artifactId>jspecify</artifactId>
26+
<version>1.0.0</version>
27+
</dependency>
28+
2329
<dependency>
2430
<groupId>io.avaje</groupId>
2531
<artifactId>junit</artifactId>

metrics/src/main/java/io/avaje/metrics/Timer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.metrics;
22

3+
import org.jspecify.annotations.Nullable;
4+
35
import java.util.function.Supplier;
46

57
/**
@@ -122,7 +124,7 @@ public interface Timer extends Metric {
122124
/**
123125
* Return the bucket range or empty string if not a bucket.
124126
*/
125-
String bucketRange();
127+
@Nullable String bucketRange();
126128

127129
/**
128130
* Statistics collected by Timer.
@@ -132,7 +134,7 @@ interface Stats extends Meter.Stats {
132134
/**
133135
* Return the bucket range for these statistics.
134136
*/
135-
default String bucketRange() {
137+
default @Nullable String bucketRange() {
136138
return null;
137139
}
138140
}

metrics/src/main/java/io/avaje/metrics/core/BaseReportName.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.avaje.metrics.core;
22

33
import io.avaje.metrics.Metric;
4+
import org.jspecify.annotations.Nullable;
45

56
abstract class BaseReportName {
67

78
final String name;
8-
String reportName;
9+
@Nullable String reportName;
910

1011
BaseReportName(String name) {
1112
this.name = name;

metrics/src/main/java/io/avaje/metrics/core/DBucketTimer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.metrics.core;
22

33
import io.avaje.metrics.Timer;
4+
import org.jspecify.annotations.Nullable;
45

56
import java.util.function.Supplier;
67

@@ -27,7 +28,7 @@ public String toString() {
2728
}
2829

2930
@Override
30-
public String bucketRange() {
31+
public @Nullable String bucketRange() {
3132
return null;
3233
}
3334

metrics/src/main/java/io/avaje/metrics/core/DCounter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ final class DCounter extends BaseReportName implements Counter {
2020
* <p>
2121
* The rateUnit should be chosen to 'scale' the statistics in a reasonable
2222
* manor - typically events per hour, minute or second.
23-
* </p>
2423
*/
2524
DCounter(String name) {
2625
super(name);

metrics/src/main/java/io/avaje/metrics/core/DTimer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.metrics.core;
22

33
import io.avaje.metrics.Timer;
4+
import org.jspecify.annotations.Nullable;
45

56
import java.util.concurrent.TimeUnit;
67
import java.util.function.Supplier;
@@ -14,7 +15,7 @@
1415
final class DTimer implements Timer {
1516

1617
private final String name;
17-
private final String bucketRange;
18+
private final @Nullable String bucketRange;
1819
private final ValueCounter successCounter;
1920
private final ValueCounter errorCounter;
2021

@@ -38,7 +39,7 @@ public String toString() {
3839
}
3940

4041
@Override
41-
public String bucketRange() {
42+
public @Nullable String bucketRange() {
4243
return bucketRange;
4344
}
4445

metrics/src/main/java/io/avaje/metrics/core/DTimerEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class DTimerEvent implements Timer.Event {
2525

2626
@Override
2727
public String toString() {
28-
return metric.toString() + " durationMillis:" + duration();
28+
return metric + " durationMillis:" + duration();
2929
}
3030

3131
/**

metrics/src/main/java/io/avaje/metrics/core/DefaultMetricProvider.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import io.avaje.metrics.spi.SpiMetricBuilder;
55
import io.avaje.metrics.spi.SpiMetricProvider;
66

7-
import java.io.IOException;
8-
import java.io.UncheckedIOException;
97
import java.util.ArrayList;
108
import java.util.Collection;
119
import java.util.List;

metrics/src/main/java/io/avaje/metrics/core/FileLines.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.avaje.metrics.core;
22

3+
import io.avaje.applog.AppLog;
4+
import org.jspecify.annotations.Nullable;
5+
36
import java.io.File;
47
import java.io.FileReader;
58
import java.io.IOException;
@@ -12,7 +15,7 @@
1215

1316
final class FileLines {
1417

15-
private static final System.Logger log = System.getLogger("io.avaje.metrics");
18+
private static final System.Logger log = AppLog.getLogger("io.avaje.metrics");
1619

1720
private final File file;
1821

@@ -65,7 +68,7 @@ List<String> readLines() {
6568
}
6669
}
6770

68-
private String readLine() {
71+
private @Nullable String readLine() {
6972
try {
7073
try (LineNumberReader lineReader = new LineNumberReader(new FileReader(file))) {
7174
return lineReader.readLine();

metrics/src/main/java/io/avaje/metrics/core/JsonWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.metrics.core;
22

33
import io.avaje.metrics.*;
4+
45
import java.io.IOException;
56
import java.io.UncheckedIOException;
67
import java.text.DecimalFormat;
@@ -114,7 +115,7 @@ public void visit(GaugeLong.Stats metric) {
114115

115116
private void writeSummary(Meter.Stats valueStats) throws IOException {
116117
// valueStats == null when BucketTimedMetric and the bucket is empty
117-
long count = (valueStats == null) ? 0 : valueStats.count();
118+
long count = valueStats.count();
118119
writeKeyNumber("count", count);
119120
if (count != 0) {
120121
buffer.append(",");

0 commit comments

Comments
 (0)