Skip to content

Commit

Permalink
[SELC-4749] feat: added interceptor for log request and response, add…
Browse files Browse the repository at this point in the history
…ed custom log in methods (#120)
  • Loading branch information
flaminiaScarciofolo authored May 13, 2024
1 parent c270c9c commit 6eeea9f
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package it.pagopa.selfcare.user.filter;


import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter;

import java.io.IOException;

@Provider
@Slf4j
public class CustomClientRequestLoggingFilter implements ResteasyReactiveClientRequestFilter {

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
ResteasyReactiveClientRequestFilter.super.filter(requestContext);
}

@Override
public void filter(ResteasyReactiveClientRequestContext requestContext) {
String endpoint = requestContext.getUri().getPath();
String query = requestContext.getUri().getQuery();
String method = requestContext.getMethod();
MDCUtils.addOperationIdAndParameters(method);
log.info("Request: method: {}, endpoint: {}, query: {}", method, endpoint, query);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package it.pagopa.selfcare.user.filter;


import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientResponseContext;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientResponseFilter;
import org.slf4j.MDC;

@Provider
@Slf4j
public class CustomClientResponseLoggingFilter implements ResteasyReactiveClientResponseFilter {

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) {
ResteasyReactiveClientResponseFilter.super.filter(requestContext, responseContext);
}

@Override
public void filter(ResteasyReactiveClientRequestContext requestContext, ClientResponseContext responseContext) {
String endpoint = requestContext.getUri().getPath();
String query = requestContext.getUri().getQuery();
String method = requestContext.getMethod();
int status = responseContext.getStatus();
log.info("Response: method: {}, endpoint: {}, query: {}, status [{}]", method, endpoint, query, status);
MDC.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.pagopa.selfcare.user.filter;

import org.slf4j.MDC;

public class MDCUtils {

public static void addOperationIdAndParameters(String operationId) {
MDC.put("sc_operation_id", operationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public class UserInfoServiceDefault implements UserInfoService {
@Override
public Uni<UserInfoResponse> findById(String userId) {
Uni<UserInfo> userInfo = UserInfo.findById(userId);
return userInfo.onItem().transform(userInfoMapper::toResponse);
return userInfo
.onItem().invoke(user -> log.info("Founded userInfo for userId: {}", userId))
.onItem().transform(userInfoMapper::toResponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public Uni<Long> updateUserCreatedAtByInstitutionAndProduct(String institutionId
Map<String, Object> filterMap = userUtils.retrieveMapForFilter(onboardedProductFilterMap, userInstitutionFilterMap);
Map<String, Object> fieldToUpdateMap = Map.of(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.createdAt.name(), createdAt,
UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now());
log.info("Update user institution with filter: {} and field to update: {}", filterMap, fieldToUpdateMap);
return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap))
.where(queryUtils.buildQueryDocument(filterMap, USER_INSTITUTION_COLLECTION));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring;
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.WorkContactResource;

Expand Down Expand Up @@ -167,6 +168,7 @@ private static String retrieveMail(UserResource user, UserInstitution institutio
} else {
email = certEmail.getEmail().getValue();
}
log.debug("retrieved Mail for user with id: {}", user.getId());
return email;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public class UserPermissionServiceImpl implements UserPermissionService {
@Override
public Uni<Boolean> hasPermission(String institutionId, String productId, PermissionTypeEnum permission, String userId) {
log.trace("hasPermission start");
log.debug("hasPermission institutionId = {}, productId = {}, permission = {}", institutionId, productId, permission);
log.debug("check permission {} for userId: {}, institutionId: {} and productId: {}", permission, userId, institutionId, productId);

return retrievePerson(userId, productId, institutionId)
.onItem().ifNotNull().invoke(userInstitution -> log.debug("UserInstitution founded for given parameters"))
.onItem().transform(userInstitution -> PermissionTypeEnum.ANY.equals(permission) || checkProductRole(userInstitution, productId, permission))
.onFailure(ResourceNotFoundException.class).recoverWithItem(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ public Uni<List<UserNotificationToSend>> updateUserRegistryAndSendNotificationTo
.institutionId(StringUtils.isNotBlank(institutionId) ? institutionId : null).build();

return Uni.combine().all()
.unis(userRegistryApi.findByIdUsingGET(USERS_FIELD_LIST_WITHOUT_FISCAL_CODE, userId),
userInstitutionService.findAllWithFilter(userInstitutionFilter.constructMap()).collect().asList())
.unis(userRegistryApi.findByIdUsingGET(USERS_FIELD_LIST_WITHOUT_FISCAL_CODE, userId)
.onItem().ifNotNull().invoke(() -> log.debug("User founded on userRegistry with userId: {}", userId)),
userInstitutionService.findAllWithFilter(userInstitutionFilter.constructMap()).collect().asList()
.onItem().ifNotNull().invoke(() -> log.debug("UserInstitution founded for userId: {} and institutionId: {}", userId, institutionId)))
.asTuple()
.onItem().transformToMulti(tuple -> findMailUuidAndUpdateUserRegistry(tuple.getItem1(), updateUserRequest)
.onItem().transformToMulti(uuidMail -> updateUserInstitutionAndSendNotification(tuple.getItem1(), tuple.getItem2(), uuidMail)))
.collect().asList();
.collect().asList()
.onItem().invoke(items -> log.trace("update {} users on userRegistry", items.size()));
}

private Multi<UserNotificationToSend> updateUserInstitutionAndSendNotification(UserResource userResource, List<UserInstitution> userInstitutions, String mailUuid) {
return Multi.createFrom().iterable(userInstitutions.stream()
.peek(userInstitution -> userInstitution.setUserMailUuid(mailUuid))
.toList())
.onItem().transformToUniAndMerge(userInstitutionService::persistOrUpdate)
.onItem().invoke(() -> log.debug("UserInstitution updated successfully"))
.onItem().transformToMultiAndMerge(userInstitution -> sendKafkaNotification(userResource, userInstitution));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public Uni<Void> checkProductRole(String productId, PartyRole role, String produ
if (StringUtils.isNotBlank(productRole) && StringUtils.isNotBlank(productId)) {
try {
productService.validateProductRole(productId, productRole, role);
log.debug("Product role {} is valid for product {}", productRole, productId);
} catch (IllegalArgumentException e) {
throw new InvalidRequestException(e.getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion apps/user-ms/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ smallrye.jwt.claims.groups=USER
smallrye.jwt.path.sub=name

## OPEN API ##
uarkus.smallrye-openapi.info-title=User API
quarkus.smallrye-openapi.info-title=User API
%dev.quarkus.smallrye-openapi.info-title=User API (development)
%uat.quarkus.smallrye-openapi.info-title=User API (test)
quarkus.smallrye-openapi.info-version=1.0.0
Expand Down

0 comments on commit 6eeea9f

Please sign in to comment.