use of io.opencensus.trace.SpanBuilder in project instrumentation-java by census-instrumentation.
the class HelloWorldServer method performWork.
// A helper function that performs some work in its own Span.
private static void performWork(Span parent) {
SpanBuilder spanBuilder = tracer.spanBuilderWithExplicitParent("internal_work", parent).setRecordEvents(true);
try (Scope scope = spanBuilder.startScopedSpan()) {
Span span = tracer.getCurrentSpan();
span.putAttribute("my_attribute", AttributeValue.stringAttributeValue("blue"));
span.addAnnotation("Performing work.");
// Working hard here.
sleepFor(20);
span.addAnnotation("Done work.");
}
}
use of io.opencensus.trace.SpanBuilder in project instrumentation-java by census-instrumentation.
the class TracerImplTest method createSpanBuilder.
@Test
public void createSpanBuilder() {
SpanBuilder spanBuilder = tracer.spanBuilderWithExplicitParent(SPAN_NAME, BlankSpan.INSTANCE);
assertThat(spanBuilder).isInstanceOf(SpanBuilderImpl.class);
}
use of io.opencensus.trace.SpanBuilder in project instrumentation-java by census-instrumentation.
the class TracerImplTest method createSpanBuilderWithRemoteParet.
@Test
public void createSpanBuilderWithRemoteParet() {
SpanBuilder spanBuilder = tracer.spanBuilderWithRemoteParent(SPAN_NAME, SpanContext.INVALID);
assertThat(spanBuilder).isInstanceOf(SpanBuilderImpl.class);
}
use of io.opencensus.trace.SpanBuilder in project instrumentation-java by census-instrumentation.
the class HttpClientHandler method handleStart.
/**
* Instrument a request for tracing and stats before it is sent.
*
* <p>This method will create a span in current context to represent the HTTP call. The created
* span will be serialized and propagated to the server.
*
* <p>The generated span will NOT be set as current context. User can control when to enter the
* scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
*
* @param parent the parent {@link Span}. {@code null} indicates using current span.
* @param carrier the entity that holds the HTTP information.
* @param request the request entity.
* @return the {@link HttpRequestContext} that contains stats and trace data associated with the
* request.
* @since 0.19
*/
public HttpRequestContext handleStart(@Nullable Span parent, C carrier, Q request) {
checkNotNull(carrier, "carrier");
checkNotNull(request, "request");
if (parent == null) {
parent = tracer.getCurrentSpan();
}
String spanName = getSpanName(request, extractor);
SpanBuilder builder = tracer.spanBuilderWithExplicitParent(spanName, parent);
Span span = builder.setSpanKind(Kind.CLIENT).startSpan();
if (span.getOptions().contains(Options.RECORD_EVENTS)) {
addSpanRequestAttributes(span, request, extractor);
}
// inject propagation header
SpanContext spanContext = span.getContext();
if (!spanContext.equals(SpanContext.INVALID)) {
textFormat.inject(spanContext, carrier, setter);
}
return getNewContext(span, tagger.getCurrentTagContext());
}
use of io.opencensus.trace.SpanBuilder in project instrumentation-java by census-instrumentation.
the class HttpServerHandler method handleStart.
/**
* Instrument an incoming request before it is handled.
*
* <p>This method will create a span under the deserialized propagated parent context. If the
* parent context is not present, the span will be created under the current context.
*
* <p>The generated span will NOT be set as current context. User can control when to enter the
* scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
*
* @param carrier the entity that holds the HTTP information.
* @param request the request entity.
* @return the {@link HttpRequestContext} that contains stats and trace data associated with the
* request.
* @since 0.19
*/
public HttpRequestContext handleStart(C carrier, Q request) {
checkNotNull(carrier, "carrier");
checkNotNull(request, "request");
SpanBuilder spanBuilder = null;
String spanName = getSpanName(request, extractor);
// de-serialize the context
SpanContext spanContext = null;
try {
spanContext = textFormat.extract(carrier, getter);
} catch (SpanContextParseException e) {
// TODO: Currently we cannot distinguish between context parse error and missing context.
// Logging would be annoying so we just ignore this error and do not even log a message.
}
if (spanContext == null || publicEndpoint) {
spanBuilder = tracer.spanBuilder(spanName);
} else {
spanBuilder = tracer.spanBuilderWithRemoteParent(spanName, spanContext);
}
Span span = spanBuilder.setSpanKind(Kind.SERVER).startSpan();
if (publicEndpoint && spanContext != null) {
span.addLink(Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN));
}
if (span.getOptions().contains(Options.RECORD_EVENTS)) {
addSpanRequestAttributes(span, request, extractor);
}
return getNewContext(span, tagger.getCurrentTagContext());
}
Aggregations