Java Code Examples for io.opencensus.trace.SpanId#fromBytes()
The following examples show how to use
io.opencensus.trace.SpanId#fromBytes() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryFormatImpl.java From opencensus-java with Apache License 2.0 | 5 votes |
@Override public SpanContext fromByteArray(byte[] bytes) throws SpanContextParseException { checkNotNull(bytes, "bytes"); if (bytes.length == 0 || bytes[0] != VERSION_ID) { throw new SpanContextParseException("Unsupported version."); } if (bytes.length < REQUIRED_FORMAT_LENGTH) { throw new SpanContextParseException("Invalid input: truncated"); } // TODO: the following logic assumes that fields are written in ID order. The spec does not say // that. If it decides not to, this logic would need to be more like a loop TraceId traceId; SpanId spanId; TraceOptions traceOptions = TraceOptions.DEFAULT; int pos = 1; if (bytes[pos] == TRACE_ID_FIELD_ID) { traceId = TraceId.fromBytes(bytes, pos + ID_SIZE); pos += ID_SIZE + TraceId.SIZE; } else { // TODO: update the spec to suggest that the trace ID is not actually optional throw new SpanContextParseException("Invalid input: expected trace ID at offset " + pos); } if (bytes[pos] == SPAN_ID_FIELD_ID) { spanId = SpanId.fromBytes(bytes, pos + ID_SIZE); pos += ID_SIZE + SpanId.SIZE; } else { // TODO: update the spec to suggest that the span ID is not actually optional. throw new SpanContextParseException("Invalid input: expected span ID at offset " + pos); } // Check to see if we are long enough to include an options field, and also that the next field // is an options field. Per spec we simply stop parsing at first unknown field instead of // failing. if (bytes.length > pos && bytes[pos] == TRACE_OPTION_FIELD_ID) { if (bytes.length < ALL_FORMAT_LENGTH) { throw new SpanContextParseException("Invalid input: truncated"); } traceOptions = TraceOptions.fromByte(bytes[pos + ID_SIZE]); } return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT); }
Example 2
Source File: CloudTraceFormatTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@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)); }
Example 3
Source File: CloudTraceFormat.java From opencensus-java with Apache License 2.0 | 4 votes |
private static SpanId longToSpanId(long x) { ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE); buffer.putLong(x); return SpanId.fromBytes(buffer.array()); }