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();
}
}
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);
}
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));
}
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);
}
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);
}
Aggregations