use of io.opencensus.tags.propagation.TagContextDeserializationException 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);
}
}
use of io.opencensus.tags.propagation.TagContextDeserializationException 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());
}
}
use of io.opencensus.tags.propagation.TagContextDeserializationException 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;
}
Aggregations