Search in sources :

Example 21 with Span

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);
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) SpanContext(io.opencensus.trace.SpanContext) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue) Span(io.opencensus.trace.Span) Map(java.util.Map) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) Test(org.junit.Test)

Example 22 with Span

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;
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) EXCHANGE(org.apache.ignite.spi.tracing.Scope.EXCHANGE) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) BeforeClass(org.junit.BeforeClass) AttributeValue(io.opencensus.trace.AttributeValue) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) SpanData(io.opencensus.trace.export.SpanData) Samplers(io.opencensus.trace.samplers.Samplers) ArrayList(java.util.ArrayList) Map(java.util.Map) After(org.junit.After) SpanId(io.opencensus.trace.SpanId) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) LinkedList(java.util.LinkedList) Tracing(io.opencensus.trace.Tracing) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) COMMUNICATION(org.apache.ignite.spi.tracing.Scope.COMMUNICATION) Before(org.junit.Before) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Scope(org.apache.ignite.spi.tracing.Scope) OpenCensusTraceExporter(org.apache.ignite.spi.tracing.opencensus.OpenCensusTraceExporter) Collectors(java.util.stream.Collectors) Span(io.opencensus.trace.Span) SpanExporter(io.opencensus.trace.export.SpanExporter) TX(org.apache.ignite.spi.tracing.Scope.TX) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) TracingSpi(org.apache.ignite.spi.tracing.TracingSpi) Functions(io.opencensus.common.Functions) Collections(java.util.Collections) AttributeValue.stringAttributeValue(io.opencensus.trace.AttributeValue.stringAttributeValue) TracingConfigurationManager(org.apache.ignite.spi.tracing.TracingConfigurationManager) AttributeValue(io.opencensus.trace.AttributeValue) AttributeValue.stringAttributeValue(io.opencensus.trace.AttributeValue.stringAttributeValue) SpanData(io.opencensus.trace.export.SpanData) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 23 with Span

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();
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) TagContext(io.opencensus.tags.TagContext) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) Span(io.opencensus.trace.Span) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Test(org.junit.Test)

Example 24 with Span

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();
}
Also used : FakeSpan(io.opencensus.contrib.http.jaxrs.JaxrsClientFilterTest.FakeSpan) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) TagContext(io.opencensus.tags.TagContext) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) FakeSpan(io.opencensus.contrib.http.jaxrs.JaxrsClientFilterTest.FakeSpan) Span(io.opencensus.trace.Span) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 25 with Span

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

Aggregations

Span (io.opencensus.trace.Span)47 Test (org.junit.Test)17 Benchmark (org.openjdk.jmh.annotations.Benchmark)14 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)14 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)14 Scope (io.opencensus.common.Scope)8 BlankSpan (io.opencensus.trace.BlankSpan)8 ArrayList (java.util.ArrayList)4 SpanBuilder (io.opencensus.trace.SpanBuilder)3 SpanContext (io.opencensus.trace.SpanContext)3 ApiException (com.google.api.gax.rpc.ApiException)2 ByteString (com.google.protobuf.ByteString)2 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)2 Metric (io.opencensus.metrics.export.Metric)2 TagContext (io.opencensus.tags.TagContext)2 AttributeValue (io.opencensus.trace.AttributeValue)2 SpanId (io.opencensus.trace.SpanId)2 TraceId (io.opencensus.trace.TraceId)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 TraceParams (io.opencensus.trace.config.TraceParams)2