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