private static SpanId longToSpanId(long x) { ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.putLong(x); return SpanId.fromBytes(buffer.array()); }
private static SpanId longToSpanId(long x) { ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.putLong(x); return SpanId.fromBytes(buffer.array()); }
@SuppressWarnings("deprecation") private static SpanContext fromSleuthSpan(org.springframework.cloud.sleuth.Span span) { return SpanContext.create( TraceId.fromBytes( ByteBuffer.allocate(TraceId.SIZE) .putLong(span.getTraceIdHigh()) .putLong(span.getTraceId()) .array()), SpanId.fromBytes(ByteBuffer.allocate(SpanId.SIZE).putLong(span.getSpanId()).array()), Boolean.TRUE.equals(span.isExportable()) ? sampledOptions : notSampledOptions); } }
/** * Returns a {@code SpanId} built from a byte representation. * * @param src the representation of the {@code SpanId}. * @return a {@code SpanId} whose representation is given by the {@code src} parameter. * @throws NullPointerException if {@code src} is null. * @throws IllegalArgumentException if {@code src.length} is not {@link SpanId#SIZE}. * @since 0.5 */ public static SpanId fromBytes(byte[] src) { Utils.checkNotNull(src, "src"); // TODO: Remove this extra condition. Utils.checkArgument(src.length == SIZE, "Invalid size: expected %s, got %s", SIZE, src.length); return fromBytes(src, 0); }
/** * 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 parseWithShortSpanIdAndSamplingShouldSucceed() throws SpanContextParseException { final String spanId = "1"; ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.putLong(Long.parseLong(spanId)); SpanId expectedSpanId = SpanId.fromBytes(buffer.array()); parseSuccess( constructHeader(TRACE_ID_BASE16, spanId, SAMPLED), SpanContext.create(TRACE_ID, expectedSpanId, TRACE_OPTIONS_SAMPLED)); } }
@Test public void spanId_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); tester.addEqualityGroup(SpanId.INVALID, SpanId.INVALID); tester.addEqualityGroup(first, SpanId.fromBytes(Arrays.copyOf(firstBytes, firstBytes.length))); tester.addEqualityGroup( second, SpanId.fromBytes(Arrays.copyOf(secondBytes, secondBytes.length))); tester.testEquals(); }
@Test public void spanId_CompareTo() { assertThat(first.compareTo(second)).isGreaterThan(0); assertThat(second.compareTo(first)).isLessThan(0); assertThat(first.compareTo(SpanId.fromBytes(firstBytes))).isEqualTo(0); }
spanId = SpanId.fromBytes(bytes, pos + ID_SIZE); pos += ID_SIZE + SpanId.SIZE; } else {
spanId = SpanId.fromBytes(bytes, pos + ID_SIZE); pos += ID_SIZE + SpanId.SIZE; } else {
@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()); } }
@Test public void isValid() { assertThat(SpanContext.INVALID.isValid()).isFalse(); assertThat( SpanContext.create( TraceId.fromBytes(firstTraceIdBytes), SpanId.INVALID, TraceOptions.DEFAULT) .isValid()) .isFalse(); assertThat( SpanContext.create( TraceId.INVALID, SpanId.fromBytes(firstSpanIdBytes), TraceOptions.DEFAULT) .isValid()) .isFalse(); assertThat(first.isValid()).isTrue(); assertThat(second.isValid()).isTrue(); }
private static SpanContext sampleSpanContext() { return SpanContext.create( TraceId.fromBytes(new byte[] {FF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), SpanId.fromBytes(new byte[] {0, 0, 0, 0, 0, 0, 1, 0}), TraceOptions.builder().setIsSampled(true).build(), Tracestate.builder().build()); }
@Test public void spanContext_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); tester.addEqualityGroup( first, SpanContext.create( TraceId.fromBytes(firstTraceIdBytes), SpanId.fromBytes(firstSpanIdBytes), TraceOptions.DEFAULT), SpanContext.create( TraceId.fromBytes(firstTraceIdBytes), SpanId.fromBytes(firstSpanIdBytes), TraceOptions.builder().setIsSampled(false).build(), firstTracestate)); tester.addEqualityGroup( second, SpanContext.create( TraceId.fromBytes(secondTraceIdBytes), SpanId.fromBytes(secondSpanIdBytes), TraceOptions.builder().setIsSampled(true).build(), secondTracestate)); tester.testEquals(); }
private static List<Link> sampleLinks() { return Lists.newArrayList( Link.fromSpanContext( SpanContext.create( TraceId.fromBytes( new byte[] {FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, FF, 0}), SpanId.fromBytes(new byte[] {0, 0, 0, 0, 0, 0, 2, 0}), TraceOptions.builder().setIsSampled(false).build(), Tracestate.builder().build()), Link.Type.CHILD_LINKED_SPAN, ImmutableMap.of( "Bool", AttributeValue.booleanAttributeValue(true), "Long", AttributeValue.longAttributeValue(299792458L), "String", AttributeValue.stringAttributeValue( "Man is condemned to be free; because once thrown into the world, " + "he is responsible for everything he does. -- Sartre")))); } }
SpanData.create( sampleSpanContext(), SpanId.fromBytes(new byte[] {(byte) 0x7F, FF, FF, FF, FF, FF, FF, FF}), true, "test",