Package io.opencensus.trace.propagation
Class BinaryFormat
- java.lang.Object
-
- io.opencensus.trace.propagation.BinaryFormat
-
- Direct Known Subclasses:
BinaryFormat.NoopBinaryFormat
,BinaryFormatImpl
public abstract class BinaryFormat extends java.lang.Object
This is a helper class forSpanContext
propagation on the wire using binary encoding.Example of usage on the client:
private static final Tracer tracer = Tracing.getTracer(); private static final BinaryFormat binaryFormat = Tracing.getPropagationComponent().getBinaryFormat(); void onSendRequest() { try (Scope ss = tracer.spanBuilder("Sent.MyRequest").startScopedSpan()) { byte[] binaryValue = binaryFormat.toByteArray(tracer.getCurrentContext().context()); // Send the request including the binaryValue and wait for the response. } }
Example of usage on the server:
private static final Tracer tracer = Tracing.getTracer(); private static final BinaryFormat binaryFormat = Tracing.getPropagationComponent().getBinaryFormat(); void onRequestReceived() { // Get the binaryValue from the request. SpanContext spanContext = SpanContext.INVALID; try { if (binaryValue != null) { spanContext = binaryFormat.fromByteArray(binaryValue); } } catch (SpanContextParseException e) { // Maybe log the exception. } try (Scope ss = tracer.spanBuilderWithRemoteParent("Recv.MyRequest", spanContext).startScopedSpan()) { // Handle request and send response back. } }
- Since:
- 0.5
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
BinaryFormat.NoopBinaryFormat
-
Field Summary
Fields Modifier and Type Field Description (package private) static BinaryFormat.NoopBinaryFormat
NOOP_BINARY_FORMAT
-
Constructor Summary
Constructors Constructor Description BinaryFormat()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SpanContext
fromBinaryValue(byte[] bytes)
Deprecated.SpanContext
fromByteArray(byte[] bytes)
Parses theSpanContext
from a byte array using the binary format.(package private) static BinaryFormat
getNoopBinaryFormat()
Returns the no-op implementation of theBinaryFormat
.byte[]
toBinaryValue(SpanContext spanContext)
Deprecated.byte[]
toByteArray(SpanContext spanContext)
Serializes aSpanContext
into a byte array using the binary format.
-
-
-
Field Detail
-
NOOP_BINARY_FORMAT
static final BinaryFormat.NoopBinaryFormat NOOP_BINARY_FORMAT
-
-
Method Detail
-
toBinaryValue
@Deprecated public byte[] toBinaryValue(SpanContext spanContext)
Deprecated.Serializes aSpanContext
into a byte array using the binary format.- Parameters:
spanContext
- theSpanContext
to serialize.- Returns:
- the serialized binary value.
- Throws:
java.lang.NullPointerException
- if thespanContext
isnull
.- Since:
- 0.5
-
toByteArray
public byte[] toByteArray(SpanContext spanContext)
Serializes aSpanContext
into a byte array using the binary format.- Parameters:
spanContext
- theSpanContext
to serialize.- Returns:
- the serialized binary value.
- Throws:
java.lang.NullPointerException
- if thespanContext
isnull
.- Since:
- 0.7
-
fromBinaryValue
@Deprecated public SpanContext fromBinaryValue(byte[] bytes) throws java.text.ParseException
Deprecated.Parses theSpanContext
from a byte array using the binary format.- Parameters:
bytes
- a binary encoded buffer from which theSpanContext
will be parsed.- Returns:
- the parsed
SpanContext
. - Throws:
java.lang.NullPointerException
- if theinput
isnull
.java.text.ParseException
- if the version is not supported or the input is invalid- Since:
- 0.5
-
fromByteArray
public SpanContext fromByteArray(byte[] bytes) throws SpanContextParseException
Parses theSpanContext
from a byte array using the binary format.- Parameters:
bytes
- a binary encoded buffer from which theSpanContext
will be parsed.- Returns:
- the parsed
SpanContext
. - Throws:
java.lang.NullPointerException
- if theinput
isnull
.SpanContextParseException
- if the version is not supported or the input is invalid- Since:
- 0.7
-
getNoopBinaryFormat
static BinaryFormat getNoopBinaryFormat()
Returns the no-op implementation of theBinaryFormat
.- Returns:
- the no-op implementation of the
BinaryFormat
.
-
-