-
Notifications
You must be signed in to change notification settings - Fork 133
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
Implement memory metrics #652
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f51a03
memory handlers
roberttoyonaga b333de5
apply feedback from other PRs to this one.
roberttoyonaga 8f3fda9
apply feedback from related PRs.
roberttoyonaga 24281d8
fix conflicts
roberttoyonaga 10607e1
style
roberttoyonaga d7463b8
Merge branch 'main' of github.com:roberttoyonaga/opentelemetry-java-c…
roberttoyonaga 74b3569
remove java heap pool
roberttoyonaga b6d5f78
test suites for each GC types
roberttoyonaga 4a84059
remove sanity check tests
roberttoyonaga 4436706
combine memory tests per GC. Fix bug in GC duration tests to account …
roberttoyonaga 6b80cfa
use testFixtures to avoid code duplication. Rename base test class.
roberttoyonaga 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
53 changes: 53 additions & 0 deletions
53
...streaming/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/AbstractMetricsTest.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,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.data.MetricData; | ||
import io.opentelemetry.sdk.testing.assertj.MetricAssert; | ||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; | ||
import java.util.Collection; | ||
import java.util.function.Consumer; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
public class AbstractMetricsTest { | ||
|
||
static SdkMeterProvider meterProvider; | ||
static InMemoryMetricReader metricReader; | ||
static boolean isInitialized = false; | ||
|
||
@BeforeAll | ||
static void initializeOpenTelemetry() { | ||
if (isInitialized) { | ||
return; | ||
} | ||
isInitialized = true; | ||
metricReader = InMemoryMetricReader.create(); | ||
meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).build(); | ||
GlobalOpenTelemetry.set(OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build()); | ||
JfrMetrics.enable(meterProvider); | ||
} | ||
|
||
@SafeVarargs | ||
protected final void waitAndAssertMetrics(Consumer<MetricAssert>... assertions) { | ||
await() | ||
.untilAsserted( | ||
() -> { | ||
Collection<MetricData> metrics = metricReader.collectAllMetrics(); | ||
|
||
assertThat(metrics).isNotEmpty(); | ||
|
||
for (Consumer<MetricAssert> assertion : assertions) { | ||
assertThat(metrics).anySatisfy(metric -> assertion.accept(assertThat(metric))); | ||
} | ||
}); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...treaming/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/G1GcMemoryMetricTest.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,124 @@ | ||
/* | ||
* 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_ACTION; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_G1_EDEN_SPACE; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_G1_SURVIVOR_SPACE; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_GC; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.BYTES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.END_OF_MAJOR_GC; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.END_OF_MINOR_GC; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_COMMITTED; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_GC_DURATION; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_MEMORY; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_MEMORY_AFTER; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_NAME_MEMORY; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_NAME_MEMORY_AFTER; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.MILLISECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.metrics.data.HistogramData; | ||
import io.opentelemetry.sdk.metrics.data.HistogramPointData; | ||
import io.opentelemetry.sdk.metrics.data.MetricData; | ||
import io.opentelemetry.sdk.metrics.data.SumData; | ||
import org.assertj.core.api.ThrowingConsumer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class G1GcMemoryMetricTest extends AbstractMetricsTest { | ||
private void usageCheck(ThrowingConsumer<MetricData> attributeCheck) { | ||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName(METRIC_NAME_MEMORY) | ||
.hasUnit(BYTES) | ||
.hasDescription(METRIC_DESCRIPTION_MEMORY) | ||
.satisfies(attributeCheck), | ||
metric -> | ||
metric | ||
.hasName(METRIC_NAME_MEMORY_AFTER) | ||
.hasUnit(BYTES) | ||
.hasDescription(METRIC_DESCRIPTION_MEMORY_AFTER) | ||
.satisfies(attributeCheck)); | ||
} | ||
|
||
/** | ||
* This is a basic test for process.runtime.jvm.memory.usage and | ||
* process.runtime.jvm.memory.usage_after_last_gc metrics. | ||
*/ | ||
@Test | ||
void shouldHaveMemoryUsageMetrics() { | ||
System.gc(); | ||
// Test to make sure there's metric data for both eden and survivor spaces. | ||
// TODO: once G1 old gen usage added to jdk.G1HeapSummary (in JDK 21), test for it here too. | ||
usageCheck( | ||
metricData -> { | ||
SumData<?> sumData = metricData.getLongSumData(); | ||
assertThat(sumData.getPoints()) | ||
.anyMatch(p -> p.getAttributes().equals(ATTR_G1_EDEN_SPACE)) | ||
.anyMatch(p -> p.getAttributes().equals(ATTR_G1_SURVIVOR_SPACE)); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldHaveMemoryLimitMetrics() { | ||
// TODO: needs JFR support. Placeholder. | ||
} | ||
|
||
@Test | ||
void shouldHaveMemoryCommittedMetrics() { | ||
System.gc(); | ||
// TODO: need JFR support for the other G1 pools | ||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.memory.committed") | ||
.hasUnit(BYTES) | ||
.hasDescription(METRIC_DESCRIPTION_COMMITTED) | ||
.satisfies( | ||
metricData -> { | ||
SumData<?> sumData = metricData.getLongSumData(); | ||
assertThat(sumData.getPoints()) | ||
.anyMatch(p -> p.getAttributes().equals(ATTR_G1_EDEN_SPACE)); | ||
})); | ||
} | ||
|
||
@Test | ||
void shouldHaveGCDurationMetrics() { | ||
// TODO: Need a reliable way to test old and young gen GC in isolation. | ||
System.gc(); | ||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.gc.duration") | ||
.hasUnit(MILLISECONDS) | ||
.hasDescription(METRIC_DESCRIPTION_GC_DURATION) | ||
.satisfies( | ||
metricData -> { | ||
HistogramData data = metricData.getHistogramData(); | ||
assertThat(data.getPoints()) | ||
.map(HistogramPointData.class::cast) | ||
.anyMatch( | ||
p -> | ||
p.getSum() > 0 | ||
&& (p.getAttributes() | ||
.equals( | ||
Attributes.of( | ||
ATTR_GC, | ||
"G1 Young Generation", | ||
ATTR_ACTION, | ||
END_OF_MINOR_GC)) | ||
|| p.getAttributes() | ||
.equals( | ||
Attributes.of( | ||
ATTR_GC, | ||
"G1 Old Generation", | ||
ATTR_ACTION, | ||
END_OF_MAJOR_GC)))); | ||
})); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...a/io/opentelemetry/contrib/jfr/metrics/internal/memory/CodeCacheConfigurationHandler.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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics.internal.memory; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_POOL; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_TYPE; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.BYTES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.INITIAL_SIZE; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_MEMORY_INIT; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_NAME_MEMORY_INIT; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.NON_HEAP; | ||
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; | ||
|
||
/** Handles attributes with pool value CodeCache */ | ||
public final class CodeCacheConfigurationHandler implements RecordedEventHandler { | ||
private static final String EVENT_NAME = "jdk.CodeCacheConfiguration"; | ||
|
||
private static final Attributes ATTR = Attributes.of(ATTR_TYPE, NON_HEAP, ATTR_POOL, "CodeCache"); | ||
|
||
private volatile long initialSize = 0; | ||
|
||
public CodeCacheConfigurationHandler() { | ||
initializeMeter(defaultMeter()); | ||
} | ||
|
||
@Override | ||
public void initializeMeter(Meter meter) { | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME_MEMORY_INIT) | ||
.setDescription(METRIC_DESCRIPTION_MEMORY_INIT) | ||
.setUnit(BYTES) | ||
.buildWithCallback(measurement -> measurement.record(initialSize, ATTR)); | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public void accept(RecordedEvent event) { | ||
if (event.hasField(INITIAL_SIZE)) { | ||
initialSize = event.getLong(INITIAL_SIZE); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Duration> getPollingDuration() { | ||
return Optional.of(Duration.ofSeconds(1)); | ||
} | ||
} |
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.
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.
Side note: This abstract base class needs some work:
AbstractJfrTest
JfrMetrics
can't be cancelled once started. We should change that.Can do all this in a future PR.
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.
Yup I agree with all those points.
Maybe
@BeforeEach
test I can reinitialize JFR streaming and all openTelemetry variables, and@AfterEach
test the JFR streaming can be ended. I think that should be enough for tests to fully clean up after themselves.I was trying to figure out a good way of doing this today. Maybe something related to test fixtures. I'll have to investigate more.
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.
Update:
I renamed the base test class to
AbstractJfrTest
. I was able to get thetestFixtures
working. So now the class resides in a single place, instead of being duplicated for each GC type.