Skip to content

Commit 0abc0f6

Browse files
committed
* sse: support client side "x-trace-id" header
Signed-off-by: neo <[email protected]>
1 parent 4d59f4e commit 0abc0f6

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* sse: send ErrorResponse to client via "event: error" on exception
66
* sse: log clientIP on sse:close action
77
* log-exporter: export action and event in parquet format
8+
* sse: support client side "x-trace-id" header
89

910
### 9.1.6 (2/10/2025 - 2/25/2025)
1011

core-ng/src/main/java/core/framework/internal/web/sse/ServerSentEventCloseHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import java.util.List;
12+
import java.util.Map;
1213

1314
class ServerSentEventCloseHandler<T> implements ExchangeCompletionListener {
1415
private static final Logger LOGGER = LoggerFactory.getLogger(ServerSentEventCloseHandler.class);
1516
final ServerSentEventContextImpl<T> context;
1617
private final LogManager logManager;
1718
private final ChannelImpl<T> channel;
18-
private final String clientIP;
19+
private final Map<String, String> logContext;
1920

20-
ServerSentEventCloseHandler(LogManager logManager, ChannelImpl<T> channel, ServerSentEventContextImpl<T> context, String clientIP) {
21+
ServerSentEventCloseHandler(LogManager logManager, ChannelImpl<T> channel, ServerSentEventContextImpl<T> context, Map<String, String> logContext) {
2122
this.logManager = logManager;
2223
this.channel = channel;
2324
this.context = context;
24-
this.clientIP = clientIP;
25+
this.logContext = logContext;
2526
}
2627

2728
@Override
@@ -36,7 +37,11 @@ public void exchangeEvent(HttpServerExchange exchange, NextListener next) {
3637
List<String> refIds = List.of(channel.refId);
3738
actionLog.refIds = refIds;
3839
actionLog.correlationIds = refIds;
39-
actionLog.context.put("client_ip", List.of(clientIP));
40+
41+
for (Map.Entry<String, String> entry : logContext.entrySet()) {
42+
actionLog.context(entry.getKey(), entry.getValue());
43+
}
44+
4045
if (!channel.groups.isEmpty()) actionLog.context("group", channel.groups.toArray());
4146
context.remove(channel);
4247
channel.shutdown();

core-ng/src/main/java/core/framework/internal/web/sse/ServerSentEventHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import java.nio.ByteBuffer;
2828
import java.time.Duration;
2929
import java.util.HashMap;
30+
import java.util.List;
3031
import java.util.Map;
3132

3233
public class ServerSentEventHandler implements HttpHandler {
3334
// persistent connection, use longer max process time
3435
// though LB backend timeout is to 600s, for long time sse, it should be processed via message queue
3536
static final long MAX_PROCESS_TIME_IN_NANO = Duration.ofSeconds(300).toNanos();
37+
static final HttpString HEADER_TRACE_ID = new HttpString("x-trace-id");
3638

3739
private static final HttpString LAST_EVENT_ID = new HttpString("Last-Event-ID");
3840
private final Logger logger = LoggerFactory.getLogger(ServerSentEventHandler.class);
@@ -97,9 +99,18 @@ void handle(HttpServerExchange exchange, StreamSinkChannel sink) {
9799

98100
channel = new ChannelImpl<>(exchange, sink, support.context, support.builder, actionLog.id);
99101
actionLog.context("channel", channel.id);
102+
103+
Map<String, String> logContext = new HashMap<>();
104+
logContext.put("client_id", request.clientIP());
105+
String traceId = exchange.getRequestHeaders().getFirst(HEADER_TRACE_ID); // used by frontend to trace request
106+
if (traceId != null) {
107+
actionLog.context.put("trace_id", List.of(traceId));
108+
logContext.put("trace_id", traceId);
109+
}
110+
100111
sink.getWriteSetter().set(channel.writeListener);
101112
support.context.add(channel);
102-
exchange.addExchangeCompleteListener(new ServerSentEventCloseHandler<>(logManager, channel, support.context, request.clientIP()));
113+
exchange.addExchangeCompleteListener(new ServerSentEventCloseHandler<>(logManager, channel, support.context, logContext));
103114

104115
channel.sendBytes(Strings.bytes("retry: 5000\n\n")); // set browser retry to 5s
105116

0 commit comments

Comments
 (0)