Skip to content

Commit c1ad4f9

Browse files
authored
Support 30 dimensions per dimension set (#106)
Support 30 dimensions in a set for metrics and throw exception if the limit is exceeded.
1 parent 262c63c commit c1ad4f9

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ allprojects {
2828
targetCompatibility = '1.8'
2929
}
3030

31-
version = '1.0.6'
31+
version = '2.0.0'
3232
}
3333

3434
java {

src/main/java/software/amazon/cloudwatchlogs/emf/Constants.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ public class Constants {
2323

2424
public static final int MAX_METRICS_PER_EVENT = 100;
2525

26+
public static final int MAX_DIMENSION_SET_SIZE = 30;
27+
2628
public static final int MAX_DATAPOINTS_PER_METRIC = 100;
2729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon.cloudwatchlogs.emf.exception;
18+
19+
import software.amazon.cloudwatchlogs.emf.Constants;
20+
21+
public class DimensionSetExceededException extends RuntimeException {
22+
23+
public DimensionSetExceededException() {
24+
super(
25+
"Maximum number of dimensions allowed are "
26+
+ Constants.MAX_DIMENSION_SET_SIZE
27+
+ ". Account for default dimensions if not using setDimensions.");
28+
}
29+
30+
public DimensionSetExceededException(String message) {
31+
super(message);
32+
}
33+
}

src/main/java/software/amazon/cloudwatchlogs/emf/model/DimensionSet.java

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import lombok.AccessLevel;
2323
import lombok.AllArgsConstructor;
2424
import lombok.Getter;
25+
import software.amazon.cloudwatchlogs.emf.Constants;
26+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
2527

2628
/** A combination of dimension values. */
2729
public class DimensionSet {
@@ -149,6 +151,10 @@ private static DimensionEntry entryOf(String key, String value) {
149151
* @param value Value of the dimension
150152
*/
151153
public void addDimension(String dimension, String value) {
154+
if (this.getDimensionKeys().size() >= Constants.MAX_DIMENSION_SET_SIZE) {
155+
throw new DimensionSetExceededException();
156+
}
157+
152158
this.getDimensionRecords().put(dimension, value);
153159
}
154160

@@ -161,6 +167,12 @@ public void addDimension(String dimension, String value) {
161167
*/
162168
public DimensionSet add(DimensionSet other) {
163169
DimensionSet mergedDimensionSet = new DimensionSet();
170+
int mergedDimensionSetSize =
171+
this.getDimensionKeys().size() + other.dimensionRecords.keySet().size();
172+
if (mergedDimensionSetSize > Constants.MAX_DIMENSION_SET_SIZE) {
173+
throw new DimensionSetExceededException();
174+
}
175+
164176
mergedDimensionSet.dimensionRecords.putAll(dimensionRecords);
165177
mergedDimensionSet.dimensionRecords.putAll(other.dimensionRecords);
166178
return mergedDimensionSet;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon.cloudwatchlogs.emf.model;
18+
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
23+
24+
public class DimensionSetTest {
25+
@Test
26+
public void testAddDimension() {
27+
int dimensionsToBeAdded = 30;
28+
DimensionSet dimensionSet = generateDimensionSet(dimensionsToBeAdded);
29+
30+
assertEquals(dimensionsToBeAdded, dimensionSet.getDimensionKeys().size());
31+
}
32+
33+
@Test
34+
public void testAddDimensionLimitExceeded() {
35+
Exception exception =
36+
assertThrows(
37+
DimensionSetExceededException.class,
38+
() -> {
39+
int dimensionSetSize = 33;
40+
generateDimensionSet(dimensionSetSize);
41+
});
42+
43+
String expectedMessage = "Maximum number of dimensions";
44+
String actualMessage = exception.getMessage();
45+
46+
assertTrue(actualMessage.contains(expectedMessage));
47+
}
48+
49+
@Test
50+
public void testMergeDimensionSets() {
51+
Exception exception =
52+
assertThrows(
53+
DimensionSetExceededException.class,
54+
() -> {
55+
int dimensionSetSize = 28;
56+
int otherDimensionSetSize = 5;
57+
DimensionSet dimensionSet = generateDimensionSet(dimensionSetSize);
58+
DimensionSet otherDimensionSet =
59+
generateDimensionSet(otherDimensionSetSize);
60+
dimensionSet.add(otherDimensionSet);
61+
});
62+
String expectedMessage = "Maximum number of dimensions";
63+
String actualMessage = exception.getMessage();
64+
65+
assertTrue(actualMessage.contains(expectedMessage));
66+
}
67+
68+
private DimensionSet generateDimensionSet(int numOfDimensions) {
69+
DimensionSet dimensionSet = new DimensionSet();
70+
71+
for (int i = 0; i < numOfDimensions; i++) {
72+
dimensionSet.addDimension("Dimension" + i, "value" + i);
73+
}
74+
75+
return dimensionSet;
76+
}
77+
}

0 commit comments

Comments
 (0)