Skip to content

Commit

Permalink
Merge pull request #1953 from emeroad/TraceV2/TraceFormatV2
Browse files Browse the repository at this point in the history
#1819 trace format v2
  • Loading branch information
emeroad authored Aug 2, 2016
2 parents 2df3040 + 31bae0a commit 25b1b7b
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public SpanBo buildSpanBo(TSpan tSpan) {
return spanBo;
}

private SpanBo newSpanBo(TSpan tSpan) {
// for test
SpanBo newSpanBo(TSpan tSpan) {
final SpanBo spanBo = new SpanBo();
spanBo.setAgentId(tSpan.getAgentId());
spanBo.setApplicationId(tSpan.getApplicationName());
Expand Down Expand Up @@ -123,8 +124,8 @@ private SpanBo newSpanBo(TSpan tSpan) {
}



private SpanEventBo newSpanEventBo(BasicSpan basicSpan, TSpanEvent tSpanEvent) {
// for test
SpanEventBo newSpanEventBo(BasicSpan basicSpan, TSpanEvent tSpanEvent) {
if (basicSpan == null) {
throw new NullPointerException("basicSpan must not be null");
}
Expand Down Expand Up @@ -198,6 +199,21 @@ private void bind(SpanEventBo spanEvent, TSpanEvent tSpanEvent) {


public SpanChunkBo buildSpanChunkBo(TSpanChunk tSpanChunk) {
final SpanChunkBo spanChunkBo = newSpanChunkBo(tSpanChunk);

List<TSpanEvent> spanEventList = tSpanChunk.getSpanEventList();
List<SpanEventBo> spanEventBoList = buildSpanEventBoList(spanChunkBo, spanEventList);
spanChunkBo.addSpanEventBoList(spanEventBoList);


long acceptedTime = acceptedTimeService.getAcceptedTime();
spanChunkBo.setCollectorAcceptTime(acceptedTime);

return spanChunkBo;
}

// for test
SpanChunkBo newSpanChunkBo(TSpanChunk tSpanChunk) {
final SpanChunkBo spanChunkBo = new SpanChunkBo();
spanChunkBo.setAgentId(tSpanChunk.getAgentId());
spanChunkBo.setApplicationId(tSpanChunk.getApplicationName());
Expand All @@ -223,15 +239,6 @@ public SpanChunkBo buildSpanChunkBo(TSpanChunk tSpanChunk) {

spanChunkBo.setSpanId(tSpanChunk.getSpanId());
spanChunkBo.setEndPoint(tSpanChunk.getEndPoint());

List<TSpanEvent> spanEventList = tSpanChunk.getSpanEventList();
List<SpanEventBo> spanEventBoList = buildSpanEventBoList(spanChunkBo, spanEventList);
spanChunkBo.addSpanEventBoList(spanEventBoList);


long acceptedTime = acceptedTimeService.getAcceptedTime();
spanChunkBo.setCollectorAcceptTime(acceptedTime);

return spanChunkBo;
}

Expand Down Expand Up @@ -284,6 +291,7 @@ private AnnotationBo newAnnotationBo(TAnnotation tAnnotation) {
byte typeCode = transcoder.getTypeCode(value);
byte[] encodeObject = transcoder.encode(value, typeCode);

annotationBo.setValue(value);
annotationBo.setValueType(typeCode);
annotationBo.setByteValue(encodeObject);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.navercorp.pinpoint.common.server.bo;

import com.navercorp.pinpoint.common.util.TransactionIdUtils;
import com.navercorp.pinpoint.thrift.dto.TAnnotation;
import com.navercorp.pinpoint.thrift.dto.TAnnotationValue;
import com.navercorp.pinpoint.thrift.dto.TIntStringValue;
import com.navercorp.pinpoint.thrift.dto.TSpan;
import com.navercorp.pinpoint.thrift.dto.TSpanChunk;
import com.navercorp.pinpoint.thrift.dto.TSpanEvent;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author Woonduk Kang(emeroad)
*/
public class RandomTSpan {

private final Random random = new Random();

public TSpan randomTSpan() {
final TSpan tSpan = new TSpan();
tSpan.setAgentId("agentId");
tSpan.setApplicationName("appName");
tSpan.setAgentStartTime(System.currentTimeMillis());

tSpan.setTransactionId(TransactionIdUtils.formatByteBuffer("agent", System.currentTimeMillis(), RandomUtils.nextLong(0, Long.MAX_VALUE)));
tSpan.setSpanId(random.nextLong());
tSpan.setParentSpanId(RandomUtils.nextInt(0, 100000));
tSpan.setStartTime(System.currentTimeMillis() + RandomUtils.nextInt(0, 1000));
tSpan.setElapsed(RandomUtils.nextInt(0, 2000));
tSpan.setRpc(RandomStringUtils.random(10));

tSpan.setServiceType(randomServerServiceType());
tSpan.setEndPoint(RandomStringUtils.random(20));
tSpan.setRemoteAddr(RandomStringUtils.random(20));

List<TAnnotation> tAnnotationList = randomTAnnotationList();
if (CollectionUtils.isNotEmpty(tAnnotationList)) {
tSpan.setAnnotations(tAnnotationList);
}
tSpan.setFlag((short) RandomUtils.nextInt(0, 4));
tSpan.setErr((short) RandomUtils.nextInt(0, 1));
// tSpan.setSpanEventList()
tSpan.setParentApplicationName("parentApp");
tSpan.setParentApplicationType(randomServerServiceType());
tSpan.setAcceptorHost("acceptHost");
tSpan.setApiId(RandomUtils.nextInt(0, 5000));
tSpan.setApplicationServiceType(randomServerServiceType());
if (random.nextBoolean()) {
TIntStringValue exceptionInfo = new TIntStringValue();
exceptionInfo.setIntValue(RandomUtils.nextInt(0, 5000));
exceptionInfo.setStringValue(RandomStringUtils.random(100));
tSpan.setExceptionInfo(exceptionInfo);
}
tSpan.setLoggingTransactionInfo((byte) RandomUtils.nextInt(0, 256));
return tSpan;
}

private short randomServerServiceType() {
// Server (1000 ~ 1899)
return (short) RandomUtils.nextInt(1000, 1899);
}

public List<TAnnotation> randomTAnnotationList() {
int annotationSize = RandomUtils.nextInt(0, 3);
List<TAnnotation> result = new ArrayList<>();
for (int i = 0; i < annotationSize; i++) {
result.add(randomTAnnotation(i));
}
return result;
}

public TAnnotation randomTAnnotation(int key) {
TAnnotation tAnnotation = new TAnnotation();
// sort order
tAnnotation.setKey(key);
TAnnotationValue tAnnotationValue = new TAnnotationValue();
tAnnotationValue.setStringValue(RandomStringUtils.random(10));
tAnnotation.setValue(tAnnotationValue);
return tAnnotation;
}

public TSpanEvent randomTSpanEvent(TSpan tSpan, short sequence) {
TSpanEvent tSpanEvent = new TSpanEvent();
// @deprecated
// tSpanEvent.setSpanId();
tSpanEvent.setSequence(sequence);
tSpanEvent.setStartElapsed(RandomUtils.nextInt(0, 1000));
tSpanEvent.setEndElapsed(RandomUtils.nextInt(0, 1000));
tSpanEvent.setRpc(RandomStringUtils.random(10));
// Database (2000 ~ 2899)
tSpanEvent.setServiceType((short) RandomUtils.nextInt(2000, 2889));
tSpanEvent.setEndPoint(RandomStringUtils.random(10));

List<TAnnotation> tAnnotationList = randomTAnnotationList();
if (CollectionUtils.isNotEmpty(tAnnotationList)) {
tSpan.setAnnotations(tAnnotationList);
}
tSpanEvent.setDepth(RandomUtils.nextInt(0, 256));
tSpanEvent.setNextSpanId(random.nextLong());

tSpanEvent.setDestinationId(RandomStringUtils.random(20));
tSpanEvent.setApiId(RandomUtils.nextInt(0, 65535));

tSpanEvent.setAsyncId(randomNegative(RandomUtils.nextInt(0, 10)));
tSpanEvent.setNextAsyncId(random.nextInt());
tSpanEvent.setAsyncSequence((short) RandomUtils.nextInt(0, Short.MAX_VALUE));

return tSpanEvent;
}

private int randomNegative(int value) {
if (random.nextBoolean()) {
return -value;
}
return value;
}

public TSpanChunk randomTSpanChunk() {
final TSpanChunk tSpanChunk = new TSpanChunk();
tSpanChunk.setAgentId("agentId");
tSpanChunk.setApplicationName("appName");
tSpanChunk.setAgentStartTime(System.currentTimeMillis());

tSpanChunk.setTransactionId(TransactionIdUtils.formatByteBuffer("agent", System.currentTimeMillis(), RandomUtils.nextLong(0, Long.MAX_VALUE)));
tSpanChunk.setSpanId(random.nextLong());

tSpanChunk.setServiceType(randomServerServiceType());
tSpanChunk.setEndPoint(RandomStringUtils.random(20));

// tSpanChunk.setSpanEventList()
tSpanChunk.setApplicationServiceType(randomServerServiceType());
return tSpanChunk;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.navercorp.pinpoint.common.server.bo;

import com.navercorp.pinpoint.common.util.TransactionIdUtils;
import com.navercorp.pinpoint.thrift.dto.TAnnotation;
import com.navercorp.pinpoint.thrift.dto.TSpan;
import com.navercorp.pinpoint.thrift.dto.TSpanChunk;
import com.navercorp.pinpoint.thrift.dto.TSpanEvent;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Assert;

import java.nio.ByteBuffer;
import java.util.List;

/**
* @author Woonduk Kang(emeroad)
*/
public class SpanFactoryAssert {

public void assertSpan(TSpan tSpan, SpanBo spanBo) {
Assert.assertEquals(tSpan.getAgentId(), spanBo.getAgentId());
Assert.assertEquals(tSpan.getApplicationName(), spanBo.getApplicationId());
Assert.assertEquals(tSpan.getAgentStartTime(), spanBo.getAgentStartTime());


ByteBuffer byteBuffer = TransactionIdUtils.formatByteBuffer(spanBo.getTraceAgentId(), spanBo.getTraceAgentStartTime(), spanBo.getTraceTransactionSequence());
Assert.assertEquals(ByteBuffer.wrap(tSpan.getTransactionId()), byteBuffer);

Assert.assertEquals(tSpan.getSpanId(), spanBo.getSpanId());
Assert.assertEquals(tSpan.getParentSpanId(), spanBo.getParentSpanId());
Assert.assertEquals(tSpan.getStartTime(), spanBo.getStartTime());
Assert.assertEquals(tSpan.getElapsed(), spanBo.getElapsed());
Assert.assertEquals(tSpan.getElapsed(), spanBo.getElapsed());
Assert.assertEquals(tSpan.getRpc(), spanBo.getRpc());

Assert.assertEquals(tSpan.getServiceType(), spanBo.getServiceType());
Assert.assertEquals(tSpan.getEndPoint(), spanBo.getEndPoint());
Assert.assertEquals(tSpan.getRemoteAddr(), spanBo.getRemoteAddr());

assertAnnotation(tSpan.getAnnotations(), spanBo.getAnnotationBoList());

Assert.assertEquals(tSpan.getFlag(), spanBo.getFlag());
Assert.assertEquals(tSpan.getErr(), spanBo.getErrCode());

Assert.assertEquals(tSpan.getParentApplicationName(), spanBo.getParentApplicationId());
Assert.assertEquals(tSpan.getParentApplicationType(), spanBo.getParentApplicationServiceType());
Assert.assertEquals(tSpan.getAcceptorHost(), spanBo.getAcceptorHost());

Assert.assertEquals(tSpan.getApiId(), spanBo.getApiId());
Assert.assertEquals(tSpan.getApplicationServiceType(), spanBo.getApplicationServiceType());


boolean hasException = tSpan.getExceptionInfo() != null;
Assert.assertEquals(hasException, spanBo.hasException());
if (hasException) {
Assert.assertEquals(tSpan.getExceptionInfo().getIntValue(), spanBo.getExceptionId());
Assert.assertEquals(tSpan.getExceptionInfo().getStringValue(), spanBo.getExceptionMessage());
}

Assert.assertEquals(tSpan.getLoggingTransactionInfo(), spanBo.getLoggingTransactionInfo());

}

public void assertAnnotation(List<TAnnotation> tAnnotationList, List<AnnotationBo> annotationBoList) {
if (CollectionUtils.isEmpty(tAnnotationList) && CollectionUtils.isEmpty(annotationBoList)) {
return;
}
Assert.assertEquals(tAnnotationList.size(), annotationBoList.size());
if (tAnnotationList.isEmpty()) {
return;
}


for (int i = 0; i < tAnnotationList.size(); i++) {
TAnnotation tAnnotation = tAnnotationList.get(i);
AnnotationBo annotationBo = annotationBoList.get(i);

Assert.assertEquals(tAnnotation.getKey(), annotationBo.getKey());
Assert.assertEquals(tAnnotation.getValue().getStringValue(), annotationBo.getValue());
}
}

public void assertSpanEvent(TSpanEvent tSpanEvent, SpanEventBo spanEventBo) {
Assert.assertEquals(tSpanEvent.getSequence(), spanEventBo.getSequence());
Assert.assertEquals(tSpanEvent.getStartElapsed(), spanEventBo.getStartElapsed());
Assert.assertEquals(tSpanEvent.getEndElapsed(), spanEventBo.getEndElapsed());

Assert.assertEquals(tSpanEvent.getRpc(), spanEventBo.getRpc());
Assert.assertEquals(tSpanEvent.getServiceType(), spanEventBo.getServiceType());
Assert.assertEquals(tSpanEvent.getEndPoint(), spanEventBo.getEndPoint());

assertAnnotation(tSpanEvent.getAnnotations(), spanEventBo.getAnnotationBoList());

Assert.assertEquals(tSpanEvent.getDepth(), spanEventBo.getDepth());
Assert.assertEquals(tSpanEvent.getNextSpanId(), spanEventBo.getNextSpanId());
Assert.assertEquals(tSpanEvent.getDestinationId(), spanEventBo.getDestinationId());

Assert.assertEquals(tSpanEvent.getApiId(), spanEventBo.getApiId());

boolean hasException = tSpanEvent.getExceptionInfo() != null;
Assert.assertEquals(hasException, spanEventBo.hasException());
if (hasException) {
Assert.assertEquals(tSpanEvent.getExceptionInfo().getIntValue(), spanEventBo.getExceptionId());
Assert.assertEquals(tSpanEvent.getExceptionInfo().getStringValue(), spanEventBo.getExceptionMessage());
}

Assert.assertEquals(tSpanEvent.getAsyncId(), spanEventBo.getAsyncId());
Assert.assertEquals(tSpanEvent.getNextAsyncId(), spanEventBo.getNextAsyncId());
Assert.assertEquals(tSpanEvent.getAsyncSequence(), spanEventBo.getAsyncSequence());
}


public void assertSpanChunk(TSpanChunk tSpanChunk, SpanChunkBo spanChunkBo) {
Assert.assertEquals(tSpanChunk.getAgentId(), spanChunkBo.getAgentId());
Assert.assertEquals(tSpanChunk.getApplicationName(), spanChunkBo.getApplicationId());
Assert.assertEquals(tSpanChunk.getAgentStartTime(), spanChunkBo.getAgentStartTime());


ByteBuffer byteBuffer = TransactionIdUtils.formatByteBuffer(spanChunkBo.getTraceAgentId(), spanChunkBo.getTraceAgentStartTime(), spanChunkBo.getTraceTransactionSequence());
Assert.assertEquals(ByteBuffer.wrap(tSpanChunk.getTransactionId()), byteBuffer);

Assert.assertEquals(tSpanChunk.getSpanId(), spanChunkBo.getSpanId());

Assert.assertEquals(tSpanChunk.getServiceType(), spanChunkBo.getServiceType());
Assert.assertEquals(tSpanChunk.getEndPoint(), spanChunkBo.getEndPoint());
Assert.assertEquals(tSpanChunk.getApplicationServiceType(), spanChunkBo.getApplicationServiceType());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.navercorp.pinpoint.common.server.bo;

import com.navercorp.pinpoint.thrift.dto.TSpan;
import com.navercorp.pinpoint.thrift.dto.TSpanChunk;
import com.navercorp.pinpoint.thrift.dto.TSpanEvent;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;

/**
* @author Woonduk Kang(emeroad)
*/
public class SpanFactoryTest {

private final SpanFactory spanFactory = new SpanFactory();

private SpanFactoryAssert spanFactoryAssert = new SpanFactoryAssert();

private RandomTSpan random = new RandomTSpan();


@Test
public void buildSpanBo() throws Exception {
TSpan tSpan = random.randomTSpan();

SpanBo spanBo = spanFactory.newSpanBo(tSpan);

spanFactoryAssert.assertSpan(tSpan, spanBo);
}


@Test
public void buildSpanChunkBo() throws Exception {
TSpanChunk tSpanChunk = random.randomTSpanChunk();

SpanChunkBo spanChunkBo = spanFactory.newSpanChunkBo(tSpanChunk);

spanFactoryAssert.assertSpanChunk(tSpanChunk, spanChunkBo);

}

@Test
public void newSpanEventBo() throws Exception {
TSpan tSpan = random.randomTSpan();
SpanBo spanBo = spanFactory.newSpanBo(tSpan);

TSpanEvent tSpanEvent = random.randomTSpanEvent(tSpan, (short) RandomUtils.nextInt(0, 100));
SpanEventBo spanEventBo = spanFactory.newSpanEventBo(spanBo, tSpanEvent);

spanFactoryAssert.assertSpanEvent(tSpanEvent, spanEventBo);

}

}

0 comments on commit 25b1b7b

Please sign in to comment.