问题描述
在 Java 中,我试图通过 HTTP POST 请求 (org.apache.http.client.methods.HttpPost) 传播 TraceContext。我将上下文参数传递给标题如下:
try (Scope scope = tracer.spanBuilder("firstSpan").startScopedSpan()) {
SpanContext spanContext = tracer.getCurrentSpan().getContext();
StringBuilder builder =
new StringBuilder()
.append(spanContext.getTraceId().toLowerBase16())
.append(SPAN_ID_DELIMITER)
.append(UnsignedLongs.toString(spanIdToLong(spanContext.getSpanId())))
.append(TRACE_OPTION_DELIMITER)
.append(spanContext.getTraceOptions().isSampled() ? SAMPLED : NOT_SAMPLED);
httpPost.addHeader(HEADER_NAME,builder.toString());
HttpResponse response = reportSession.getHttpClient().execute(httpPost,reportSession.getHttpContext());
请求由 Servlet(在单独的 JAR 中)处理,我在其中检索参数并尝试打开一个新的 Span:
String headerStr = req.getHeader(HEADER_NAME);
TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0,TRACE_ID_SIZE));
int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER,TRACE_ID_SIZE);
CharSequence spanIdStr = headerStr.subSequence(SPAN_ID_START_POS,traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);
SpanId spanId = longToSpanId(UnsignedLongs.parseUnsignedLong(spanIdStr.toString(),10));
TraceOptions traceOptions = OPTIONS_NOT_SAMPLED;
if (traceOptionsPos > 0) {
String traceOptionsstr = headerStr.substring(traceOptionsPos + TRACE_OPTION_DELIMITER_SIZE);
if ((UnsignedInts.parseUnsignedInt(traceOptionsstr,10) & CLOUD_TRACE_IS_SAMPLED) != 0) {
traceOptions = OPTIONS_SAMPLED;
}
}
SpanContext context = SpanContext.create(traceId,spanId,traceOptions,TRACESTATE_DEFAULT);
try (Scope scope = tracer.spanBuilderWithRemoteParent("newSpan",context).setSpanKind(Kind.SERVER).startScopedSpan()) {
// execute Servlet code
}
但是在 Google Cloud 平台中,我唯一能看到的 Span 是“firstSpan”,而监视器中不存在“newSpan”...有人可以帮我吗?
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)