-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*Description of changes:* Adds contract tests to verify the expected behavior of the following changes: - #817 - #898 - #901 Starts up sample apps with the instrumented SDK and verifies that metrics are received for each of the supported JMX target systems. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
12 changed files
with
800 additions
and
46 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
81 changes: 81 additions & 0 deletions
81
...t/java/software/amazon/opentelemetry/appsignals/test/base/JMXMetricsContractTestBase.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,81 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.appsignals.test.base; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.proto.metrics.v1.NumberDataPoint; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import software.amazon.opentelemetry.appsignals.test.utils.JMXMetricsConstants; | ||
|
||
public abstract class JMXMetricsContractTestBase extends ContractTestBase { | ||
|
||
@Override | ||
protected Map<String, String> getApplicationEnvironmentVariables() { | ||
return Map.of( | ||
"JAVA_TOOL_OPTIONS", "-javaagent:" + MOUNT_PATH, | ||
"OTEL_METRIC_EXPORT_INTERVAL", "100", // 100 ms | ||
"OTEL_METRICS_EXPORTER", "none", | ||
"OTEL_LOGS_EXPORTER", "none", | ||
"OTEL_TRACES_EXPORTER", "none", | ||
"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf", | ||
"OTEL_JMX_ENABLED", "true", | ||
"OTEL_AWS_JMX_EXPORTER_METRICS_ENDPOINT", COLLECTOR_HTTP_ENDPOINT + "/v1/metrics"); | ||
} | ||
|
||
protected void doTestMetrics() { | ||
var response = appClient.get("/success").aggregate().join(); | ||
|
||
assertThat(response.status().isSuccess()).isTrue(); | ||
assertMetrics(); | ||
} | ||
|
||
protected void assertMetrics() { | ||
var metrics = mockCollectorClient.getRuntimeMetrics(getExpectedMetrics()); | ||
metrics.forEach( | ||
metric -> { | ||
var dataPoints = metric.getMetric().getGauge().getDataPointsList(); | ||
assertGreaterThanOrEqual(dataPoints, getThreshold(metric.getMetric().getName())); | ||
}); | ||
} | ||
|
||
protected abstract Set<String> getExpectedMetrics(); | ||
|
||
protected long getThreshold(String metricName) { | ||
long threshold = 0; | ||
switch (metricName) { | ||
// If maximum memory size is undefined, then value is -1 | ||
// https://docs.oracle.com/en/java/javase/17/docs/api/java.management/java/lang/management/MemoryUsage.html#getMax() | ||
case JMXMetricsConstants.JVM_HEAP_MAX: | ||
case JMXMetricsConstants.JVM_NON_HEAP_MAX: | ||
case JMXMetricsConstants.JVM_POOL_MAX: | ||
threshold = -1; | ||
default: | ||
} | ||
return threshold; | ||
} | ||
|
||
private void assertGreaterThanOrEqual(List<NumberDataPoint> dps, long threshold) { | ||
assertDataPoints(dps, (value) -> assertThat(value).isGreaterThanOrEqualTo(threshold)); | ||
} | ||
|
||
private void assertDataPoints(List<NumberDataPoint> dps, Consumer<Long> consumer) { | ||
dps.forEach(datapoint -> consumer.accept(datapoint.getAsInt())); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../src/test/java/software/amazon/opentelemetry/appsignals/test/misc/jmx/JVMMetricsTest.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 Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.appsignals.test.misc.jmx; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import software.amazon.opentelemetry.appsignals.test.base.JMXMetricsContractTestBase; | ||
import software.amazon.opentelemetry.appsignals.test.utils.JMXMetricsConstants; | ||
|
||
/** | ||
* Tests in this class validate that the SDK will emit JVM metrics when Application Signals runtime | ||
* metrics are enabled. | ||
*/ | ||
@Testcontainers(disabledWithoutDocker = true) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class JVMMetricsTest extends JMXMetricsContractTestBase { | ||
@Test | ||
void testJVMMetrics() { | ||
doTestMetrics(); | ||
} | ||
|
||
@Override | ||
protected String getApplicationImageName() { | ||
return "aws-appsignals-tests-http-server-spring-mvc"; | ||
} | ||
|
||
@Override | ||
protected String getApplicationWaitPattern() { | ||
return ".*Started Application.*"; | ||
} | ||
|
||
@Override | ||
protected Set<String> getExpectedMetrics() { | ||
return JMXMetricsConstants.JVM_METRICS_SET; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getApplicationExtraEnvironmentVariables() { | ||
return Map.of("OTEL_JMX_TARGET_SYSTEM", "jvm"); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...t/java/software/amazon/opentelemetry/appsignals/test/misc/jmx/KafkaBrokerMetricsTest.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,96 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.appsignals.test.misc.jmx; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.*; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.KafkaContainer; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
import org.testcontainers.images.PullPolicy; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.MountableFile; | ||
import software.amazon.opentelemetry.appsignals.test.base.JMXMetricsContractTestBase; | ||
import software.amazon.opentelemetry.appsignals.test.utils.JMXMetricsConstants; | ||
|
||
@Testcontainers(disabledWithoutDocker = true) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class KafkaBrokerMetricsTest extends JMXMetricsContractTestBase { | ||
@Test | ||
void testKafkaMetrics() { | ||
assertMetrics(); | ||
} | ||
|
||
@Override | ||
protected GenericContainer<?> getApplicationContainer() { | ||
return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) | ||
.withImagePullPolicy(PullPolicy.alwaysPull()) | ||
.withNetworkAliases("kafkaBroker") | ||
.withNetwork(network) | ||
.withLogConsumer(new Slf4jLogConsumer(applicationLogger)) | ||
.withCopyFileToContainer(MountableFile.forHostPath(AGENT_PATH), MOUNT_PATH) | ||
.withEnv(getApplicationEnvironmentVariables()) | ||
.withEnv(getApplicationExtraEnvironmentVariables()) | ||
.waitingFor(getApplicationWaitCondition()) | ||
.withKraft(); | ||
} | ||
|
||
@BeforeAll | ||
public void setup() throws IOException, InterruptedException { | ||
application.start(); | ||
application.execInContainer( | ||
"/bin/sh", | ||
"-c", | ||
"/usr/bin/kafka-topics --bootstrap-server=localhost:9092 --create --topic kafka_topic --partitions 3 --replication-factor 1"); | ||
mockCollectorClient = getMockCollectorClient(); | ||
} | ||
|
||
// don't use the default clients | ||
@BeforeEach | ||
@Override | ||
protected void setupClients() {} | ||
|
||
@Override | ||
protected String getApplicationImageName() { | ||
return "aws-appsignals-tests-kafka"; | ||
} | ||
|
||
@Override | ||
protected String getApplicationWaitPattern() { | ||
return ".* Kafka Server started .*"; | ||
} | ||
|
||
@Override | ||
protected Set<String> getExpectedMetrics() { | ||
return JMXMetricsConstants.KAFKA_METRICS_SET; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getApplicationExtraEnvironmentVariables() { | ||
return Map.of( | ||
"JAVA_TOOL_OPTIONS", // kafka broker container will not complete startup if agent is set | ||
"", | ||
"KAFKA_OPTS", // replace java tool options with kafka opts | ||
"-javaagent:" + MOUNT_PATH, | ||
"KAFKA_AUTO_CREATE_TOPICS_ENABLE", | ||
"false", | ||
"OTEL_JMX_TARGET_SYSTEM", | ||
"kafka"); | ||
} | ||
} |
Oops, something went wrong.