Skip to content

Change DimensionSetExceededException to a checked exception #122

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 6 commits into from
Sep 16, 2022
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
3 changes: 2 additions & 1 deletion canarytests/agent/src/main/java/emf/canary/ECSRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import software.amazon.cloudwatchlogs.emf.config.Configuration;
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void run() {
"Platform", "ECS",
"Agent", "CloudWatchAgent",
"Version", version));
} catch (InvalidNamespaceException | InvalidDimensionException e) {
} catch (InvalidNamespaceException | InvalidDimensionException | DimensionSetExceededException e) {
System.out.println(e);
}

Expand Down
6 changes: 4 additions & 2 deletions examples/agent/src/main/java/agent/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
import software.amazon.cloudwatchlogs.emf.environment.DefaultEnvironment;
import software.amazon.cloudwatchlogs.emf.environment.Environment;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
Expand All @@ -19,13 +20,14 @@ public static void main(String[] args) {
emitMetric(environment);
emitMetric(environment);
emitMetric(environment);
} catch (InvalidMetricException | InvalidDimensionException e) {
} catch (InvalidMetricException | InvalidDimensionException | DimensionSetExceededException e) {
System.out.println(e);
}
environment.getSink().shutdown().orTimeout(360_000L, TimeUnit.MILLISECONDS);
}

private static void emitMetric(Environment environment) throws InvalidDimensionException, InvalidMetricException {
private static void emitMetric(Environment environment)
throws InvalidDimensionException, InvalidMetricException, DimensionSetExceededException {
MetricsLogger logger = new MetricsLogger(environment);
logger.setDimensions(DimensionSet.of("Operation", "Agent"));
logger.putMetric("ExampleMetric", 100, Unit.MILLISECONDS);
Expand Down
3 changes: 2 additions & 1 deletion examples/lambda/src/main/java/Handler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
Expand All @@ -19,7 +20,7 @@ public String handleRequest(Map<String, String> event, Context context) {
try {
logger.putDimensions(DimensionSet.of("Service", "Aggregator"));
logger.putMetric("ProcessingLatency", 100, Unit.MILLISECONDS);
} catch (InvalidDimensionException | InvalidMetricException e) {
} catch (InvalidDimensionException | InvalidMetricException | DimensionSetExceededException e) {
System.out.println(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
import software.amazon.cloudwatchlogs.emf.environment.DefaultEnvironment;
import software.amazon.cloudwatchlogs.emf.environment.Environment;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
Expand All @@ -50,7 +51,8 @@ public class MetricsLoggerIntegrationTest {
private DimensionSet dimensions = DimensionSet.of(dimensionName, dimensionValue);
private EMFIntegrationTestHelper testHelper = new EMFIntegrationTestHelper();

public MetricsLoggerIntegrationTest() throws InvalidDimensionException {}
public MetricsLoggerIntegrationTest()
throws InvalidDimensionException, DimensionSetExceededException {}

@Before
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import software.amazon.cloudwatchlogs.emf.Constants;

public class DimensionSetExceededException extends RuntimeException {
public class DimensionSetExceededException extends Exception {

public DimensionSetExceededException() {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public class DimensionSet {
* @param v1 Value of the single dimension
* @return a DimensionSet from the parameters
* @throws InvalidDimensionException if the dimension name or value is invalid
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public static DimensionSet of(String d1, String v1) throws InvalidDimensionException {
public static DimensionSet of(String d1, String v1)
throws InvalidDimensionException, DimensionSetExceededException {
return fromEntries(entryOf(d1, v1));
}

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

Expand All @@ -71,9 +74,10 @@ public static DimensionSet of(String d1, String v1, String d2, String v2)
* @param v3 Value of the third dimension
* @return a DimensionSet from the parameters
* @throws InvalidDimensionException if the dimension name or value is invalid
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public static DimensionSet of(String d1, String v1, String d2, String v2, String d3, String v3)
throws InvalidDimensionException {
throws InvalidDimensionException, DimensionSetExceededException {
return fromEntries(entryOf(d1, v1), entryOf(d2, v2), entryOf(d3, v3));
}

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

return fromEntries(entryOf(d1, v1), entryOf(d2, v2), entryOf(d3, v3), entryOf(d4, v4));
}
Expand All @@ -113,6 +118,7 @@ public static DimensionSet of(
* @param v5 Value of the fifth dimension
* @return a DimensionSet from the parameters
* @throws InvalidDimensionException if the dimension name or value is invalid
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public static DimensionSet of(
String d1,
Expand All @@ -125,7 +131,7 @@ public static DimensionSet of(
String v4,
String d5,
String v5)
throws InvalidDimensionException {
throws InvalidDimensionException, DimensionSetExceededException {

return fromEntries(
entryOf(d1, v1),
Expand All @@ -136,7 +142,7 @@ public static DimensionSet of(
}

private static DimensionSet fromEntries(DimensionEntry... entries)
throws InvalidDimensionException {
throws InvalidDimensionException, DimensionSetExceededException {
DimensionSet ds = new DimensionSet();
for (DimensionEntry entry : entries) {
ds.addDimension(entry.key, entry.value);
Expand All @@ -154,8 +160,10 @@ private static DimensionEntry entryOf(String key, String value) {
* @param dimension Name of the dimension
* @param value Value of the dimension
* @throws InvalidDimensionException if the dimension name or value is invalid
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public void addDimension(String dimension, String value) throws InvalidDimensionException {
public void addDimension(String dimension, String value)
throws InvalidDimensionException, DimensionSetExceededException {
Validator.validateDimensionSet(dimension, value);

if (this.getDimensionKeys().size() >= Constants.MAX_DIMENSION_SET_SIZE) {
Expand All @@ -171,8 +179,9 @@ public void addDimension(String dimension, String value) throws InvalidDimension
*
* @param other Other dimension sets to merge with current
* @return a new DimensionSet from combining the current DimensionSet with other
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public DimensionSet add(DimensionSet other) {
public DimensionSet add(DimensionSet other) throws DimensionSetExceededException {
DimensionSet mergedDimensionSet = new DimensionSet();
int mergedDimensionSetSize =
this.getDimensionKeys().size() + other.dimensionRecords.keySet().size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import lombok.*;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;

/** Represents the MetricDirective part of the EMF schema. */
@AllArgsConstructor
Expand Down Expand Up @@ -87,7 +88,7 @@ Collection<MetricDefinition> getAllMetrics() {
}

@JsonProperty("Dimensions")
List<Set<String>> getAllDimensionKeys() {
List<Set<String>> getAllDimensionKeys() throws DimensionSetExceededException {
return getAllDimensions().stream()
.map(DimensionSet::getDimensionKeys)
.collect(Collectors.toList());
Expand Down Expand Up @@ -128,7 +129,7 @@ void resetDimensions(boolean useDefault) {
* Return all the dimension sets. If there's a default dimension set, the custom dimensions are
* prepended with the default dimensions.
*/
List<DimensionSet> getAllDimensions() {
List<DimensionSet> getAllDimensions() throws DimensionSetExceededException {
if (!shouldUseDefaultDimension) {
return dimensions;
}
Expand All @@ -137,9 +138,12 @@ List<DimensionSet> getAllDimensions() {
return Arrays.asList(defaultDimensions);
}

return dimensions.stream()
.map(dim -> defaultDimensions.add(dim))
.collect(Collectors.toList());
List<DimensionSet> allDimensions = new ArrayList<>();
for (DimensionSet dim : dimensions) {
allDimensions.add(defaultDimensions.add(dim));
}

return allDimensions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.*;
import lombok.Getter;
import software.amazon.cloudwatchlogs.emf.Constants;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
Expand Down Expand Up @@ -179,13 +180,20 @@ public void putDimension(DimensionSet dimensionSet) {
* @param dimension the name of the dimension
* @param value the value associated with the dimension
* @throws InvalidDimensionException if the dimension is invalid
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public void putDimension(String dimension, String value) throws InvalidDimensionException {
public void putDimension(String dimension, String value)
throws InvalidDimensionException, DimensionSetExceededException {
metricDirective.putDimensionSet(DimensionSet.of(dimension, value));
}

/** @return the list of dimensions that has been added, including default dimensions. */
public List<DimensionSet> getDimensions() {
/**
* Get list of all dimensions including default dimensions
*
* @return the list of dimensions that has been added, including default dimensions.
* @throws DimensionSetExceededException if the number of dimensions exceeds the limit
*/
public List<DimensionSet> getDimensions() throws DimensionSetExceededException {
return metricDirective.getAllDimensions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.With;
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;

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

/** Return the target members that are referenced by metrics, dimensions and properties. */
@JsonAnyGetter
Map<String, Object> getTargetMembers() {
Map<String, Object> getTargetMembers() throws DimensionSetExceededException {
Map<String, Object> targetMembers = new HashMap<>();
targetMembers.putAll(properties);
targetMembers.putAll(getDimensions());
Expand All @@ -74,7 +75,7 @@ Map<String, Object> getTargetMembers() {
}

/** Return a list of all dimensions that are referenced by each dimension set. */
Map<String, String> getDimensions() {
Map<String, String> getDimensions() throws DimensionSetExceededException {
Map<String, String> dimensions = new HashMap<>();
for (MetricDirective mc : aws.getCloudWatchMetrics()) {
for (DimensionSet dimensionSet : mc.getAllDimensions()) {
Expand Down
Loading