|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.appsignals.test.misc; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import io.opentelemetry.proto.metrics.v1.NumberDataPoint; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.jupiter.api.Nested; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.TestInstance; |
| 27 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 28 | +import software.amazon.opentelemetry.appsignals.test.base.ContractTestBase; |
| 29 | +import software.amazon.opentelemetry.appsignals.test.utils.AppSignalsConstants; |
| 30 | + |
| 31 | +public class RuntimeMetricsTest { |
| 32 | + private abstract static class RuntimeMetricsContractTestBase extends ContractTestBase { |
| 33 | + @Override |
| 34 | + protected String getApplicationImageName() { |
| 35 | + return "aws-appsignals-tests-http-server-spring-mvc"; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected String isRuntimeEnabled() { |
| 40 | + return "true"; |
| 41 | + } |
| 42 | + |
| 43 | + protected String getApplicationWaitPattern() { |
| 44 | + return ".*Started Application.*"; |
| 45 | + } |
| 46 | + |
| 47 | + protected void doTestRuntimeMetrics() { |
| 48 | + var response = appClient.get("/success").aggregate().join(); |
| 49 | + |
| 50 | + assertThat(response.status().isSuccess()).isTrue(); |
| 51 | + assertRuntimeMetrics(); |
| 52 | + } |
| 53 | + |
| 54 | + protected void assertRuntimeMetrics() { |
| 55 | + var metrics = |
| 56 | + mockCollectorClient.getRuntimeMetrics( |
| 57 | + Set.of( |
| 58 | + AppSignalsConstants.JVM_GC_METRIC, |
| 59 | + AppSignalsConstants.JVM_GC_COUNT, |
| 60 | + AppSignalsConstants.JVM_HEAP_USED, |
| 61 | + AppSignalsConstants.JVM_NON_HEAP_USED, |
| 62 | + AppSignalsConstants.JVM_AFTER_GC, |
| 63 | + AppSignalsConstants.JVM_POOL_USED, |
| 64 | + AppSignalsConstants.JVM_THREAD_COUNT, |
| 65 | + AppSignalsConstants.JVM_CLASS_LOADED, |
| 66 | + AppSignalsConstants.JVM_CPU_TIME, |
| 67 | + AppSignalsConstants.JVM_CPU_UTILIZATION, |
| 68 | + AppSignalsConstants.LATENCY_METRIC, |
| 69 | + AppSignalsConstants.ERROR_METRIC, |
| 70 | + AppSignalsConstants.FAULT_METRIC)); |
| 71 | + metrics.forEach( |
| 72 | + metric -> { |
| 73 | + var dataPoints = metric.getMetric().getGauge().getDataPointsList(); |
| 74 | + assertNonNegativeValue(dataPoints); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private void assertNonNegativeValue(List<NumberDataPoint> dps) { |
| 79 | + dps.forEach( |
| 80 | + datapoint -> { |
| 81 | + Assert.assertTrue(datapoint.getAsInt() >= 0); |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Testcontainers(disabledWithoutDocker = true) |
| 87 | + @TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 88 | + @Nested |
| 89 | + class ValidateRuntimeMetricsTest extends RuntimeMetricsContractTestBase { |
| 90 | + @Test |
| 91 | + void testRuntimeMetrics() { |
| 92 | + doTestRuntimeMetrics(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments