1717
1818import static org .assertj .core .api .Assertions .assertThat ;
1919
20- import io .opentelemetry .proto .metrics .v1 .NumberDataPoint ;
20+ import io .opentelemetry .proto .metrics .v1 .Metric ;
2121import java .util .List ;
2222import java .util .Set ;
23- import org .junit .Assert ;
2423import org .junit .jupiter .api .Nested ;
2524import org .junit .jupiter .api .Test ;
2625import org .junit .jupiter .api .TestInstance ;
2726import org .testcontainers .junit .jupiter .Testcontainers ;
2827import software .amazon .opentelemetry .appsignals .test .base .ContractTestBase ;
2928import software .amazon .opentelemetry .appsignals .test .utils .AppSignalsConstants ;
29+ import software .amazon .opentelemetry .appsignals .test .utils .ResourceScopeMetric ;
3030
3131public class RuntimeMetricsTest {
3232 private abstract static class RuntimeMetricsContractTestBase extends ContractTestBase {
@@ -55,7 +55,7 @@ protected void assertRuntimeMetrics() {
5555 var metrics =
5656 mockCollectorClient .getRuntimeMetrics (
5757 Set .of (
58- AppSignalsConstants .JVM_GC_METRIC ,
58+ AppSignalsConstants .JVM_GC_DURATION ,
5959 AppSignalsConstants .JVM_GC_COUNT ,
6060 AppSignalsConstants .JVM_HEAP_USED ,
6161 AppSignalsConstants .JVM_NON_HEAP_USED ,
@@ -68,18 +68,88 @@ protected void assertRuntimeMetrics() {
6868 AppSignalsConstants .LATENCY_METRIC ,
6969 AppSignalsConstants .ERROR_METRIC ,
7070 AppSignalsConstants .FAULT_METRIC ));
71- metrics .forEach (
72- metric -> {
73- var dataPoints = metric .getMetric ().getGauge ().getDataPointsList ();
74- assertNonNegativeValue (dataPoints );
75- });
71+
72+ testResourceAttributes (metrics );
73+ for (String metricName : List .of (AppSignalsConstants .JVM_POOL_USED )) {
74+ testGaugeMetrics (metrics , metricName , "name" );
75+ }
76+ for (String metricName :
77+ List .of (
78+ AppSignalsConstants .JVM_HEAP_USED ,
79+ AppSignalsConstants .JVM_NON_HEAP_USED ,
80+ AppSignalsConstants .JVM_AFTER_GC ,
81+ AppSignalsConstants .JVM_THREAD_COUNT ,
82+ AppSignalsConstants .JVM_CLASS_LOADED ,
83+ AppSignalsConstants .JVM_CPU_UTILIZATION )) {
84+ testGaugeMetrics (metrics , metricName , "" );
85+ }
86+ for (String metricName :
87+ List .of (AppSignalsConstants .JVM_GC_DURATION , AppSignalsConstants .JVM_GC_COUNT )) {
88+ testCounterMetrics (metrics , metricName , "name" );
89+ }
90+ for (String metricName : List .of (AppSignalsConstants .JVM_CPU_TIME )) {
91+ testCounterMetrics (metrics , metricName , "" );
92+ }
93+ }
94+
95+ private void testGaugeMetrics (
96+ List <ResourceScopeMetric > resourceScopeMetrics , String metricName , String attributeKey ) {
97+ for (ResourceScopeMetric rsm : resourceScopeMetrics ) {
98+ Metric metric = rsm .getMetric ();
99+ if (metricName .equals (metric .getName ())) {
100+ assertThat (metric .getGauge ().getDataPointsList ())
101+ .as (metricName + " is not empty" )
102+ .isNotEmpty ();
103+ assertThat (metric .getGauge ().getDataPointsList ())
104+ .as (metricName + " is valid" )
105+ .allMatch (
106+ dp -> {
107+ boolean valid = true ;
108+ if (!attributeKey .isEmpty ()) {
109+ valid =
110+ dp .getAttributesList ().stream ()
111+ .anyMatch (attribute -> attribute .getKey ().equals (attributeKey ));
112+ }
113+ return valid && dp .getAsInt () >= 0 ;
114+ });
115+ }
116+ }
117+ }
118+
119+ private void testCounterMetrics (
120+ List <ResourceScopeMetric > resourceScopeMetrics , String metricName , String attributeKey ) {
121+ for (ResourceScopeMetric rsm : resourceScopeMetrics ) {
122+ Metric metric = rsm .getMetric ();
123+ if (metricName .equals (metric .getName ())) {
124+ assertThat (metric .getSum ().getDataPointsList ())
125+ .as (metricName + " is not empty" )
126+ .isNotEmpty ();
127+ assertThat (metric .getSum ().getDataPointsList ())
128+ .as (metricName + " is valid" )
129+ .allMatch (
130+ dp -> {
131+ boolean valid = true ;
132+ if (!attributeKey .isEmpty ()) {
133+ valid =
134+ dp .getAttributesList ().stream ()
135+ .anyMatch (attribute -> attribute .getKey ().equals (attributeKey ));
136+ }
137+ return valid && dp .getAsInt () >= 0 ;
138+ });
139+ }
140+ }
76141 }
77142
78- private void assertNonNegativeValue (List <NumberDataPoint > dps ) {
79- dps .forEach (
80- datapoint -> {
81- Assert .assertTrue (datapoint .getAsInt () >= 0 );
82- });
143+ private void testResourceAttributes (List <ResourceScopeMetric > resourceScopeMetrics ) {
144+ for (ResourceScopeMetric rsm : resourceScopeMetrics ) {
145+ assertThat (rsm .getResource ().getResource ().getAttributesList ())
146+ .anyMatch (
147+ attr ->
148+ attr .getKey ().equals (AppSignalsConstants .AWS_LOCAL_SERVICE )
149+ && attr .getValue ()
150+ .getStringValue ()
151+ .equals (getApplicationOtelServiceName ()));
152+ }
83153 }
84154 }
85155
0 commit comments