Class BinaryFormat

java.lang.Object
io.opencensus.trace.propagation.BinaryFormat

public abstract class BinaryFormat extends Object
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
  • Constructor Details

    • BinaryFormat

      public BinaryFormat()
  • Method Details