Java Code Examples for io.opencensus.trace.TraceOptions#DEFAULT

The following examples show how to use io.opencensus.trace.TraceOptions#DEFAULT . 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: B3Format.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public <C /*>>> extends @NonNull Object*/> SpanContext extract(C carrier, Getter<C> getter)
    throws SpanContextParseException {
  checkNotNull(carrier, "carrier");
  checkNotNull(getter, "getter");
  try {
    TraceId traceId;
    String traceIdStr = getter.get(carrier, X_B3_TRACE_ID);
    if (traceIdStr != null) {
      if (traceIdStr.length() == TraceId.SIZE) {
        // This is an 8-byte traceID.
        traceIdStr = UPPER_TRACE_ID + traceIdStr;
      }
      traceId = TraceId.fromLowerBase16(traceIdStr);
    } else {
      throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
    }
    SpanId spanId;
    String spanIdStr = getter.get(carrier, X_B3_SPAN_ID);
    if (spanIdStr != null) {
      spanId = SpanId.fromLowerBase16(spanIdStr);
    } else {
      throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
    }
    TraceOptions traceOptions = TraceOptions.DEFAULT;
    if (SAMPLED_VALUE.equals(getter.get(carrier, X_B3_SAMPLED))
        || FLAGS_VALUE.equals(getter.get(carrier, X_B3_FLAGS))) {
      traceOptions = TraceOptions.builder().setIsSampled(true).build();
    }
    return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
  } catch (IllegalArgumentException e) {
    throw new SpanContextParseException("Invalid input.", e);
  }
}
 
Example 2
Source File: BinaryFormatImpl.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}