Class BinaryFormat
java.lang.Object
io.opencensus.trace.propagation.BinaryFormat
- Direct Known Subclasses:
BinaryFormat.NoopBinaryFormat, BinaryFormatImpl
This is a helper class for
SpanContext 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 -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfromBinaryValue(byte[] bytes) Deprecated.fromByteArray(byte[] bytes) Parses theSpanContextfrom a byte array using the binary format.(package private) static BinaryFormatReturns the no-op implementation of theBinaryFormat.byte[]toBinaryValue(SpanContext spanContext) Deprecated.byte[]toByteArray(SpanContext spanContext) Serializes aSpanContextinto a byte array using the binary format.
-
Field Details
-
NOOP_BINARY_FORMAT
-
-
Constructor Details
-
BinaryFormat
public BinaryFormat()
-
-
Method Details
-
toBinaryValue
Deprecated.Serializes aSpanContextinto a byte array using the binary format.- Parameters:
spanContext- theSpanContextto serialize.- Returns:
- the serialized binary value.
- Throws:
NullPointerException- if thespanContextisnull.- Since:
- 0.5
-
toByteArray
Serializes aSpanContextinto a byte array using the binary format.- Parameters:
spanContext- theSpanContextto serialize.- Returns:
- the serialized binary value.
- Throws:
NullPointerException- if thespanContextisnull.- Since:
- 0.7
-
fromBinaryValue
Deprecated.Parses theSpanContextfrom a byte array using the binary format.- Parameters:
bytes- a binary encoded buffer from which theSpanContextwill be parsed.- Returns:
- the parsed
SpanContext. - Throws:
NullPointerException- if theinputisnull.ParseException- if the version is not supported or the input is invalid- Since:
- 0.5
-
fromByteArray
Parses theSpanContextfrom a byte array using the binary format.- Parameters:
bytes- a binary encoded buffer from which theSpanContextwill be parsed.- Returns:
- the parsed
SpanContext. - Throws:
NullPointerException- if theinputisnull.SpanContextParseException- if the version is not supported or the input is invalid- Since:
- 0.7
-
getNoopBinaryFormat
Returns the no-op implementation of theBinaryFormat.- Returns:
- the no-op implementation of the
BinaryFormat.
-
fromByteArray(byte[]).