io.opencensus.trace.Tracer.withSpan()方法的使用及代码示例

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

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

Tracer.withSpan介绍

[英]Enters the scope of code where the given Span is in the current Context, and returns an object that represents that scope. The scope is exited when the returned object is closed.

Supports try-with-resource idiom.

Can be called with BlankSpan to enter a scope of code where tracing is stopped.

Example of usage:

private static Tracer tracer = Tracing.getTracer();span.end(); 
} 
}

Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.

Example of usage prior to Java SE7:

private static Tracer tracer = Tracing.getTracer();finally  
ws.close(); 
} 
span.end(); 
} 
}

[中]输入给定范围在当前上下文中的代码范围,并返回表示该范围的对象。当返回的对象关闭时,作用域退出。
支持try with resource习惯用法。
可以使用BlankSpan调用,以输入停止跟踪的代码范围。
用法示例:

private static Tracer tracer = Tracing.getTracer();span.end(); 
} 
}

在JavaSE7之前,可以使用finally块来确保资源关闭,而不管try语句是正常完成还是突然完成。
Java SE7之前的使用示例:

private static Tracer tracer = Tracing.getTracer();finally  
ws.close(); 
} 
span.end(); 
} 
}

代码示例

代码示例来源: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: googleapis/google-cloud-java

@Override
public TransactionContext resetForRetry() {
 if (txn == null || !txn.isAborted() && txnState != TransactionState.ABORTED) {
  throw new IllegalStateException(
    "resetForRetry can only be called if the previous attempt" + " aborted");
 }
 try (Scope s = tracer.withSpan(span)) {
  txn = session.newTransaction();
  txn.ensureTxn();
  txnState = TransactionState.STARTED;
  return txn;
 }
}

代码示例来源: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

@Override
public RewriteResponse continueRewrite(RewriteResponse previousResponse) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CONTINUE_REWRITE);
 Scope scope = tracer.withSpan(span);
 try {
  return rewrite(previousResponse.rewriteRequest, previousResponse.rewriteToken);
 } finally {
  scope.close();
  span.end();
 }
}

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

@Override
public RewriteResponse openRewrite(RewriteRequest rewriteRequest) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_OPEN_REWRITE);
 Scope scope = tracer.withSpan(span);
 try {
  return rewrite(rewriteRequest, null);
 } finally {
  scope.close();
  span.end();
 }
}

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

@Override
public ReadContext singleUse() {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().singleUse();
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public ReadContext singleUse(TimestampBound bound) {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().singleUse(bound);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public ReadOnlyTransaction singleUseReadOnlyTransaction() {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().singleUseReadOnlyTransaction();
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().readOnlyTransaction(bound);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public TransactionManager transactionManager() {
 Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadWriteSession().transactionManager();
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public long executePartitionedUpdate(Statement stmt) {
 Span span = tracer.spanBuilder(PARTITION_DML_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadWriteSession().executePartitionedUpdate(stmt);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public ReadOnlyTransaction singleUseReadOnlyTransaction(TimestampBound bound) {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().singleUseReadOnlyTransaction(bound);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public TransactionRunner readWriteTransaction() {
 Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadWriteSession().readWriteTransaction();
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public ReadOnlyTransaction readOnlyTransaction() {
 Span span = tracer.spanBuilder(READ_ONLY_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadSession().readOnlyTransaction();
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 }
}

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

@Override
public Timestamp write(Iterable<Mutation> mutations) throws SpannerException {
 Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadWriteSession().write(mutations);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 } finally {
  span.end();
 }
}

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

@Override
public Timestamp writeAtLeastOnce(Iterable<Mutation> mutations) throws SpannerException {
 Span span = tracer.spanBuilder(READ_WRITE_TRANSACTION).startSpan();
 try (Scope s = tracer.withSpan(span)) {
  return pool.getReadWriteSession().writeAtLeastOnce(mutations);
 } catch (RuntimeException e) {
  TraceUtil.endSpanWithFailure(span, e);
  throw e;
 } finally {
  span.end();
 }
}

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

@Override
public StorageObject patch(StorageObject storageObject, Map<Option, ?> options) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_PATCH_OBJECT);
 Scope scope = tracer.withSpan(span);
 try {
  return patchCall(storageObject, options).execute();
 } catch (IOException ex) {
  span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
  throw translate(ex);
 } finally {
  scope.close();
  span.end();
 }
}

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

@Override
public Notification createNotification(String bucket, Notification notification) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CREATE_NOTIFICATION);
 Scope scope = tracer.withSpan(span);
 try {
  return storage.notifications().insert(bucket, notification).execute();
 } catch (IOException ex) {
  span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
  throw translate(ex);
 } finally {
  scope.close();
  span.end();
 }
}

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

@Override
public ObjectAccessControl createDefaultAcl(ObjectAccessControl acl) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CREATE_OBJECT_DEFAULT_ACL);
 Scope scope = tracer.withSpan(span);
 try {
  return storage.defaultObjectAccessControls().insert(acl.getBucket(), acl).execute();
 } catch (IOException ex) {
  span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
  throw translate(ex);
 } finally {
  scope.close();
  span.end();
 }
}

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

@Override
public List<ObjectAccessControl> listDefaultAcls(String bucket) {
 Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_LIST_OBJECT_DEFAULT_ACLS);
 Scope scope = tracer.withSpan(span);
 try {
  return storage.defaultObjectAccessControls().list(bucket).execute().getItems();
 } catch (IOException ex) {
  span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
  throw translate(ex);
 } finally {
  scope.close();
  span.end();
 }
}

相关文章