Search in sources :

Example 1 with TagContextBuilder

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

the class TagMapImplTest method put_nullKey.

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

Example 2 with TagContextBuilder

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

the class TagMapImplTest method remove_nullKey.

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

Example 3 with TagContextBuilder

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

the class TaggerImplTest method currentBuilder_RemoveDuplicateTags.

@Test
public void currentBuilder_RemoveDuplicateTags() {
    Tag tag1 = Tag.create(K1, V1);
    Tag tag2 = Tag.create(K1, V2);
    TagContext tagContextWithDuplicateTags = new SimpleTagContext(tag1, tag2);
    TagContextBuilder result = getResultOfCurrentBuilder(tagContextWithDuplicateTags);
    assertThat(tagContextToList(result.build())).containsExactly(tag2);
}
Also used : TagContext(io.opencensus.tags.TagContext) Tag(io.opencensus.tags.Tag) TagContextBuilder(io.opencensus.tags.TagContextBuilder) Test(org.junit.Test)

Example 4 with TagContextBuilder

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

the class QuickStart method main.

/**
 * Main launcher for the QuickStart example.
 */
public static void main(String[] args) throws InterruptedException {
    TagContextBuilder tagContextBuilder = tagger.currentBuilder().put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5"));
    SpanBuilder spanBuilder = tracer.spanBuilder("my.org/ProcessVideo").setRecordEvents(true).setSampler(Samplers.alwaysSample());
    viewManager.registerView(VIDEO_SIZE_VIEW);
    LoggingTraceExporter.register();
    // Record the processed video size.
    try (Scope scopedTags = tagContextBuilder.buildScoped();
        Scope scopedSpan = spanBuilder.startScopedSpan()) {
        tracer.getCurrentSpan().addAnnotation("Start processing video.");
        // Sleep for [0,10] milliseconds to fake work.
        Thread.sleep(new Random().nextInt(10) + 1);
        statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25 * MiB).record();
        tracer.getCurrentSpan().addAnnotation("Finished processing video.");
    } catch (Exception e) {
        tracer.getCurrentSpan().addAnnotation("Exception thrown when processing video.");
        tracer.getCurrentSpan().setStatus(Status.UNKNOWN);
        logger.severe(e.getMessage());
    }
    logger.info("Wait longer than the reporting duration...");
    // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
    // TODO(songya): remove the gap once we add a shutdown hook for exporting unflushed spans.
    Thread.sleep(5100);
    ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME);
    logger.info(String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData));
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Scope(io.opencensus.common.Scope) Random(java.util.Random) ViewData(io.opencensus.stats.ViewData) TagContextBuilder(io.opencensus.tags.TagContextBuilder)

Example 5 with TagContextBuilder

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

the class TagContextSerializationTest method testSerializeTooLargeTagContext.

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