Skip to content

Commit

Permalink
Skip adding dimensions with empty dimension value into default dimens…
Browse files Browse the repository at this point in the history
…ion (#126)

* Skip adding dimensions with empty dimension value into default dimensions

Co-authored-by: Jing (Paggy) Nie <[email protected]>
  • Loading branch information
paggynie and paggynie authored Oct 6, 2022
1 parent 19ef5ca commit e7de627
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MetricsLogger> any) {
rwl.readLock().lock();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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) {
Expand Down

0 comments on commit e7de627

Please sign in to comment.