Skip to content

Commit

Permalink
fix #1500 (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromevdl committed Nov 8, 2023
1 parent 7918d9a commit 0f3e896
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ private static void captureRequestAndTraceId(MetricsLogger metricsLogger) {

private static String defaultNameSpace() {
MetricsContext context = MetricsLoggerHelper.metricsContext();
return "aws-embedded-metrics".equals(context.getNamespace()) ?
SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE") : context.getNamespace();
if ("aws-embedded-metrics".equals(context.getNamespace())) {
String namespace = SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE");
return namespace != null ? namespace : "aws-embedded-metrics";
} else {
return context.getNamespace();
}
}

private static Optional<String> awsRequestId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ void singleMetricsCaptureUtility() {
.containsEntry("Dimension1", "Value1")
.containsKey("_aws")
.containsEntry("xray_trace_id", "1-5759e988-bd862e3fe1be46a994272793");

Map<String, Object> aws = (Map<String, Object>) logAsJson.get("_aws");

assertThat(aws.get("CloudWatchMetrics"))
.asString()
.contains("Namespace=test");
});
}
}

@Test
void singleMetricsCaptureUtilityWithNullNamespace() {
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class);
MockedStatic<software.amazon.lambda.powertools.core.internal.SystemWrapper> internalWrapper = mockStatic(
software.amazon.lambda.powertools.core.internal.SystemWrapper.class)) {
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda");
// POWERTOOLS_METRICS_NAMESPACE is not defined

MetricsUtils.withSingleMetric("Metric1", 1, Unit.COUNT,
metricsLogger -> metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")));

assertThat(out.toString())
.satisfies(s ->
{
Map<String, Object> logAsJson = readAsJson(s);

Map<String, Object> aws = (Map<String, Object>) logAsJson.get("_aws");

assertThat(aws.get("CloudWatchMetrics"))
.asString()
.contains("Namespace=aws-embedded-metrics");
});
}
}
Expand Down

0 comments on commit 0f3e896

Please sign in to comment.