Search in sources :

Example 1 with TagValueWithMetadata

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

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

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

Example 4 with TagValueWithMetadata

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

the class CorrelationContextFormat method decodeTag.

// Decodes tag key, value and metadata from the encoded string tag, then puts it into the tag map.
// The format of encoded string tag is name1=value1;properties1=p1;properties2=p2.
private static void decodeTag(String stringTag, Map<TagKey, TagValueWithMetadata> tags) {
    String keyWithValue;
    int firstPropertyIndex = stringTag.indexOf(TAG_PROPERTIES_DELIMITER);
    if (firstPropertyIndex != -1) {
        // Tag with properties.
        keyWithValue = stringTag.substring(0, firstPropertyIndex);
    // TODO(songya): support decoding tag properties.
    } else {
        // Tag without properties.
        keyWithValue = stringTag;
    }
    List<String> keyValuePair = TAG_KEY_VALUE_SPLITTER.splitToList(keyWithValue);
    checkArgument(keyValuePair.size() == 2, "Malformed tag " + stringTag);
    TagKey key = TagKey.create(keyValuePair.get(0).trim());
    TagValue value = TagValue.create(keyValuePair.get(1).trim());
    TagValueWithMetadata valueWithMetadata = TagValueWithMetadata.create(value, METADATA_UNLIMITED_PROPAGATION);
    tags.put(key, valueWithMetadata);
}
Also used : TagValueWithMetadata(io.opencensus.implcore.tags.TagValueWithMetadata) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Aggregations

TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)4 TagKey (io.opencensus.tags.TagKey)4 TagMapImpl (io.opencensus.implcore.tags.TagMapImpl)2 TagValue (io.opencensus.tags.TagValue)2 TagContextDeserializationException (io.opencensus.tags.propagation.TagContextDeserializationException)2 HashMap (java.util.HashMap)2 Tag (io.opencensus.tags.Tag)1 Nullable (javax.annotation.Nullable)1