|
27 | 27 | import java.nio.ByteBuffer;
|
28 | 28 | import java.time.Duration;
|
29 | 29 | import java.util.HashMap;
|
| 30 | +import java.util.List; |
30 | 31 | import java.util.Map;
|
31 | 32 |
|
32 | 33 | public class ServerSentEventHandler implements HttpHandler {
|
33 | 34 | // persistent connection, use longer max process time
|
34 | 35 | // though LB backend timeout is to 600s, for long time sse, it should be processed via message queue
|
35 | 36 | static final long MAX_PROCESS_TIME_IN_NANO = Duration.ofSeconds(300).toNanos();
|
| 37 | + static final HttpString HEADER_TRACE_ID = new HttpString("x-trace-id"); |
36 | 38 |
|
37 | 39 | private static final HttpString LAST_EVENT_ID = new HttpString("Last-Event-ID");
|
38 | 40 | private final Logger logger = LoggerFactory.getLogger(ServerSentEventHandler.class);
|
@@ -97,9 +99,18 @@ void handle(HttpServerExchange exchange, StreamSinkChannel sink) {
|
97 | 99 |
|
98 | 100 | channel = new ChannelImpl<>(exchange, sink, support.context, support.builder, actionLog.id);
|
99 | 101 | 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 | + |
100 | 111 | sink.getWriteSetter().set(channel.writeListener);
|
101 | 112 | 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)); |
103 | 114 |
|
104 | 115 | channel.sendBytes(Strings.bytes("retry: 5000\n\n")); // set browser retry to 5s
|
105 | 116 |
|
|
0 commit comments