io.opencensus.tags.propagation.TagContextDeserializationException Java Examples

The following examples show how to use io.opencensus.tags.propagation.TagContextDeserializationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeserializeNonConsecutiveDuplicateKeys()
    throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key2", "Value2", output);
  encodeTagToOutput("Key3", "Value3", output);
  encodeTagToOutput("Key1", "Value4", output);
  encodeTagToOutput("Key2", "Value5", output);
  TagContext expected =
      tagger
          .emptyBuilder()
          .put(TagKey.create("Key1"), TagValue.create("Value4"))
          .put(TagKey.create("Key2"), TagValue.create("Value5"))
          .put(TagKey.create("Key3"), TagValue.create("Value3"))
          .build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #2
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void roundTrip()
    throws TagContextSerializationException, TagContextDeserializationException {
  Tag[] tags = new Tag[40];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create(generateRandom(10)),
            TagValue.create(generateRandom(10)),
            METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  textFormat.inject(tagContext, carrier, setter);
  TagContext actual = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(actual))
      .containsExactlyElementsIn(TagsTestUtil.tagContextToList(tagContext));
}
 
Example #3
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeserializeNonConsecutiveDuplicateTags()
    throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key2", "Value2", output);
  encodeTagToOutput("Key3", "Value3", output);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key2", "Value2", output);
  TagContext expected =
      tagger
          .emptyBuilder()
          .put(TagKey.create("Key1"), TagValue.create("Value1"))
          .put(TagKey.create("Key2"), TagValue.create("Value2"))
          .put(TagKey.create("Key3"), TagValue.create("Value3"))
          .build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #4
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void stopParsingAtUnknownField() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key2", "Value2", output);

  // Write unknown field ID 1.
  output.write(1);
  output.write(new byte[] {1, 2, 3, 4});

  encodeTagToOutput("Key3", "Value3", output);

  // key 3 should not be included
  TagContext expected =
      tagger
          .emptyBuilder()
          .put(TagKey.create("Key1"), TagValue.create("Value1"))
          .put(TagKey.create("Key2"), TagValue.create("Value2"))
          .build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #5
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
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) {
    throw new TagContextDeserializationException(exn.toString()); // byte array format error.
  }
}
 
Example #6
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private static Map<TagKey, TagValueWithMetadata> parseTags(ByteBuffer buffer)
    throws TagContextDeserializationException {
  Map<TagKey, TagValueWithMetadata> tags = new HashMap<TagKey, TagValueWithMetadata>();
  int limit = buffer.limit();
  int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
  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 {
      // Stop parsing at the first unknown field ID, since there is no way to know its length.
      // 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;
}
 
Example #7
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeMultipleTags() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key2", "Value2", output);
  TagContext expected =
      tagger
          .emptyBuilder()
          .put(TagKey.create("Key1"), TagValue.create("Value1"))
          .put(TagKey.create("Key2"), TagValue.create("Value2"))
          .build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #8
Source File: StatsTestUtils.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
  String serializedString = new String(bytes, UTF_8);
  if (serializedString.startsWith(EXTRA_TAG_HEADER_VALUE_PREFIX)) {
    return tagger.emptyBuilder()
        .putLocal(EXTRA_TAG,
            TagValue.create(serializedString.substring(EXTRA_TAG_HEADER_VALUE_PREFIX.length())))
        .build();
  } else {
    throw new TagContextDeserializationException("Malformed value");
  }
}
 
Example #9
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeDuplicateKeys() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key1", "Value2", output);
  TagContext expected =
      tagger.emptyBuilder().put(TagKey.create("Key1"), TagValue.create("Value2")).build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #10
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeDuplicateTags() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key1", "Value1", output);
  encodeTagToOutput("Key1", "Value1", output);
  TagContext expected =
      tagger.emptyBuilder().put(TagKey.create("Key1"), TagValue.create("Value1")).build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
 
Example #11
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void stopParsingAtUnknownTagAtStart() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);

  // Write unknown field ID 1.
  output.write(1);
  output.write(new byte[] {1, 2, 3, 4});

  encodeTagToOutput("Key", "Value", output);
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(tagger.empty());
}
 
Example #12
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_WithUnknownProperties() throws TagContextDeserializationException {
  Map<String, String> carrier =
      Collections.singletonMap(CORRELATION_CONTEXT, "k1=v1;property1=p1;property2=p2,k2=v2");
  Tag expected = Tag.create(K1, TagValue.create("v1"), METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected, T2);
}
 
Example #13
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_TrimSpaces() throws TagContextDeserializationException {
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, "k1= v1, k2=v2 ");
  Tag expected1 = Tag.create(K1, V1, METADATA_UNLIMITED_PROPAGATION);
  Tag expected2 = Tag.create(K2, V2, METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected1, expected2);
}
 
Example #14
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_OverrideTagWithSpaces() throws TagContextDeserializationException {
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, "k1= v1, k1=v2 ");
  Tag expected = Tag.create(K1, V2, METADATA_UNLIMITED_PROPAGATION);
  TagContext tagContext = textFormat.extract(carrier, getter);
  assertThat(TagsTestUtil.tagContextToList(tagContext)).containsExactly(expected);
}
 
Example #15
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_TagKeyTooLong() throws TagContextDeserializationException {
  String longKey = generateRandom(300);
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, longKey + "=v1");
  thrown.expect(TagContextDeserializationException.class);
  textFormat.extract(carrier, getter);
}
 
Example #16
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void extract_TagValueTooLong() throws TagContextDeserializationException {
  String longValue = generateRandom(300);
  Map<String, String> carrier = Collections.singletonMap(CORRELATION_CONTEXT, "k1=" + longValue);
  thrown.expect(TagContextDeserializationException.class);
  textFormat.extract(carrier, getter);
}
 
Example #17
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void extract_TaggingDisabled()
    throws TagContextDeserializationException, TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  textFormat.inject(makeTagContext(T1), carrier, setter);
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(TagsTestUtil.tagContextToList(textFormat.extract(carrier, getter))).isEmpty();
  tagsComponent.setState(TaggingState.ENABLED);
}
 
Example #18
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void fromByteArray_TaggingDisabled()
    throws TagContextDeserializationException, TagContextSerializationException {
  byte[] serialized = serializer.toByteArray(tagContext);
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();
}
 
Example #19
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromByteArray_TaggingReenabled()
    throws TagContextDeserializationException, TagContextSerializationException {
  byte[] serialized = serializer.toByteArray(tagContext);
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();
  tagsComponent.setState(TaggingState.ENABLED);
  assertThat(serializer.fromByteArray(serialized)).isEqualTo(tagContext);
}
 
Example #20
Source File: StatsTestUtils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
  String serializedString = new String(bytes, UTF_8);
  if (serializedString.startsWith(EXTRA_TAG_HEADER_VALUE_PREFIX)) {
    return tagger.emptyBuilder()
        .put(EXTRA_TAG,
            TagValue.create(serializedString.substring(EXTRA_TAG_HEADER_VALUE_PREFIX.length())))
        .build();
  } else {
    throw new TagContextDeserializationException("Malformed value");
  }
}
 
Example #21
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static final TagKey createTagKey(String name) throws TagContextDeserializationException {
  try {
    return TagKey.create(name);
  } catch (IllegalArgumentException e) {
    throw new TagContextDeserializationException("Invalid tag key: " + name, e);
  }
}
 
Example #22
Source File: StackdriverMetrics.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/** Extract opencensus context(if any) from environment. */
private static TagContext deserializeContext() {
  String serializedContext = System.getenv("OPENCENSUS_STATS_CONTEXT");
  if (serializedContext == null) {
    return Tags.getTagger().empty();
  }

  TagContextBinarySerializer serializer = Tags.getTagPropagationComponent().getBinarySerializer();

  try {
    return serializer.fromByteArray(Base64.getDecoder().decode(serializedContext));
  } catch (TagContextDeserializationException e) {
    return Tags.getTagger().empty();
  }
}
 
Example #23
Source File: NoopTags.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public <C /*>>> extends @NonNull Object*/> TagContext extract(C carrier, Getter<C> getter)
    throws TagContextDeserializationException {
  Utils.checkNotNull(carrier, "carrier");
  Utils.checkNotNull(getter, "getter");
  return getNoopTagContext();
}
 
Example #24
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextBinarySerializer()
    throws TagContextDeserializationException, TagContextSerializationException {
  assertThat(NoopTags.getNoopTagContextBinarySerializer().toByteArray(TAG_CONTEXT))
      .isEqualTo(new byte[0]);
  assertThat(NoopTags.getNoopTagContextBinarySerializer().fromByteArray(new byte[5]))
      .isEqualTo(NoopTags.getNoopTagContext());
}
 
Example #25
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextBinarySerializer_FromByteArray_DisallowsNull()
    throws TagContextDeserializationException {
  TagContextBinarySerializer noopSerializer = NoopTags.getNoopTagContextBinarySerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.fromByteArray(null);
}
 
Example #26
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat()
    throws TagContextDeserializationException, TagContextSerializationException {
  NoopTags.getNoopTagContextTextSerializer().inject(TAG_CONTEXT, new Object(), NOOP_SETTER);
  assertThat(NoopTags.getNoopTagContextTextSerializer().extract(new Object(), NOOP_GETTER))
      .isEqualTo(NoopTags.getNoopTagContext());
}
 
Example #27
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat_extract_DisallowsNullCarrier()
    throws TagContextDeserializationException {
  TagContextTextFormat noopSerializer = NoopTags.getNoopTagContextTextSerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.extract(null, NOOP_GETTER);
}
 
Example #28
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat_extract_DisallowsNullGetter()
    throws TagContextDeserializationException {
  TagContextTextFormat noopSerializer = NoopTags.getNoopTagContextTextSerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.extract(new Object(), null);
}
 
Example #29
Source File: CorrelationContextFormat.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #30
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeOneTag() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  encodeTagToOutput("Key", "Value", output);
  TagContext expected =
      tagger.emptyBuilder().put(TagKey.create("Key"), TagValue.create("Value")).build();
  assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}