Skip to content

Commit

Permalink
Rwa 2681 log limits (#600)
Browse files Browse the repository at this point in the history
* Quick wins for reducing logs

* sampling healthchecks telemetry
  • Loading branch information
raghera authored Jul 25, 2023
1 parent e3a2d6d commit d630f9f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
21 changes: 21 additions & 0 deletions lib/applicationinsights.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,26 @@
"connectionString": "${file:/mnt/secrets/wa/app-insights-connection-string}",
"role": {
"name": "WA Case Event Handler"
},
"sampling": {
"percentage": 100
},
"preview": {
"sampling": {
"overrides": [
{
"telemetryKind": "request",
"attributes": [
{
"key": "http.url",
"value": "https?://[^/]+/health.*",
"matchType": "regexp"
}
],
"percentage": 1
}
]
}
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.hmcts.reform.wacaseeventhandler.clients;

import com.azure.messaging.servicebus.ServiceBusException;
import com.azure.messaging.servicebus.ServiceBusReceiverClient;
import com.azure.messaging.servicebus.ServiceBusSessionReceiverClient;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -32,7 +33,7 @@ public CcdCaseEventsConsumer(ServiceBusConfiguration serviceBusConfiguration,
@SuppressWarnings("squid:S2189")
public void run() {
try (ServiceBusSessionReceiverClient sessionReceiver =
serviceBusConfiguration.createCcdCaseEventsSessionReceiver()) {
serviceBusConfiguration.createCcdCaseEventsSessionReceiver()) {
while (keepRun) {
consumeMessage(sessionReceiver);
}
Expand All @@ -42,28 +43,38 @@ public void run() {
@SuppressWarnings({"PMD.DataflowAnomalyAnalysis"})
protected void consumeMessage(ServiceBusSessionReceiverClient sessionReceiver) {
try (ServiceBusReceiverClient receiver = sessionReceiver.acceptNextSession()) {

if (receiver == null) {
log.warn("ServiceBusReceiverClient receiver was null.");
return;

}

receiver.receiveMessages(1).forEach(
message -> {
try {
String messageId = message.getMessageId();
String sessionId = message.getSessionId();
log.info("Received CCD Case Event message with id '{}' and case id '{}'", messageId, sessionId);
log.info("Received CCD Case Event message with id '{}' and case id '{}'",
messageId, sessionId);

eventMessageReceiverService.handleCcdCaseEventAsbMessage(messageId, sessionId,
new String(message.getBody().toBytes()));
new String(message.getBody().toBytes()));
receiver.complete(message);

log.info("CCD Case Event message with id '{}' handled successfully", messageId);
} catch (Exception ex) {
log.error("Error processing CCD Case Event message with id '{}' - "
+ "abandon the processing and ASB will re-deliver it", message.getMessageId());
+ "abandon the processing and ASB will re-deliver it", message.getMessageId());
receiver.abandon(message);
}
});
} catch (IllegalStateException ex) {
log.info("Timeout: No CCD Case Event messages received waiting for next session.");
log.info("Timeout: No CCD Case Event messages received waiting for next session {}", ex.getMessage());
} catch (ServiceBusException ex) {
log.error("Error occurred while receiving messages {}", ex.getMessage());
} catch (Exception ex) {
log.error("Error occurred while closing the session", ex);
log.error("Error occurred while closing the session {}", ex.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void consumeMessage(ServiceBusReceiverClient receiver) {
}
});
} catch (Exception ex) {
log.error("Error occurred while completing the message processing", ex);
log.error("Error occurred while completing the message processing {}", ex.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public MessageReadinessConsumer(DeadLetterQueuePeekService deadLetterQueuePeekSe
@Override
@Transactional
public void run() {
log.info("Running message readiness check");
log.debug("Running message readiness check");
try {
final List<CaseEventMessageEntity> allMessageInNewState =
caseEventMessageRepository.getAllMessagesInNewState();

log.info("Number of messages to check the readiness {}", allMessageInNewState.size());
log.debug("Number of messages to check the readiness {}", allMessageInNewState.size());

allMessageInNewState.forEach(this::checkMessageToMoveToReadyState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CaseEventMessage handleAsbMessage(String messageId, String sessionId, Str
}

public CaseEventMessage handleCcdCaseEventAsbMessage(String messageId, String sessionId, String message) {
log.info("Received CCD Case Events ASB message with id '{}'", messageId);
log.debug("Received CCD Case Events ASB message with id '{}'", messageId);

return handleMessage(messageId, sessionId, message, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void given_session_is_accepted_when_receiver_complete_throws_error() {
publishMessageToReceiver();

doThrow(new ServiceBusException(new Exception(), ServiceBusErrorSource.UNKNOWN)).doNothing()
.when(receiverClient).complete(receivedMessage);
.when(receiverClient).complete(receivedMessage);

underTest.consumeMessage(sessionReceiverClient);

Expand All @@ -86,7 +86,7 @@ void given_session_is_accepted_when_receiver_complete_throws_error_on_both_calls
publishMessageToReceiver();

doThrow(new ServiceBusException(new Exception(), ServiceBusErrorSource.UNKNOWN))
.when(receiverClient).complete(receivedMessage);
.when(receiverClient).complete(receivedMessage);
doThrow(new ServiceBusException(new Exception(), ServiceBusErrorSource.UNKNOWN))
.when(receiverClient).abandon(receivedMessage);

Expand Down Expand Up @@ -167,4 +167,17 @@ private void publishMessageToReceiver() {
when(receiverClient.receiveMessages(1)).thenReturn(new IterableStream<>(iterableStreamFlux));
}

@Test
void should_not_throw_npe_if_receiver_is_null() {

when(sessionReceiverClient.acceptNextSession()).thenReturn(null);

underTest.consumeMessage(sessionReceiverClient);

verify(receiverClient, Mockito.times(0)).complete(receivedMessage);
verify(receiverClient, Mockito.times(0)).abandon(any());
verify(receiverClient, Mockito.times(0)).deadLetter(any(), any());

}

}

0 comments on commit d630f9f

Please sign in to comment.