-
Notifications
You must be signed in to change notification settings - Fork 964
JMX metrics unit conversion #13448
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
Merged
trask
merged 22 commits into
open-telemetry:main
from
robsunday:jmx-metric-unit-conversion
Mar 20, 2025
Merged
JMX metrics unit conversion #13448
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8d93dbf
Added unit conversion support for YAML
robsunday b7aaa52
Fixed few warnings
robsunday ad56046
Missing files added
robsunday da63cef
Fixes for @Nullable
robsunday c05a990
Merge branch 'main' into jmx-metric-unit-conversion
robsunday d6b87e2
Fixed typo
robsunday 061e3c7
Documentation updated.
robsunday 1391ad7
spotless
robsunday 05b1a3d
Update instrumentation/jmx-metrics/javaagent/README.md
robsunday 4ffbbfe
Microseconds support added
robsunday 8f481f3
Exception class changed
robsunday b9f2052
Unit test improvements
robsunday 95cde46
Update instrumentation/jmx-metrics/javaagent/README.md
robsunday d4bbe24
Merge branch 'jmx-metric-unit-conversion' of github.com:robsunday/ope…
robsunday 7559b7a
Readme update
robsunday 1964f82
Spotless
robsunday c64358d
Merge branch 'main' into jmx-metric-unit-conversion
robsunday a2c6845
Class structure update
robsunday 40fe2b2
Got rid of isConvertingToDouble flag which is not needed now
robsunday 67c6fb3
Merge branch 'open-telemetry:main' into jmx-metric-unit-conversion
robsunday 5dcc6fe
JavaDoc update
robsunday d85cf3f
Merge branch 'main' into jmx-metric-unit-conversion
robsunday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...rics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/UnitConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jmx.engine; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* This class is responsible for converting a value between metric units using defined conversion | ||
* algorithms. | ||
*/ | ||
class UnitConverter { | ||
private static final Map<String, UnitConverter> conversionMappings = new HashMap<>(); | ||
|
||
static { | ||
registerConversion("ms", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toMillis(1)); | ||
registerConversion("us", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toMicros(1)); | ||
registerConversion("ns", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toNanos(1)); | ||
} | ||
|
||
private final Function<Number, Number> convertingFunction; | ||
|
||
/** | ||
* Get an instance of converter that is able to convert a value from a given source to a target | ||
* unit. | ||
* | ||
* @param sourceUnit a source unit supported by requested converter | ||
* @param targetUnit a target unit supported by requested converter | ||
* @return an instance of converter, or {@literal null} if {@code sourceUnit} is {@literal null} | ||
* or empty, which means that there is no conversion needed. | ||
* @throws IllegalArgumentException if {@code targetUnit} is empty, or matching converter was not | ||
* found for provided units. | ||
*/ | ||
@Nullable | ||
public static UnitConverter getInstance(@Nullable String sourceUnit, String targetUnit) { | ||
if (targetUnit.isEmpty()) { | ||
throw new IllegalArgumentException("Non empty targetUnit must be provided"); | ||
} | ||
|
||
if (sourceUnit == null || sourceUnit.isEmpty()) { | ||
// No conversion is needed | ||
return null; | ||
} | ||
|
||
String converterKey = buildConverterKey(sourceUnit, targetUnit); | ||
UnitConverter converter = conversionMappings.get(converterKey); | ||
if (converter == null) { | ||
throw new IllegalArgumentException( | ||
"Unsupported conversion from [" + sourceUnit + "] to [" + targetUnit + "]"); | ||
} | ||
|
||
return converter; | ||
} | ||
|
||
/** | ||
* Register new converter instance that can then be retrieved with {@link #getInstance(String, | ||
* String)}. | ||
* | ||
* @param sourceUnit a source unit supported by the converter | ||
* @param targetUnit a target unit supported by the converter | ||
* @param convertingFunction a function that implements algorithm of conversion between {@code | ||
* sourceUnit} and {@code targetUnit} | ||
* @throws IllegalArgumentException if source or target unit is empty, or when there is converter | ||
* already registered for given {@code sourceUnit} and {@code targetUnit} | ||
*/ | ||
// visible for testing | ||
static void registerConversion( | ||
String sourceUnit, String targetUnit, Function<Number, Number> convertingFunction) { | ||
if (sourceUnit.isEmpty()) { | ||
throw new IllegalArgumentException("Non empty sourceUnit must be provided"); | ||
} | ||
if (targetUnit.isEmpty()) { | ||
throw new IllegalArgumentException("Non empty targetUnit must be provided"); | ||
} | ||
|
||
String converterKey = buildConverterKey(sourceUnit, targetUnit); | ||
|
||
if (conversionMappings.containsKey(converterKey)) { | ||
throw new IllegalArgumentException( | ||
"Conversion from [" + sourceUnit + "] to [" + targetUnit + "] already defined"); | ||
} | ||
conversionMappings.put(converterKey, new UnitConverter(convertingFunction)); | ||
} | ||
|
||
private static String buildConverterKey(String sourceUnit, String targetUnit) { | ||
return sourceUnit + "->" + targetUnit; | ||
} | ||
|
||
/** | ||
* Create an instance of converter | ||
* | ||
* @param convertingFunction an algorithm applied when converting value | ||
*/ | ||
UnitConverter(Function<Number, Number> convertingFunction) { | ||
this.convertingFunction = convertingFunction; | ||
} | ||
|
||
public Number convert(Number value) { | ||
return convertingFunction.apply(value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[for reviewer] unit is required by semconv, so it should not be nullable