Skip to content

Migrate "runtime-metrics" test from groovy to java #8928

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

Merged
merged 6 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,17 @@

package io.opentelemetry.instrumentation.javaagent.runtimemetrics.java17;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.awaitility.Awaitility.await;

import io.opentelemetry.instrumentation.testing.AgentTestRunner;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.testing.assertj.MetricAssert;
import java.util.Collection;
import java.util.function.Consumer;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class JfrRuntimeMetricsTest {
@SafeVarargs
private static void waitAndAssertMetrics(Consumer<MetricAssert>... assertions) {
await()
.untilAsserted(
() -> {
Collection<MetricData> metrics = AgentTestRunner.instance().getExportedMetrics();
assertThat(metrics).isNotEmpty();
for (Consumer<MetricAssert> assertion : assertions) {
assertThat(metrics).anySatisfy(metric -> assertion.accept(assertThat(metric)));
}
});
}

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@BeforeAll
static void setUp() {
Expand All @@ -45,7 +31,8 @@ void shouldHaveDefaultMetrics() {
// This should generate some events
System.gc();

waitAndAssertMetrics(
testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry-java17",
metric -> metric.hasName("process.runtime.jvm.cpu.longlock"),
metric -> metric.hasName("process.runtime.jvm.cpu.limit"),
metric -> metric.hasName("process.runtime.jvm.cpu.context_switch"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.javaagent.runtimemetrics.java17;

import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class JmxRuntimeMetricsTest {

@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

@Test
void runtimeMetricsAreEnabled() {
// Force a gc to "ensure" gc metrics
System.gc();

testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry-java8",
metric -> metric.hasName("process.runtime.jvm.classes.loaded"),
metric -> metric.hasName("process.runtime.jvm.classes.unloaded"),
metric -> metric.hasName("process.runtime.jvm.classes.current_loaded"),
metric -> metric.hasName("process.runtime.jvm.system.cpu.load_1m"),
metric -> metric.hasName("process.runtime.jvm.system.cpu.utilization"),
metric -> metric.hasName("process.runtime.jvm.cpu.utilization"),
metric -> metric.hasName("process.runtime.jvm.gc.duration"),
metric -> metric.hasName("process.runtime.jvm.memory.init"),
metric -> metric.hasName("process.runtime.jvm.memory.usage"),
metric -> metric.hasName("process.runtime.jvm.memory.committed"),
metric -> metric.hasName("process.runtime.jvm.memory.limit"),
metric -> metric.hasName("process.runtime.jvm.memory.usage_after_last_gc"),
metric -> metric.hasName("process.runtime.jvm.threads.count"),
metric -> metric.hasName("process.runtime.jvm.buffer.limit"),
metric -> metric.hasName("process.runtime.jvm.buffer.count"),
metric -> metric.hasName("process.runtime.jvm.buffer.usage"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.testing.assertj.MetricAssert;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.time.Duration;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.assertj.core.api.ListAssert;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
Expand Down Expand Up @@ -76,6 +80,13 @@ public List<MetricData> metrics() {
return testRunner.getExportedMetrics();
}

/** Returns a list of metrics, filtered by instrumentation name */
public List<MetricData> instrumentationMetrics(String instrumentationName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have analogous methods for any other signal; and it's only used in this class. WDYT about making it private?

Suggested change
public List<MetricData> instrumentationMetrics(String instrumentationName) {
private List<MetricData> instrumentationMetrics(String instrumentationName) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended it to be a public helper, analogous to metrics() but filtered to a specific instrumentation. It's only used internally now, but I could see it being useful elsewhere. If you think it's too much YAGNI then I will make it private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it private -- there's just one use case for that now; and I think all 3 signals should have very similar API, so if we're introducing some new thing here we should do similar thing everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I relent. :) I think you have a higher standard for test code than I do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I'm just thinking that YAGNI (probably) when it comes to making that method public

return metrics().stream()
.filter(m -> m.getInstrumentationScopeInfo().getName().equals(instrumentationName))
.collect(Collectors.toList());
}

/** Return a list of all captured logs. */
public List<LogRecordData> logRecords() {
return testRunner.getExportedLogRecords();
Expand All @@ -100,6 +111,22 @@ public void waitAndAssertMetrics(
&& data.getName().equals(metricName))));
}

@SafeVarargs
public final void waitAndAssertMetrics(
String instrumentationName, Consumer<MetricAssert>... assertions) {
await()
.untilAsserted(
() -> {
Collection<MetricData> metrics = instrumentationMetrics(instrumentationName);
assertThat(metrics).isNotEmpty();
for (Consumer<MetricAssert> assertion : assertions) {
assertThat(metrics)
.anySatisfy(
metric -> assertion.accept(OpenTelemetryAssertions.assertThat(metric)));
}
});
}

/**
* Removes all captured telemetry data. After calling this method {@link #spans()} and {@link
* #metrics()} will return empty lists until more telemetry data is captured.
Expand Down