Skip to content

Commit 9c2bea5

Browse files
authored
fix invalid jmx state metrics empty unit (#14194)
1 parent 5f062cb commit 9c2bea5

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/UnitConverter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@ class UnitConverter {
3333
* @param sourceUnit a source unit supported by requested converter
3434
* @param targetUnit a target unit supported by requested converter
3535
* @return an instance of converter, or {@literal null} if {@code sourceUnit} is {@literal null}
36-
* or empty, which means that there is no conversion needed.
37-
* @throws IllegalArgumentException if {@code targetUnit} is empty, or matching converter was not
38-
* found for provided units.
36+
* or empty or if {@code targetUnit} is empty, which means that there is no conversion needed.
37+
* @throws IllegalArgumentException if matching converter was not found for provided units.
3938
*/
4039
@Nullable
4140
public static UnitConverter getInstance(@Nullable String sourceUnit, String targetUnit) {
42-
if (targetUnit.isEmpty()) {
43-
throw new IllegalArgumentException("Non empty targetUnit must be provided");
44-
}
45-
46-
if (sourceUnit == null || sourceUnit.isEmpty()) {
41+
if (sourceUnit == null || sourceUnit.isEmpty() || targetUnit.isEmpty()) {
4742
// No conversion is needed
4843
return null;
4944
}

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/UnitConverterTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.instrumentation.jmx.engine;
77

8+
import static org.assertj.core.api.Assertions.assertThat;
89
import static org.assertj.core.api.Assertions.assertThatThrownBy;
910
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.assertNull;
1111

1212
import org.junit.jupiter.api.Test;
1313
import org.junit.jupiter.params.ParameterizedTest;
@@ -58,10 +58,11 @@ void shouldHandleUnsupportedConversion(String sourceUnit, String targetUnit) {
5858
@CsvSource({
5959
", s", // null -> "s"
6060
"'', s", // "" -> "s"
61+
"1, ''", // empty target unit
6162
})
6263
void shouldSkipConversionWhenSourceUnitNotSpecified(String sourceUnit, String targetUnit) {
6364
UnitConverter converter = UnitConverter.getInstance(sourceUnit, targetUnit);
64-
assertNull(converter);
65+
assertThat(converter).isNull();
6566
}
6667

6768
@Test

0 commit comments

Comments
 (0)