private static long convertSpanId(final SpanId spanId) { final byte[] bytes = spanId.getBytes(); long result = 0; for (int i = 0; i < Long.SIZE / Byte.SIZE; i++) { result <<= Byte.SIZE; result |= (bytes[i] & 0xff); } if (result < 0) { return -result; } return result; }
private static long spanIdToLong(SpanId spanId) { ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.put(spanId.getBytes()); return buffer.getLong(0); } }
private static long spanIdToLong(SpanId spanId) { ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.put(spanId.getBytes()); return buffer.getLong(0); } }
private static String encodeSpanId(SpanId spanId) { return BaseEncoding.base16().lowerCase().encode(spanId.getBytes()); }
/** Emits the list of SampledRequets with a header. */ private static void emitSpans(PrintWriter out, Formatter formatter, Collection<SpanData> spans) { out.write("<pre>\n"); formatter.format("%-23s %18s%n", "When", "Elapsed(s)"); out.write("-------------------------------------------\n"); for (SpanData span : spans) { tracer .getCurrentSpan() .addAnnotation( "Render span.", ImmutableMap.<String, AttributeValue>builder() .put( "SpanId", AttributeValue.stringAttributeValue( BaseEncoding.base16() .lowerCase() .encode(span.getContext().getSpanId().getBytes()))) .build()); emitSingleSpan(formatter, span); } out.write("</pre>\n"); }
/** * Converts {@code SpanContext} to AppEngine {@code CloudTraceContext}. * * @param spanContext the {@code SpanContext}. * @return the converted AppEngine {@code CloudTraceContext}. * @since 0.14 */ public static CloudTraceContext toCloudTraceContext(SpanContext spanContext) { checkNotNull(spanContext, "spanContext"); ByteBuffer traceIdBuf = ByteBuffer.wrap(spanContext.getTraceId().getBytes()); TraceIdProto traceIdProto = TraceIdProto.newBuilder().setHi(traceIdBuf.getLong()).setLo(traceIdBuf.getLong()).build(); ByteBuffer spanIdBuf = ByteBuffer.wrap(spanContext.getSpanId().getBytes()); return new CloudTraceContext( traceIdProto.toByteArray(), spanIdBuf.getLong(), spanContext.getTraceOptions().isSampled() ? 1L : 0L); }
@Test public void invalidSpanId() { assertThat(SpanId.INVALID.getBytes()).isEqualTo(new byte[8]); }
: NOT_SAMPLED_TRACE_ID_COLOR, BaseEncoding.base16().lowerCase().encode(spanContext.getTraceId().getBytes()), BaseEncoding.base16().lowerCase().encode(spanContext.getSpanId().getBytes()), BaseEncoding.base16() .lowerCase() .encode( span.getParentSpanId() == null ? SpanId.INVALID.getBytes() : span.getParentSpanId().getBytes()));
private static Link toLinkProto(io.opencensus.trace.Link link) { return Link.newBuilder() .setTraceId(toByteString(link.getTraceId().getBytes())) .setSpanId(toByteString(link.getSpanId().getBytes())) .setType(toLinkTypeProto(link.getType())) .setAttributes(toAttributesBuilderProto(link.getAttributes(), 0)) .build(); }
.setType(Span.Link.Type.CHILD_LINKED_SPAN) .setTraceId(toByteString(traceId.getBytes())) .setSpanId(toByteString(spanId.getBytes())) .setAttributes(Span.Attributes.newBuilder().build()) .build()) assertThat(span.getName()).isEqualTo(toTruncatableStringProto(SPAN_NAME)); assertThat(span.getTraceId()).isEqualTo(toByteString(traceId.getBytes())); assertThat(span.getSpanId()).isEqualTo(toByteString(spanId.getBytes())); assertThat(span.getParentSpanId()).isEqualTo(toByteString(parentSpanId.getBytes())); assertThat(span.getStartTime()).isEqualTo(startTime); assertThat(span.getEndTime()).isEqualTo(endTime);