diff --git a/src/main/java/software/amazon/cloudformation/LambdaWrapper.java b/src/main/java/software/amazon/cloudformation/LambdaWrapper.java index 56e7b6a2..bfe18df7 100644 --- a/src/main/java/software/amazon/cloudformation/LambdaWrapper.java +++ b/src/main/java/software/amazon/cloudformation/LambdaWrapper.java @@ -487,11 +487,8 @@ private void publishExceptionMetric(final Action action, final Throwable ex, fin private void publishExceptionCodeAndCountMetric(final Action action, final HandlerErrorCode handlerErrorCode, final boolean thrown) { if (this.metricsPublisherProxy != null) { - EnumSet.allOf(HandlerErrorCode.class).stream() - // publishing 0 value for all (if not thrown) otherwise filtered - .filter(errorCode -> errorCode.equals(handlerErrorCode) || !thrown) - .forEach(errorCode -> this.metricsPublisherProxy.publishExceptionByErrorCodeMetric(Instant.now(), action, - errorCode, thrown)); + EnumSet.allOf(HandlerErrorCode.class).forEach(errorCode -> this.metricsPublisherProxy + .publishExceptionByErrorCodeMetric(Instant.now(), action, errorCode, thrown && errorCode == handlerErrorCode)); this.metricsPublisherProxy.publishExceptionCountMetric(Instant.now(), action, thrown); } } diff --git a/src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java b/src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java index 68ba0793..b1eb20e1 100755 --- a/src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java +++ b/src/test/java/software/amazon/cloudformation/LambdaWrapperTest.java @@ -392,19 +392,16 @@ public void invokeHandler_InProgress_returnsInProgress(final String requestDataP verifyInitialiseRuntime(); // all metrics should be published, once for a single invocation - verify(providerMetricsPublisher, times(1)).publishInvocationMetric(any(Instant.class), eq(action)); - verify(providerMetricsPublisher, times(1)).publishDurationMetric(any(Instant.class), eq(action), anyLong()); + verify(providerMetricsPublisher).publishInvocationMetric(any(Instant.class), eq(action)); + verify(providerMetricsPublisher).publishDurationMetric(any(Instant.class), eq(action), anyLong()); // verify that model validation occurred for CREATE/UPDATE/DELETE if (action == Action.CREATE || action == Action.UPDATE || action == Action.DELETE) { - verify(validator, times(1)).validateObject(any(JSONObject.class), any(JSONObject.class)); + verify(validator).validateObject(any(JSONObject.class), any(JSONObject.class)); // verify output response verifyHandlerResponse(out, ProgressEvent.builder().status(OperationStatus.IN_PROGRESS) .resourceModel(TestModel.builder().property1("abc").property2(123).build()).build()); - verify(providerMetricsPublisher, atLeastOnce()).publishExceptionByErrorCodeMetric(any(Instant.class), eq(action), - any(), eq(Boolean.FALSE)); - verify(providerMetricsPublisher).publishExceptionCountMetric(any(Instant.class), eq(action), eq(Boolean.FALSE)); } else { verifyHandlerResponse(out, ProgressEvent.builder().status(OperationStatus.FAILED) @@ -412,11 +409,13 @@ public void invokeHandler_InProgress_returnsInProgress(final String requestDataP .build()); verify(providerMetricsPublisher).publishExceptionMetric(any(Instant.class), eq(action), any(TerminalException.class), eq(HandlerErrorCode.InternalFailure)); - verify(providerMetricsPublisher).publishExceptionByErrorCodeMetric(any(Instant.class), eq(action), - eq(HandlerErrorCode.InternalFailure), eq(Boolean.TRUE)); - verify(providerMetricsPublisher).publishExceptionCountMetric(any(Instant.class), eq(action), eq(Boolean.TRUE)); + } + verify(providerMetricsPublisher, atLeastOnce()).publishExceptionByErrorCodeMetric(any(Instant.class), eq(action), + any(), any(Boolean.class)); + verify(providerMetricsPublisher).publishExceptionCountMetric(any(Instant.class), any(), any(Boolean.class)); + // validation failure metric should not be published verifyNoMoreInteractions(providerMetricsPublisher); @@ -811,8 +810,8 @@ public void invokeHandler_metricPublisherThrowable_returnsFailureResponse() thro // verify initialiseRuntime was called and initialised dependencies verifyInitialiseRuntime(); - verify(providerMetricsPublisher).publishExceptionByErrorCodeMetric(any(Instant.class), any(Action.class), - any(HandlerErrorCode.class), any(Boolean.class)); + verify(providerMetricsPublisher, atLeastOnce()).publishExceptionByErrorCodeMetric(any(Instant.class), + any(Action.class), any(HandlerErrorCode.class), any(Boolean.class)); verify(providerMetricsPublisher).publishExceptionCountMetric(any(Instant.class), any(Action.class), any(Boolean.class));