本文整理汇总了Java中brave.Tracer.SpanInScope类的典型用法代码示例。如果您正苦于以下问题:Java SpanInScope类的具体用法?Java SpanInScope怎么用?Java SpanInScope使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpanInScope类属于brave.Tracer包,在下文中一共展示了SpanInScope类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runFilter
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Override
public ZuulFilterResult runFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
Span span = clientHandler.handleSend(injector, ctx);
saveHeadersAsInvocationContext(ctx, span);
SpanInScope scope = tracer.withSpanInScope(span);
log.debug("Generated tracing span {} for {}", span, ctx.getRequest().getMethod());
ctx.getRequest().setAttribute(SpanInScope.class.getName(), scope);
ZuulFilterResult result = super.runFilter();
log.debug("Result of Zuul filter is [{}]", result.getStatus());
if (ExecutionStatus.SUCCESS != result.getStatus()) {
log.debug("The result of Zuul filter execution was not successful thus will close the current span {}", span);
clientHandler.handleReceive(ctx.getResponse(), result.getException(), span);
scope.close();
}
return result;
}
示例2: handle
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
Span span = tracingDelegate.createSpan(invocation);
try (SpanInScope scope = tracer.tracer().withSpanInScope(span)) {
LOGGER.debug("{}: Generated tracing span for {}",
tracingDelegate.name(),
invocation.getOperationName());
invocation.next(onResponse(invocation, asyncResp, span));
} catch (Exception e) {
LOGGER.debug("{}: Failed invocation on {}",
tracingDelegate.name(),
invocation.getOperationName(),
e);
tracingDelegate.onResponse(span, null, e);
throw e;
}
}
示例3: execute
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Override
public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception {
Span span = tracer.nextSpan();
injector.inject(span.context(), req.headers());
// For no-op spans, we only need to inject into headers and don't set any other attributes.
if (span.isNoop()) {
return delegate().execute(ctx, req);
}
final String method = ctx.method().name();
span.kind(Kind.CLIENT).name(method).start();
SpanContextUtil.setupContext(ctx, span, tracer);
ctx.log().addListener(log -> finishSpan(span, log), RequestLogAvailability.COMPLETE);
try (SpanInScope ignored = tracer.withSpanInScope(span)) {
return delegate().execute(ctx, req);
}
}
示例4: serve
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
TraceContextOrSamplingFlags contextOrFlags = extractor.extract(req.headers());
Span span = contextOrFlags.context() != null ? tracer.joinSpan(contextOrFlags.context())
: tracer.newTrace(contextOrFlags.samplingFlags());
// For no-op spans, nothing special to do.
if (span.isNoop()) {
return delegate().serve(ctx, req);
}
final String method = ctx.method().name();
span.kind(Kind.SERVER).name(method).start();
SpanContextUtil.setupContext(ctx, span, tracer);
ctx.log().addListener(log -> closeSpan(span, log), RequestLogAvailability.COMPLETE);
try (SpanInScope ignored = tracer.withSpanInScope(span)) {
return delegate().serve(ctx, req);
}
}
示例5: subsequentChildrenNestProperly_BraveStyle
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Test public void subsequentChildrenNestProperly_BraveStyle() {
// this test is semantically identical to subsequentChildrenNestProperly_OTStyle, but uses
// the Brave API instead of the OpenTracing API.
Long shouldBeIdOfSpanA;
Long idOfSpanB;
Long shouldBeIdOfSpanB;
Long parentIdOfSpanB;
Long parentIdOfSpanC;
Span spanA = brave.tracer().newTrace().name("spanA").start();
Long idOfSpanA = spanA.context().spanId();
try (SpanInScope scopeA = brave.tracer().withSpanInScope(spanA)) {
Span spanB = brave.tracer().newChild(spanA.context()).name("spanB").start();
idOfSpanB = spanB.context().spanId();
parentIdOfSpanB = spanB.context().parentId();
try (SpanInScope scopeB = brave.tracer().withSpanInScope(spanB)) {
shouldBeIdOfSpanB = brave.currentTraceContext().get().spanId();
} finally {
spanB.finish();
}
shouldBeIdOfSpanA = brave.currentTraceContext().get().spanId();
Span spanC = brave.tracer().newChild(spanA.context()).name("spanC").start();
parentIdOfSpanC = spanC.context().parentId();
try (SpanInScope scopeC = brave.tracer().withSpanInScope(spanC)) {
// nothing to do here
} finally {
spanC.finish();
}
} finally {
spanA.finish();
}
assertEquals("SpanA should have been active again after closing B", idOfSpanA,
shouldBeIdOfSpanA);
assertEquals("SpanB should have been active prior to its closure", idOfSpanB,
shouldBeIdOfSpanB);
assertEquals("SpanB's parent should be SpanA", idOfSpanA, parentIdOfSpanB);
assertEquals("SpanC's parent should be SpanA", idOfSpanA, parentIdOfSpanC);
}
示例6: run
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
((SpanInScope) context.getRequest().getAttribute(SpanInScope.class.getName())).close();
clientHandler.handleReceive(context.getResponse(), null, tracer.currentSpan());
log.debug("Closed span {} for {}", tracer.currentSpan(), context.getRequest().getMethod());
return null;
}
示例7: invoke
import brave.Tracer.SpanInScope; //导入依赖的package包/类
<T> T invoke(String spanName, String path, ThrowableSupplier<T> supplier) throws Throwable {
Span span = createSpan(spanName, path);
try (SpanInScope spanInScope = tracer.withSpanInScope(span)) {
return supplier.get();
} catch (Throwable throwable) {
span.tag("error", throwable.getClass().getSimpleName() + ": " + throwable.getMessage());
throw throwable;
} finally {
span.finish();
}
}
示例8: startsNewChildSpan
import brave.Tracer.SpanInScope; //导入依赖的package包/类
@Test
public void startsNewChildSpan() throws Exception {
CyclicBarrier cyclicBarrier = new CyclicBarrier(nThreads);
CompletableFuture<?>[] futures = new CompletableFuture[nThreads];
for (int i = 0; i < nThreads; i++) {
futures[i] = CompletableFuture.runAsync(() -> {
Span currentSpan = tracing.tracer().newTrace().start();
waitTillAllAreReady(cyclicBarrier);
try (SpanInScope spanInScope = tracing.tracer().withSpanInScope(currentSpan)) {
assertThat(tracingAdviser.invoke(spanName, path, supplier), is(expected));
} catch (Throwable throwable) {
fail(throwable.getMessage());
} finally {
currentSpan.finish();
}
}, Executors.newFixedThreadPool(nThreads));
}
CompletableFuture.allOf(futures).join();
assertThat(traces.size(), is(nThreads));
for (Queue<zipkin2.Span> queue : traces.values()) {
zipkin2.Span child = queue.poll();
assertThat(child.name(), is(spanName));
zipkin2.Span parent = queue.poll();
assertThat(child.parentId(), is(parent.id()));
assertThat(child.traceId(), is(parent.traceId()));
assertThat(tracedValues(child), contains(this.getClass().getCanonicalName()));
}
}
示例9: BraveScope
import brave.Tracer.SpanInScope; //导入依赖的package包/类
/**
* @param source the BraveActiveSpanSource that created this BraveActiveSpan
* @param scope a SpanInScope to be closed upon deactivation of this ActiveSpan
* @param wrapped the wrapped BraveSpan to which we will delegate all span operations
*/
BraveScope(BraveScopeManager source, SpanInScope scope, BraveSpan wrapped,
boolean finishSpanOnClose) {
this.source = source;
this.scope = scope;
this.wrapped = wrapped;
this.finishSpanOnClose = finishSpanOnClose;
}
示例10: setupContext
import brave.Tracer.SpanInScope; //导入依赖的package包/类
/**
* Sets up the {@link RequestContext} to push and pop the {@link Span} whenever it is entered/exited.
*/
public static void setupContext(RequestContext ctx, Span span, Tracer tracer) {
ctx.onEnter(unused -> SPAN_IN_THREAD.set(tracer.withSpanInScope(span)));
ctx.onExit(unused -> {
SpanInScope spanInScope = SPAN_IN_THREAD.get();
if (spanInScope != null) {
spanInScope.close();
SPAN_IN_THREAD.remove();
}
});
}