-
Notifications
You must be signed in to change notification settings - Fork 130
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
Add buffer handlers and implement buffer metrics #650
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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")); | ||
})))); | ||
} | ||
} |
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.
What's the significance of the polling duration? Why 1s vs. 5s, 10s, etc?
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.
The JFR streaming system flushes the in memory JFR buffers holding event data to the disk repo at 1s intervals, at which point they may be consumed by the handlers. So polling at 1s intervals should provide the most up to date data without adding unnecessary overhead.