@@ -112,6 +112,37 @@ void singleMetricsCaptureUtility() {
112
112
.containsEntry ("Dimension1" , "Value1" )
113
113
.containsKey ("_aws" )
114
114
.containsEntry ("xray_trace_id" , "1-5759e988-bd862e3fe1be46a994272793" );
115
+
116
+ Map <String , Object > aws = (Map <String , Object >) logAsJson .get ("_aws" );
117
+
118
+ assertThat (aws .get ("CloudWatchMetrics" ))
119
+ .asString ()
120
+ .contains ("Namespace=test" );
121
+ });
122
+ }
123
+ }
124
+
125
+ @ Test
126
+ void singleMetricsCaptureUtilityWithNullNamespace () {
127
+ try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class );
128
+ MockedStatic <software .amazon .lambda .powertools .core .internal .SystemWrapper > internalWrapper = mockStatic (
129
+ software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
130
+ mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
131
+ // POWERTOOLS_METRICS_NAMESPACE is not defined
132
+
133
+ MetricsUtils .withSingleMetric ("Metric1" , 1 , Unit .COUNT ,
134
+ metricsLogger -> metricsLogger .setDimensions (DimensionSet .of ("Dimension1" , "Value1" )));
135
+
136
+ assertThat (out .toString ())
137
+ .satisfies (s ->
138
+ {
139
+ Map <String , Object > logAsJson = readAsJson (s );
140
+
141
+ Map <String , Object > aws = (Map <String , Object >) logAsJson .get ("_aws" );
142
+
143
+ assertThat (aws .get ("CloudWatchMetrics" ))
144
+ .asString ()
145
+ .contains ("Namespace=aws-embedded-metrics" );
115
146
});
116
147
}
117
148
}
0 commit comments