Search in sources :

Example 46 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class ScopedTagMapTest method setCurrentTagsWithBuilder.

@Test
public void setCurrentTagsWithBuilder() {
    assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
    Scope scope = tagger.emptyBuilder().put(KEY_1, VALUE_1).buildScoped();
    try {
        assertThat(tagContextToList(tagger.getCurrentTagContext())).containsExactly(Tag.create(KEY_1, VALUE_1));
    } finally {
        scope.close();
    }
    assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 47 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class CurrentTagMapUtilsTest method testWithTagMapUsingWrap.

@Test
public void testWithTagMapUsingWrap() {
    Runnable runnable;
    Scope scopedTags = CurrentTagMapUtils.withTagMap(tagContext);
    try {
        assertThat(CurrentTagMapUtils.getCurrentTagMap()).isSameInstanceAs(tagContext);
        runnable = Context.current().wrap(new Runnable() {

            @Override
            public void run() {
                assertThat(CurrentTagMapUtils.getCurrentTagMap()).isSameInstanceAs(tagContext);
            }
        });
    } finally {
        scopedTags.close();
    }
    assertThat(tagContextToList(CurrentTagMapUtils.getCurrentTagMap())).isEmpty();
    // When we run the runnable we will have the TagContext in the current Context.
    runnable.run();
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 48 with Scope

use of io.opencensus.common.Scope in project beam by apache.

the class BeamFnMapTaskExecutor method execute.

@Override
public void execute() throws Exception {
    Tracer tracer = Tracing.getTracer();
    SpanBuilder builder = tracer.spanBuilder("MapTaskExecutor.Span").setRecordEvents(true);
    // Start the progress tracker before execution (which blocks until execution is finished).
    try (Scope unused = builder.startScopedSpan();
        AutoCloseable unused2 = progressTrackerCloseable(progressTracker)) {
        tracer.getCurrentSpan().addAnnotation("About to execute");
        super.execute();
        tracer.getCurrentSpan().addAnnotation("Done with execute");
    }
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Scope(io.opencensus.common.Scope) Tracer(io.opencensus.trace.Tracer)

Aggregations

Scope (io.opencensus.common.Scope)48 Test (org.junit.Test)18 Span (io.opencensus.trace.Span)8 Timestamp (com.google.cloud.Timestamp)5 TagContext (io.opencensus.tags.TagContext)5 SpanBuilder (io.opencensus.trace.SpanBuilder)5 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)5 ArrayList (java.util.ArrayList)4 Instant (org.joda.time.Instant)4 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)3 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)2 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)2 ByteString (com.google.protobuf.ByteString)2 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)2 MeasureMap (io.opencensus.stats.MeasureMap)2 TagContextBuilder (io.opencensus.tags.TagContextBuilder)2 AttributeValue (io.opencensus.trace.AttributeValue)2 Tracer (io.opencensus.trace.Tracer)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 HashMap (java.util.HashMap)2