Search in sources :

Example 11 with NonThrowingCloseable

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

the class StatsRunner method main.

/** Main method. */
public static void main(String[] args) {
    System.out.println("Hello Stats World");
    System.out.println("Default Tags: " + DEFAULT);
    System.out.println("Current Tags: " + factory.getCurrentStatsContext());
    StatsContext tags1 = DEFAULT.with(K1, V1, K2, V2);
    try (NonThrowingCloseable scopedStatsCtx1 = factory.withStatsContext(tags1)) {
        System.out.println("  Current Tags: " + factory.getCurrentStatsContext());
        System.out.println("  Current == Default + tags1: " + factory.getCurrentStatsContext().equals(tags1));
        StatsContext tags2 = tags1.with(K3, V3, K4, V4);
        try (NonThrowingCloseable scopedStatsCtx2 = factory.withStatsContext(tags2)) {
            System.out.println("    Current Tags: " + factory.getCurrentStatsContext());
            System.out.println("    Current == Default + tags1 + tags2: " + factory.getCurrentStatsContext().equals(tags2));
            factory.getCurrentStatsContext().record(MeasurementMap.of(M1, 0.2, M2, 0.4));
        }
    }
    System.out.println("Current == Default: " + factory.getCurrentStatsContext().equals(DEFAULT));
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) StatsContext(com.google.instrumentation.stats.StatsContext)

Example 12 with NonThrowingCloseable

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

the class StatsContextFactoryTest method testWithStatsContextUsingWrap.

@Test
public void testWithStatsContextUsingWrap() {
    Runnable runnable;
    NonThrowingCloseable scopedStatsCtx = factory.withStatsContext(statsContext);
    try {
        assertThat(factory.getCurrentStatsContext()).isSameAs(statsContext);
        runnable = Context.current().wrap(new Runnable() {

            @Override
            public void run() {
                assertThat(factory.getCurrentStatsContext()).isSameAs(statsContext);
            }
        });
    } finally {
        scopedStatsCtx.close();
    }
    assertThat(factory.getCurrentStatsContext()).isEqualTo(defaultCtx);
    // When we run the runnable we will have the statsContext in the current Context.
    runnable.run();
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Test(org.junit.Test)

Example 13 with NonThrowingCloseable

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

the class TracerTest method startScopedSpanChild.

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

Example 14 with NonThrowingCloseable

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

the class MultiSpansContextTracing method doSomeMoreWork.

private static void doSomeMoreWork() {
    // Create a child Span of the current Span.
    Span span = tracer.spanBuilder("MyChildSpan").startSpan();
    try (NonThrowingCloseable ws = tracer.withSpan(span)) {
        doSomeOtherWork();
    }
    span.end();
}
Also used : NonThrowingCloseable(com.google.instrumentation.common.NonThrowingCloseable) Span(com.google.instrumentation.trace.Span)

Example 15 with NonThrowingCloseable

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

the class SpanBuilderTest method startScopedSpanRoot.

@Test
public void startScopedSpanRoot() {
    when(spanFactory.startSpan(isNull(Span.class), same(SPAN_NAME), eq(new StartSpanOptions()))).thenReturn(span);
    NonThrowingCloseable ss = spanBuilder.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)

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