Search in sources :

Example 1 with TagContextSerializationException

use of io.opencensus.tags.propagation.TagContextSerializationException 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 2 with TagContextSerializationException

use of io.opencensus.tags.propagation.TagContextSerializationException 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)2 TagContextSerializationException (io.opencensus.tags.propagation.TagContextSerializationException)2 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1