Skip to content

Fix: Context copying is disabling default dimensions #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/agent/src/main/java/agent/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public static void main(String[] args) {
} catch (InvalidMetricException | InvalidDimensionException | DimensionSetExceededException e) {
System.out.println(e);
}
environment.getSink().shutdown().orTimeout(360_000L, TimeUnit.MILLISECONDS);

try {
environment.getSink().shutdown().get(360_000L, TimeUnit.MILLISECONDS);
} catch (Exception ignored) {
}
}

private static void emitMetric(Environment environment)
Expand Down
5 changes: 4 additions & 1 deletion examples/ecs-firelens/src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public static void main(String[] args) throws Exception {
private static void registerShutdownHook() {
// https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/
Signal.handle(new Signal("TERM"), sig -> {
env.getSink().shutdown().orTimeout(1_000L, TimeUnit.MILLISECONDS);
try {
env.getSink().shutdown().get(1_000L, TimeUnit.MILLISECONDS);
} catch (Exception ignored) {
}
System.exit(0);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package software.amazon.cloudwatchlogs.emf.environment;

import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import software.amazon.cloudwatchlogs.emf.Constants;
import software.amazon.cloudwatchlogs.emf.config.Configuration;
Expand All @@ -36,11 +37,14 @@ protected AgentBasedEnvironment(Configuration config) {

@Override
public String getName() {
if (config.getServiceName().isEmpty()) {
log.warn("Unknown ServiceName.");
return Constants.UNKNOWN;
Optional<String> serviceName = config.getServiceName();

if (serviceName.isPresent()) {
return serviceName.get();
}
return config.getServiceName().get();

log.warn("Unknown ServiceName.");
return Constants.UNKNOWN;
}

@Override
Expand All @@ -64,13 +68,13 @@ public String getLogStreamName() {
public ISink getSink() {
if (sink == null) {
Endpoint endpoint;
if (config.getAgentEndpoint().isEmpty()) {
if (config.getAgentEndpoint().isPresent()) {
endpoint = Endpoint.fromURL(config.getAgentEndpoint().get());
} else {
log.info(
"Endpoint is not defined. Using default: {}",
Endpoint.DEFAULT_TCP_ENDPOINT);
endpoint = Endpoint.DEFAULT_TCP_ENDPOINT;
} else {
endpoint = Endpoint.fromURL(config.getAgentEndpoint().get());
}
sink =
new AgentSink(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package software.amazon.cloudwatchlogs.emf.environment;

import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import software.amazon.cloudwatchlogs.emf.Constants;
import software.amazon.cloudwatchlogs.emf.config.Configuration;
Expand All @@ -37,11 +38,14 @@ public boolean probe() {

@Override
public String getType() {
if (config.getServiceType().isEmpty()) {
log.info("Unknown ServiceType");
return Constants.UNKNOWN;
Optional<String> serviceType = config.getServiceType();

if (serviceType.isPresent()) {
return serviceType.get();
}
return config.getServiceType().get();

log.info("Unknown ServiceType");
return Constants.UNKNOWN;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ MetricDirective copyWithoutMetrics(boolean preserveDimensions) {
metricDirective.shouldUseDefaultDimension = this.shouldUseDefaultDimension;

if (preserveDimensions) {
metricDirective.setDimensions(this.dimensions);
this.dimensions.forEach(metricDirective::putDimensionSet);
}

return metricDirective;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ void whenDefaultDimension_DimensionValue_Empty() throws DimensionSetExceededExce
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");
assertTrue(dimensionKeys.contains("ServiceName"));
assertTrue(dimensionKeys.contains("ServiceType"));
}

@Test
Expand All @@ -105,8 +105,8 @@ void whenDefaultDimension_DimensionValue_Blank() throws DimensionSetExceededExce
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");
assertTrue(dimensionKeys.contains("ServiceName"));
assertTrue(dimensionKeys.contains("ServiceType"));
}

@ParameterizedTest
Expand All @@ -126,7 +126,8 @@ void whenSetDimension_withInvalidValue_thenThrowInvalidDimensionException(

@Test
void whenSetDimension_withNameTooLong_thenThrowDimensionException() {
String dimensionName = "a".repeat(Constants.MAX_DIMENSION_NAME_LENGTH + 1);
String dimensionName =
new String(new char[Constants.MAX_DIMENSION_NAME_LENGTH + 1]).replace("\0", "a");
String dimensionValue = "dimValue";
assertThrows(
InvalidDimensionException.class,
Expand All @@ -136,7 +137,8 @@ void whenSetDimension_withNameTooLong_thenThrowDimensionException() {
@Test
void whenSetDimension_withValueTooLong_thenThrowDimensionException() {
String dimensionName = "dim";
String dimensionValue = "a".repeat(Constants.MAX_DIMENSION_VALUE_LENGTH + 1);
String dimensionValue =
new String(new char[Constants.MAX_DIMENSION_VALUE_LENGTH + 1]).replace("\0", "a");
assertThrows(
InvalidDimensionException.class,
() -> DimensionSet.of(dimensionName, dimensionValue));
Expand Down Expand Up @@ -275,6 +277,19 @@ void flush_doesNotPreserveDimensions()
expectDimension("Name", null);
}

@Test
void whenMultipleFlush_withNoDimensionsChanges_keepDefaultDimensions()
throws InvalidMetricException, DimensionSetExceededException {
logger.putMetric("Count", 1);

logger.flush();
logger.flush();

expectDimension("LogGroup", "test-log-group");
expectDimension("ServiceName", "test-env-name");
expectDimension("ServiceType", "test-env-type");
}

@Test
void setDimensions_clearsAllDimensions() throws DimensionSetExceededException {
MetricsLogger logger = new MetricsLogger(envProvider);
Expand Down Expand Up @@ -302,7 +317,8 @@ void whenSetDimensions_withMultipleFlush_thenClearsDimensions()

@Test
void whenPutMetric_withTooLongName_thenThrowInvalidMetricException() {
String name1 = "a".repeat(Constants.MAX_METRIC_NAME_LENGTH + 1);
String name1 =
new String(new char[Constants.MAX_METRIC_NAME_LENGTH + 1]).replace("\0", "a");
assertThrows(InvalidMetricException.class, () -> logger.putMetric(name1, 1));
}

Expand Down Expand Up @@ -349,7 +365,8 @@ void whenSetNamespace_withInvalidValue_thenThrowInvalidNamespaceException(String

@Test
void whenSetNamespace_withNameTooLong_thenThrowInvalidNamespaceException() {
String namespace = "a".repeat(Constants.MAX_NAMESPACE_LENGTH + 1);
String namespace =
new String(new char[Constants.MAX_NAMESPACE_LENGTH + 1]).replace("\0", "a");
assertThrows(InvalidNamespaceException.class, () -> logger.setNamespace(namespace));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;

public class MetricDefinitionTest {
Expand Down Expand Up @@ -51,9 +52,9 @@ public void testSerializeMetricDefinition() throws JsonProcessingException {
@Test
public void testAddValue() {
MetricDefinition md = new MetricDefinition("Time", Unit.MICROSECONDS, 10);
assertEquals(List.of(10d), md.getValues());
assertEquals(Collections.singletonList(10d), md.getValues());

md.addValue(20);
assertEquals(List.of(10d, 20d), md.getValues());
assertEquals(Arrays.asList(10d, 20d), md.getValues());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -101,7 +102,7 @@ void testSerializeAMetricWith101DataPoints()
expectedValues.add((double) i);
}
Assertions.assertEquals(expectedValues, allMetrics.get(0).getValues());
Assertions.assertEquals(List.of(100.0), allMetrics.get(1).getValues());
Assertions.assertEquals(Collections.singletonList(100.0), allMetrics.get(1).getValues());
}

@Test
Expand All @@ -128,10 +129,12 @@ void testSerializeMetricsWith101DataPoints()
expectedValues.add((double) i);
}
Assertions.assertEquals(expectedValues, metricsFromEvent1.get(0).getValues());
Assertions.assertEquals(List.of(2.0), metricsFromEvent1.get(1).getValues());
Assertions.assertEquals(
Collections.singletonList(2.0), metricsFromEvent1.get(1).getValues());

Assertions.assertEquals(1, metricsFromEvent2.size());
Assertions.assertEquals(List.of(100.0), metricsFromEvent2.get(0).getValues());
Assertions.assertEquals(
Collections.singletonList(100.0), metricsFromEvent2.get(0).getValues());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -72,7 +73,8 @@ void testGetTargetMembers()

mc.putProperty("Prop1", "PropValue1");

Assertions.assertEquals(List.of(10.0, 20.0), rootNode.getTargetMembers().get("Count"));
Assertions.assertEquals(
Arrays.asList(10.0, 20.0), rootNode.getTargetMembers().get("Count"));
Assertions.assertEquals(100.0, rootNode.getTargetMembers().get("Latency"));
Assertions.assertEquals("DimVal1", rootNode.getTargetMembers().get("Dim1"));
Assertions.assertEquals("PropValue1", rootNode.getTargetMembers().get("Prop1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public void testEmptyLogGroupName() throws JsonProcessingException, InvalidMetri
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> emf_map =
objectMapper.readValue(
fixture.client.getMessages().get(0), new TypeReference<>() {});
fixture.client.getMessages().get(0),
new TypeReference<Map<String, Object>>() {});
Map<String, Object> metadata = (Map<String, Object>) emf_map.get("_aws");

assertFalse(metadata.containsKey("LogGroupName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void shutdownClosesAllComponentSinks() {
@Test
public void shutdownCompletesExceptionallyIfComponentSinkCompletesExceptionally() {
// arrange
CompletableFuture<Void> failedResult =
CompletableFuture.failedFuture(new RuntimeException());
CompletableFuture<Void> failedResult = new CompletableFuture<>();
failedResult.completeExceptionally(new RuntimeException());
TestSink sink1 = new TestSink();
TestSink sink2 = new TestSink(failedResult);
MultiSink multiSink = MultiSink.builder().sink(sink1).sink(sink2).build();
Expand Down