io.opencensus.trace.Tracer类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中io.opencensus.trace.Tracer类的一些代码示例,展示了Tracer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tracer类的具体详情如下:
包路径:io.opencensus.trace.Tracer
类名称:Tracer

Tracer介绍

[英]Tracer is a simple, thin class for Span creation and in-process context interaction.

Users may choose to use manual or automatic Context propagation. Because of that this class offers APIs to facilitate both usages.

The automatic context propagation is done using io.grpc.Context which is a gRPC independent implementation for in-process Context propagation mechanism which can carry scoped-values across API boundaries and between threads. Users of the library must propagate the io.grpc.Context between different threads.

Example usage with automatic context propagation:

class MyClass } 
} 
}

Example usage with manual context propagation:

class MyClass finally  
// To make sure we end the span even in case of an exception. 
childSpan.end();  // Manually end the span. 
} 
} 
} 
}

[中]Tracer是一个用于创建跨度和进程内上下文交互的简单而精简的类。
用户可以选择使用手动或自动上下文传播。正因为如此,这个类提供了API来促进这两种用法。
自动上下文传播是使用io完成的。grpc。Context是进程内上下文传播机制的一个独立于gRPC的实现,它可以跨API边界和线程之间传递作用域值。库的用户必须传播io。grpc。不同线程之间的上下文。
自动上下文传播的示例用法:

class MyClass } 
} 
}

手动上下文传播的用法示例:

class MyClass finally  
// To make sure we end the span even in case of an exception. 
childSpan.end();  // Manually end the span. 
} 
} 
} 
}

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/** Transaction functions that returns its result in the provided SettableFuture. */
private <T> void runTransaction(
  final Transaction.Function<T> transactionCallback,
  final SettableApiFuture<T> resultFuture,
  final TransactionOptions options) {
 // span is intentionally not ended here. It will be ended by runTransactionAttempt on success
 // or error.
 Span span = tracer.spanBuilder("CloudFirestore.Transaction").startSpan();
 try (Scope s = tracer.withSpan(span)) {
  runTransactionAttempt(transactionCallback, resultFuture, options, span);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

TransactionManagerImpl(SessionImpl session) {
 this.session = session;
 this.span = Tracing.getTracer().getCurrentSpan();
}

代码示例来源:origin: googleapis/google-cloud-java

protected ResumableStreamIterator(int maxBufferSize, String streamName, Span parent) {
 checkArgument(maxBufferSize >= 0);
 this.maxBufferSize = maxBufferSize;
 this.span = tracer.spanBuilderWithExplicitParent(streamName, parent).startSpan();
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void startSpanWithParentFromContext() {
 Scope ws = tracer.withSpan(span);
 try {
  assertThat(tracer.getCurrentSpan()).isSameAs(span);
  when(tracer.spanBuilderWithExplicitParent(same(SPAN_NAME), same(span)))
    .thenReturn(spanBuilder);
  assertThat(tracer.spanBuilder(SPAN_NAME)).isSameAs(spanBuilder);
 } finally {
  ws.close();
 }
}

代码示例来源:origin: googleapis/google-cloud-java

/** Helper method to start a span. */
private Span startSpan(String spanName) {
 return tracer
   .spanBuilder(spanName)
   .setRecordEvents(censusHttpModule.isRecordEvents())
   .startSpan();
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
public TransactionContext begin() {
 Preconditions.checkState(txn == null, "begin can only be called once");
 try (Scope s = tracer.withSpan(span)) {
  txn = session.newTransaction();
  session.setActive(this);
  txn.ensureTxn();
  txnState = TransactionState.STARTED;
  return txn;
 }
}

代码示例来源:origin: io.grpc/grpc-core

ClientCallTracer(@Nullable Span parentSpan, MethodDescriptor<?, ?> method) {
 checkNotNull(method, "method");
 this.isSampledToLocalTracing = method.isSampledToLocalTracing();
 this.span =
   censusTracer
     .spanBuilderWithExplicitParent(
       generateTraceSpanName(false, method.getFullMethodName()),
       parentSpan)
     .setRecordEvents(true)
     .startSpan();
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
 public void doNotCrash_NoopImplementation() throws Exception {
  SpanBuilder spanBuilder = tracer.spanBuilder("MySpanName");
  spanBuilder.setParentLinks(Collections.<Span>emptyList());
  spanBuilder.setRecordEvents(true);
  spanBuilder.setSampler(Samplers.alwaysSample());
  spanBuilder.setSpanKind(Kind.SERVER);
  assertThat(spanBuilder.startSpan()).isSameAs(BlankSpan.INSTANCE);
 }
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Override
public final void handle(HttpExchange httpExchange) throws IOException {
 Span span =
   tracer
     .spanBuilderWithExplicitParent(httpServerSpanName, null)
     .setRecordEvents(true)
     .startSpan();
 try (Scope ss = tracer.withSpan(span)) {
  span.putAttribute(
    "/http/method ", AttributeValue.stringAttributeValue(httpExchange.getRequestMethod()));
  httpExchange.sendResponseHeaders(200, 0);
  zpageHandler.emitHtml(
    uriQueryToMap(httpExchange.getRequestURI()), httpExchange.getResponseBody());
 } finally {
  httpExchange.close();
  span.end(END_SPAN_OPTIONS);
 }
}

代码示例来源:origin: census-instrumentation/opencensus-java

@MustBeClosed
@Override
public Closeable startScopedSpan(String spanName) {
 checkNotNull(spanName, "spanName");
 return Tracing.getTracer()
   .spanBuilder(spanName)
   .setSampler(Samplers.alwaysSample())
   .setRecordEvents(true)
   .startScopedSpan();
}

代码示例来源:origin: io.grpc/grpc-core

ServerTracer(String fullMethodName, @Nullable SpanContext remoteSpan) {
 checkNotNull(fullMethodName, "fullMethodName");
 this.span =
   censusTracer
     .spanBuilderWithRemoteParent(
       generateTraceSpanName(true, fullMethodName),
       remoteSpan)
     .setRecordEvents(true)
     .startSpan();
}

代码示例来源:origin: googleapis/google-cloud-java

Span opSpan = tracer.spanBuilderWithExplicitParent(COMMIT, span).startSpan();
try (Scope s = tracer.withSpan(opSpan)) {
 CommitResponse commitResponse =
   runWithRetries(

代码示例来源:origin: census-instrumentation/opencensus-java

@MustBeClosed
private static Scope newExportScope() {
 // Start a new span with explicit sampler (with low probability) to avoid the case when user
 // sets the default sampler to always sample and we get the Thrift span of the Jaeger
 // export call always sampled and go to an infinite loop.
 return tracer.spanBuilder(EXPORT_SPAN_NAME).setSampler(lowProbabilitySampler).startScopedSpan();
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void getCurrentSpan_WithSpan() {
 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
 Scope ws = noopTracer.withSpan(span);
 try {
  assertThat(noopTracer.getCurrentSpan()).isSameAs(span);
 } finally {
  ws.close();
 }
 assertThat(noopTracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void defaultSpanBuilderWithRemoteParent_NullParent() {
 assertThat(noopTracer.spanBuilderWithRemoteParent(SPAN_NAME, null).startSpan())
   .isSameAs(BlankSpan.INSTANCE);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Before
public void setUp() throws SpanContextParseException {
 MockitoAnnotations.initMocks(this);
 handler =
   new HttpServerHandler<Object, Object, Object>(
     tracer, extractor, textFormat, textFormatGetter, false);
 handlerForPublicEndpoint =
   new HttpServerHandler<Object, Object, Object>(
     tracer, extractor, textFormat, textFormatGetter, true);
 when(tracer.spanBuilderWithRemoteParent(any(String.class), same(spanContextRemote)))
   .thenReturn(spanBuilderWithRemoteParent);
 when(tracer.spanBuilderWithExplicitParent(any(String.class), any(Span.class)))
   .thenReturn(spanBuilderWithLocalParent);
 when(spanBuilderWithRemoteParent.startSpan()).thenReturn(spanWithRemoteParent);
 when(spanBuilderWithLocalParent.startSpan()).thenReturn(spanWithLocalParent);
 when(textFormat.extract(same(carrier), same(textFormatGetter))).thenReturn(spanContextRemote);
}

代码示例来源:origin: GoogleCloudPlatform/java-docs-samples

.spanBuilderWithExplicitParent(SAMPLE_SPAN, null)
 .setSampler(Samplers.alwaysSample())
 .startScopedSpan()) {
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void startScopedSpan() {
 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
 Scope scope = spanBuilder.startScopedSpan();
 try {
  assertThat(tracer.getCurrentSpan()).isSameAs(span);
 } finally {
  scope.close();
 }
 verify(span).end(EndSpanOptions.DEFAULT);
 assertThat(tracer.getCurrentSpan()).isSameAs(BlankSpan.INSTANCE);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void handleStartShouldCreateChildSpanInCurrentContext() {
 Scope scope = tracer.withSpan(parentSpan);
 try {
  HttpRequestContext context = handler.handleStart(null, carrier, request);
  verify(tracer).spanBuilderWithExplicitParent(any(String.class), same(parentSpan));
  assertThat(context.span).isEqualTo(childSpan);
 } finally {
  scope.close();
 }
}

代码示例来源:origin: census-instrumentation/opencensus-java

/**
 * Returns a {@link SpanBuilder} to create and start a new child {@link Span} as a child of to the
 * current {@code Span} if any, otherwise creates a root {@code Span}.
 *
 * <p>See {@link SpanBuilder} for usage examples.
 *
 * <p>This <b>must</b> be used to create a {@code Span} when automatic Context propagation is
 * used.
 *
 * <p>This is equivalent with:
 *
 * <pre>{@code
 * tracer.spanBuilderWithExplicitParent("MySpanName",tracer.getCurrentSpan());
 * }</pre>
 *
 * @param spanName The name of the returned Span.
 * @return a {@code SpanBuilder} to create and start a new {@code Span}.
 * @throws NullPointerException if {@code spanName} is {@code null}.
 * @since 0.5
 */
public final SpanBuilder spanBuilder(String spanName) {
 return spanBuilderWithExplicitParent(spanName, CurrentSpanUtils.getCurrentSpan());
}

相关文章