Skip to content

Commit 005f734

Browse files
author
Mark Kuhn
authored
Change DimensionSetExceededException to a checked exception (#122)
* change DimensionSetExceededException to a checked exception * fix tests * fix integ tests
1 parent 125a637 commit 005f734

File tree

15 files changed

+120
-61
lines changed

15 files changed

+120
-61
lines changed

canarytests/agent/src/main/java/emf/canary/ECSRunnable.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import software.amazon.cloudwatchlogs.emf.config.Configuration;
44
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
5+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
56
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
67
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
78
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
@@ -33,7 +34,7 @@ public void run() {
3334
"Platform", "ECS",
3435
"Agent", "CloudWatchAgent",
3536
"Version", version));
36-
} catch (InvalidNamespaceException | InvalidDimensionException e) {
37+
} catch (InvalidNamespaceException | InvalidDimensionException | DimensionSetExceededException e) {
3738
System.out.println(e);
3839
}
3940

examples/agent/src/main/java/agent/App.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
44
import software.amazon.cloudwatchlogs.emf.environment.DefaultEnvironment;
55
import software.amazon.cloudwatchlogs.emf.environment.Environment;
6+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
67
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
78
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
89
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
@@ -19,13 +20,14 @@ public static void main(String[] args) {
1920
emitMetric(environment);
2021
emitMetric(environment);
2122
emitMetric(environment);
22-
} catch (InvalidMetricException | InvalidDimensionException e) {
23+
} catch (InvalidMetricException | InvalidDimensionException | DimensionSetExceededException e) {
2324
System.out.println(e);
2425
}
2526
environment.getSink().shutdown().orTimeout(360_000L, TimeUnit.MILLISECONDS);
2627
}
2728

28-
private static void emitMetric(Environment environment) throws InvalidDimensionException, InvalidMetricException {
29+
private static void emitMetric(Environment environment)
30+
throws InvalidDimensionException, InvalidMetricException, DimensionSetExceededException {
2931
MetricsLogger logger = new MetricsLogger(environment);
3032
logger.setDimensions(DimensionSet.of("Operation", "Agent"));
3133
logger.putMetric("ExampleMetric", 100, Unit.MILLISECONDS);

examples/lambda/src/main/java/Handler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.amazonaws.services.lambda.runtime.Context;
22
import com.amazonaws.services.lambda.runtime.RequestHandler;
3+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
34
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
45
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
56
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
@@ -19,7 +20,7 @@ public String handleRequest(Map<String, String> event, Context context) {
1920
try {
2021
logger.putDimensions(DimensionSet.of("Service", "Aggregator"));
2122
logger.putMetric("ProcessingLatency", 100, Unit.MILLISECONDS);
22-
} catch (InvalidDimensionException | InvalidMetricException e) {
23+
} catch (InvalidDimensionException | InvalidMetricException | DimensionSetExceededException e) {
2324
System.out.println(e);
2425
}
2526

src/integration-test/java/software/amazon/cloudwatchlogs/emf/MetricsLoggerIntegrationTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
3434
import software.amazon.cloudwatchlogs.emf.environment.DefaultEnvironment;
3535
import software.amazon.cloudwatchlogs.emf.environment.Environment;
36+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
3637
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
3738
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
3839
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
@@ -50,7 +51,8 @@ public class MetricsLoggerIntegrationTest {
5051
private DimensionSet dimensions = DimensionSet.of(dimensionName, dimensionValue);
5152
private EMFIntegrationTestHelper testHelper = new EMFIntegrationTestHelper();
5253

53-
public MetricsLoggerIntegrationTest() throws InvalidDimensionException {}
54+
public MetricsLoggerIntegrationTest()
55+
throws InvalidDimensionException, DimensionSetExceededException {}
5456

5557
@Before
5658
public void setUp() {

src/main/java/software/amazon/cloudwatchlogs/emf/exception/DimensionSetExceededException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import software.amazon.cloudwatchlogs.emf.Constants;
2020

21-
public class DimensionSetExceededException extends RuntimeException {
21+
public class DimensionSetExceededException extends Exception {
2222

2323
public DimensionSetExceededException() {
2424
super(

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public class DimensionSet {
4040
* @param v1 Value of the single dimension
4141
* @return a DimensionSet from the parameters
4242
* @throws InvalidDimensionException if the dimension name or value is invalid
43+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
4344
*/
44-
public static DimensionSet of(String d1, String v1) throws InvalidDimensionException {
45+
public static DimensionSet of(String d1, String v1)
46+
throws InvalidDimensionException, DimensionSetExceededException {
4547
return fromEntries(entryOf(d1, v1));
4648
}
4749

@@ -54,9 +56,10 @@ public static DimensionSet of(String d1, String v1) throws InvalidDimensionExcep
5456
* @param v2 Value of the second dimension
5557
* @return a DimensionSet from the parameters
5658
* @throws InvalidDimensionException if the dimension name or value is invalid
59+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
5760
*/
5861
public static DimensionSet of(String d1, String v1, String d2, String v2)
59-
throws InvalidDimensionException {
62+
throws InvalidDimensionException, DimensionSetExceededException {
6063
return fromEntries(entryOf(d1, v1), entryOf(d2, v2));
6164
}
6265

@@ -71,9 +74,10 @@ public static DimensionSet of(String d1, String v1, String d2, String v2)
7174
* @param v3 Value of the third dimension
7275
* @return a DimensionSet from the parameters
7376
* @throws InvalidDimensionException if the dimension name or value is invalid
77+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
7478
*/
7579
public static DimensionSet of(String d1, String v1, String d2, String v2, String d3, String v3)
76-
throws InvalidDimensionException {
80+
throws InvalidDimensionException, DimensionSetExceededException {
7781
return fromEntries(entryOf(d1, v1), entryOf(d2, v2), entryOf(d3, v3));
7882
}
7983

@@ -90,10 +94,11 @@ public static DimensionSet of(String d1, String v1, String d2, String v2, String
9094
* @param v4 Value of the fourth dimension
9195
* @return a DimensionSet from the parameters
9296
* @throws InvalidDimensionException if the dimension name or value is invalid
97+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
9398
*/
9499
public static DimensionSet of(
95100
String d1, String v1, String d2, String v2, String d3, String v3, String d4, String v4)
96-
throws InvalidDimensionException {
101+
throws InvalidDimensionException, DimensionSetExceededException {
97102

98103
return fromEntries(entryOf(d1, v1), entryOf(d2, v2), entryOf(d3, v3), entryOf(d4, v4));
99104
}
@@ -113,6 +118,7 @@ public static DimensionSet of(
113118
* @param v5 Value of the fifth dimension
114119
* @return a DimensionSet from the parameters
115120
* @throws InvalidDimensionException if the dimension name or value is invalid
121+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
116122
*/
117123
public static DimensionSet of(
118124
String d1,
@@ -125,7 +131,7 @@ public static DimensionSet of(
125131
String v4,
126132
String d5,
127133
String v5)
128-
throws InvalidDimensionException {
134+
throws InvalidDimensionException, DimensionSetExceededException {
129135

130136
return fromEntries(
131137
entryOf(d1, v1),
@@ -136,7 +142,7 @@ public static DimensionSet of(
136142
}
137143

138144
private static DimensionSet fromEntries(DimensionEntry... entries)
139-
throws InvalidDimensionException {
145+
throws InvalidDimensionException, DimensionSetExceededException {
140146
DimensionSet ds = new DimensionSet();
141147
for (DimensionEntry entry : entries) {
142148
ds.addDimension(entry.key, entry.value);
@@ -154,8 +160,10 @@ private static DimensionEntry entryOf(String key, String value) {
154160
* @param dimension Name of the dimension
155161
* @param value Value of the dimension
156162
* @throws InvalidDimensionException if the dimension name or value is invalid
163+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
157164
*/
158-
public void addDimension(String dimension, String value) throws InvalidDimensionException {
165+
public void addDimension(String dimension, String value)
166+
throws InvalidDimensionException, DimensionSetExceededException {
159167
Validator.validateDimensionSet(dimension, value);
160168

161169
if (this.getDimensionKeys().size() >= Constants.MAX_DIMENSION_SET_SIZE) {
@@ -171,8 +179,9 @@ public void addDimension(String dimension, String value) throws InvalidDimension
171179
*
172180
* @param other Other dimension sets to merge with current
173181
* @return a new DimensionSet from combining the current DimensionSet with other
182+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
174183
*/
175-
public DimensionSet add(DimensionSet other) {
184+
public DimensionSet add(DimensionSet other) throws DimensionSetExceededException {
176185
DimensionSet mergedDimensionSet = new DimensionSet();
177186
int mergedDimensionSetSize =
178187
this.getDimensionKeys().size() + other.dimensionRecords.keySet().size();

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.ConcurrentHashMap;
2323
import java.util.stream.Collectors;
2424
import lombok.*;
25+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
2526

2627
/** Represents the MetricDirective part of the EMF schema. */
2728
@AllArgsConstructor
@@ -87,7 +88,7 @@ Collection<MetricDefinition> getAllMetrics() {
8788
}
8889

8990
@JsonProperty("Dimensions")
90-
List<Set<String>> getAllDimensionKeys() {
91+
List<Set<String>> getAllDimensionKeys() throws DimensionSetExceededException {
9192
return getAllDimensions().stream()
9293
.map(DimensionSet::getDimensionKeys)
9394
.collect(Collectors.toList());
@@ -128,7 +129,7 @@ void resetDimensions(boolean useDefault) {
128129
* Return all the dimension sets. If there's a default dimension set, the custom dimensions are
129130
* prepended with the default dimensions.
130131
*/
131-
List<DimensionSet> getAllDimensions() {
132+
List<DimensionSet> getAllDimensions() throws DimensionSetExceededException {
132133
if (!shouldUseDefaultDimension) {
133134
return dimensions;
134135
}
@@ -137,9 +138,12 @@ List<DimensionSet> getAllDimensions() {
137138
return Arrays.asList(defaultDimensions);
138139
}
139140

140-
return dimensions.stream()
141-
.map(dim -> defaultDimensions.add(dim))
142-
.collect(Collectors.toList());
141+
List<DimensionSet> allDimensions = new ArrayList<>();
142+
for (DimensionSet dim : dimensions) {
143+
allDimensions.add(defaultDimensions.add(dim));
144+
}
145+
146+
return allDimensions;
143147
}
144148

145149
/**

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.*;
2222
import lombok.Getter;
2323
import software.amazon.cloudwatchlogs.emf.Constants;
24+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
2425
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
2526
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
2627
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
@@ -179,13 +180,20 @@ public void putDimension(DimensionSet dimensionSet) {
179180
* @param dimension the name of the dimension
180181
* @param value the value associated with the dimension
181182
* @throws InvalidDimensionException if the dimension is invalid
183+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
182184
*/
183-
public void putDimension(String dimension, String value) throws InvalidDimensionException {
185+
public void putDimension(String dimension, String value)
186+
throws InvalidDimensionException, DimensionSetExceededException {
184187
metricDirective.putDimensionSet(DimensionSet.of(dimension, value));
185188
}
186189

187-
/** @return the list of dimensions that has been added, including default dimensions. */
188-
public List<DimensionSet> getDimensions() {
190+
/**
191+
* Get list of all dimensions including default dimensions
192+
*
193+
* @return the list of dimensions that has been added, including default dimensions.
194+
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
195+
*/
196+
public List<DimensionSet> getDimensions() throws DimensionSetExceededException {
189197
return metricDirective.getAllDimensions();
190198
}
191199

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import lombok.AllArgsConstructor;
3030
import lombok.Getter;
3131
import lombok.With;
32+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
3233

3334
/** Represents the root of the EMF schema. */
3435
@AllArgsConstructor
@@ -60,7 +61,7 @@ Map<String, Object> getProperties() {
6061

6162
/** Return the target members that are referenced by metrics, dimensions and properties. */
6263
@JsonAnyGetter
63-
Map<String, Object> getTargetMembers() {
64+
Map<String, Object> getTargetMembers() throws DimensionSetExceededException {
6465
Map<String, Object> targetMembers = new HashMap<>();
6566
targetMembers.putAll(properties);
6667
targetMembers.putAll(getDimensions());
@@ -74,7 +75,7 @@ Map<String, Object> getTargetMembers() {
7475
}
7576

7677
/** Return a list of all dimensions that are referenced by each dimension set. */
77-
Map<String, String> getDimensions() {
78+
Map<String, String> getDimensions() throws DimensionSetExceededException {
7879
Map<String, String> dimensions = new HashMap<>();
7980
for (MetricDirective mc : aws.getCloudWatchMetrics()) {
8081
for (DimensionSet dimensionSet : mc.getAllDimensions()) {

0 commit comments

Comments
 (0)