Skip to content

Commit

Permalink
runtime-v2: synchronize LogContext creation
Browse files Browse the repository at this point in the history
Trying an easy fix first.
  • Loading branch information
ibodrov committed May 7, 2024
1 parent 8c604ed commit 7b5bc74
Showing 1 changed file with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import javax.el.ELException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -93,7 +94,7 @@ public void eval(Runtime runtime, State state, ThreadId threadId) {
UUID correlationId = getCorrelationId();
Context ctx = contextFactory.create(runtime, state, threadId, step, correlationId);

logContext = getLogContext(runtime, ctx, correlationId);
logContext = initLogContext(runtime, ctx, correlationId);
if (logContext == null) {
ContextProvider.withContext(ctx, () -> execute(runtime, state, threadId));
} else {
Expand Down Expand Up @@ -134,17 +135,23 @@ protected LogContext getLogContext() {
return logContext;
}

private LogContext getLogContext(Runtime runtime, Context ctx, UUID correlationId) {
if (logContext != null) {
return logContext;
}
private LogContext initLogContext(Runtime runtime, Context ctx, UUID correlationId) {
// TODO create logContext outside of StepCommand
synchronized (this) {
if (logContext != null) {
if (!Objects.equals(correlationId, this.logContext.correlationId())) {
log.warn("initLogContext -> correlationId mismatch, requested: {}, current: {}", correlationId, this.logContext.correlationId());
}
return logContext;
}

String segmentName = getSegmentName(ctx, getStep());
if (segmentName == null) {
return null;
}
String segmentName = getSegmentName(ctx, getStep());
if (segmentName == null) {
return null;
}

return buildLogContext(runtime, segmentName, correlationId);
return logContext = buildLogContext(runtime, segmentName, correlationId);
}
}

private LogContext buildLogContext(Runtime runtime, String segmentName, UUID correlationId) {
Expand Down Expand Up @@ -192,9 +199,9 @@ private static String getExceptionMessage(Exception e) {

if (e instanceof ELException) {
return
ExceptionUtils.getExceptionList(e).stream()
.map(Throwable::getMessage)
.collect(Collectors.joining(". "));
ExceptionUtils.getExceptionList(e).stream()
.map(Throwable::getMessage)
.collect(Collectors.joining(". "));
}

List<Throwable> exceptions = ExceptionUtils.getExceptionList(e);
Expand Down

0 comments on commit 7b5bc74

Please sign in to comment.