Search in sources :

Example 11 with Tag

use of io.opencensus.tags.Tag 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 12 with Tag

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

the class CorrelationContextFormatTest method extract_OverrideTagWithSpaces.

@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);
}
Also used : TagContext(io.opencensus.tags.TagContext) Tag(io.opencensus.tags.Tag) Test(org.junit.Test)

Example 13 with Tag

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

the class CorrelationContextFormatTest method inject_TooManyTags.

@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);
}
Also used : TagContext(io.opencensus.tags.TagContext) HashMap(java.util.HashMap) Tag(io.opencensus.tags.Tag) Test(org.junit.Test)

Example 14 with Tag

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

the class CorrelationContextFormatTest method inject_SizeTooLarge.

@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);
}
Also used : TagContext(io.opencensus.tags.TagContext) HashMap(java.util.HashMap) Tag(io.opencensus.tags.Tag) Test(org.junit.Test)

Example 15 with Tag

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

the class CorrelationContextFormatTest method extract_WithUnknownProperties.

@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);
}
Also used : TagContext(io.opencensus.tags.TagContext) Tag(io.opencensus.tags.Tag) Test(org.junit.Test)

Aggregations

Tag (io.opencensus.tags.Tag)18 Test (org.junit.Test)13 TagContext (io.opencensus.tags.TagContext)10 HashMap (java.util.HashMap)5 TagContextBuilder (io.opencensus.tags.TagContextBuilder)2 TagContextSerializationException (io.opencensus.tags.propagation.TagContextSerializationException)2 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 TagMapImpl (io.opencensus.implcore.tags.TagMapImpl)1 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)1 TagKey (io.opencensus.tags.TagKey)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1