-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDurableLogger.java
More file actions
136 lines (117 loc) · 4.8 KB
/
Copy pathDurableLogger.java
File metadata and controls
136 lines (117 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.logging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import software.amazon.lambda.durable.DurableContext;
import software.amazon.lambda.durable.StepContext;
import software.amazon.lambda.durable.context.BaseContext;
import software.amazon.lambda.durable.model.SafeCloseable;
/**
* Logger wrapper that adds durable execution context to log entries via MDC and optionally suppresses logs during
* replay.
*/
public class DurableLogger {
static final String MDC_DURABLE_EXECUTION_ARN = "durableExecutionArn";
static final String MDC_EXECUTION_ARN = "executionArn";
static final String MDC_REQUEST_ID = "requestId";
static final String MDC_OPERATION_ID = "operationId";
static final String MDC_CONTEXT_ID = "contextId";
static final String MDC_OPERATION_NAME = "operationName";
static final String MDC_CONTEXT_NAME = "contextName";
static final String MDC_ATTEMPT = "attempt";
public static final DurableLogger INSTANCE = new DurableLogger(LoggerFactory.getLogger(DurableLogger.class));
private static final SafeCloseable AUTO_CLOSER = DurableLogger::detachContext;
private final Logger delegate;
/**
* Creates a DurableLogger wrapping the given SLF4J logger with execution context MDC entries.
*
* @param delegate the SLF4J logger to wrap
*/
public DurableLogger(Logger delegate) {
this.delegate = delegate;
}
public static SafeCloseable attachContext() {
var context = BaseContext.getCurrentContext();
if (context != null) {
injectMdcProperties(context);
}
return AUTO_CLOSER;
}
public static void detachContext() {
var context = BaseContext.getCurrentContext();
if (context != null) {
MDC.clear();
}
}
private static void injectMdcProperties(BaseContext context) {
var config = context.getDurableConfig().getLoggerConfig();
// execution arn
if (config.oldKeyNames()) {
MDC.put(MDC_DURABLE_EXECUTION_ARN, context.getExecutionArn());
} else {
MDC.put(MDC_EXECUTION_ARN, context.getExecutionArn());
}
// lambda request id
var requestId =
context.getLambdaContext() != null ? context.getLambdaContext().getAwsRequestId() : null;
if (requestId != null) {
MDC.put(MDC_REQUEST_ID, requestId);
}
if (context instanceof DurableContext) {
// context thread - context id and name
if (context.getContextId() != null) {
if (config.oldKeyNames()) {
MDC.put(MDC_CONTEXT_ID, context.getContextId());
} else {
MDC.put(MDC_OPERATION_ID, context.getContextId());
}
}
if (context.getContextName() != null) {
if (config.oldKeyNames()) {
MDC.put(MDC_CONTEXT_NAME, context.getContextName());
} else {
MDC.put(MDC_OPERATION_NAME, context.getContextName());
}
}
} else if (context instanceof StepContext stepContext) {
// In step context, context id is the operation id, context name is the operation name
var operationId = context.getContextId();
MDC.put(MDC_OPERATION_ID, operationId);
if (context.getContextName() != null) {
MDC.put(MDC_OPERATION_NAME, context.getContextName());
}
MDC.put(MDC_ATTEMPT, String.valueOf(stepContext.getAttempt()));
}
}
public void trace(String format, Object... args) {
log(() -> delegate.trace(format, args));
}
public void debug(String format, Object... args) {
log(() -> delegate.debug(format, args));
}
public void info(String format, Object... args) {
log(() -> delegate.info(format, args));
}
public void warn(String format, Object... args) {
log(() -> delegate.warn(format, args));
}
public void error(String format, Object... args) {
log(() -> delegate.error(format, args));
}
public void error(String message, Throwable t) {
log(() -> delegate.error(message, t));
}
private boolean shouldSuppress(BaseContext context) {
return context instanceof DurableContext durableContext
&& context.getDurableConfig().getLoggerConfig().suppressReplayLogs()
&& durableContext.isReplaying();
}
private void log(Runnable logAction) {
var threadLocalContext = BaseContext.getCurrentContext();
if (!shouldSuppress(threadLocalContext)) {
logAction.run();
}
}
}