Search in sources :

Example 1 with TagContextDeserializationException

use of io.opencensus.tags.propagation.TagContextDeserializationException in project instrumentation-java by census-instrumentation.

the class CorrelationContextFormat method extract.

@Override
public <C> /*>>> extends @NonNull Object*/
TagContext extract(C carrier, Getter<C> getter) throws TagContextDeserializationException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    if (State.DISABLED.equals(state.getInternal())) {
        return TagMapImpl.EMPTY;
    }
    @Nullable String correlationContext = getter.get(carrier, CORRELATION_CONTEXT);
    if (correlationContext == null) {
        throw new TagContextDeserializationException(CORRELATION_CONTEXT + " not present.");
    }
    try {
        if (correlationContext.isEmpty()) {
            return TagMapImpl.EMPTY;
        }
        Map<TagKey, TagValueWithMetadata> tags = new HashMap<>();
        List<String> stringTags = TAG_SPLITTER.splitToList(correlationContext);
        for (String stringTag : stringTags) {
            decodeTag(stringTag, tags);
        }
        return new TagMapImpl(tags);
    } catch (IllegalArgumentException e) {
        throw new TagContextDeserializationException("Invalid TagContext: " + correlationContext, e);
    }
}
Also used : TagMapImpl(io.opencensus.implcore.tags.TagMapImpl) TagContextDeserializationException(io.opencensus.tags.propagation.TagContextDeserializationException) TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) HashMap(java.util.HashMap) TagKey(io.opencensus.tags.TagKey) Nullable(javax.annotation.Nullable)

Example 2 with TagContextDeserializationException

use of io.opencensus.tags.propagation.TagContextDeserializationException in project instrumentation-java by census-instrumentation.

the class BinarySerializationUtils method deserializeBinary.

// Deserializes input to TagContext based on the binary format standard.
// The encoded tags are of the form: <version_id><encoded_tags>
static TagMapImpl deserializeBinary(byte[] bytes) throws TagContextDeserializationException {
    try {
        if (bytes.length == 0) {
            // Does not allow empty byte array.
            throw new TagContextDeserializationException("Input byte[] can not be empty.");
        }
        ByteBuffer buffer = ByteBuffer.wrap(bytes).asReadOnlyBuffer();
        int versionId = buffer.get();
        if (versionId > VERSION_ID || versionId < 0) {
            throw new TagContextDeserializationException("Wrong Version ID: " + versionId + ". Currently supports version up to: " + VERSION_ID);
        }
        return new TagMapImpl(parseTags(buffer));
    } catch (BufferUnderflowException exn) {
        // byte array format error.
        throw new TagContextDeserializationException(exn.toString());
    }
}
Also used : TagMapImpl(io.opencensus.implcore.tags.TagMapImpl) TagContextDeserializationException(io.opencensus.tags.propagation.TagContextDeserializationException) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 3 with TagContextDeserializationException

use of io.opencensus.tags.propagation.TagContextDeserializationException in project instrumentation-java by census-instrumentation.

the class BinarySerializationUtils method parseTags.

private static Map<TagKey, TagValueWithMetadata> parseTags(ByteBuffer buffer) throws TagContextDeserializationException {
    Map<TagKey, TagValueWithMetadata> tags = new HashMap<TagKey, TagValueWithMetadata>();
    int limit = buffer.limit();
    // Here chars are equivalent to bytes, since we're using ascii chars.
    int totalChars = 0;
    while (buffer.position() < limit) {
        int type = buffer.get();
        if (type == TAG_FIELD_ID) {
            TagKey key = createTagKey(decodeString(buffer));
            TagValue val = createTagValue(key, decodeString(buffer));
            totalChars += key.getName().length();
            totalChars += val.asString().length();
            tags.put(key, TagValueWithMetadata.create(val, METADATA_UNLIMITED_PROPAGATION));
        } else {
            // TODO(sebright): Consider storing the rest of the byte array in the TagContext.
            break;
        }
    }
    if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
        throw new TagContextDeserializationException("Size of TagContext exceeds the maximum serialized size " + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
    }
    return tags;
}
Also used : TagContextDeserializationException(io.opencensus.tags.propagation.TagContextDeserializationException) TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) HashMap(java.util.HashMap) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Aggregations

TagContextDeserializationException (io.opencensus.tags.propagation.TagContextDeserializationException)3 TagMapImpl (io.opencensus.implcore.tags.TagMapImpl)2 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)2 TagKey (io.opencensus.tags.TagKey)2 HashMap (java.util.HashMap)2 TagValue (io.opencensus.tags.TagValue)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Nullable (javax.annotation.Nullable)1