-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buffer handlers and implement buffer metrics (#650)
**Description:** Add handlers for buffer metrics `process.runtime.jvm.buffer.limit`, `process.runtime.jvm.buffer.count`, and `process.runtime.jvm.buffer.usage`. This was a part of a larger PR here: #644. `jfr-streaming/src/main/java/io/opentelemetry/contrib/jfr/metrics/internal/Constants.java` has been kept the same as it was in the larger PR because it's used in all of the smaller constituent PRs. This will hopefully also avoid some conflicts as the PRs are sequentially merged. As a result, there are some constants that are defined, but not used in this PR.
- Loading branch information
1 parent
ef32cf6
commit 777ba03
Showing
4 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
90 changes: 90 additions & 0 deletions
90
...a/io/opentelemetry/contrib/jfr/metrics/internal/buffer/DirectBufferStatisticsHandler.java
This file contains 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,90 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics.internal.buffer; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_POOL; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.BYTES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_BUFFERS; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler.defaultMeter; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import jdk.jfr.consumer.RecordedEvent; | ||
|
||
public final class DirectBufferStatisticsHandler implements RecordedEventHandler { | ||
private static final String METRIC_NAME_USAGE = "process.runtime.jvm.buffer.usage"; | ||
private static final String METRIC_NAME_LIMIT = "process.runtime.jvm.buffer.limit"; | ||
private static final String METRIC_NAME_COUNT = "process.runtime.jvm.buffer.count"; | ||
private static final String METRIC_DESCRIPTION_USAGE = "Measure of memory used by buffers"; | ||
private static final String METRIC_DESCRIPTION_LIMIT = | ||
"Measure of total memory capacity of buffers"; | ||
private static final String METRIC_DESCRIPTION_COUNT = "Number of buffers in the pool"; | ||
private static final String COUNT = "count"; | ||
private static final String MAX_CAPACITY = "maxCapacity"; | ||
private static final String MEMORY_USED = "memoryUsed"; | ||
|
||
private static final String EVENT_NAME = "jdk.DirectBufferStatistics"; | ||
private static final Attributes ATTR = Attributes.of(ATTR_POOL, "direct"); | ||
|
||
private volatile long usage = 0; | ||
private volatile long limit = 0; | ||
private volatile long count = 0; | ||
|
||
public DirectBufferStatisticsHandler() { | ||
initializeMeter(defaultMeter()); | ||
} | ||
|
||
@Override | ||
public void accept(RecordedEvent ev) { | ||
if (ev.hasField(COUNT)) { | ||
count = ev.getLong(COUNT); | ||
} | ||
if (ev.hasField(MAX_CAPACITY)) { | ||
limit = ev.getLong(MAX_CAPACITY); | ||
} | ||
if (ev.hasField(MEMORY_USED)) { | ||
usage = ev.getLong(MEMORY_USED); | ||
} | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public void initializeMeter(Meter meter) { | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME_USAGE) | ||
.setDescription(METRIC_DESCRIPTION_USAGE) | ||
.setUnit(BYTES) | ||
.buildWithCallback( | ||
measurement -> { | ||
measurement.record(usage, ATTR); | ||
}); | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME_LIMIT) | ||
.setDescription(METRIC_DESCRIPTION_LIMIT) | ||
.setUnit(BYTES) | ||
.buildWithCallback( | ||
measurement -> { | ||
measurement.record(limit, ATTR); | ||
}); | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME_COUNT) | ||
.setDescription(METRIC_DESCRIPTION_COUNT) | ||
.setUnit(UNIT_BUFFERS) | ||
.buildWithCallback(measurement -> measurement.record(count, ATTR)); | ||
} | ||
|
||
@Override | ||
public Optional<Duration> getPollingDuration() { | ||
return Optional.of(Duration.ofSeconds(1)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
jfr-streaming/src/test/java/io/opentelemetry/contrib/jfr/metrics/BufferMetricTest.java
This file contains 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,83 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_POOL; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.BYTES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_BUFFERS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BufferMetricTest extends AbstractMetricsTest { | ||
|
||
/** | ||
* This is a basic test that allocates some buffers and tests to make sure the resulting JFR event | ||
* was handled and turned into the expected metrics. | ||
* | ||
* <p>This test handles all 3 buffer related metrics defined in the OpenTelemetry Java runtime | ||
* Semantic Conventions. | ||
* | ||
* <p>Currently JFR only has support for the "direct" buffer pool. The "mapped" and "mapped - | ||
* 'non-volatile memory'" pools do not have corresponding JFR events. In the future, events should | ||
* be added for those missing pools. | ||
*/ | ||
@Test | ||
void shouldHaveJfrLoadedClassesCountEvents() { | ||
ByteBuffer buffer = ByteBuffer.allocateDirect(10000); | ||
buffer.put("test".getBytes(StandardCharsets.UTF_8)); | ||
|
||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.buffer.count") | ||
.hasDescription("Number of buffers in the pool") | ||
.hasUnit(UNIT_BUFFERS) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> { | ||
assertThat(pointData.getValue()).isGreaterThan(0); | ||
assertThat(pointData.getAttributes()) | ||
.isEqualTo(Attributes.of(ATTR_POOL, "direct")); | ||
}))), | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.buffer.limit") | ||
.hasDescription("Measure of total memory capacity of buffers") | ||
.hasUnit(BYTES) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> { | ||
assertThat(pointData.getValue()).isGreaterThan(0); | ||
assertThat(pointData.getAttributes()) | ||
.isEqualTo(Attributes.of(ATTR_POOL, "direct")); | ||
}))), | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.buffer.usage") | ||
.hasDescription("Measure of memory used by buffers") | ||
.hasUnit(BYTES) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> { | ||
assertThat(pointData.getValue()).isGreaterThan(0); | ||
assertThat(pointData.getAttributes()) | ||
.isEqualTo(Attributes.of(ATTR_POOL, "direct")); | ||
})))); | ||
} | ||
} |