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();
}
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();
}
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);
}
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);
}
}
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);
}
}
Aggregations