Search in sources :

Example 1 with TagMapImpl

use of io.opencensus.implcore.tags.TagMapImpl in project instrumentation-java by census-instrumentation.

the class RecordUtils method getTagMap.

static Map<TagKey, TagValueWithMetadata> getTagMap(TagContext ctx) {
    if (ctx instanceof TagMapImpl) {
        return ((TagMapImpl) ctx).getTags();
    }
    Map<TagKey, TagValueWithMetadata> tags = Maps.newHashMap();
    for (Iterator<Tag> i = InternalUtils.getTags(ctx); i.hasNext(); ) {
        Tag tag = i.next();
        tags.put(tag.getKey(), TagValueWithMetadata.create(tag.getValue(), tag.getTagMetadata()));
    }
    return tags;
}
Also used : TagMapImpl(io.opencensus.implcore.tags.TagMapImpl) TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) TagKey(io.opencensus.tags.TagKey) Tag(io.opencensus.tags.Tag)

Example 2 with TagMapImpl

use of io.opencensus.implcore.tags.TagMapImpl 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 3 with TagMapImpl

use of io.opencensus.implcore.tags.TagMapImpl 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)

Aggregations

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