Search in sources :

Example 6 with Tag

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

the class TaggerImplTest method getCurrentTagContext_RemoveDuplicatesFromUnknownTagContext.

@Test
public void getCurrentTagContext_RemoveDuplicatesFromUnknownTagContext() {
    Tag tag1 = Tag.create(K1, V1);
    Tag tag2 = Tag.create(K1, V2);
    TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
    TagContext result = getResultOfGetCurrentTagContext(tagContextWithDuplicateTags);
    assertThat(tagContextToList(result)).containsExactly(tag2);
}
Also used : TagContext(io.opencensus.tags.TagContext) Tag(io.opencensus.tags.Tag) Test(org.junit.Test)

Example 7 with Tag

use of io.opencensus.tags.Tag 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 8 with Tag

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

the class CorrelationContextFormat method inject.

@Override
public <C> /*>>> extends @NonNull Object*/
void inject(TagContext tagContext, C carrier, Setter<C> setter) throws TagContextSerializationException {
    checkNotNull(tagContext, "tagContext");
    checkNotNull(carrier, "carrier");
    checkNotNull(setter, "setter");
    if (State.DISABLED.equals(state.getInternal())) {
        return;
    }
    try {
        StringBuilder stringBuilder = new StringBuilder(TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
        // Here chars are equivalent to bytes, since we're using ascii chars.
        int totalChars = 0;
        int totalTags = 0;
        for (Iterator<Tag> i = InternalUtils.getTags(tagContext); i.hasNext(); ) {
            Tag tag = i.next();
            if (TagTtl.NO_PROPAGATION.equals(tag.getTagMetadata().getTagTtl())) {
                continue;
            }
            if (stringBuilder.length() > 0) {
                stringBuilder.append(TAG_DELIMITER);
            }
            totalTags++;
            totalChars += encodeTag(tag, stringBuilder);
        }
        checkArgument(totalTags <= MAX_NUMBER_OF_TAGS, "Number of tags in the TagContext exceeds limit " + MAX_NUMBER_OF_TAGS);
        // Note per W3C spec, only the length of tag key and value counts towards the total length.
        // Length of properties (a.k.a TagMetadata) does not count.
        checkArgument(totalChars <= TAGCONTEXT_SERIALIZED_SIZE_LIMIT, "Size of TagContext exceeds the maximum serialized size " + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
        setter.put(carrier, CORRELATION_CONTEXT, stringBuilder.toString());
    } catch (IllegalArgumentException e) {
        throw new TagContextSerializationException("Failed to serialize TagContext", e);
    }
}
Also used : Tag(io.opencensus.tags.Tag) TagContextSerializationException(io.opencensus.tags.propagation.TagContextSerializationException)

Example 9 with Tag

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

the class TaggerImpl method toTagMapImpl.

private static TagMapImpl toTagMapImpl(TagContext tags) {
    if (tags instanceof TagMapImpl) {
        return (TagMapImpl) tags;
    } else {
        Iterator<Tag> i = InternalUtils.getTags(tags);
        if (!i.hasNext()) {
            return TagMapImpl.EMPTY;
        }
        TagMapBuilderImpl builder = new TagMapBuilderImpl();
        while (i.hasNext()) {
            Tag tag = i.next();
            if (tag != null) {
                TagContextUtils.addTagToBuilder(tag, builder);
            }
        }
        return builder.build();
    }
}
Also used : Tag(io.opencensus.tags.Tag)

Example 10 with Tag

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

the class BinarySerializationUtils method serializeBinary.

// Serializes a TagContext to the on-the-wire format.
// Encoded tags are of the form: <version_id><encoded_tags>
static byte[] serializeBinary(TagContext tags) throws TagContextSerializationException {
    // Use a ByteArrayDataOutput to avoid needing to handle IOExceptions.
    final ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
    byteArrayDataOutput.write(VERSION_ID);
    // Here chars are equivalent to bytes, since we're using ascii chars.
    int totalChars = 0;
    for (Iterator<Tag> i = InternalUtils.getTags(tags); i.hasNext(); ) {
        Tag tag = i.next();
        if (TagTtl.NO_PROPAGATION.equals(tag.getTagMetadata().getTagTtl())) {
            continue;
        }
        totalChars += tag.getKey().getName().length();
        totalChars += tag.getValue().asString().length();
        encodeTag(tag, byteArrayDataOutput);
    }
    if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
        throw new TagContextSerializationException("Size of TagContext exceeds the maximum serialized size " + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
    }
    return byteArrayDataOutput.toByteArray();
}
Also used : ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) Tag(io.opencensus.tags.Tag) TagContextSerializationException(io.opencensus.tags.propagation.TagContextSerializationException)

Aggregations

Tag (io.opencensus.tags.Tag)18 Test (org.junit.Test)13 TagContext (io.opencensus.tags.TagContext)10 HashMap (java.util.HashMap)5 TagContextBuilder (io.opencensus.tags.TagContextBuilder)2 TagContextSerializationException (io.opencensus.tags.propagation.TagContextSerializationException)2 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 TagMapImpl (io.opencensus.implcore.tags.TagMapImpl)1 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)1 TagKey (io.opencensus.tags.TagKey)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1