Skip to content

Commit 25471f9

Browse files
authored
align jmx state metrics with semconv (#15093)
1 parent 45735e6 commit 25471f9

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

instrumentation/jmx-metrics/README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ The metric descriptions will remain undefined, unless they are provided by the q
229229
### State Metrics
230230

231231
Some JMX attributes expose current state as a non-numeric MBean attribute, in order to capture those as metrics it is recommended to use the special `state` metric type.
232+
233+
This type of metric fits the [semantic conventions recommendations for state metric](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/how-to-write-conventions/status-metrics.md),
234+
using the `.status` suffix is compliant with the [naming recommendations](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/how-to-write-conventions/status-metrics.md#naming).
235+
232236
For example, with Tomcat connector, the `Catalina:type=Connector,port=*` MBean has `stateName` (of type `String`), we can define the following rule:
233237

234238
```yaml
@@ -238,56 +242,56 @@ rules:
238242
mapping:
239243
stateName:
240244
type: state
241-
metric: tomcat.connector
245+
metric: tomcat.connector.status
242246
metricAttribute:
243247
port: param(port)
244-
connector_state:
248+
tomcat.connector.state:
245249
ok: STARTED
246250
failed: [STOPPED,FAILED]
247251
degraded: '*'
248252
```
249253

250-
For a given value of `port`, let's say `8080` This will capture the `tomcat.connector.state` metric of type `updowncounter` with value `0` or `1` and the `state` metric attribute will have a value in [`ok`,`failed`,`degraded`].
251-
For every sample, 3 metrics will be captured for each value of `state` depending on the value of `stateName`:
254+
For a given value of `port`, let's say `8080` This will capture the `tomcat.connector.status` metric of type `updowncounter` with value `0` or `1` and the `tomcat.connector.state` metric attribute will have a value in [`ok`,`failed`,`degraded`].
255+
For every sample, 3 metrics will be captured for each value of `tomcat.connector.state` depending on the value of `stateName`:
252256

253257
When `stateName` = `STARTED`, we have:
254258

255-
- `tomcat.connector` value = `1`, attributes `port` = `8080` and `connector_state` = `ok`
256-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `failed`
257-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `degraded`
259+
- `tomcat.connector.status` value = `1`, attributes `port` = `8080` and `tomcat.connector.state` = `ok`
260+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `failed`
261+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `degraded`
258262

259263
When `stateName` = `STOPPED` or `FAILED`, we have:
260264

261-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `ok`
262-
- `tomcat.connector` value = `1`, attributes `port` = `8080` and `connector_state` = `failed`
263-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `degraded`
265+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `ok`
266+
- `tomcat.connector.status` value = `1`, attributes `port` = `8080` and `tomcat.connector.state` = `failed`
267+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `degraded`
264268

265269
For other values of `stateName`, we have:
266270

267-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `ok`
268-
- `tomcat.connector` value = `0`, attributes `port` = `8080` and `connector_state` = `failed`
269-
- `tomcat.connector` value = `1`, attributes `port` = `8080` and `connector_state` = `degraded`
271+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `ok`
272+
- `tomcat.connector.status` value = `0`, attributes `port` = `8080` and `tomcat.connector.state` = `failed`
273+
- `tomcat.connector.status` value = `1`, attributes `port` = `8080` and `tomcat.connector.state` = `degraded`
270274

271275
Each state key can be mapped to one or more values of the MBean attribute using:
272276
- a string literal or a string array
273277
- a `*` character to provide default option and avoid enumerating all values, this value must be quoted in YAML
274278

275279
Exactly one `*` value must be present in the mapping to ensure all possible values of the MBean attribute can be mapped to a state key.
276280

277-
The default value indicated by `*` does not require a dedicated state key. For example, if we want to have `connector_state` metric attribute with values `on` or `off`, we can use:
281+
The default value indicated by `*` does not require a dedicated state key. For example, if we want to have `tomcat.connector.state` metric attribute with values `on` or `off`, we can use:
278282
```yaml
279-
connector_state:
283+
tomcat.connector.state:
280284
on: STARTED
281285
off: [STOPPED,FAILED,'*']
282286
```
283287
In the particular case where only two values are defined, we can simplify further by explicitly defining one state and rely on default for the other.
284288
```yaml
285-
connector_state:
289+
tomcat.connector.state:
286290
on: STARTED
287291
off: '*'
288292
```
289293

290-
State metrics do not have a unit (nor source unit) and use an empty string `""` as unit.
294+
State metrics do not need to define `unit` nor `sourceUnit` attributes, the unit of the metric will always be `1`.
291295

292296
### Metric attributes modifiers
293297

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static List<MetricExtractor> createStateMappingExtractors(
207207
metricInfo.getMetricName(),
208208
metricInfo.getDescription(),
209209
null,
210-
"",
210+
"1",
211211
MetricInfo.Type.UPDOWNCOUNTER);
212212

213213
extractors.add(new MetricExtractor(extractor, stateMetricInfo, stateMetricAttributes));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ void testStateMetricConf() throws Exception {
453453
assertThat(me.getInfo().getMetricName()).isEqualTo("state_metric");
454454
assertThat(me.getInfo().getType()).isEqualTo(MetricInfo.Type.UPDOWNCOUNTER);
455455
assertThat(me.getInfo().getUnit())
456-
.describedAs("state metric unit should be an empty string")
457-
.isEmpty();
456+
.describedAs("state metric unit should be 1")
457+
.isEqualTo("1");
458458

459459
assertThat(me.getAttributes()).hasSize(1);
460460
MetricAttribute stateAttribute = me.getAttributes().get(0);

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/yaml/MetricStructureTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void metricAttribute_beanParam(String target, String value, String expectedValue
8080
"param()", // missing parameter
8181
"param( )", // missing parameter with empty string
8282
"param(name)a", // something after parenthesis
83-
"lowercase()", // misng target in modifier
83+
"lowercase()", // missing target in modifier
8484
"lowercase(param(name)", // missing parenthesis for modifier
8585
"lowercase(missing(name))", // non-existing target within modifier
8686
"lowercase(param())", // missing parameter in modifier

0 commit comments

Comments
 (0)