Search in sources :

Example 1 with NonThrowingCloseable

use of com.google.instrumentation.common.NonThrowingCloseable in project instrumentation-java by census-instrumentation.

the class StatsContextFactoryTest method testWithStatsContext.

@Test
public void testWithStatsContext() {
    assertThat(factory.getCurrentStatsContext()).isEqualTo(defaultCtx);
    NonThrowingCloseable scopedStatsCtx = factory.withStatsContext(statsContext);
    try {
        assertThat(factory.getCurrentStatsContext()).isEqualTo(statsContext);
    } finally {
        scopedStatsCtx.close();
    }
    assertThat(factory.getCurrentStatsContext()).isEqualTo(defaultCtx);
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Test(org.junit.Test)

Example 2 with NonThrowingCloseable

use of com.google.instrumentation.common.NonThrowingCloseable in project instrumentation-java by census-instrumentation.

the class BasicContextTracing method main.

/** Main method. */
public static void main(String[] args) {
    Span span = tracer.spanBuilder("MyRootSpan").becomeRoot().startSpan();
    try (NonThrowingCloseable ws = tracer.withSpan(span)) {
        doWork();
    }
    span.end();
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Span(com.google.instrumentation.trace.Span)

Example 3 with NonThrowingCloseable

use of com.google.instrumentation.common.NonThrowingCloseable in project instrumentation-java by census-instrumentation.

the class MultiSpansContextTracing method main.

/** Main method. */
public static void main(String[] args) {
    TraceExporter.LoggingServiceHandler.registerService(Tracing.getTraceExporter());
    Span span = tracer.spanBuilder("MyRootSpan").becomeRoot().startSpan();
    try (NonThrowingCloseable ws = tracer.withSpan(span)) {
        doWork();
    }
    span.end();
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Span(com.google.instrumentation.trace.Span)

Example 4 with NonThrowingCloseable

use of com.google.instrumentation.common.NonThrowingCloseable in project instrumentation-java by census-instrumentation.

the class TracerTest method startScopedSpanRoot.

@Test
public void startScopedSpanRoot() {
    Tracer mockTracer = new MockTracer(spanFactory);
    when(spanFactory.startSpan(isNull(Span.class), same(SPAN_NAME), eq(new StartSpanOptions()))).thenReturn(span);
    NonThrowingCloseable ss = mockTracer.spanBuilder(SPAN_NAME).becomeRoot().startScopedSpan();
    try {
        assertThat(tracer.getCurrentSpan()).isSameAs(span);
    } finally {
        ss.close();
    }
    verify(span).end(same(EndSpanOptions.DEFAULT));
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Test(org.junit.Test)

Example 5 with NonThrowingCloseable

use of com.google.instrumentation.common.NonThrowingCloseable in project instrumentation-java by census-instrumentation.

the class TracerTest method propagationViaRunnable.

@Test
public void propagationViaRunnable() {
    Runnable runnable = null;
    NonThrowingCloseable ws = tracer.withSpan(span);
    try {
        assertThat(tracer.getCurrentSpan()).isSameAs(span);
        runnable = Context.current().wrap(new Runnable() {

            @Override
            public void run() {
                assertThat(tracer.getCurrentSpan()).isSameAs(span);
            }
        });
    } finally {
        ws.close();
    }
    assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
    // When we run the runnable we will have the span in the current Context.
    runnable.run();
    assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Test(org.junit.Test)

Aggregations

NonThrowingCloseable (com.google.instrumentation.common.NonThrowingCloseable)17 Test (org.junit.Test)13 Span (com.google.instrumentation.trace.Span)3 StatsContext (com.google.instrumentation.stats.StatsContext)1