Search in sources :

Example 1 with Scope

use of io.opencensus.common.Scope in project java-docs-samples by GoogleCloudPlatform.

the class TracingSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: TracingSample <instance_id> <database_id>");
        return;
    }
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    // Installs a handler for /tracez page.
    ZPageHandlers.startHttpServerAndRegisterAll(8080);
    // Installs an exporter for stack driver traces.
    StackdriverExporter.createAndRegister();
    Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(Arrays.asList(SAMPLE_SPAN));
    // Installs an exporter for stack driver stats.
    StackdriverStatsExporter.createAndRegister();
    RpcViews.registerAllCumulativeViews();
    // Name of your instance & database.
    String instanceId = args[0];
    String databaseId = args[1];
    try {
        // Creates a database client
        DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
        // Queries the database
        try (Scope ss = Tracing.getTracer().spanBuilderWithExplicitParent(SAMPLE_SPAN, null).setSampler(Samplers.alwaysSample()).startScopedSpan()) {
            ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
            System.out.println("\n\nResults:");
            // Prints the results
            while (resultSet.next()) {
                System.out.printf("%d\n\n", resultSet.getLong(0));
            }
        }
    } finally {
        // Closes the client which will free up the resources used
        spanner.close();
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Scope(io.opencensus.common.Scope) ResultSet(com.google.cloud.spanner.ResultSet) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Example 2 with Scope

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

the class CurrentSpanUtilsTest method withSpan_CloseDetaches.

@Test
public void withSpan_CloseDetaches() {
    assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE);
    Scope ws = CurrentSpanUtils.withSpan(span, false);
    try {
        assertThat(CurrentSpanUtils.getCurrentSpan()).isSameInstanceAs(span);
    } finally {
        ws.close();
    }
    assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE);
    verifyZeroInteractions(span);
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 3 with Scope

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

the class CurrentSpanUtilsTest method withSpan_CloseDetachesAndEndsSpan.

@Test
public void withSpan_CloseDetachesAndEndsSpan() {
    assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE);
    Scope ss = CurrentSpanUtils.withSpan(span, true);
    try {
        assertThat(CurrentSpanUtils.getCurrentSpan()).isSameInstanceAs(span);
    } finally {
        ss.close();
    }
    assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE);
    verify(span).end(same(EndSpanOptions.DEFAULT));
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 4 with Scope

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

the class SpanBuilderTest method startScopedSpan.

@Test
public void startScopedSpan() {
    assertThat(tracer.getCurrentSpan()).isSameInstanceAs(BlankSpan.INSTANCE);
    Scope scope = spanBuilder.startScopedSpan();
    try {
        assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span);
    } finally {
        scope.close();
    }
    verify(span).end(EndSpanOptions.DEFAULT);
    assertThat(tracer.getCurrentSpan()).isSameInstanceAs(BlankSpan.INSTANCE);
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 5 with Scope

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

the class TracerTest method getCurrentSpan_WithSpan.

@Test
public void getCurrentSpan_WithSpan() {
    assertThat(noopTracer.getCurrentSpan()).isSameInstanceAs(BlankSpan.INSTANCE);
    Scope ws = noopTracer.withSpan(span);
    try {
        assertThat(noopTracer.getCurrentSpan()).isSameInstanceAs(span);
    } finally {
        ws.close();
    }
    assertThat(noopTracer.getCurrentSpan()).isSameInstanceAs(BlankSpan.INSTANCE);
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

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