use of io.opencensus.trace.Span in project grpc-java by grpc.
the class AbstractInteropTest method censusContextsPropagated.
@Test(timeout = 10000)
public void censusContextsPropagated() {
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
Assume.assumeTrue(customCensusModulePresent());
Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
assertTrue(clientParentSpan.getContext().getTraceId().isValid());
Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
ctx = ContextUtils.withValue(ctx, clientParentSpan);
Context origCtx = ctx.attach();
try {
blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
Context serverCtx = contextCapture.get();
assertNotNull(serverCtx);
FakeTagContext statsCtx = (FakeTagContext) io.opencensus.tags.unsafe.ContextUtils.getValue(serverCtx);
assertNotNull(statsCtx);
Map<TagKey, TagValue> tags = statsCtx.getTags();
boolean tagFound = false;
for (Map.Entry<TagKey, TagValue> tag : tags.entrySet()) {
if (tag.getKey().equals(StatsTestUtils.EXTRA_TAG)) {
assertEquals(TagValue.create("extra value"), tag.getValue());
tagFound = true;
}
}
assertTrue("tag not found", tagFound);
Span span = ContextUtils.getValue(serverCtx);
assertNotNull(span);
SpanContext spanContext = span.getContext();
assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());
} finally {
ctx.detach(origCtx);
}
}
use of io.opencensus.trace.Span in project ignite by apache.
the class AbstractTracingTest method checkSpanExistences.
/**
* Checks that there's at least one span with given spanType and attributes.
*
* @param spanType Span type to be found.
* @param expAttrs Expected attributes.
* @return {@code true} if Span with given type and attributes was found, false otherwise.
*/
boolean checkSpanExistences(SpanType spanType, /* tagName: tagValue*/
Map<String, String> expAttrs) {
java.util.List<SpanData> gotSpans = hnd.allSpans().filter(span -> spanType.spanName().equals(span.getName())).collect(Collectors.toList());
for (SpanData specificTypeSpans : gotSpans) {
Map<String, AttributeValue> attrs = specificTypeSpans.getAttributes().getAttributeMap();
boolean matchFound = true;
for (Map.Entry<String, String> entry : expAttrs.entrySet()) {
if (!entry.getValue().equals(attributeValueToString(attrs.get(entry.getKey())))) {
matchFound = false;
break;
}
}
if (matchFound && expAttrs.size() == attrs.size())
return true;
}
return false;
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class JaxrsClientFilterTest method testResponseFilter.
@Test
public void testResponseFilter() throws Exception {
Span span = new FakeSpan(SpanContext.INVALID, null);
TagContext tagContext = mock(TagContext.class);
HttpRequestContext context = createHttpRequestContext(span, tagContext);
ClientRequestContext requestContext = mock(ClientRequestContext.class);
when(requestContext.getProperty("opencensus.context")).thenReturn(context);
ClientResponseContext responseContext = mock(ClientResponseContext.class);
filter.filter(requestContext, responseContext);
verify(requestContext).getProperty("opencensus.context");
verify(responseContext, times(1)).getStatus();
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class JaxrsContainerFilterTest method testResponseFilter.
@Test
public void testResponseFilter() throws Exception {
Span span = new FakeSpan(SpanContext.INVALID, null);
TagContext tagContext = mock(TagContext.class);
HttpRequestContext context = JaxrsClientFilterTest.createHttpRequestContext(span, tagContext);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getMatchedURIs()).thenReturn(Collections.singletonList("/resource/{route}"));
ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
when(requestContext.getProperty("opencensus.context")).thenReturn(context);
when(requestContext.getUriInfo()).thenReturn(uriInfo);
ContainerResponseContext responseContext = mock(ContainerResponseContext.class);
filter.filter(requestContext, responseContext);
verify(requestContext).getProperty("opencensus.context");
verify(responseContext, times(1)).getStatus();
}
use of io.opencensus.trace.Span 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