Skip to content

Commit 3eec808

Browse files
authored
add a Parent-Request-Id header (#1365)
add a Parent-Request-Id header
1 parent fc02be9 commit 3eec808

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: add a Parent-Request-Id header
4+
links:
5+
- https://github.com/palantir/tracing-java/pull/1365

tracing-api/src/main/java/com/palantir/tracing/api/TraceHttpHeaders.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.palantir.tracing.api;
1818

19-
/** Zipkin-compatible HTTP header names. */
19+
/** HTTP header names used by tracing-java. */
2020
public interface TraceHttpHeaders {
2121
String TRACE_ID = "X-B3-TraceId";
2222
/**
@@ -39,4 +39,12 @@ public interface TraceHttpHeaders {
3939
*/
4040
@Deprecated
4141
String ORIGINATING_SPAN_ID = "X-OrigSpanId";
42+
43+
/**
44+
* The {@code Parent-Request-Id} header represents the requestId of the caller, if present.
45+
* This provides a direct causal connection between requests up a call stack. This can be an advantage
46+
* over traceId which provides a broad set of data without information to understand the call
47+
* pathing unless spans are sampled.
48+
*/
49+
String PARENT_REQUEST_ID = "Parent-Request-Id";
4250
}

tracing/src/main/java/com/palantir/tracing/Tracers.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ public static <T> void addTracingHeaders(
278278
if (forUserAgent.isPresent()) {
279279
tracingHeadersEnrichingFunction.addHeader(TraceHttpHeaders.FOR_USER_AGENT, forUserAgent.get(), state);
280280
}
281+
Optional<String> requestId = traceMetadata.getRequestId();
282+
if (requestId.isPresent()) {
283+
tracingHeadersEnrichingFunction.addHeader(TraceHttpHeaders.PARENT_REQUEST_ID, requestId.get(), state);
284+
}
281285
}
282286

283287
private static final class ListenableFutureSpanListener implements Runnable {

tracing/src/test/java/com/palantir/tracing/TracersTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,32 @@ public void testAddTracingHeaders_populates() {
907907
"X-B3-TraceId", "defaultTraceId",
908908
"X-B3-Sampled", "1"));
909909
assertThat(headers).containsKey("X-B3-SpanId");
910+
// no requestId set when span type is local
911+
assertThat(headers).doesNotContainKey("Parent-Request-Id");
912+
}
913+
914+
@Test
915+
public void testAddTracingHeaders_addsParentRequestId() {
916+
// need to set span type to SERVER_INCOMING to generate a requestId
917+
Tracer.initTraceWithSpan(
918+
Observability.SAMPLE,
919+
"defaultTraceId",
920+
Optional.of("forUserAgent"),
921+
"rootOperation",
922+
SpanType.SERVER_INCOMING);
923+
Optional<TraceMetadata> traceMetadata = Tracer.maybeGetTraceMetadata();
924+
assertThat(traceMetadata).isPresent();
925+
926+
Optional<String> requestId = traceMetadata.get().getRequestId();
927+
assertThat(requestId).isPresent();
928+
929+
Map<String, String> headers = new HashMap<>();
930+
TracingHeadersEnrichingFunction<Map<String, String>> enrichingFunction =
931+
(headerName, headerValue, state) -> state.put(headerName, headerValue);
932+
933+
Tracers.addTracingHeaders(headers, enrichingFunction);
934+
935+
assertThat(headers).containsEntry("Parent-Request-Id", requestId.get());
910936
}
911937

912938
private static Callable<Void> newTraceExpectingCallable(String expectedOperation) {

0 commit comments

Comments
 (0)