Commit 62d4df65 authored by SGupta79's avatar SGupta79
Browse files

SLI logs implementation

parent 60ce9f1f
Pipeline #26807 passed with stages
in 19 minutes and 31 seconds
package org.opengroup.osdu.wks.provider.azure.pubsub;
package org.opengroup.osdu.wks.provider.azure.pubsub;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.microsoft.azure.servicebus.IMessage;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.wks.config.ThreadScopeContextHolder;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.logging.SliLogger;
import org.opengroup.osdu.wks.logging.SliLoggerPayload;
import org.opengroup.osdu.wks.model.RawRecordDetails;
import org.opengroup.osdu.wks.provider.azure.config.ThreadDpsHeaders;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
......@@ -36,8 +39,15 @@ public class ProcessWKSTransform {
@Autowired
private ThreadDpsHeaders dpsHeaders;
@Autowired
private SliLogger sliLogger;
public void initiateWksTransformation(IMessage message) {
String dataPartitionId = null;
String correlationId = null;
try {
String messageBody = new String(message.getMessageBody().getBinaryData().get(0), UTF_8);
......@@ -47,8 +57,8 @@ public class ProcessWKSTransform {
String messageId = jsonRoot.getAsJsonObject().get(ID).getAsString();
message.setMessageId(messageId);
String dataPartitionId = messageData.getAsJsonObject().get(DpsHeaders.DATA_PARTITION_ID).getAsString();
String correlationId = messageData.getAsJsonObject().get(DpsHeaders.CORRELATION_ID).getAsString();
dataPartitionId = messageData.getAsJsonObject().get(DpsHeaders.DATA_PARTITION_ID).getAsString();
correlationId = messageData.getAsJsonObject().get(DpsHeaders.CORRELATION_ID).getAsString();
MDC.setContextMap(mdcContextMap.getContextMap(correlationId, dataPartitionId));
dpsHeaders.setThreadContext(dataPartitionId,correlationId);
......@@ -65,14 +75,36 @@ public class ProcessWKSTransform {
} catch (BadRequestException e) {
LOGGER.error("Bad Request Reason: {}, pubsub message id: {}", e.getErrorMsg(),
message.getMessageId(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(message.getMessageId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.BAD_REQUEST.value()).build());
} catch (ApplicationException e) {
LOGGER.error("Application Error Reason: {}, pubsub message id: {}", e.getErrorMsg(),
message.getMessageId(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(message.getMessageId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
} catch (NullPointerException e) {
LOGGER.error("Invalid format for message with id: {}", message.getMessageId(), e);
}
LOGGER.error("Invalid format for message with id: {}", message.getMessageId(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(message.getMessageId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
} catch (JsonSyntaxException e) {
LOGGER.error("Error occurred in payload parsing with id: {}", message.getMessageId(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(message.getMessageId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
}
catch (Exception e) {
LOGGER.error("Exception encountered processing the message with id: {}", message.getMessageId(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(message.getMessageId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
}
ThreadScopeContextHolder.getContext().clear();
......
......@@ -25,6 +25,7 @@ import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.logging.SliLogger;
import org.opengroup.osdu.wks.model.RawRecordDetails;
import org.opengroup.osdu.wks.provider.azure.config.ThreadDpsHeaders;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
......@@ -65,6 +66,10 @@ public class ProcessWKSTransformTest {
@Spy
private ThreadDpsHeaders dpsHeaders;
@Mock
private SliLogger sliLogger;
@BeforeEach
public void init() {
......@@ -80,7 +85,7 @@ public class ProcessWKSTransformTest {
processWKSTransform.initiateWksTransformation(message);
verify(message, times(1)).getMessageBody();
verify(message, times(1)).getMessageId();
verify(message, times(3)).getMessageId();
}
@Test
......@@ -92,7 +97,7 @@ public class ProcessWKSTransformTest {
verify(mdcContextMap, times(1)).getContextMap(correlationId, dataPartitionId);
verify(message, times(1)).getMessageBody();
verify(message, times(1)).getMessageId();
verify(message, times(2)).getMessageId();
verify(message, times(1)).setMessageId(messageId);
}
......@@ -107,7 +112,7 @@ public class ProcessWKSTransformTest {
verify(mdcContextMap, times(1)).getContextMap(correlationId, dataPartitionId);
verify(message, times(1)).getMessageBody();
verify(message, times(2)).getMessageId();
verify(message, times(3)).getMessageId();
verify(message, times(1)).setMessageId(messageId);
}
......
package org.opengroup.osdu.wks.logging;
import java.util.HashMap;
import java.util.Map;
import org.opengroup.osdu.core.common.logging.DefaultLogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
@Component
@RequiredArgsConstructor
public class SliLogger {
private final DefaultLogger logger;
public void writeLog(SliLoggerPayload payload) {
this.logger.info("sli.log", getSliLog(payload), getHeader(payload));
}
private String getSliLog(SliLoggerPayload payload) {
Map<String, Object> jsonPayload = createSliPayload(payload);
return String.format("%s: ", jsonPayload.toString());
}
private Map<String, String> getHeader(SliLoggerPayload payload) {
Map<String, String> jsonPayload = new HashMap<>();
jsonPayload.put(DpsHeaders.DATA_PARTITION_ID, payload.getDataPartitionId());
jsonPayload.put(DpsHeaders.CORRELATION_ID, payload.getCorrelationId());
return jsonPayload;
}
private Map<String, Object> createSliPayload(SliLoggerPayload payload) {
Map<String, Object> jsonPayload = new HashMap<>();
jsonPayload.put("status", payload.getStatus());
jsonPayload.put("statusCode", payload.getStatusCode());
jsonPayload.put("id", payload.getRecordId());
jsonPayload.put(DpsHeaders.DATA_PARTITION_ID, payload.getDataPartitionId());
jsonPayload.put(DpsHeaders.CORRELATION_ID, payload.getCorrelationId());
return jsonPayload;
}
}
package org.opengroup.osdu.wks.logging;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class SliLoggerPayload {
private String recordId;
private int statusCode;
private Boolean status;
private String dataPartitionId;
private String correlationId;
}
......@@ -14,6 +14,8 @@ import org.opengroup.osdu.wks.config.ThreadScopeContextHolder;
import org.opengroup.osdu.wks.constants.Constants;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.logging.SliLogger;
import org.opengroup.osdu.wks.logging.SliLoggerPayload;
import org.opengroup.osdu.wks.model.MappingsModel;
import org.opengroup.osdu.wks.model.RawRecordDetails;
import org.opengroup.osdu.wks.model.RelationshipStatus;
......@@ -25,6 +27,7 @@ import org.opengroup.osdu.wks.util.WksTransformator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -61,6 +64,10 @@ public class WKSServiceImpl implements WKSService {
@Autowired
private SchemaService schemaService;
@Autowired
private SliLogger sliLogger;
/**
* This method applies the WKS transformation on the valid raw records
......@@ -145,15 +152,27 @@ public class WKSServiceImpl implements WKSService {
LOGGER.info(Constants.TRANFORMATION_SUCCESSFUL, rawRecord.getId(),
wksRecordTree.get(ID).asString(), wksRecordTree.get(KIND));
sliLogger.writeLog(SliLoggerPayload.builder().status(true).recordId(rawRecord.getId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.OK.value()).build());
}
catch (ApplicationException e) {
LOGGER.warn(Constants.TRANSFORMATION_FAILED_FOR_GIVEN_WKS_KIND, rawRecord.getId(), mappings.getTargetSchemaKind(), e.getErrorMsg(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(rawRecord.getId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
}
}
} catch (ApplicationException e) {
LOGGER.warn(Constants.TRANFORMATION_FAILED, rawRecord.getId(), e.getErrorMsg(), e);
sliLogger.writeLog(SliLoggerPayload.builder().status(false).recordId(rawRecord.getId())
.correlationId(correlationId).dataPartitionId(dataPartitionId)
.statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value()).build());
}
}
......
......@@ -23,6 +23,7 @@ import org.opengroup.osdu.wks.config.RequestIdentity;
import org.opengroup.osdu.wks.constants.Constants;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.logging.SliLogger;
import org.opengroup.osdu.wks.model.AttributeMappingModel;
import org.opengroup.osdu.wks.model.MappingsModel;
import org.opengroup.osdu.wks.model.RawRecordDetails;
......@@ -78,6 +79,9 @@ public class WKSServiceImplTest {
@InjectMocks
private WKSServiceImpl wksService;
@Mock
private SliLogger sliLogger;
@Rule
public ExpectedException expectedException = ExpectedException.none();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment