@Override public String toString() { return "TraceOptions{sampled=" + isSampled() + "}"; }
/** * Converts AppEngine {@code CloudTraceContext} to {@code SpanContext}. * * @param cloudTraceContext the AppEngine {@code CloudTraceContext}. * @return the converted {@code SpanContext}. * @since 0.14 */ public static SpanContext fromCloudTraceContext(CloudTraceContext cloudTraceContext) { checkNotNull(cloudTraceContext, "cloudTraceContext"); try { // Extract the trace ID from the binary protobuf CloudTraceContext#traceId. TraceIdProto traceIdProto = TraceIdProto.parseFrom(cloudTraceContext.getTraceId()); ByteBuffer traceIdBuf = ByteBuffer.allocate(TraceId.SIZE); traceIdBuf.putLong(traceIdProto.getHi()); traceIdBuf.putLong(traceIdProto.getLo()); ByteBuffer spanIdBuf = ByteBuffer.allocate(SpanId.SIZE); spanIdBuf.putLong(cloudTraceContext.getSpanId()); return SpanContext.create( TraceId.fromBytes(traceIdBuf.array()), SpanId.fromBytes(spanIdBuf.array()), TraceOptions.builder().setIsSampled(cloudTraceContext.isTraceEnabled()).build(), TRACESTATE_DEFAULT); } catch (com.google.protobuf.InvalidProtocolBufferException e) { throw new RuntimeException(e); } }
@Test public void getOptions() { assertThat(TraceOptions.DEFAULT.getOptions()).isEqualTo(0); assertThat(TraceOptions.builder().setIsSampled(false).build().getOptions()).isEqualTo(0); assertThat(TraceOptions.builder().setIsSampled(true).build().getOptions()).isEqualTo(1); assertThat(TraceOptions.builder().setIsSampled(true).setIsSampled(false).build().getOptions()) .isEqualTo(0); assertThat(TraceOptions.fromByte(FIRST_BYTE).getOptions()).isEqualTo(-1); assertThat(TraceOptions.fromByte(SECOND_BYTE).getOptions()).isEqualTo(1); assertThat(TraceOptions.fromByte(THIRD_BYTE).getOptions()).isEqualTo(6); }
@Test public void toFromBase16() { assertThat(TraceOptions.fromLowerBase16("ff", 0).toLowerBase16()).isEqualTo("ff"); assertThat(TraceOptions.fromLowerBase16("01", 0).toLowerBase16()).isEqualTo("01"); assertThat(TraceOptions.fromLowerBase16("06", 0).toLowerBase16()).isEqualTo("06"); }
@Test public void traceOptions_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); tester.addEqualityGroup(TraceOptions.DEFAULT); tester.addEqualityGroup( TraceOptions.fromByte(SECOND_BYTE), TraceOptions.builder().setIsSampled(true).build()); tester.addEqualityGroup(TraceOptions.fromByte(FIRST_BYTE)); tester.testEquals(); }
/** * Builds and returns a {@code TraceOptions} with the desired options. * * @return a {@code TraceOptions} with the desired options. * @since 0.5 */ public TraceOptions build() { return fromByte(options); } }
private int optionsToFlags(final TraceOptions traceOptions) { // Attempt to minimise allocations, since TraceOptions#getBytes currently creates a defensive // copy: traceOptions.copyBytesTo(optionsBuffer, optionsBuffer.length - 1); return Ints.fromByteArray(optionsBuffer); }
/** * Returns the lowercase base16 encoding of this {@code TraceOptions}. * * @return the lowercase base16 encoding of this {@code TraceOptions}. * @since 0.18 */ public String toLowerBase16() { char[] chars = new char[BASE16_SIZE]; copyLowerBase16To(chars, 0); return new String(chars); }
traceOptions = TraceOptions.fromLowerBase16(traceparent, TRACE_OPTION_OFFSET); } catch (IllegalArgumentException e) { throw new SpanContextParseException("Invalid traceparent: " + traceparent, e);
traceId, SpanId.generateRandomId(randomHandler.current()), TraceOptions.builder().setIsSampled(true).build()), spanBuilderOptions) .startSpan(); assertThat(childSpan.getContext().isValid()).isTrue(); assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId); assertThat(childSpan.getContext().getTraceOptions().isSampled()).isTrue(); childSpan.end(); assertThat(childSpan.getContext().isValid()).isTrue(); assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId); assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse(); childSpan.end();
@Test public void spanContext_ToString() { assertThat(first.toString()).contains(TraceId.fromBytes(firstTraceIdBytes).toString()); assertThat(first.toString()).contains(SpanId.fromBytes(firstSpanIdBytes).toString()); assertThat(first.toString()).contains(TraceOptions.DEFAULT.toString()); assertThat(second.toString()).contains(TraceId.fromBytes(secondTraceIdBytes).toString()); assertThat(second.toString()).contains(SpanId.fromBytes(secondSpanIdBytes).toString()); assertThat(second.toString()) .contains(TraceOptions.builder().setIsSampled(true).build().toString()); } }
/** * Returns a {@code TraceOptions} whose representation is copied from the {@code src} beginning at * the {@code srcOffset} offset. * * @param src the buffer where the representation of the {@code TraceOptions} is copied. * @param srcOffset the offset in the buffer where the representation of the {@code TraceOptions} * begins. * @return a {@code TraceOptions} whose representation is copied from the buffer. * @throws NullPointerException if {@code src} is null. * @throws IndexOutOfBoundsException if {@code srcOffset+TraceOptions.SIZE} is greater than {@code * src.length}. * @since 0.5 * @deprecated use {@link #fromByte(byte)}. */ @Deprecated public static TraceOptions fromBytes(byte[] src, int srcOffset) { Utils.checkIndex(srcOffset, src.length); return fromByte(src[srcOffset]); }
@Override public byte[] toByteArray(SpanContext spanContext) { checkNotNull(spanContext, "spanContext"); byte[] bytes = new byte[ALL_FORMAT_LENGTH]; bytes[VERSION_ID_OFFSET] = VERSION_ID; bytes[TRACE_ID_FIELD_ID_OFFSET] = TRACE_ID_FIELD_ID; spanContext.getTraceId().copyBytesTo(bytes, TRACE_ID_OFFSET); bytes[SPAN_ID_FIELD_ID_OFFSET] = SPAN_ID_FIELD_ID; spanContext.getSpanId().copyBytesTo(bytes, SPAN_ID_OFFSET); bytes[TRACE_OPTION_FIELD_ID_OFFSET] = TRACE_OPTION_FIELD_ID; spanContext.getTraceOptions().copyBytesTo(bytes, TRACE_OPTIONS_OFFSET); return bytes; }
spanContext.getSpanId().copyLowerBase16To(chars, SPAN_ID_OFFSET); chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER; spanContext.getTraceOptions().copyLowerBase16To(chars, TRACE_OPTION_OFFSET); setter.put(carrier, TRACEPARENT, new String(chars)); List<Tracestate.Entry> entries = spanContext.getTracestate().getEntries();
traceOptions = TraceOptions.fromLowerBase16(traceparent, TRACE_OPTION_OFFSET); } catch (IllegalArgumentException e) { throw new SpanContextParseException("Invalid traceparent: " + traceparent, e);
private static boolean isAnyParentLinkSampled(List<Span> parentLinks) { for (Span parentLink : parentLinks) { if (parentLink.getContext().getTraceOptions().isSampled()) { return true; } } return false; }