io.opencensus.tags.propagation.TagContextSerializationException Java Examples

The following examples show how to use io.opencensus.tags.propagation.TagContextSerializationException. 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: 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 #2
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
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);
  int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
  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();
}
 
Example #3
Source File: TagContextSerializationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeTooLargeTagContext() throws TagContextSerializationException {
  TagContextBuilder builder = tagger.emptyBuilder();
  for (int i = 0; i < BinarySerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) {
    // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8.
    String str;
    if (i < 10) {
      str = "000" + i;
    } else if (i < 100) {
      str = "00" + i;
    } else if (i < 1000) {
      str = "0" + i;
    } else {
      str = String.valueOf(i);
    }
    builder.put(TagKey.create(str), TagValue.create(str));
  }
  // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte
  // more than limit.
  builder.put(TagKey.create("last"), TagValue.create("last1"));

  TagContext tagContext = builder.build();
  thrown.expect(TagContextSerializationException.class);
  thrown.expectMessage("Size of TagContext exceeds the maximum serialized size ");
  serializer.toByteArray(tagContext);
}
 
Example #4
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 #5
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 #6
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 #7
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void toByteArray_TaggingReenabled() throws TagContextSerializationException {
  final byte[] serialized = serializer.toByteArray(tagContext);
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(serializer.toByteArray(tagContext)).isEmpty();
  tagsComponent.setState(TaggingState.ENABLED);
  assertThat(serializer.toByteArray(tagContext)).isEqualTo(serialized);
}
 
Example #8
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 #9
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_SizeTooLarge() throws TagContextSerializationException {
  Tag[] tags = new Tag[40];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create(generateRandom(240)),
            TagValue.create(generateRandom(240)),
            METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  thrown.expect(TagContextSerializationException.class);
  textFormat.inject(tagContext, carrier, setter);
}
 
Example #10
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_TooManyTags() throws TagContextSerializationException {
  Tag[] tags = new Tag[CorrelationContextFormat.MAX_NUMBER_OF_TAGS + 1];
  for (int i = 0; i < tags.length; i++) {
    tags[i] =
        Tag.create(
            TagKey.create("k" + i), TagValue.create("v" + i), METADATA_UNLIMITED_PROPAGATION);
  }
  TagContext tagContext = makeTagContext(tags);
  Map<String, String> carrier = new HashMap<String, String>();
  thrown.expect(TagContextSerializationException.class);
  textFormat.inject(tagContext, carrier, setter);
}
 
Example #11
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void inject_TaggingDisabled() throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  tagsComponent.setState(TaggingState.DISABLED);
  textFormat.inject(makeTagContext(T1, T2), carrier, setter);
  assertThat(carrier).isEmpty();
  tagsComponent.setState(TaggingState.ENABLED);
}
 
Example #12
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_MixedPropagatingAndNonPropagatingTags()
    throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  Tag tag = Tag.create(K1, V1, METADATA_NO_PROPAGATION);
  textFormat.inject(makeTagContext(T1, tag, T2), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "k1=v1,k2=v2");
}
 
Example #13
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void inject_SkipNonPropagatingTag() throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  Tag tag = Tag.create(K1, V1, METADATA_NO_PROPAGATION);
  textFormat.inject(makeTagContext(tag), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "");
}
 
Example #14
Source File: CorrelationContextFormat.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
    int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
    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);
  }
}
 
Example #15
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat_inject_DisallowsNullSetter()
    throws TagContextSerializationException {
  TagContextTextFormat noopSerializer = NoopTags.getNoopTagContextTextSerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.inject(TAG_CONTEXT, new Object(), null);
}
 
Example #16
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat_inject_DisallowsNullCarrier()
    throws TagContextSerializationException {
  TagContextTextFormat noopSerializer = NoopTags.getNoopTagContextTextSerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.inject(TAG_CONTEXT, null, NOOP_SETTER);
}
 
Example #17
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextTextFormat_inject_DisallowsNullTagContext()
    throws TagContextSerializationException {
  TagContextTextFormat noopSerializer = NoopTags.getNoopTagContextTextSerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.inject(null, new Object(), NOOP_SETTER);
}
 
Example #18
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 #19
Source File: NoopTagsTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void noopTagContextBinarySerializer_ToByteArray_DisallowsNull()
    throws TagContextSerializationException {
  TagContextBinarySerializer noopSerializer = NoopTags.getNoopTagContextBinarySerializer();
  thrown.expect(NullPointerException.class);
  noopSerializer.toByteArray(null);
}
 
Example #20
Source File: NoopTags.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Override
public <C /*>>> extends @NonNull Object*/> void inject(
    TagContext tagContext, C carrier, Setter<C> setter)
    throws TagContextSerializationException {
  Utils.checkNotNull(tagContext, "tagContext");
  Utils.checkNotNull(carrier, "carrier");
  Utils.checkNotNull(setter, "setter");
}
 
Example #21
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void inject_Empty() throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  textFormat.inject(makeTagContext(), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "");
}
 
Example #22
Source File: CorrelationContextFormatTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void inject() throws TagContextSerializationException {
  Map<String, String> carrier = new HashMap<String, String>();
  textFormat.inject(makeTagContext(T1, T2), carrier, setter);
  assertThat(carrier).containsExactly(CORRELATION_CONTEXT, "k1=v1,k2=v2");
}
 
Example #23
Source File: TagContextBinarySerializerImpl.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toByteArray(TagContext tags) throws TagContextSerializationException {
  return state.getInternal() == State.DISABLED
      ? EMPTY_BYTE_ARRAY
      : BinarySerializationUtils.serializeBinary(tags);
}
 
Example #24
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void toByteArray_TaggingDisabled() throws TagContextSerializationException {
  tagsComponent.setState(TaggingState.DISABLED);
  assertThat(serializer.toByteArray(tagContext)).isEmpty();
}
 
Example #25
Source File: TagContextBinarySerializerImplTest.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Test
public void toByteArray_SkipNonPropagatingTag() throws TagContextSerializationException {
  byte[] versionIdBytes = new byte[] {BinarySerializationUtils.VERSION_ID};
  assertThat(serializer.toByteArray(tagContextWithNonPropagatingTag)).isEqualTo(versionIdBytes);
}