Search in sources :

Example 1 with SpanBuilder

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.");
    }
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Scope(io.opencensus.common.Scope) Span(io.opencensus.trace.Span)

Example 2 with SpanBuilder

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);
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Test(org.junit.Test)

Example 3 with SpanBuilder

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);
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) Test(org.junit.Test)

Example 4 with SpanBuilder

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());
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) SpanContext(io.opencensus.trace.SpanContext) Span(io.opencensus.trace.Span)

Example 5 with SpanBuilder

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());
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) SpanContext(io.opencensus.trace.SpanContext) SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) Span(io.opencensus.trace.Span)

Aggregations

SpanBuilder (io.opencensus.trace.SpanBuilder)10 Scope (io.opencensus.common.Scope)6 Span (io.opencensus.trace.Span)3 Test (org.junit.Test)3 SpanContext (io.opencensus.trace.SpanContext)2 Tracer (io.opencensus.trace.Tracer)2 Random (java.util.Random)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpResponse (com.google.api.client.http.HttpResponse)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 MeasureMap (io.opencensus.stats.MeasureMap)1 ViewData (io.opencensus.stats.ViewData)1 TagContextBuilder (io.opencensus.tags.TagContextBuilder)1 SpanContextParseException (io.opencensus.trace.propagation.SpanContextParseException)1 IOException (java.io.IOException)1