Skip to content

Commit

Permalink
chore: Modified logging context to have only requestId and userEmail (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-nair authored May 31, 2024
1 parent d118576 commit d4645bd
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.appsmith.external.constants.MDCConstants.OBSERVATION;
import static com.appsmith.external.constants.MDCConstants.SPAN_ID;
import static com.appsmith.external.constants.MDCConstants.THREAD;
import static com.appsmith.external.constants.MDCConstants.TRACE_ID;

@Configuration
Expand Down Expand Up @@ -84,7 +83,6 @@ private void copyToMdc(Context context) {
if (!context.isEmpty() && context.hasKey(LogHelper.CONTEXT_MAP)) {
Map<String, String> map = context.get(LogHelper.CONTEXT_MAP);

map.put(THREAD, Thread.currentThread().getName());
Optional<Observation> observationOptional = context.getOrEmpty(OBSERVATION);
observationOptional.ifPresent(observation -> {
TracingObservationHandler.TracingContext tracingContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -18,10 +15,7 @@
import reactor.util.context.Context;

import java.util.Map;
import java.util.UUID;

import static com.appsmith.external.constants.MDCConstants.SESSION_ID_LOG;
import static com.appsmith.external.constants.MDCConstants.THREAD;
import static com.appsmith.external.constants.MDCConstants.USER_EMAIL;
import static java.util.stream.Collectors.toMap;

Expand All @@ -44,15 +38,9 @@ public class MDCFilter implements WebFilter {

public static final String REQUEST_ID_HEADER = "X-Request-Id";

private static final String REQUEST_ID_LOG = "requestId";
private static final String SESSION = "SESSION";

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
try {
// Using beforeCommit here ensures that the function `addContextToHttpResponse` isn't run immediately
// It is only run when the response object is being created
exchange.getResponse().beforeCommit(() -> addContextToHttpResponse(exchange.getResponse()));
return ReactiveSecurityContextHolder.getContext()
.map(ctx -> ctx.getAuthentication().getPrincipal())
.flatMap(principal -> {
Expand All @@ -76,55 +64,17 @@ private Context addRequestHeadersToContext(
}

final String internalRequestId = request.getHeaders().getFirst(INTERNAL_REQUEST_ID_HEADER);
if (!StringUtils.isEmpty(internalRequestId)) {
contextMap.put(INTERNAL_REQUEST_ID_HEADER, internalRequestId);
contextMap.put(REQUEST_ID_LOG, internalRequestId);
}
contextMap.put(INTERNAL_REQUEST_ID_HEADER, internalRequestId);

final String requestId = request.getHeaders().getFirst(REQUEST_ID_HEADER);
if (!StringUtils.isEmpty(requestId)) {
contextMap.put(REQUEST_ID_HEADER, requestId);
}

contextMap.put(SESSION_ID_LOG, getSessionId(request));

// This is for the initial thread that started the request,
// any reactive forking will generate a new thread
contextMap.put(THREAD, Thread.currentThread().getName());

// Set the MDC context here for regular non-reactive logs
MDC.setContextMap(contextMap);

// Setting the context map to the reactive context. This will be used in the reactive logger to print the MDC
return context.put(LogHelper.CONTEXT_MAP, contextMap);
}

private Mono<Void> addContextToHttpResponse(final ServerHttpResponse response) {
return Mono.deferContextual(Mono::just)
.doOnNext(ctx -> {
if (!ctx.hasKey(LogHelper.CONTEXT_MAP)) {
return;
}

final Map<String, String> contextMap = ctx.get(LogHelper.CONTEXT_MAP);

final HttpHeaders httpHeaders = response.getHeaders();
httpHeaders.set(MDC_HEADER_PREFIX + THREAD, contextMap.get(THREAD));
})
.then();
}

private String getSessionId(final ServerHttpRequest request) {
final HttpCookie cookie = request.getCookies().getFirst(SESSION);
return cookie != null ? cookie.getValue() : "";
}

private String getOrCreateInternalRequestId(final ServerHttpRequest request) {
final String header = request.getHeaders().getFirst(INTERNAL_REQUEST_ID_HEADER);
if (!StringUtils.isEmpty(header)) {
return header;
}

return UUID.randomUUID().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ appsmith.codec.max-in-memory-size=${APPSMITH_CODEC_SIZE:150}
logging.level.root=info
logging.level.com.appsmith=debug
logging.level.com.external.plugins=debug
logging.pattern.console=[%d{ISO8601, UTC}] %X - %m%n
logging.pattern.console=[%d{ISO8601, UTC}] requestId=%X{X-Appsmith-Request-Id} userEmail=%X{userEmail} traceId=%X{traceId} spanId=%X{spanId} - %m%n

#Spring security
spring.security.oauth2.client.registration.google.client-id=${APPSMITH_OAUTH2_GOOGLE_CLIENT_ID:missing_value_sentinel}
Expand Down

This file was deleted.

0 comments on commit d4645bd

Please sign in to comment.