Commit aedd58cf authored by Kishore Battula's avatar Kishore Battula
Browse files

Merge branch 'haaggarw/Slf4jMDCContext' into 'master'

Adding mdc context in slf4j logger

See merge request !2
parents e1a18626 8cc21971
Pipeline #6077 passed with stages
in 14 minutes and 52 seconds
......@@ -56,7 +56,7 @@
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<version>0.0.17</version>
<version>0.0.19</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
......@@ -134,6 +134,41 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
package org.opengroup.osdu.wks.provider.azure.credentials;
import org.opengroup.osdu.azure.logging.Slf4JLogger;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.interfaces.UserCredential;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
@Configuration
public class JwtTokenGenerator implements UserCredential {
private final static Logger LOGGER = LoggerFactory.getLogger(JwtTokenGenerator.class);
private static final String BEARER = "Bearer ";
@Autowired
......@@ -24,11 +26,11 @@ public class JwtTokenGenerator implements UserCredential {
private AzureServicePrincipal azureServicePrincipal;
@Autowired
private ILogger logger;
private Slf4JLogger logger;
@Override
public String getIdToken(TenantInfo tenant) throws ApplicationException {
// TODO Tenant based generation of JWT Token, this will be part of the data partitioning work
// Azure service principal will have access to all data partitions
String token = null;
......@@ -41,7 +43,7 @@ public class JwtTokenGenerator implements UserCredential {
}
catch (UnsupportedEncodingException e) {
logger.error(azureBootstrapConfig.getLogPrefix(), e.getMessage(), Collections.emptyMap());
LOGGER.error(e.getMessage());
throw new ApplicationException(e.getMessage(), e.getCause());
}
......
......@@ -9,6 +9,7 @@ import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import lombok.Getter;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -58,9 +59,6 @@ public class AzureBootstrapConfig {
@Value("${max_concurrent_calls}")
private String maxConcurrentCalls;
@Value("${LOG_PREFIX}")
private String logPrefix;
@Bean
@Named("STORAGE_ACCOUNT_NAME")
public String storageAccount() {
......@@ -98,7 +96,11 @@ public class AzureBootstrapConfig {
}
@Bean
@Named("SUBSCRIPTION_CLIENT")
public MDCContextMap mdcContextMap() {
return new MDCContextMap();
}
@Bean
public SubscriptionClient subscriptionClient() {
String entityPath = serviceBusTopic + "/subscriptions/" + serviceBusTopicSubscription;
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(
......@@ -118,7 +120,6 @@ public class AzureBootstrapConfig {
}
@Bean
@Named("AZURE_SERVICE_PRINCIPAL")
public AzureServicePrincipal getAzureServicePrincipal() {
return new AzureServicePrincipal();
}
......
......@@ -4,41 +4,34 @@ import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.SubscriptionClient;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MessageHandler implements IMessageHandler {
private final static Logger LOGGER = LoggerFactory.getLogger(MessageHandler.class);
private final SubscriptionClient receiveClient;
private final ProcessWKSTransform processWKSTransform;
private final ILogger logger;
private final AzureBootstrapConfig azureBootstrapConfig;
public MessageHandler(SubscriptionClient client, ProcessWKSTransform processWKSTransform, ILogger logger, AzureBootstrapConfig azureBootstrapConfig) {
public MessageHandler(SubscriptionClient client, ProcessWKSTransform processWKSTransform, AzureBootstrapConfig azureBootstrapConfig) {
this.receiveClient = client;
this.processWKSTransform = processWKSTransform;
this.logger = logger;
this.azureBootstrapConfig = azureBootstrapConfig;
}
@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
boolean ack = this.processWKSTransform.initiateWksTransformation(message);
if(ack) {
return receiveClient.completeAsync(message.getLockToken());
}
else {
return receiveClient.abandonAsync(message.getLockToken());
}
this.processWKSTransform.initiateWksTransformation(message);
return this.receiveClient.completeAsync(message.getLockToken());
}
@Override
public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
logger.error(azureBootstrapConfig.getLogPrefix(),exceptionPhase + "-" + throwable.getMessage(), Collections.emptyMap());
LOGGER.error("{} - {}", exceptionPhase, throwable.getMessage());
}
}
......@@ -4,13 +4,16 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.microsoft.azure.servicebus.IMessage;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.model.RawRecordDetails;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
import org.opengroup.osdu.wks.service.WKSService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
......@@ -22,33 +25,35 @@ import static java.nio.charset.StandardCharsets.UTF_8;
@Component
public class ProcessWKSTransform {
private final static Logger LOGGER = LoggerFactory.getLogger(ProcessWKSTransform.class);
private static final String DATA = "data";
@Autowired
private WKSService wKSService;
@Autowired
private ILogger logger;
private AzureBootstrapConfig azureBootstrapConfig;
@Autowired
private AzureBootstrapConfig azureBootstrapConfig;
private MDCContextMap mdcContextMap;
public boolean initiateWksTransformation(IMessage message) {
public void initiateWksTransformation(IMessage message) {
String dataPartitionId = message.getProperties().get(DpsHeaders.DATA_PARTITION_ID).toString();
String correlationId = message.getProperties().get(DpsHeaders.CORRELATION_ID).toString();
MDC.setContextMap(mdcContextMap.getContextMap(correlationId, dataPartitionId));
try {
RawRecordDetails[] rawRecordDetails = retrieveDataFromMessage(message);
wKSService.transform(rawRecordDetails, dataPartitionId, correlationId);
} catch (BadRequestException e) {
logger.error(azureBootstrapConfig.getLogPrefix(), String.format("Bad Request Reason: %s, pubsub message id: %s", e.getErrorMsg(),
message.getMessageId()), Collections.emptyMap());
LOGGER.error(String.format("Bad Request Reason: %s, pubsub message id: %s", e.getErrorMsg(),
message.getMessageId()));
} catch (ApplicationException e) {
logger.error(azureBootstrapConfig.getLogPrefix(), String.format("Application Error Reason: %s, pubsub message id: %s", e.getErrorMsg(),
message.getMessageId()), Collections.emptyMap());
return false;
LOGGER.error(String.format("Application Error Reason: %s, pubsub message id: %s", e.getErrorMsg(),
message.getMessageId()));
}
return true;
MDC.clear();
}
private RawRecordDetails[] retrieveDataFromMessage(IMessage message) throws BadRequestException {
......
......@@ -3,21 +3,19 @@ package org.opengroup.osdu.wks.provider.azure.pubsub;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.SubscriptionClient;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.interfaces.SubscriptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Component
public class SubscriptionManagerImpl implements SubscriptionManager {
@Autowired
private ILogger logger;
private final static Logger LOGGER = LoggerFactory.getLogger(SubscriptionManagerImpl.class);
@Autowired
private SubscriptionClientFactory subscriptionClientFactory;
......@@ -41,7 +39,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
private void registerMessageHandler(SubscriptionClient subscriptionClient, ExecutorService executorService) {
try {
MessageHandler messageHandler = new MessageHandler(subscriptionClient, processWKSTransform, logger, azureBootstrapConfig);
MessageHandler messageHandler = new MessageHandler(subscriptionClient, processWKSTransform, azureBootstrapConfig);
subscriptionClient.registerMessageHandler(
messageHandler,
......@@ -49,7 +47,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
executorService);
} catch (InterruptedException | ServiceBusException e) {
logger.error(azureBootstrapConfig.getLogPrefix(), String.format("Error registering message handler %s", e.getMessage()), Collections.emptyMap());
LOGGER.error(String.format("Error registering message handler %s", e.getMessage()));
}
}
......
......@@ -2,27 +2,24 @@ package org.opengroup.osdu.wks.provider.azure.storage;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opengroup.osdu.azure.blobstorage.BlobStore;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.wks.model.MappingsModel;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.interfaces.MappingStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
@Component
public class MappingStoreImpl implements MappingStore {
private final static Logger LOGGER = LoggerFactory.getLogger(MappingStoreImpl.class);
private static final String JSON_EXTENSION = ".json";
@Autowired
private BlobStore blobStore;
@Autowired
private ILogger logger;
@Autowired
private AzureBootstrapConfig azureBootstrapConfig;
......@@ -35,7 +32,7 @@ public class MappingStoreImpl implements MappingStore {
ObjectMapper mapper = new ObjectMapper();
mappings = mapper.readValue(content, MappingsModel.class);
} catch (Exception e) {
logger.error(azureBootstrapConfig.getLogPrefix(), String.format("Error while processing mappings from blob store %s", e.getMessage()), Collections.emptyMap());
LOGGER.error(String.format("Error while processing mappings from blob store %s", e.getMessage()));
}
return mappings;
}
......
package org.opengroup.osdu.wks.provider.azure.utils;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import java.util.HashMap;
import java.util.Map;
public class MDCContextMap {
public Map<String, String> getContextMap(String correlationId, String dataPartitionId) {
final Map<String, String> contextMap = new HashMap<>();
contextMap.put(DpsHeaders.CORRELATION_ID, correlationId);
contextMap.put(DpsHeaders.DATA_PARTITION_ID, dataPartitionId);
return contextMap;
}
}
......@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOG_PREFIX=wks
service.domain.name=${service_domain_name}
......@@ -53,4 +52,4 @@ STORAGE_API=${storage_service_endpoint}
executor-n-threads=${executor_n_threads}
# Specified the maximum number of concurrent calls to the callback the message pump should initiate
max-concurrent-calls=${max_concurrent_calls}
max-concurrent-calls=${max_concurrent_calls}
\ No newline at end of file
......@@ -7,13 +7,11 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
......@@ -31,7 +29,6 @@ public class JwtTokenGeneratorTest {
private static final String clientSecret = "client-secret";
private static final String tenantID = "tenant-id";
private static final String appResourceId = "app-resource-id";
private static final String logPrefix = "log-prefix";
@InjectMocks
private JwtTokenGenerator jwtTokenGenerator;
......@@ -42,9 +39,6 @@ public class JwtTokenGeneratorTest {
@Mock
private AzureBootstrapConfig azureBootstrapConfig;
@Mock
private ILogger logger;
@BeforeEach
public void init() {
when(azureBootstrapConfig.getClientId()).thenReturn(clientId);
......@@ -73,7 +67,6 @@ public class JwtTokenGeneratorTest {
public void shouldThrowApplicationException() throws UnsupportedEncodingException {
doThrow(UnsupportedEncodingException.class).when(azureServicePrincipal).getIdToken(clientId, clientSecret, tenantID, appResourceId);
when(azureBootstrapConfig.getLogPrefix()).thenReturn(logPrefix);
ApplicationException exception = assertThrows(ApplicationException.class, () -> {
jwtTokenGenerator.getIdToken(new TenantInfo());
......@@ -81,7 +74,6 @@ public class JwtTokenGeneratorTest {
assertNotNull(exception);
verify(azureServicePrincipal, times(1)).getIdToken(clientId, clientSecret, tenantID, appResourceId);
verify(logger, times(1)).error(logPrefix, null, Collections.emptyMap());
verify(azureBootstrapConfig, times(1)).getClientId();
verify(azureBootstrapConfig, times(1)).getClientSecret();
verify(azureBootstrapConfig, times(1)).getTenantId();
......
......@@ -7,8 +7,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
......@@ -25,7 +28,7 @@ public class AzureBootstrapConfigTest {
private SecretClient kv;
@Test
public void kvSecret_checksForNullResponse() {
public void kvSecretChecksForNullResponse() {
doReturn(null).when(kv).getSecret("secret-name");
......@@ -37,7 +40,7 @@ public class AzureBootstrapConfigTest {
}
@Test
public void kvSecret_checksForNullValueWithinResponse() {
public void kvSecretChecksForNullValueWithinResponse() {
doReturn(null).when(secret).getValue();
doReturn(secret).when(kv).getSecret("secret-name");
......@@ -49,7 +52,7 @@ public class AzureBootstrapConfigTest {
}
@Test
public void config_returnsCorrectSecret_cosmosKey() {
public void configReturnsCorrectSecretCosmosKey() {
doReturn("cosmos-key-secret").when(secret).getValue();
doReturn(secret).when(kv).getSecret("cosmos-primary-key");
......@@ -58,12 +61,28 @@ public class AzureBootstrapConfigTest {
}
@Test
public void config_returnsCorrectSecret_cosmosEndpoint() {
public void configReturnsCorrectSecretCosmosEndpoint() {
doReturn("cosmos-endpoint-secret").when(secret).getValue();
doReturn(secret).when(kv).getSecret("cosmos-endpoint");
String secretValue = bootstrapConfig.cosmosEndpoint(kv);
assertEquals( "cosmos-endpoint-secret", secretValue);
}
@Test
public void azureServicePrincipalShouldNotBeNull() {
AzureServicePrincipal azureServicePrincipal = bootstrapConfig.getAzureServicePrincipal();
assertNotNull(azureServicePrincipal);
}
@Test
public void MDCContextMapShouldNotBeNull() {
MDCContextMap mdcContextMap = bootstrapConfig.mdcContextMap();
assertNotNull(mdcContextMap);
}
}
......@@ -11,7 +11,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.util.UUID;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -40,18 +40,10 @@ public class MessageHandlerTest {
@Test
public void shouldInvokeCompleteAsync() {
doReturn(true).when(processWKSTransform).initiateWksTransformation(message);
doNothing().when(processWKSTransform).initiateWksTransformation(message);
messageHandler.onMessageAsync(message);
verify(subscriptionClient, times(1)).completeAsync(uuid);
verify(processWKSTransform, times(1)).initiateWksTransformation(message);
}
@Test
public void shouldInvokeAbandonAsync() {
doReturn(false).when(processWKSTransform).initiateWksTransformation(message);
messageHandler.onMessageAsync(message);
verify(subscriptionClient, times(1)).abandonAsync(uuid);
verify(processWKSTransform, times(1)).initiateWksTransformation(message);
}
}
......@@ -22,20 +22,18 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.exceptions.BadRequestException;
import org.opengroup.osdu.wks.model.RawRecordDetails;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.azure.utils.MDCContextMap;
import org.opengroup.osdu.wks.service.WKSService;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
......@@ -53,8 +51,8 @@ public class ProcessWKSTransformTest {
private static final String emptyMessage = "{}";
private static final String validMessage = "{\"message\":{\"data\":[{\"id\":\"opendes:at:wellbore-b3Blbm\",\"kind\":\"opendes:at:wellbore:1.0.0\",\"op\":\"create\"}],\"account-id\":\"opendes\",\"data-partition-id\":\"opendes\",\"correlation-id\":\"908fcf8d-30c5-4c74-a0ae-ab47b48b7a85\"}}";
private static final String noDataMessage = "{\"message\":{\"data\":[],\"account-id\":\"opendes\",\"data-partition-id\":\"opendes\",\"correlation-id\":\"908fcf8d-30c5-4c74-a0ae-ab47b48b7a85\"}}";
private static final String logPrefix = "log-prefix";
private static final String messageId = "message-id";
@InjectMocks
private ProcessWKSTransform processWKSTransform;
......@@ -68,71 +66,64 @@ public class ProcessWKSTransformTest {
private AzureBootstrapConfig azureBootstrapConfig;
@Mock
private ILogger logger;
private MDCContextMap mdcContextMap;
@BeforeEach
public void init() {
when(message.getProperties()).thenReturn(getMessageProperties());
lenient().when(message.getMessageId()).thenReturn(messageId);
lenient().when(azureBootstrapConfig.getLogPrefix()).thenReturn(logPrefix);
}
@Test
public void shouldReturnTrueWhenBadRequestExceptionInRetrieveDataFromMessage() {
public void shouldRaiseBadRequestExceptionInRetrieveDataFromMessage() {
when(message.getMessageBody()).thenReturn(getMessageBody(