From f9538c9c35dafa2cb05682ba07c01c737f48314d Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 5 Aug 2021 10:45:05 +1000 Subject: [PATCH] [Test] Allow trace.id in default thread context (#76089) A new trace.id header is added by #74210. It is handled almost the same way as x-opaque-id. Specifically, it gets passed into default thread context. This means the existing assertion should expect it in addition to x-opaque-id. --- .../java/org/elasticsearch/http/netty4/Netty4HttpClient.java | 1 + .../main/java/org/elasticsearch/transport/Transports.java | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java index c4ab52601c25a..4139db762c6fb 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java @@ -87,6 +87,7 @@ public List get(SocketAddress remoteAddress, String... uris) t final HttpRequest httpRequest = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, uris[i]); httpRequest.headers().add(HOST, "localhost"); httpRequest.headers().add("X-Opaque-ID", String.valueOf(i)); + httpRequest.headers().add("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"); requests.add(httpRequest); } return sendRequests(remoteAddress, requests); diff --git a/server/src/main/java/org/elasticsearch/transport/Transports.java b/server/src/main/java/org/elasticsearch/transport/Transports.java index 65d52c83649e4..46fbe55e0432e 100644 --- a/server/src/main/java/org/elasticsearch/transport/Transports.java +++ b/server/src/main/java/org/elasticsearch/transport/Transports.java @@ -13,6 +13,7 @@ import org.elasticsearch.tasks.Task; import java.util.Arrays; +import java.util.Set; public enum Transports { ; @@ -52,7 +53,9 @@ public static boolean assertNotTransportThread(String reason) { public static boolean assertDefaultThreadContext(ThreadContext threadContext) { assert threadContext.getRequestHeadersOnly().isEmpty() || - threadContext.getRequestHeadersOnly().size() == 1 && threadContext.getRequestHeadersOnly().containsKey(Task.X_OPAQUE_ID) : + threadContext.getRequestHeadersOnly().keySet().equals(Set.of(Task.X_OPAQUE_ID)) || + threadContext.getRequestHeadersOnly().keySet().equals(Set.of(Task.TRACE_ID)) || + threadContext.getRequestHeadersOnly().keySet().equals(Set.of(Task.X_OPAQUE_ID, Task.TRACE_ID)) : "expected empty context but was " + threadContext.getRequestHeadersOnly() + " on " + Thread.currentThread().getName(); return true; }