diff --git a/src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java b/src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java index 54aa0ee4..cf4a0cb6 100644 --- a/src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java +++ b/src/main/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLogger.java @@ -22,10 +22,11 @@ import java.util.function.Supplier; import lombok.Getter; import lombok.Setter; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import software.amazon.cloudwatchlogs.emf.environment.Environment; import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider; +import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException; +import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException; import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException; import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException; import software.amazon.cloudwatchlogs.emf.exception.InvalidTimestampException; @@ -259,19 +260,25 @@ public MetricsLogger setTimestamp(Instant timestamp) throws InvalidTimestampExce return this; } - @SneakyThrows private void configureContextForEnvironment(MetricsContext context, Environment environment) { if (context.hasDefaultDimensions()) { return; } DimensionSet defaultDimension = new DimensionSet(); - defaultDimension.addDimension("LogGroup", environment.getLogGroupName()); - defaultDimension.addDimension("ServiceName", environment.getName()); - defaultDimension.addDimension("ServiceType", environment.getType()); + setDefaultDimension(defaultDimension, "LogGroup", environment.getLogGroupName()); + setDefaultDimension(defaultDimension, "ServiceName", environment.getName()); + setDefaultDimension(defaultDimension, "ServiceType", environment.getType()); context.setDefaultDimensions(defaultDimension); environment.configureContext(context); } + private void setDefaultDimension(DimensionSet defaultDimension, String dimKey, String dimVal) { + try { + defaultDimension.addDimension(dimKey, dimVal); + } catch (InvalidDimensionException | DimensionSetExceededException ignored) { + } + } + private MetricsLogger applyReadLock(Supplier any) { rwl.readLock().lock(); try { diff --git a/src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java b/src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java index 089e0dba..4b163189 100644 --- a/src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java +++ b/src/test/java/software/amazon/cloudwatchlogs/emf/logger/MetricsLoggerTest.java @@ -21,6 +21,7 @@ import java.time.Instant; import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -84,6 +85,30 @@ void putDimensions_setsDimension() sink.getContext().getDimensions().get(0).getDimensionValue(dimensionName)); } + @Test + void whenDefaultDimension_DimensionValue_Empty() throws DimensionSetExceededException { + when(environment.getLogGroupName()).thenReturn(""); + logger.flush(); + + assertEquals(1, sink.getContext().getDimensions().size()); + Set dimensionKeys = sink.getContext().getDimensions().get(0).getDimensionKeys(); + assertEquals(2, dimensionKeys.size()); + dimensionKeys.contains("ServiceName"); + dimensionKeys.contains("ServiceType"); + } + + @Test + void whenDefaultDimension_DimensionValue_Blank() throws DimensionSetExceededException { + when(environment.getLogGroupName()).thenReturn(" "); + logger.flush(); + + assertEquals(1, sink.getContext().getDimensions().size()); + Set dimensionKeys = sink.getContext().getDimensions().get(0).getDimensionKeys(); + assertEquals(2, dimensionKeys.size()); + dimensionKeys.contains("ServiceName"); + dimensionKeys.contains("ServiceType"); + } + @ParameterizedTest @ValueSource(strings = {"", " ", "ƊĪⱮḔǸŠƗȌŅ", ":dim"}) void whenSetDimension_withInvalidName_thenThrowInvalidDimensionException(String dimensionName) {