Skip to content

Fixed a bug of carrying default dimensions over flushes #75

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 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ MetricDirective createMetricDirective() {
return newMetricDirective;
}

void setMetricDirective(MetricDirective metricDirective) {
cloudWatchMetrics = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new ArrayList<>(1) or Collections.singletonList(...) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, I'll leave this as is for now. For now, we only expect to have one element in this list. But this may change in the future.

cloudWatchMetrics.add(metricDirective);
}

/**
* Test if there's any metric added.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ List<DimensionSet> getAllDimensions() {
boolean hasNoMetrics() {
return this.getMetrics().isEmpty();
}

/**
* Create a copy of the metric directive without having the existing metrics
*
* @return A object of metric directive
*/
MetricDirective copyWithoutMetrics() {
MetricDirective metricDirective = new MetricDirective();
metricDirective.setDefaultDimensions(this.defaultDimensions);
metricDirective.setDimensions(this.dimensions);
metricDirective.setNamespace(this.namespace);
metricDirective.shouldUseDefaultDimension = this.shouldUseDefaultDimension;
return metricDirective;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public MetricsContext(RootNode rootNode) {
metricDirective = rootNode.getAws().createMetricDirective();
}

public MetricsContext(MetricDirective metricDirective) {
this();
this.rootNode.getAws().setMetricDirective(metricDirective);
this.metricDirective = metricDirective;
}

public MetricsContext(
String namespace,
Map<String, Object> properties,
Expand Down Expand Up @@ -191,11 +197,7 @@ public void putMetadata(String key, Object value) {

/** @return Creates an independently flushable context. */
public MetricsContext createCopyWithContext() {
return new MetricsContext(
this.metricDirective.getNamespace(),
this.rootNode.getProperties(),
this.metricDirective.getDimensions(),
this.metricDirective.getDefaultDimensions());
return new MetricsContext(metricDirective.copyWithoutMetrics());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package software.amazon.cloudwatchlogs.emf.logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -201,6 +202,53 @@ public void testNoDefaultDimensionsAfterSetDimension() {
expectDimension("Name", "Test");
}

@Test
public void testFlushPreserveDimensions() {
MetricsLogger logger = new MetricsLogger(envProvider);
logger.setDimensions(DimensionSet.of("Name", "Test"));
logger.flush();
expectDimension("Name", "Test");

logger.flush();
expectDimension("Name", "Test");
}

@Test
public void testFlushDoesntPreserveMetrics() {
MetricsLogger logger = new MetricsLogger(envProvider);
logger.setDimensions(DimensionSet.of("Name", "Test"));
logger.putMetric("Count", 1.0);
logger.flush();
assertTrue(sink.getLogEvents().get(0).contains("Count"));

logger.flush();
assertFalse(sink.getLogEvents().get(0).contains("Count"));
}

@Test
public void testNoDimensionsAfterSetEmptyDimensionSet() {
MetricsLogger logger = new MetricsLogger(envProvider);

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

List<DimensionSet> dimensions = sink.getContext().getDimensions();
assertEquals(0, dimensions.size());
}

@Test
public void testNoDimensionsAfterSetEmptyDimensionSetWithMultipleFlush() {
MetricsLogger logger = new MetricsLogger(envProvider);

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

assertEquals(0, sink.getContext().getDimensions().size());

logger.flush();
assertEquals(0, sink.getContext().getDimensions().size());
}

private void expectDimension(String dimension, String value) {
List<DimensionSet> dimensions = sink.getContext().getDimensions();
assertEquals(dimensions.size(), 1);
Expand Down