Search in sources :

Example 11 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class ScopedTagMapTest method withTagContext.

@Test
public void withTagContext() {
    assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
    TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1).build();
    Scope scope = tagger.withTagContext(scopedTags);
    try {
        assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
    } finally {
        scope.close();
    }
    assertThat(tagContextToList(tagger.getCurrentTagContext())).isEmpty();
}
Also used : Scope(io.opencensus.common.Scope) TagContext(io.opencensus.tags.TagContext) Test(org.junit.Test)

Example 12 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class CurrentTagMapUtilsTest method testWithTagMap.

@Test
public void testWithTagMap() {
    assertThat(tagContextToList(CurrentTagMapUtils.getCurrentTagMap())).isEmpty();
    Scope scopedTags = CurrentTagMapUtils.withTagMap(tagContext);
    try {
        assertThat(CurrentTagMapUtils.getCurrentTagMap()).isSameInstanceAs(tagContext);
    } finally {
        scopedTags.close();
    }
    assertThat(tagContextToList(CurrentTagMapUtils.getCurrentTagMap())).isEmpty();
}
Also used : Scope(io.opencensus.common.Scope) Test(org.junit.Test)

Example 13 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class JaxrsContainerFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    HttpRequestContext context = (HttpRequestContext) requestContext.getProperty(CONTEXT_PROPERTY);
    if (context == null) {
        // request came through this filter
        return;
    }
    Scope scope = (Scope) requestContext.getProperty(SPAN_PROPERTY);
    if (scope != null) {
        scope.close();
    }
    if (responseContext.getLength() > 0) {
        handler.handleMessageSent(context, responseContext.getLength());
    }
    ExtendedContainerRequest extendedRequest = new ExtendedContainerRequest(requestContext, info);
    handler.handleEnd(context, extendedRequest, responseContext, null);
}
Also used : Scope(io.opencensus.common.Scope) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext)

Example 14 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class OcHttpServletFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // only interested in http requests
    if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpResp = (HttpServletResponse) response;
        HttpRequestContext context = handler.handleStart(httpReq, httpReq);
        OcHttpServletListener listener = new OcHttpServletListener(handler, context);
        httpReq.setAttribute(OcHttpServletUtil.OPENCENSUS_SERVLET_LISTENER, listener);
        int length = httpReq.getContentLength();
        if (length > 0) {
            handler.handleMessageReceived(context, length);
        }
        Scope scope = Tracing.getTracer().withSpan(handler.getSpanFromContext(context));
        try {
            chain.doFilter(httpReq, httpResp);
        } finally {
            scope.close();
        }
        if (httpReq.isAsyncStarted()) {
            AsyncContext async = httpReq.getAsyncContext();
            async.addListener(listener, httpReq, httpResp);
        } else {
            OcHttpServletUtil.recordMessageSentEvent(handler, context, httpResp);
            handler.handleEnd(context, httpReq, httpResp, null);
        }
    } else {
        // pass request through unchanged
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Scope(io.opencensus.common.Scope) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) AsyncContext(javax.servlet.AsyncContext)

Example 15 with Scope

use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandler method export.

@Override
public void export(Collection<SpanData> spanDataList) {
    // Start a new span with explicit 1/10000 sampling probability to avoid the case when user
    // sets the default sampler to always sample and we get the gRPC span of the stackdriver
    // export call always sampled and go to an infinite loop.
    io.opencensus.trace.Span span = tracer.spanBuilder(EXPORT_STACKDRIVER_TRACES).setSampler(probabilitySampler).setRecordEvents(true).startSpan();
    Scope scope = tracer.withSpan(span);
    try {
        List<Span> spans = new ArrayList<>(spanDataList.size());
        for (SpanData spanData : spanDataList) {
            spans.add(generateSpan(spanData, RESOURCE_LABELS, fixedAttributes));
        }
        // Sync call because it is already called for a batch of data, and on a separate thread.
        // TODO(bdrutu): Consider to make this async in the future.
        traceServiceClient.batchWriteSpans(projectName, spans);
    } finally {
        scope.close();
        span.end(END_SPAN_OPTIONS);
    }
}
Also used : Scope(io.opencensus.common.Scope) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Span(com.google.devtools.cloudtrace.v2.Span)

Aggregations

Scope (io.opencensus.common.Scope)48 Test (org.junit.Test)18 Span (io.opencensus.trace.Span)8 Timestamp (com.google.cloud.Timestamp)5 TagContext (io.opencensus.tags.TagContext)5 SpanBuilder (io.opencensus.trace.SpanBuilder)5 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)5 ArrayList (java.util.ArrayList)4 Instant (org.joda.time.Instant)4 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)3 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)2 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)2 ByteString (com.google.protobuf.ByteString)2 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)2 MeasureMap (io.opencensus.stats.MeasureMap)2 TagContextBuilder (io.opencensus.tags.TagContextBuilder)2 AttributeValue (io.opencensus.trace.AttributeValue)2 Tracer (io.opencensus.trace.Tracer)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 HashMap (java.util.HashMap)2