Search in sources :

Example 6 with TagContextBuilder

use of io.opencensus.tags.TagContextBuilder in project instrumentation-java by census-instrumentation.

the class TagContextSerializationTest method testSerialize.

private void testSerialize(Tag... tags) throws IOException, TagContextSerializationException {
    TagContextBuilder builder = tagger.emptyBuilder();
    for (Tag tag : tags) {
        builder.put(tag.getKey(), tag.getValue());
    }
    byte[] actual = serializer.toByteArray(builder.build());
    Collection<List<Tag>> tagPermutation = Collections2.permutations(Arrays.asList(tags));
    Set<String> possibleOutputs = new HashSet<String>();
    for (List<Tag> list : tagPermutation) {
        ByteArrayOutputStream expected = new ByteArrayOutputStream();
        expected.write(BinarySerializationUtils.VERSION_ID);
        for (Tag tag : list) {
            expected.write(BinarySerializationUtils.TAG_FIELD_ID);
            encodeString(tag.getKey().getName(), expected);
            encodeString(tag.getValue().asString(), expected);
        }
        possibleOutputs.add(new String(expected.toByteArray(), Charsets.UTF_8));
    }
    assertThat(possibleOutputs).contains(new String(actual, Charsets.UTF_8));
}
Also used : List(java.util.List) TagContextBuilder(io.opencensus.tags.TagContextBuilder) Tag(io.opencensus.tags.Tag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashSet(java.util.HashSet)

Example 7 with TagContextBuilder

use of io.opencensus.tags.TagContextBuilder in project instrumentation-java by census-instrumentation.

the class TagMapImplTest method put_nullValue.

@Test
public void put_nullValue() {
    TagContext tags = new TagMapImpl(ImmutableMap.of(K1, VM1));
    TagContextBuilder builder = tagger.toBuilder(tags);
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("value");
    builder.put(K2, null);
}
Also used : TagContext(io.opencensus.tags.TagContext) TagContextBuilder(io.opencensus.tags.TagContextBuilder) Test(org.junit.Test)

Example 8 with TagContextBuilder

use of io.opencensus.tags.TagContextBuilder in project instrumentation-java by census-instrumentation.

the class TagContextRoundtripTest method testRoundtrip_TagContextWithMaximumSize.

@Test
public void testRoundtrip_TagContextWithMaximumSize() throws Exception {
    TagContextBuilder builder = tagger.emptyBuilder();
    for (int i = 0; i < BinarySerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8; i++) {
        // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8.
        // Add 1024 tags, the total size should just be 8192.
        String str;
        if (i < 10) {
            str = "000" + i;
        } else if (i < 100) {
            str = "00" + i;
        } else if (i < 1000) {
            str = "0" + i;
        } else {
            str = "" + i;
        }
        builder.put(TagKey.create(str), TagValue.create(str));
    }
    testRoundtripSerialization(builder.build());
}
Also used : TagContextBuilder(io.opencensus.tags.TagContextBuilder) Test(org.junit.Test)

Example 9 with TagContextBuilder

use of io.opencensus.tags.TagContextBuilder in project instrumentation-java by census-instrumentation.

the class TaggerImplTest method emptyBuilder.

@Test
public void emptyBuilder() {
    TagContextBuilder builder = tagger.emptyBuilder();
    assertThat(builder).isInstanceOf(TagMapBuilderImpl.class);
    assertThat(tagContextToList(builder.build())).isEmpty();
}
Also used : TagContextBuilder(io.opencensus.tags.TagContextBuilder) Test(org.junit.Test)

Example 10 with TagContextBuilder

use of io.opencensus.tags.TagContextBuilder in project instrumentation-java by census-instrumentation.

the class TaggerImplTest method emptyBuilder_TaggingReenabled.

@Test
public void emptyBuilder_TaggingReenabled() {
    tagsComponent.setState(TaggingState.DISABLED);
    assertThat(tagger.emptyBuilder()).isSameInstanceAs(NoopTagMapBuilder.INSTANCE);
    tagsComponent.setState(TaggingState.ENABLED);
    TagContextBuilder builder = tagger.emptyBuilder();
    assertThat(builder).isInstanceOf(TagMapBuilderImpl.class);
    assertThat(tagContextToList(builder.put(K1, V1).build())).containsExactly(Tag.create(K1, V1));
}
Also used : TagContextBuilder(io.opencensus.tags.TagContextBuilder) Test(org.junit.Test)

Aggregations

TagContextBuilder (io.opencensus.tags.TagContextBuilder)15 Test (org.junit.Test)13 TagContext (io.opencensus.tags.TagContext)9 Tag (io.opencensus.tags.Tag)2 Scope (io.opencensus.common.Scope)1 ViewData (io.opencensus.stats.ViewData)1 SpanBuilder (io.opencensus.trace.SpanBuilder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1