Skip to content

Commit

Permalink
链路输出 Header 信息,默认关闭,通过-Ddd.trace.headers.enabled=true进行开启 #69
Browse files Browse the repository at this point in the history
  • Loading branch information
liurui committed Jan 10, 2024
1 parent a3464cd commit 911e0ad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.instrumentation.servlet.ServletBlockingHelper;
import java.security.Principal;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -36,17 +37,30 @@ public static boolean onEnter(
}

final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
httpServletResponse.setHeader("guance_trace_id", GlobalTracer.get().getTraceId());
Object spanAttr = request.getAttribute(DD_SPAN_ATTRIBUTE);

Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
StringBuffer requestHeader = new StringBuffer("");
while(headerNames.hasMoreElements()){
String headerName = headerNames.nextElement();
requestHeader.append(headerName+"\t\t\t\t:"+httpServletRequest.getHeader(headerName)).append("\n");
}


final boolean hasServletTrace = spanAttr instanceof AgentSpan;
if (hasServletTrace) {
// Tracing might already be applied by the FilterChain or a parent request (forward/include).
AgentSpan span = (AgentSpan)spanAttr;
span.setTag("servlet.request_header",requestHeader.toString());
return false;
}

if (response instanceof HttpServletResponse) {
// Default value for checking for uncaught error later
InstrumentationContext.get(ServletResponse.class, Integer.class).put(response, 200);
((HttpServletResponse) response).setHeader("guance_trace_id", GlobalTracer.get().getTraceId());

}

final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(httpServletRequest);
Expand All @@ -55,7 +69,7 @@ public static boolean onEnter(
scope.setAsyncPropagation(true);
DECORATE.afterStart(span);
DECORATE.onRequest(span, httpServletRequest, httpServletRequest, extractedContext);

span.setTag("servlet.request_header",requestHeader.toString());
httpServletRequest.setAttribute(DD_SPAN_ATTRIBUTE, span);
httpServletRequest.setAttribute(
CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.bytebuddy.asm.Advice;
import java.util.Enumeration;

public class Servlet3Advice {

Expand All @@ -40,7 +41,6 @@ public static boolean onEnter(

final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
final HttpServletResponse httpServletResponse = (HttpServletResponse) response;

Object dispatchSpan = request.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE);
if (dispatchSpan instanceof AgentSpan) {
request.removeAttribute(DD_DISPATCH_SPAN_ATTRIBUTE);
Expand All @@ -55,11 +55,49 @@ public static boolean onEnter(
return false;
}
httpServletResponse.setHeader("guance_trace_id", GlobalTracer.get().getTraceId());
StringBuffer requestHeader = new StringBuffer("");
StringBuffer responseHeader = new StringBuffer("");
boolean tracerHeader = Config.get().isTracerHeaderEnabled();
if (tracerHeader) {
Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
int count = 0;
while (headerNames.hasMoreElements()) {
if (count==0){
requestHeader.append("{");
}else{
requestHeader.append(",");
}
String headerName = headerNames.nextElement();
requestHeader.append("\"").append(headerName).append("\":").append("\"").append(httpServletRequest.getHeader(headerName).replace("\"","")).append("\"\n");
count ++;
}
if (count>0){
requestHeader.append("}");
}
count = 0;
for (String headerName : httpServletResponse.getHeaderNames()) {
if (count==0){
responseHeader.append("{");
}else{
responseHeader.append(",");
}
responseHeader.append("\"").append(headerName).append("\":").append("\"").append(httpServletResponse.getHeader(headerName)).append("\"\n");
count ++;
}

if (count>0){
responseHeader.append("}");
}

}
finishSpan = true;

Object spanAttrValue = request.getAttribute(DD_SPAN_ATTRIBUTE);
final boolean hasServletTrace = spanAttrValue instanceof AgentSpan;
if (hasServletTrace) {
AgentSpan span = (AgentSpan)spanAttrValue;
span.setTag("servlet.request_header",requestHeader.toString());
span.setTag("servlet.response_header",responseHeader.toString());
// Tracing might already be applied by other instrumentation,
// the FilterChain or a parent request (forward/include).
return false;
Expand All @@ -73,6 +111,9 @@ public static boolean onEnter(
DECORATE.afterStart(span);
DECORATE.onRequest(span, httpServletRequest, httpServletRequest, extractedContext);

span.setTag("servlet.request_header",requestHeader.toString());
span.setTag("servlet.response_header",responseHeader.toString());

httpServletRequest.setAttribute(DD_SPAN_ATTRIBUTE, span);
httpServletRequest.setAttribute(
CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId());
Expand Down Expand Up @@ -115,7 +156,6 @@ public static void stopSpan(

if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
final HttpServletResponse resp = (HttpServletResponse) response;

final AgentSpan span = scope.span();

if (request.isAsyncStarted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,7 @@ public final class ConfigDefaults {

static final boolean DEFAULT_LOG_PATTERN_REPLACE=false;

static final boolean DEFAULT_TRACE_HEADER_ENABLED = false;

private ConfigDefaults() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,7 @@ public final class TracerConfig {

public static final String TRACE_FLUSH_INTERVAL = "trace.flush.interval";

public static final String TRACE_HEADER_ENABLED = "trace.headers.enabled";

private TracerConfig() {}
}
10 changes: 10 additions & 0 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_AGENT_PORT;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_AGENT_V05_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_ANALYTICS_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_HEADER_ENABLED;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_HTTP_RESOURCE_REMOVE_TRAILING_SLASH;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_LONG_RUNNING_FLUSH_INTERVAL;
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_PROPAGATION_EXTRACT_FIRST;
Expand Down Expand Up @@ -408,6 +409,7 @@
import static datadog.trace.api.config.TracerConfig.TRACE_CLIENT_IP_HEADER;
import static datadog.trace.api.config.TracerConfig.TRACE_CLIENT_IP_RESOLVER_ENABLED;
import static datadog.trace.api.config.TracerConfig.TRACE_GIT_METADATA_ENABLED;
import static datadog.trace.api.config.TracerConfig.TRACE_HEADER_ENABLED;
import static datadog.trace.api.config.TracerConfig.TRACE_HTTP_CLIENT_PATH_RESOURCE_NAME_MAPPING;
import static datadog.trace.api.config.TracerConfig.TRACE_HTTP_RESOURCE_REMOVE_TRAILING_SLASH;
import static datadog.trace.api.config.TracerConfig.TRACE_HTTP_SERVER_PATH_RESOURCE_NAME_MAPPING;
Expand Down Expand Up @@ -884,6 +886,7 @@ static class HostNameHolder {
private final boolean elasticsearchBodyAndParamsEnabled;
private final boolean sparkTaskHistogramEnabled;
private final boolean jaxRsExceptionAsErrorsEnabled;
private final boolean tracerHeaderEnabled;

private final float traceFlushIntervalSeconds;

Expand Down Expand Up @@ -1874,6 +1877,7 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
cwsEnabled = configProvider.getBoolean(CWS_ENABLED, DEFAULT_CWS_ENABLED);
cwsTlsRefresh = configProvider.getInteger(CWS_TLS_REFRESH, DEFAULT_CWS_TLS_REFRESH);

tracerHeaderEnabled = configProvider.getBoolean(TRACE_HEADER_ENABLED, DEFAULT_TRACE_HEADER_ENABLED);
dataStreamsEnabled =
configProvider.getBoolean(DATA_STREAMS_ENABLED, DEFAULT_DATA_STREAMS_ENABLED);

Expand Down Expand Up @@ -3200,6 +3204,10 @@ public boolean isJaxRsExceptionAsErrorEnabled() {
return jaxRsExceptionAsErrorsEnabled;
}

public boolean isTracerHeaderEnabled(){
return tracerHeaderEnabled;
}

/**
* @return A map of tags to be applied only to the local application root span.
*/
Expand Down Expand Up @@ -3982,6 +3990,8 @@ public String toString() {
+ httpClientSplitByDomain
+ ", httpResourceRemoveTrailingSlash"
+ httpResourceRemoveTrailingSlash
+ ", tracerHeaderEnabled="
+tracerHeaderEnabled
+ ", dbClientSplitByInstance="
+ dbClientSplitByInstance
+ ", dbClientSplitByInstanceTypeSuffix="
Expand Down

0 comments on commit 911e0ad

Please sign in to comment.