@Test public void testExtractXCloudTraceContext_spanIdZero() { String xCloudTraceContext = "8fd836bcfe241ee19a057679a77ba317/0;o=1"; XCloudTraceContextExtractor extractor = new XCloudTraceContextExtractor<>( B3Propagation.FACTORY.get(), (request, key) -> xCloudTraceContext); TraceContextOrSamplingFlags context = extractor.extract(new Object()); assertThat(context.traceIdContext().traceId()).isEqualTo(-7348336952112078057L); assertThat(context.traceIdContext().traceIdHigh()).isEqualTo(-8081649345970823455L); assertThat(context.traceIdContext().sampled()).isTrue(); }
@Override public TraceContextOrSamplingFlags extract(R request) { if (request == null) throw new NullPointerException("request == null"); TraceContextOrSamplingFlags context = primary.extract(request); if (context != TraceContextOrSamplingFlags.EMPTY) return context; TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY; String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME); if (xCloudTraceContext != null) { String[] tokens = xCloudTraceContext.split("/"); long[] traceId = convertHexTraceIdToLong(tokens[0]); if (traceId != null) { long spanId = 0; Boolean traceTrue = null; if (tokens.length >= 2) { String[] traceOptionTokens = tokens[1].split(";"); if (traceOptionTokens.length >= 1 && !traceOptionTokens[0].isEmpty()) { spanId = parseUnsignedLong(traceOptionTokens[0]); } if (traceOptionTokens.length >= 2) { traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]); } } if (spanId == 0) { result = TraceContextOrSamplingFlags.create( TraceIdContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .sampled(traceTrue) .build()); } else { result = TraceContextOrSamplingFlags.create( TraceContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .spanId(spanId) .sampled(traceTrue) .build()); } } } return result; }
XCloudTraceContextExtractor implements Extractor<R> { @Override public TraceContextOrSamplingFlags extract(R request) { if (request == null) throw new NullPointerException("request == null"); TraceContextOrSamplingFlags context = primary.extract(request); if (context != TraceContextOrSamplingFlags.EMPTY) return context; TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY; String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME); if (xCloudTraceContext != null) { String[] tokens = xCloudTraceContext.split("/"); long[] traceId = convertHexTraceIdToLong(tokens[0]); if (traceId != null) { long spanId = 0; Boolean traceTrue = null; if (tokens.length >= 2) { String[] traceOptionTokens = tokens[1].split(";"); if (traceOptionTokens.length >= 1 && !traceOptionTokens[0].isEmpty()) { spanId = parseUnsignedLong(traceOptionTokens[0]); } if (traceOptionTokens.length >= 2) { traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]); } } if (spanId == 0) { result = TraceContextOrSamplingFlags.create( TraceIdContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .sampled(traceTrue) .build()); } else { result = TraceContextOrSamplingFlags.create( TraceContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .spanId(spanId) .sampled(traceTrue) .build()); } } } return result; } }
XCloudTraceContextExtractor implements Extractor<R> { @Override public TraceContextOrSamplingFlags extract(R request) { if (request == null) throw new NullPointerException("request == null"); TraceContextOrSamplingFlags context = primary.extract(request); if (context != TraceContextOrSamplingFlags.EMPTY) return context; TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY; String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME); if (xCloudTraceContext != null) { String[] tokens = xCloudTraceContext.split("/"); long[] traceId = convertHexTraceIdToLong(tokens[0]); if (traceId != null) { long spanId = 0; Boolean traceTrue = null; if (tokens.length >= 2) { String[] traceOptionTokens = tokens[1].split(";"); if (traceOptionTokens.length >= 1 && !traceOptionTokens[0].isEmpty()) { spanId = parseUnsignedLong(traceOptionTokens[0]); } if (traceOptionTokens.length >= 2) { traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]); } } if (spanId == 0) { result = TraceContextOrSamplingFlags.create( TraceIdContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .sampled(traceTrue) .build()); } else { result = TraceContextOrSamplingFlags.create( TraceContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .spanId(spanId) .sampled(traceTrue) .build()); } } } return result; } XCloudTraceContextExtractor(Propagation<String> primary, Getter<R, String> getter); }
XCloudTraceContextExtractor implements Extractor<R> { @Override public TraceContextOrSamplingFlags extract(R request) { if (request == null) throw new NullPointerException("request == null"); TraceContextOrSamplingFlags context = primary.extract(request); if (context != TraceContextOrSamplingFlags.EMPTY) return context; TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY; String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME); if (xCloudTraceContext != null) { String[] tokens = xCloudTraceContext.split("/"); long[] traceId = convertHexTraceIdToLong(tokens[0]); if (traceId != null) { long spanId = 0; Boolean traceTrue = null; if (tokens.length >= 2) { String[] traceOptionTokens = tokens[1].split(";"); if (traceOptionTokens.length >= 1 && !traceOptionTokens[0].isEmpty()) { spanId = parseUnsignedLong(traceOptionTokens[0]); } if (traceOptionTokens.length >= 2) { traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]); } } if (spanId == 0) { result = TraceContextOrSamplingFlags.create( TraceIdContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .sampled(traceTrue) .build()); } else { result = TraceContextOrSamplingFlags.create( TraceContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .spanId(spanId) .sampled(traceTrue) .build()); } } } return result; } XCloudTraceContextExtractor(Propagation<String> primary, Getter<R, String> getter); @Override TraceContextOrSamplingFlags extract(R request); }
XCloudTraceContextExtractor implements Extractor<R> { @Override public TraceContextOrSamplingFlags extract(R request) { if (request == null) throw new NullPointerException("request == null"); TraceContextOrSamplingFlags context = primary.extract(request); if (context != TraceContextOrSamplingFlags.EMPTY) return context; TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY; String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME); if (xCloudTraceContext != null) { String[] tokens = xCloudTraceContext.split("/"); long[] traceId = convertHexTraceIdToLong(tokens[0]); if (traceId != null) { long spanId = 0; Boolean traceTrue = null; if (tokens.length >= 2) { String[] traceOptionTokens = tokens[1].split(";"); if (traceOptionTokens.length >= 1 && !traceOptionTokens[0].isEmpty()) { spanId = parseUnsignedLong(traceOptionTokens[0]); } if (traceOptionTokens.length >= 2) { traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]); } } if (spanId == 0) { result = TraceContextOrSamplingFlags.create( TraceIdContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .sampled(traceTrue) .build()); } else { result = TraceContextOrSamplingFlags.create( TraceContext.newBuilder() .traceIdHigh(traceId[0]) .traceId(traceId[1]) .spanId(spanId) .sampled(traceTrue) .build()); } } } return result; } XCloudTraceContextExtractor(Propagation<String> primary, Getter<R, String> getter); @Override TraceContextOrSamplingFlags extract(R request);  }