-
Notifications
You must be signed in to change notification settings - Fork 124
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
+845
−299
Merged
Changes from 9 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))); | ||
} | ||
}); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...eaming/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/G1GcDurationMetricTest.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,52 @@ | ||
/* | ||
* 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_GC; | ||
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_GC_DURATION; | ||
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.PointData; | ||
import org.assertj.core.api.ThrowingConsumer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class G1GcDurationMetricTest extends AbstractMetricsTest { | ||
static class AttributeCheck implements ThrowingConsumer<PointData> { | ||
roberttoyonaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
public void acceptThrows(PointData pointData) { | ||
// Each point must have attributes of one of the following variation: | ||
// First sort by Major/Minor, then by GC. | ||
if (pointData.getAttributes().get(ATTR_ACTION).equals(END_OF_MINOR_GC)) { | ||
assertThat(pointData.getAttributes()) | ||
.isEqualTo(Attributes.of(ATTR_GC, "G1 Young Generation", ATTR_ACTION, END_OF_MINOR_GC)); | ||
} else { | ||
assertThat(pointData.getAttributes()) | ||
.isEqualTo(Attributes.of(ATTR_GC, "G1 Old Generation", ATTR_ACTION, END_OF_MAJOR_GC)); | ||
} | ||
} | ||
} | ||
|
||
@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) | ||
.hasHistogramSatisfying( | ||
histogram -> | ||
histogram.hasPointsSatisfying( | ||
point -> point.hasSumGreaterThan(0).satisfies(new AttributeCheck())))); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...g/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/G1MemoryCommittedMetricTest.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,40 @@ | ||
/* | ||
* 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_G1_EDEN_SPACE; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.BYTES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.METRIC_DESCRIPTION_COMMITTED; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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 G1MemoryCommittedMetricTest extends AbstractMetricsTest { | ||
private void check(ThrowingConsumer<MetricData> attributeCheck) { | ||
roberttoyonaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.memory.committed") | ||
.hasUnit(BYTES) | ||
.hasDescription(METRIC_DESCRIPTION_COMMITTED) | ||
.satisfies(attributeCheck)); | ||
} | ||
|
||
@Test | ||
void shouldHaveMemoryCommittedMetrics() { | ||
System.gc(); | ||
// TODO: need JFR support for the other G1 pools | ||
check( | ||
metricData -> { | ||
SumData<?> sumData = metricData.getLongSumData(); | ||
assertThat(sumData.getPoints()) | ||
.anyMatch(p -> p.getAttributes().equals(ATTR_G1_EDEN_SPACE)); | ||
}); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...aming/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/G1MemoryLimitMetricTest.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,15 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class G1MemoryLimitMetricTest extends AbstractMetricsTest { | ||
roberttoyonaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Test | ||
void shouldHaveMemoryLimitMetrics() { | ||
// TODO: needs JFR support. Placeholder. | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...aming/src/g1GcTest/java/io/opentelemetry/contrib/jfr/metrics/G1MemoryUsageMetricTest.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,57 @@ | ||
/* | ||
* 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_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.BYTES; | ||
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 org.assertj.core.api.Assertions.assertThat; | ||
|
||
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 G1MemoryUsageMetricTest extends AbstractMetricsTest { | ||
|
||
private void check(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. | ||
check( | ||
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)); | ||
}); | ||
} | ||
} |
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
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.