static TraceContextOrSamplingFlags tryParseSamplingFlags(CharSequence b3, int pos) { int flags = parseFlags(b3, pos); if (flags == 0) return null; return TraceContextOrSamplingFlags.create(SamplingFlags.toSamplingFlags(flags)); }
@Override public void accept(CurrentTraceContext current) { try (Scope ws = current.newScope(TraceContext.newBuilder().traceId(1L).spanId(2L).build())) { } } }
@Override public void accept(Propagation<?> propagation) { TraceContext.Injector<Map<Object, String>> injector = propagation.injector(Map::put); TraceContext.Extractor<Map<Object, String>> extractor = propagation.extractor(Map::get); TraceContext ctx = TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(false).build(); Map<Object, String> map = new LinkedHashMap<>(); injector.inject(ctx, map); assertThat(extractor.extract(map).context()) .isEqualToIgnoringGivenFields(ctx, "traceIdString", "spanIdString"); } }
@Override public TraceContextOrSamplingFlags extract(C carrier) { TraceContextOrSamplingFlags result = delegate.extract(carrier); // always allocate in case fields are added late Extra fields = propagation.factory.extraFactory.create(); for (int i = 0, length = propagation.keys.size(); i < length; i++) { String maybeValue = getter.get(carrier, propagation.keys.get(i)); if (maybeValue == null) continue; fields.put(propagation.factory.keyToField[i], maybeValue); } return result.toBuilder().addExtra(fields).build(); } }
@Override public CurrentTraceContext getObject() { CurrentTraceContext.Builder builder = ThreadLocalCurrentTraceContext.newBuilder(); if (scopeDecorators != null) { for (ScopeDecorator scopeDecorator : scopeDecorators) { builder.addScopeDecorator(scopeDecorator); } } return builder.build(); }
@Override public void inject(TraceContext traceContext, C carrier) { setter.put(carrier, propagation.traceIdKey, traceContext.traceIdString()); setter.put(carrier, propagation.spanIdKey, traceContext.spanIdString()); String parentId = traceContext.parentIdString(); if (parentId != null) { setter.put(carrier, propagation.parentSpanIdKey, parentId); } if (traceContext.debug()) { setter.put(carrier, propagation.debugKey, "1"); } else if (traceContext.sampled() != null) { setter.put(carrier, propagation.sampledKey, traceContext.sampled() ? "1" : "0"); } } }
ExtraFieldInjector(ExtraFieldPropagation<K> propagation, Setter<C, K> setter) { this.propagation = propagation; this.delegate = propagation.delegate.injector(setter); this.setter = setter; }
/** Wraps the input so that it executes with the same context as now. */ public <C> Callable<C> wrap(Callable<C> task) { final TraceContext invocationContext = get(); class CurrentTraceContextCallable implements Callable<C> { @Override public C call() throws Exception { try (Scope scope = maybeScope(invocationContext)) { return task.call(); } } } return new CurrentTraceContextCallable(); }
/** * Like {@link #writeB3SingleFormatAsBytes(TraceContext)}, but for carriers with byte array or * byte buffer values. For example, {@link ByteBuffer#wrap(byte[])} can wrap the result. */ public static byte[] writeB3SingleFormatAsBytes(TraceContext context) { char[] buffer = getCharBuffer(); int length = writeB3SingleFormat(context, context.parentIdAsLong(), buffer); return asciiToNewByteArray(buffer, length); }
@Override public void inject(TraceContext traceContext, C carrier) { setter.put(carrier, propagation.b3Key, B3SingleFormat.writeB3SingleFormat(traceContext)); } }
ExtraFieldPropagation(Factory factory, Propagation.KeyFactory<K> keyFactory, List<K> keys) { this.factory = factory; this.delegate = factory.delegate.create(keyFactory); this.keys = keys; }
@Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof RealSpan)) return false; return context.equals(((RealSpan) o).context); }
/** Uses a non-inheritable static thread local */ public static CurrentTraceContext create() { return new ThreadLocalCurrentTraceContext(new Builder(), DEFAULT); }
/** * True implies {@link #sampled()}, and is additionally a request to override any storage or * collector layer sampling. Defaults to false. */ public final boolean debug() { return debug(flags); }
TraceIdContext idContextWithFlags(int flags) { TraceIdContext traceIdContext = (TraceIdContext) value; return new TraceIdContext(flags, traceIdContext.traceIdHigh, traceIdContext.traceId); } }
Factory(Propagation.Factory delegate, String[] fieldNames, String[] keyNames, int[] keyToField) { this.delegate = delegate; this.keyToField = keyToField; this.fieldNames = fieldNames; this.keyNames = keyNames; this.extraFactory = new ExtraFactory(fieldNames); }
@Override public void accept(CurrentTraceContext current) { current.newScope(TraceContext.newBuilder().traceId(1L).spanId(2L).build()); } }