Skip to content

Commit d6b87e2

Browse files
committed
Fixed typo
Added support for default sourceUnit defined on rule level
1 parent c05a990 commit d6b87e2

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/JmxRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public MetricDef buildMetricDef() throws Exception {
158158
getUnit(),
159159
getMetricType());
160160
} else {
161-
metricInfo = m.buildMetricInfo(prefix, niceAttributeName, getUnit(), getMetricType());
161+
metricInfo = m.buildMetricInfo(prefix, niceAttributeName, getSourceUnit(), getUnit(), getMetricType());
162162
}
163163

164164
List<MetricAttribute> ownAttributes = getAttributeList();

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/Metric.java

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void setDesc(String desc) {
4949
MetricInfo buildMetricInfo(
5050
@Nullable String prefix,
5151
String attributeName,
52+
String defaultSourceUnit,
5253
String defaultUnit,
5354
MetricInfo.Type defaultType) {
5455
String metricName;
@@ -64,6 +65,9 @@ MetricInfo buildMetricInfo(
6465
}
6566

6667
String sourceUnit = getSourceUnit();
68+
if (sourceUnit == null) {
69+
sourceUnit = defaultSourceUnit;
70+
}
6771

6872
String unit = getUnit();
6973
if (unit == null) {

instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/yaml/MetricStructure.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* <li>the metric type
2020
* <li>the metric attributes
2121
* <li>the unit
22+
* <li>the sourceUnit
2223
*
2324
* <p>Known subclasses are {@link JmxRule} and {@link Metric}.
2425
*/
@@ -45,7 +46,7 @@ abstract class MetricStructure {
4546
private Map<String, Object> metricAttribute;
4647
private StateMapping stateMapping = StateMapping.empty();
4748
private static final String STATE_MAPPING_DEFAULT = "*";
48-
private String sourcetUnit;
49+
private String sourceUnit;
4950
private String unit;
5051

5152
private MetricInfo.Type metricType;
@@ -60,11 +61,11 @@ void setType(String t) {
6061
}
6162

6263
public String getSourceUnit() {
63-
return sourcetUnit;
64+
return sourceUnit;
6465
}
6566

6667
public void setSourceUnit(String unit) {
67-
this.sourcetUnit = validateUnit(unit.trim());
68+
this.sourceUnit = validateUnit(unit.trim());
6869
}
6970

7071
public String getUnit() {

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

+8
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,14 @@ void testConf3() {
163163
+ " LABEL_KEY2: beanattr(ATTRIBUTE)\n"
164164
+ " prefix: PREFIX.\n"
165165
+ " type: upDownCounter\n"
166+
+ " sourceUnit: DEFAULT_SOURCE_UNIT\n"
166167
+ " unit: DEFAULT_UNIT\n"
167168
+ " mapping:\n"
168169
+ " A.b:\n"
169170
+ " metric: METRIC_NAME1\n"
170171
+ " type: counter\n"
171172
+ " desc: DESCRIPTION1\n"
173+
+ " sourceUnit: SOURCE_UNIT1\n"
172174
+ " unit: UNIT1\n"
173175
+ " metricAttribute:\n"
174176
+ " LABEL_KEY3: const(CONSTANT)\n"
@@ -186,6 +188,7 @@ void testConf4() throws Exception {
186188
assertThat(defs).hasSize(1);
187189

188190
JmxRule jmxDef = defs.get(0);
191+
assertThat(jmxDef.getSourceUnit()).isEqualTo("DEFAULT_SOURCE_UNIT");
189192
assertThat(jmxDef.getUnit()).isEqualTo("DEFAULT_UNIT");
190193
assertThat(jmxDef.getMetricType()).isEqualTo(MetricInfo.Type.UPDOWNCOUNTER);
191194

@@ -205,6 +208,7 @@ void testConf4() throws Exception {
205208
MetricInfo metricInfo = m.getInfo();
206209
assertThat(metricInfo.getMetricName()).isEqualTo("PREFIX.METRIC_NAME1");
207210
assertThat(metricInfo.getDescription()).isEqualTo("DESCRIPTION1");
211+
assertThat(metricInfo.getSourceUnit()).isEqualTo("SOURCE_UNIT1");
208212
assertThat(metricInfo.getUnit()).isEqualTo("UNIT1");
209213
assertThat(metricInfo.getType()).isEqualTo(MetricInfo.Type.COUNTER);
210214
})
@@ -219,6 +223,7 @@ void testConf4() throws Exception {
219223
MetricInfo metricInfo = m.getInfo();
220224
assertThat(metricInfo.getMetricName()).isEqualTo("PREFIX.METRIC_NAME2");
221225
assertThat(metricInfo.getDescription()).isEqualTo("DESCRIPTION2");
226+
assertThat(metricInfo.getSourceUnit()).isEqualTo(jmxDef.getSourceUnit());
222227
assertThat(metricInfo.getUnit()).isEqualTo("UNIT2");
223228
})
224229
.anySatisfy(
@@ -236,6 +241,9 @@ void testConf4() throws Exception {
236241
assertThat(metricInfo.getUnit())
237242
.describedAs("default unit should match jmx rule definition")
238243
.isEqualTo(jmxDef.getUnit());
244+
assertThat(metricInfo.getSourceUnit())
245+
.describedAs("default sourceUnit should match jmx rule definition")
246+
.isEqualTo(jmxDef.getSourceUnit());
239247
});
240248
}
241249

0 commit comments

Comments
 (0)