Search in sources :

Example 1 with TraceSegmentRef

use of org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef in project incubator-skywalking by apache.

the class SkywalkingSpanActivationTest method testExtractWithValidateContext.

@Test
public void testExtractWithValidateContext() throws Throwable {
    spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), 8080);
    startSpan();
    final Map<String, String> values = new HashMap<String, String>();
    TextMap carrier = new TextMap() {

        @Override
        public Iterator<Map.Entry<String, String>> iterator() {
            return values.entrySet().iterator();
        }

        @Override
        public void put(String key, String value) {
            values.put(key, value);
        }
    };
    values.put(SW3CarrierItem.HEADER_NAME, "1.343.222|3|1|1|#127.0.0.1:8080|#/portal/|#/testEntrySpan|434.12.12123");
    extractInterceptor.afterMethod(enhancedInstance, null, new Object[] { Format.Builtin.TEXT_MAP, carrier }, new Class[] {}, null);
    stopSpan();
    TraceSegment tracingSegment = assertTraceSemgnets();
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
    assertThat(tracingSegment.getRefs().size(), is(1));
    TraceSegmentRef ref = tracingSegment.getRefs().get(0);
    SegmentRefAssert.assertSegmentId(ref, "1.343.222");
    SegmentRefAssert.assertSpanId(ref, 3);
    SegmentRefAssert.assertEntryApplicationInstanceId(ref, 1);
    SegmentRefAssert.assertPeerHost(ref, "127.0.0.1:8080");
    assertThat(spans.size(), is(1));
    assertSpanCommonsAttribute(spans.get(0));
}
Also used : HashMap(java.util.HashMap) TraceSegmentRef(org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef) AbstractTracingSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan) TextMap(io.opentracing.propagation.TextMap) TraceSegment(org.apache.skywalking.apm.agent.core.context.trace.TraceSegment) Test(org.junit.Test)

Example 2 with TraceSegmentRef

use of org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef in project incubator-skywalking by apache.

the class KafkaConsumerInterceptorTest method testConsumerWithMessage.

@Test
public void testConsumerWithMessage() throws Throwable {
    consumerInterceptor.beforeMethod(consumerInstance, null, new Object[0], new Class[0], null);
    consumerInterceptor.afterMethod(consumerInstance, null, new Object[0], new Class[0], messages);
    List<TraceSegment> traceSegments = segmentStorage.getTraceSegments();
    assertThat(traceSegments.size(), is(1));
    TraceSegment traceSegment = traceSegments.get(0);
    List<TraceSegmentRef> refs = traceSegment.getRefs();
    assertThat(refs.size(), is(1));
    assertTraceSegmentRef(refs.get(0));
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
    assertThat(spans.size(), is(1));
    assertConsumerSpan(spans.get(0));
}
Also used : TraceSegmentRef(org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef) AbstractTracingSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan) TraceSegment(org.apache.skywalking.apm.agent.core.context.trace.TraceSegment) Test(org.junit.Test)

Example 3 with TraceSegmentRef

use of org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef in project incubator-skywalking by apache.

the class TracingContext method capture.

/**
 * Capture the snapshot of current context.
 *
 * @return the snapshot of context for cross-thread propagation
 * Ref to {@link AbstractTracerContext#capture()}
 */
@Override
public ContextSnapshot capture() {
    List<TraceSegmentRef> refs = this.segment.getRefs();
    ContextSnapshot snapshot = new ContextSnapshot(segment.getTraceSegmentId(), activeSpan().getSpanId(), segment.getRelatedGlobalTraces());
    int entryOperationId;
    String entryOperationName;
    int entryApplicationInstanceId;
    AbstractSpan firstSpan = first();
    if (refs != null && refs.size() > 0) {
        TraceSegmentRef ref = refs.get(0);
        entryOperationId = ref.getEntryOperationId();
        entryOperationName = ref.getEntryOperationName();
        entryApplicationInstanceId = ref.getEntryApplicationInstanceId();
    } else {
        entryOperationId = firstSpan.getOperationId();
        entryOperationName = firstSpan.getOperationName();
        entryApplicationInstanceId = this.segment.getApplicationInstanceId();
    }
    snapshot.setEntryApplicationInstanceId(entryApplicationInstanceId);
    if (entryOperationId == DictionaryUtil.nullValue()) {
        snapshot.setEntryOperationName(entryOperationName);
    } else {
        snapshot.setEntryOperationId(entryOperationId);
    }
    if (firstSpan.getOperationId() == DictionaryUtil.nullValue()) {
        snapshot.setParentOperationName(firstSpan.getOperationName());
    } else {
        snapshot.setParentOperationId(firstSpan.getOperationId());
    }
    return snapshot;
}
Also used : TraceSegmentRef(org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 4 with TraceSegmentRef

use of org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef in project incubator-skywalking by apache.

the class TracingContext method inject.

/**
 * Inject the context into the given carrier, only when the active span is an exit one.
 *
 * @param carrier to carry the context for crossing process.
 * @throws IllegalStateException  if the active span isn't an exit one.
 * Ref to {@link AbstractTracerContext#inject(ContextCarrier)}
 */
@Override
public void inject(ContextCarrier carrier) {
    AbstractSpan span = this.activeSpan();
    if (!span.isExit()) {
        throw new IllegalStateException("Inject can be done only in Exit Span");
    }
    WithPeerInfo spanWithPeer = (WithPeerInfo) span;
    String peer = spanWithPeer.getPeer();
    int peerId = spanWithPeer.getPeerId();
    carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
    carrier.setSpanId(span.getSpanId());
    carrier.setParentApplicationInstanceId(segment.getApplicationInstanceId());
    if (DictionaryUtil.isNull(peerId)) {
        carrier.setPeerHost(peer);
    } else {
        carrier.setPeerId(peerId);
    }
    List<TraceSegmentRef> refs = this.segment.getRefs();
    int operationId;
    String operationName;
    int entryApplicationInstanceId;
    if (refs != null && refs.size() > 0) {
        TraceSegmentRef ref = refs.get(0);
        operationId = ref.getEntryOperationId();
        operationName = ref.getEntryOperationName();
        entryApplicationInstanceId = ref.getEntryApplicationInstanceId();
    } else {
        AbstractSpan firstSpan = first();
        operationId = firstSpan.getOperationId();
        operationName = firstSpan.getOperationName();
        entryApplicationInstanceId = this.segment.getApplicationInstanceId();
    }
    carrier.setEntryApplicationInstanceId(entryApplicationInstanceId);
    if (operationId == DictionaryUtil.nullValue()) {
        carrier.setEntryOperationName(operationName);
    } else {
        carrier.setEntryOperationId(operationId);
    }
    int parentOperationId = first().getOperationId();
    if (parentOperationId == DictionaryUtil.nullValue()) {
        carrier.setParentOperationName(first().getOperationName());
    } else {
        carrier.setParentOperationId(parentOperationId);
    }
    carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces());
}
Also used : WithPeerInfo(org.apache.skywalking.apm.agent.core.context.trace.WithPeerInfo) TraceSegmentRef(org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 5 with TraceSegmentRef

use of org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef in project incubator-skywalking by apache.

the class TracingContext method continued.

/**
 * Continue the context from the given snapshot of parent thread.
 *
 * @param snapshot from {@link #capture()} in the parent thread.
 * Ref to {@link AbstractTracerContext#continued(ContextSnapshot)}
 */
@Override
public void continued(ContextSnapshot snapshot) {
    TraceSegmentRef segmentRef = new TraceSegmentRef(snapshot);
    this.segment.ref(segmentRef);
    this.activeSpan().ref(segmentRef);
    this.segment.relatedGlobalTraces(snapshot.getDistributedTraceId());
}
Also used : TraceSegmentRef(org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef)

Aggregations

TraceSegmentRef (org.apache.skywalking.apm.agent.core.context.trace.TraceSegmentRef)8 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)4 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)3 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)3 Test (org.junit.Test)2 TextMap (io.opentracing.propagation.TextMap)1 HashMap (java.util.HashMap)1 EntrySpan (org.apache.skywalking.apm.agent.core.context.trace.EntrySpan)1 LogDataEntity (org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity)1 WithPeerInfo (org.apache.skywalking.apm.agent.core.context.trace.WithPeerInfo)1