本文整理了Java中brave.Tracer
类的一些代码示例,展示了Tracer
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tracer
类的具体详情如下:
包路径:brave.Tracer
类名称:Tracer
[英]Using a tracer, you can create a root span capturing the critical path of a request. Child spans can be created to allocate latency relating to outgoing requests. When tracing single-threaded code, just run it inside a scoped span:
// Start a new trace or a span within an existing trace representing an operationcatch (RuntimeException | Error e)
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally
span.finish();
}
}
When you need more features, or finer control, use the Span type:
// Start a new trace or a span within an existing trace representing an operationcatch (RuntimeException | Error e)
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally
span.finish(); // note the scope is independent of the span. Always finish a span.
}
}
Both of the above examples report the exact same span on finish!
[中]使用跟踪器,您可以创建一个根范围来捕获请求的关键路径。可以创建子跨度来分配与传出请求相关的延迟。跟踪单线程代码时,只需在范围内运行它:
// Start a new trace or a span within an existing trace representing an operationcatch (RuntimeException | Error e)
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally
span.finish();
}
}
当需要更多功能或更精细的控制时,请使用跨度类型:
// Start a new trace or a span within an existing trace representing an operationcatch (RuntimeException | Error e)
span.error(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally
span.finish(); // note the scope is independent of the span. Always finish a span.
}
}
以上两个例子报告的完工跨度完全相同!
代码示例来源:origin: openzipkin/brave
/**
* MethodInterceptor for {@link SimpleMessageListenerContainer.ContainerDelegate#invokeListener(Channel,
* Message)}
*/
@Override public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Message message = (Message) methodInvocation.getArguments()[1];
TraceContextOrSamplingFlags extracted = springRabbitTracing.extractAndClearHeaders(message);
// named for BlockingQueueConsumer.nextMessage, which we can't currently see
Span consumerSpan = tracer.nextSpan(extracted);
Span listenerSpan = tracer.newChild(consumerSpan.context());
if (!consumerSpan.isNoop()) {
setConsumerSpan(consumerSpan, message.getMessageProperties());
// incur timestamp overhead only once
long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds();
consumerSpan.start(timestamp);
long consumerFinish = timestamp + 1L; // save a clock reading
consumerSpan.finish(consumerFinish);
// not using scoped span as we want to start with a pre-configured time
listenerSpan.name("on-message").start(consumerFinish);
}
try (SpanInScope ws = tracer.withSpanInScope(listenerSpan)) {
return methodInvocation.proceed();
} catch (Throwable t) {
listenerSpan.error(t);
throw t;
} finally {
listenerSpan.finish();
}
}
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
@RequestMapping("/traced")
public String traced() throws InterruptedException {
Span span = this.tracer.nextSpan().name("http:customTraceEndpoint").start();
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate
.getForObject("http://localhost:" + this.port + "/call", String.class);
span.finish();
return "traced/" + s;
}
代码示例来源:origin: openzipkin/brave
void joinWithSpanInScope(Tracer tracer, TraceContext context) {
Span span = tracer.joinSpan(context).name("encode").start();
try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
span.tag("foo", "bar");
span.tag("baz", "qux");
} finally {
span.finish();
}
}
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
private Span startOrContinueRenamedSpan(String spanName) {
Span currentSpan = this.tracer.currentSpan();
if (currentSpan != null) {
return currentSpan.name(spanName);
}
return this.tracer.nextSpan().name(spanName);
}
代码示例来源:origin: openzipkin/brave
/**
* Returns a new child span if there's a {@link #currentSpan()} or a new trace if there isn't.
*
* <p>Prefer {@link #startScopedSpan(String)} if you are tracing a synchronous function or code
* block.
*/
public Span nextSpan() {
TraceContext parent = currentTraceContext.get();
return parent != null ? newChild(parent) : newTrace();
}
代码示例来源:origin: openzipkin/brave
void newChildWithSpanInScope(Tracer tracer, TraceContext context) {
Span span = tracer.newChild(context).name("encode").start();
try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
span.tag("foo", "bar");
span.tag("baz", "qux");
} finally {
span.finish();
}
}
代码示例来源:origin: openzipkin/brave
Span startMessageListenerSpan(Message message) {
if (!addConsumerSpan) return jmsTracing.nextSpan(message).name("on-message").start();
TraceContextOrSamplingFlags extracted = jmsTracing.extractAndClearMessage(message);
// JMS has no visibility of the incoming message, which incidentally could be local!
Span consumerSpan = tracer.nextSpan(extracted).kind(CONSUMER).name("receive");
Span listenerSpan = tracer.newChild(consumerSpan.context());
if (!consumerSpan.isNoop()) {
long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds();
consumerSpan.start(timestamp);
if (remoteServiceName != null) consumerSpan.remoteServiceName(remoteServiceName);
jmsTracing.tagQueueOrTopic(message, consumerSpan);
long consumerFinish = timestamp + 1L; // save a clock reading
consumerSpan.finish(consumerFinish);
// not using scoped span as we want to start late
listenerSpan.name("on-message").start(consumerFinish);
}
return listenerSpan;
}
}
代码示例来源:origin: openzipkin/brave
final Span span;
if (kind.equals(Kind.CLIENT)) {
span = tracer.nextSpan();
injector.inject(span.context(), invocation.getAttachments());
} else {
TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments());
span = extracted.context() != null
? tracer.joinSpan(extracted.context())
: tracer.nextSpan(extracted);
if (!span.isNoop()) {
span.kind(kind);
String service = invoker.getInterface().getSimpleName();
String method = RpcUtils.getMethodName(invocation);
try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
Result result = invoker.invoke(invocation);
if (result.hasException()) {
代码示例来源:origin: openzipkin/brave
span = tracer.nextSpan(kafkaTracing.extractAndClearHeaders(record.headers()));
} else {
span = tracer.newChild(maybeParent);
if (!span.isNoop()) {
span.kind(Span.Kind.PRODUCER).name("send");
if (remoteServiceName != null) span.remoteServiceName(remoteServiceName);
if (record.key() instanceof String && !"".equals(record.key())) {
injector.inject(span.context(), record.headers());
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
return delegate.send(record, TracingCallback.create(callback, span, current));
} catch (RuntimeException | Error e) {
代码示例来源:origin: apache/servicecomb-java-chassis
private Span createSpan(String spanName, String path) {
Span currentSpan = tracer.currentSpan();
if (currentSpan != null) {
return tracer.newChild(currentSpan.context()).name(spanName).tag(CALL_PATH, path).start();
}
return tracer.newTrace().name(spanName).tag(CALL_PATH, path).start();
}
代码示例来源:origin: apache/cxf
@WebMethod
public Collection< Book > getBooks() {
final Span span = brave.tracer().nextSpan().name("Get Books").start();
try (SpanInScope scope = brave.tracer().withSpanInScope(span)) {
return Arrays.asList(
new Book("Apache CXF in Action", UUID.randomUUID().toString()),
new Book("Mastering Apache CXF", UUID.randomUUID().toString())
);
} finally {
span.finish();
}
}
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
Span consumerSpan = this.tracer.nextSpan(extracted);
if (!consumerSpan.isNoop()) {
consumerSpan.kind(Span.Kind.CONSUMER).start();
consumerSpan.remoteServiceName(REMOTE_SERVICE_NAME);
addTags(message, consumerSpan, channel);
this.tracing.propagation().keys());
if (log.isDebugEnabled()) {
log.debug("Created a new span in before handle" + consumerSpan);
代码示例来源:origin: openzipkin/brave
@Override public void send(Message message) throws JMSException {
Span span = createAndStartProducerSpan(null, message);
SpanInScope ws = tracer.withSpanInScope(span); // animal-sniffer mistakes this for AutoCloseable
try {
delegate.send(message);
} catch (RuntimeException | JMSException | Error e) {
span.error(e);
throw e;
} finally {
ws.close();
span.finish();
}
}
代码示例来源:origin: openzipkin/brave
void nextWithSpanInScope(Tracer tracer, TraceContextOrSamplingFlags extracted) {
Span span = tracer.nextSpan(extracted).name("encode").start();
try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
span.tag("foo", "bar");
span.tag("baz", "qux");
} finally {
span.finish();
}
}
代码示例来源:origin: apache/cxf
@Test
public void testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient() throws Exception {
final WebClient client = createWebClient("/bookstore/books", braveClientProvider);
final Span span = brave.tracer().nextSpan().name("test span").start();
try {
try (SpanInScope scope = brave.tracer().withSpanInScope(span)) {
final Future<Response> f = client.async().get();
final Response r = f.get(1, TimeUnit.SECONDS);
assertEquals(Status.OK.getStatusCode(), r.getStatus());
assertThat(brave.tracer().currentSpan().context().spanId(), equalTo(span.context().spanId()));
assertThat(TestSpanReporter.getAllSpans().size(), equalTo(3));
assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get books"));
assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books"));
assertThat(TestSpanReporter.getAllSpans().get(2).name(), equalTo("get " + client.getCurrentURI()));
assertThatTraceHeadersArePresent(r, true);
}
} finally {
span.finish();
}
// Await till flush happens, usually a second is enough
await().atMost(Duration.ONE_SECOND).until(()-> TestSpanReporter.getAllSpans().size() == 4);
assertThat(TestSpanReporter.getAllSpans().size(), equalTo(4));
assertThat(TestSpanReporter.getAllSpans().get(3).name(), equalTo("test span"));
}
代码示例来源:origin: openzipkin/brave
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call,
final Metadata headers, final ServerCallHandler<ReqT, RespT> next) {
TraceContextOrSamplingFlags extracted = extractor.extract(headers);
Span span = extracted.context() != null
? tracer.joinSpan(extracted.context())
: tracer.nextSpan(extracted);
// If grpc propagation is enabled, make sure we refresh the server method
if (grpcPropagationFormatEnabled) {
Tags tags = span.context().findExtra(Tags.class);
if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName());
}
span.kind(Span.Kind.SERVER);
parser.onStart(call, headers, span.customizer());
// startCall invokes user interceptors, so we place the span in scope here
ServerCall.Listener<ReqT> result;
SpanInScope scope = tracer.withSpanInScope(span);
try { // retrolambda can't resolve this try/finally
result = next.startCall(new TracingServerCall<>(span, call, parser), headers);
} catch (RuntimeException | Error e) {
span.error(e);
span.finish();
throw e;
} finally {
scope.close();
}
// This ensures the server implementation can see the span in scope
return new ScopingServerCallListener<>(tracer, span, result, parser);
}
代码示例来源:origin: jaegertracing/spark-dependencies
@Override
public void createChildSpan(TracingWrapper<ZipkinWrapper> parent) {
operationName = UUID.randomUUID().toString().replace("-","");
if (parent == null) {
// root node we start a new trace
span = tracing.tracer().newTrace().name(operationName + "-root")
.start();
} else {
brave.Span parentClient = parent.get().tracing.tracer().newChild(parent.get().span.context())
.kind(Kind.CLIENT)
.name(operationName + "-client")
.start();
// TODO if I finish this later the span is cached
// and joined with server span and reported as a single span.
// to properly solve this we have to look into the tags.
// However there is another problem jaeger adds only one span.kind
// (even if span contains cs,cr,sr,ss)
// And it filters out core annotations, so there is no way how to find out
// that there is a dependency link in this span.
// https://github.com/jaegertracing/jaeger/issues/451
parentClient.finish();
span = tracing.tracer().joinSpan(parentClient.context())
.name(operationName + "-server")
.kind(Kind.SERVER)
.start();
}
}
代码示例来源:origin: openzipkin/brave
if (records.isEmpty() || tracing.isNoop()) return records;
long timestamp = 0L;
Map<String, Span> consumerSpansForTopic = new LinkedHashMap<>();
Span span = consumerSpansForTopic.get(topic);
if (span == null) {
span = tracing.tracer().nextSpan(extracted);
if (!span.isNoop()) {
setConsumerSpan(topic, span);
timestamp = tracing.clock(span.context()).currentTimeMicroseconds();
span.start(timestamp);
injector.inject(span.context(), record.headers());
} else { // we extracted request-scoped data, so cannot share a consumer span.
Span span = tracing.tracer().nextSpan(extracted);
if (!span.isNoop()) {
setConsumerSpan(topic, span);
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
SpanSubscriber(Subscriber<? super T> subscriber, Context ctx, Tracing tracing,
String name) {
this.subscriber = subscriber;
this.tracer = tracing.tracer();
Span root = ctx.getOrDefault(Span.class, this.tracer.currentSpan());
if (log.isTraceEnabled()) {
log.trace("Span from context [{}]", root);
}
this.rootSpan = root;
if (log.isTraceEnabled()) {
log.trace("Stored context root span [{}]", this.rootSpan);
}
this.span = root != null ? this.tracer
.nextSpan(TraceContextOrSamplingFlags.create(root.context())).name(name)
: this.tracer.nextSpan().name(name);
if (log.isTraceEnabled()) {
log.trace("Created span [{}], with name [{}]", this.span, name);
}
this.context = ctx.put(Span.class, this.span);
}
代码示例来源:origin: xjdr/xio
@Override
public void channelRead0(ChannelHandlerContext ctx, Request request) throws Exception {
ByteBuf content =
Unpooled.copiedBuffer("Here is the default content that is returned", CharsetUtil.UTF_8);
HttpResponseStatus status = OK;
Tracer tracer = httpTracing.tracing().tracer();
if (request.endOfMessage()) {
request
.httpTraceInfo()
.getSpan()
.ifPresent(
parent -> {
if (parent instanceof brave.Span) {
Span span =
tracer.newChild(((brave.Span) parent).context()).name("child").start();
span.finish();
}
});
val response =
DefaultFullResponse.builder()
.status(status)
.headers(new DefaultHeaders())
.httpTraceInfo(request.httpTraceInfo())
.status(status)
.body(content)
.build();
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!