18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
19
import static org .assertj .core .api .Assertions .assertThatNullPointerException ;
20
20
import static org .mockito .Mockito .mockStatic ;
21
+ import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getProperty ;
21
22
import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getenv ;
22
23
23
24
import com .fasterxml .jackson .core .JsonProcessingException ;
@@ -66,7 +67,7 @@ void singleMetricsCaptureUtilityWithDefaultDimension() {
66
67
software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
67
68
mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
68
69
internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
69
- .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
70
+ .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1" );
70
71
71
72
MetricsUtils .defaultDimensions (DimensionSet .of ("Service" , "Booking" ));
72
73
@@ -96,7 +97,7 @@ void singleMetricsCaptureUtility() {
96
97
software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
97
98
mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
98
99
internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
99
- .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
100
+ .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1" );
100
101
101
102
MetricsUtils .withSingleMetric ("Metric1" , 1 , Unit .COUNT , "test" ,
102
103
metricsLogger -> metricsLogger .setDimensions (DimensionSet .of ("Dimension1" , "Value1" )));
@@ -123,7 +124,7 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
123
124
mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
124
125
mocked .when (() -> SystemWrapper .getenv ("POWERTOOLS_METRICS_NAMESPACE" )).thenReturn ("GlobalName" );
125
126
internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
126
- .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
127
+ .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1" );
127
128
128
129
MetricsUtils .withSingleMetric ("Metric1" , 1 , Unit .COUNT ,
129
130
metricsLogger -> metricsLogger .setDimensions (DimensionSet .of ("Dimension1" , "Value1" )));
@@ -165,14 +166,49 @@ void shouldThrowExceptionWhenDefaultDimensionIsNull() {
165
166
.withMessage ("Null dimension set not allowed" );
166
167
}
167
168
169
+ @ Test
170
+ void shouldUseTraceIdFromSystemPropertyIfEnvVarNotPresent () {
171
+ try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class );
172
+ MockedStatic <software .amazon .lambda .powertools .core .internal .SystemWrapper > internalWrapper = mockStatic (
173
+ software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
174
+ mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
175
+ mocked .when (() -> SystemWrapper .getenv ("POWERTOOLS_METRICS_NAMESPACE" )).thenReturn ("GlobalName" );
176
+ internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
177
+ .thenReturn (null );
178
+ internalWrapper .when (() -> getProperty ("com.amazonaws.xray.traceHeader" ))
179
+ .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1" );
180
+
181
+ MetricsUtils .withSingleMetric ("Metric1" , 1 , Unit .COUNT ,
182
+ metricsLogger -> metricsLogger .setDimensions (DimensionSet .of ("Dimension1" , "Value1" )));
183
+
184
+ assertThat (out .toString ())
185
+ .satisfies (s ->
186
+ {
187
+ Map <String , Object > logAsJson = readAsJson (s );
188
+
189
+ assertThat (logAsJson )
190
+ .containsEntry ("Metric1" , 1.0 )
191
+ .containsEntry ("Dimension1" , "Value1" )
192
+ .containsKey ("_aws" )
193
+ .containsEntry ("xray_trace_id" , "1-5759e988-bd862e3fe1be46a994272793" );
194
+
195
+ Map <String , Object > aws = (Map <String , Object >) logAsJson .get ("_aws" );
196
+
197
+ assertThat (aws .get ("CloudWatchMetrics" ))
198
+ .asString ()
199
+ .contains ("Namespace=GlobalName" );
200
+ });
201
+ }
202
+ }
203
+
168
204
private void testLogger (Consumer <Consumer <MetricsLogger >> methodToTest ) {
169
205
try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class );
170
206
MockedStatic <software .amazon .lambda .powertools .core .internal .SystemWrapper > internalWrapper = mockStatic (
171
207
software .amazon .lambda .powertools .core .internal .SystemWrapper .class )) {
172
208
mocked .when (() -> SystemWrapper .getenv ("AWS_EMF_ENVIRONMENT" )).thenReturn ("Lambda" );
173
209
mocked .when (() -> SystemWrapper .getenv ("POWERTOOLS_METRICS_NAMESPACE" )).thenReturn ("GlobalName" );
174
210
internalWrapper .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
175
- .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
211
+ .thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1" );
176
212
177
213
methodToTest .accept (metricsLogger ->
178
214
{
0 commit comments