Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include traceId as a response header #221

Merged
1 commit merged into from
Mar 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
import com.markelliot.barista.tracing.Traces;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;

public record TracingHandler(double rate, HttpHandler delegate) implements HttpHandler {
private static final HttpString TRACE_ID = HttpString.tryFromString("X-B3-TraceId");
private static final HttpString SAMPLED = HttpString.tryFromString("X-B3-Sampled");
private static final HttpString SPAN_ID = HttpString.tryFromString("X-B3-SpanId");

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Trace trace = getTraceForRequest(exchange);
Expand All @@ -35,25 +40,27 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
span.close();
next.proceed();
});
// Populate response before proceeding since later operations might commit the response.
setExchangeState(exchange, span);
delegate.handleRequest(exchange);
}

private Trace getTraceForRequest(HttpServerExchange exchange) {
String traceId = getId(exchange, "X-B3-TraceId");
String traceId = getId(exchange, TRACE_ID);
if (traceId != null) {
return Traces.create(traceId, isSampled(exchange));
}
return Traces.create(Ids.randomId(), ThreadLocalRandom.current().nextDouble() < rate);
}

private static boolean isSampled(HttpServerExchange exchange) {
String val = exchange.getRequestHeaders().getFirst("X-B3-Sampled");
String val = exchange.getRequestHeaders().getFirst(SAMPLED);
return val != null && (val.equals("1") || val.equalsIgnoreCase("true"));
}

private Span getSpanForRequest(HttpServerExchange exchange, Trace trace) {
Supplier<String> opName = opName(exchange);
String spanId = getId(exchange, "X-B3-SpanId");
String spanId = getId(exchange, SPAN_ID);
if (spanId != null) {
return trace.withParent(spanId, opName);
}
Expand All @@ -65,11 +72,15 @@ private static Supplier<String> opName(HttpServerExchange exchange) {
}

/** Gets the value of the named header the request and returns it if safe to handle. */
private static String getId(HttpServerExchange exchange, String header) {
private static String getId(HttpServerExchange exchange, HttpString header) {
String val = exchange.getRequestHeaders().getFirst(header);
if (val != null && val.length() <= 32) {
return val;
}
return null;
}

private static void setExchangeState(HttpServerExchange exchange, Span span) {
exchange.getResponseHeaders().put(TRACE_ID, span.traceId());
}
}