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);
}
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;
}
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);
}
}
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();
}
}
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();
}
Aggregations