Skip to content

Commit

Permalink
Merge branch 'awslabs:master' into fix-awslabs#99
Browse files Browse the repository at this point in the history
  • Loading branch information
markkuhn authored Aug 2, 2022
2 parents 192bca8 + dd7bffa commit 25bc48e
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ allprojects {
targetCompatibility = '1.8'
}

version = '2.0.0-beta-1'
version = '3.0.0'
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Constants {

public static final int MAX_METRICS_PER_EVENT = 100;

public static final int MAX_DIMENSION_SET_SIZE = 30;

public static final int MAX_DATAPOINTS_PER_METRIC = 100;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package software.amazon.cloudwatchlogs.emf.exception;

import software.amazon.cloudwatchlogs.emf.Constants;

public class DimensionSetExceededException extends RuntimeException {

public DimensionSetExceededException() {
super(
"Maximum number of dimensions allowed are "
+ Constants.MAX_DIMENSION_SET_SIZE
+ ". Account for default dimensions if not using setDimensions.");
}

public DimensionSetExceededException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import software.amazon.cloudwatchlogs.emf.Constants;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;

/** A combination of dimension values. */
public class DimensionSet {
Expand Down Expand Up @@ -149,6 +151,10 @@ private static DimensionEntry entryOf(String key, String value) {
* @param value Value of the dimension
*/
public void addDimension(String dimension, String value) {
if (this.getDimensionKeys().size() >= Constants.MAX_DIMENSION_SET_SIZE) {
throw new DimensionSetExceededException();
}

this.getDimensionRecords().put(dimension, value);
}

Expand All @@ -161,6 +167,12 @@ public void addDimension(String dimension, String value) {
*/
public DimensionSet add(DimensionSet other) {
DimensionSet mergedDimensionSet = new DimensionSet();
int mergedDimensionSetSize =
this.getDimensionKeys().size() + other.dimensionRecords.keySet().size();
if (mergedDimensionSetSize > Constants.MAX_DIMENSION_SET_SIZE) {
throw new DimensionSetExceededException();
}

mergedDimensionSet.dimensionRecords.putAll(dimensionRecords);
mergedDimensionSet.dimensionRecords.putAll(other.dimensionRecords);
return mergedDimensionSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package software.amazon.cloudwatchlogs.emf.model;

import static org.junit.Assert.*;

import org.junit.Test;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;

public class DimensionSetTest {
@Test
public void testAddDimension() {
int dimensionsToBeAdded = 30;
DimensionSet dimensionSet = generateDimensionSet(dimensionsToBeAdded);

assertEquals(dimensionsToBeAdded, dimensionSet.getDimensionKeys().size());
}

@Test
public void testAddDimensionLimitExceeded() {
Exception exception =
assertThrows(
DimensionSetExceededException.class,
() -> {
int dimensionSetSize = 33;
generateDimensionSet(dimensionSetSize);
});

String expectedMessage = "Maximum number of dimensions";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void testMergeDimensionSets() {
Exception exception =
assertThrows(
DimensionSetExceededException.class,
() -> {
int dimensionSetSize = 28;
int otherDimensionSetSize = 5;
DimensionSet dimensionSet = generateDimensionSet(dimensionSetSize);
DimensionSet otherDimensionSet =
generateDimensionSet(otherDimensionSetSize);
dimensionSet.add(otherDimensionSet);
});
String expectedMessage = "Maximum number of dimensions";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

private DimensionSet generateDimensionSet(int numOfDimensions) {
DimensionSet dimensionSet = new DimensionSet();

for (int i = 0; i < numOfDimensions; i++) {
dimensionSet.addDimension("Dimension" + i, "value" + i);
}

return dimensionSet;
}
}

0 comments on commit 25bc48e

Please sign in to comment.