-
Notifications
You must be signed in to change notification settings - Fork 979
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 9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,21 +27,28 @@ public enum Type { | |
// How to report the metric using OpenTelemetry API | ||
private final String metricName; // used as Instrument name | ||
@Nullable private final String description; | ||
@Nullable private final String unit; | ||
@Nullable private final String sourceUnit; | ||
private final String unit; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
private final Type type; | ||
|
||
/** | ||
* Constructor for MetricInfo. | ||
* | ||
* @param metricName a String that will be used as a metric name, it should be unique | ||
* @param description a human readable description of the metric | ||
* @param sourceUnit a human readable unit of measurement that is received from metric source | ||
* @param unit a human readable unit of measurement | ||
* @param type the instrument typ to be used for the metric | ||
*/ | ||
public MetricInfo( | ||
String metricName, @Nullable String description, String unit, @Nullable Type type) { | ||
String metricName, | ||
@Nullable String description, | ||
@Nullable String sourceUnit, | ||
String unit, | ||
@Nullable Type type) { | ||
this.metricName = metricName; | ||
this.description = description; | ||
this.sourceUnit = sourceUnit; | ||
this.unit = unit; | ||
this.type = type == null ? Type.GAUGE : type; | ||
} | ||
|
@@ -56,6 +63,10 @@ public String getDescription() { | |
} | ||
|
||
@Nullable | ||
public String getSourceUnit() { | ||
return sourceUnit; | ||
} | ||
|
||
public String getUnit() { | ||
return unit; | ||
} | ||
|
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
34 changes: 34 additions & 0 deletions
34
...library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/unit/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,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jmx.engine.unit; | ||
|
||
import java.util.function.Function; | ||
|
||
/** This class is responsible for converting a value using provided algorithm. */ | ||
public class UnitConverter { | ||
private final Function<Number, Number> convertingFunction; | ||
private final boolean convertToDouble; | ||
|
||
/** | ||
* Create an instance of converter | ||
* | ||
* @param convertingFunction an algorithm applied when converting value | ||
* @param convertToDouble indicates of algorithm will return floating point result. This must be | ||
* in-sync with algorithm implementation. | ||
*/ | ||
UnitConverter(Function<Number, Number> convertingFunction, boolean convertToDouble) { | ||
this.convertingFunction = convertingFunction; | ||
this.convertToDouble = convertToDouble; | ||
} | ||
|
||
public Number convert(Number value) { | ||
return convertingFunction.apply(value); | ||
} | ||
|
||
public boolean isConvertingToDouble() { | ||
return convertToDouble; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
.../src/main/java/io/opentelemetry/instrumentation/jmx/engine/unit/UnitConverterFactory.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,70 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jmx.engine.unit; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
|
||
public class UnitConverterFactory { | ||
private static final Map<String, UnitConverter> conversionMappings = new HashMap<>(); | ||
|
||
static { | ||
registerConverter("ms", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toMillis(1), true); | ||
registerConverter("ns", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toNanos(1), true); | ||
robsunday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private UnitConverterFactory() {} | ||
|
||
@Nullable | ||
public static UnitConverter getConverter(@Nullable String sourceUnit, String targetUnit) { | ||
robsunday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (targetUnit.isEmpty()) { | ||
throw new IllegalArgumentException("Non empty targetUnit must be provided"); | ||
} | ||
|
||
if (sourceUnit == null || sourceUnit.isEmpty()) { | ||
// No conversion is needed | ||
return null; | ||
robsunday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
String converterKey = getConverterKey(sourceUnit, targetUnit); | ||
UnitConverter converter = conversionMappings.get(converterKey); | ||
if (converter == null) { | ||
throw new IllegalStateException( | ||
robsunday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"No [" + sourceUnit + "] to [" + targetUnit + "] unit converter"); | ||
} | ||
|
||
return converter; | ||
} | ||
|
||
// visible for testing | ||
static void registerConverter( | ||
String sourceUnit, | ||
String targetUnit, | ||
Function<Number, Number> convertingFunction, | ||
boolean convertToDouble) { | ||
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 = getConverterKey(sourceUnit, targetUnit); | ||
|
||
if (conversionMappings.containsKey(converterKey)) { | ||
throw new IllegalArgumentException( | ||
"Converter from [" + sourceUnit + "] to [" + targetUnit + "] already registered"); | ||
} | ||
conversionMappings.put(converterKey, new UnitConverter(convertingFunction, convertToDouble)); | ||
} | ||
|
||
private static String getConverterKey(String sourceUnit, String targetUnit) { | ||
return sourceUnit + "->" + targetUnit; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.