From 31bae0a5021d315d9d8f1d7b9a791e5af812b929 Mon Sep 17 00:00:00 2001 From: emeroad Date: Tue, 2 Aug 2016 19:15:01 +0900 Subject: [PATCH] #1819 trace format v2 - add testcase for SpanFactory --- .../common/server/bo/SpanFactory.java | 32 ++-- .../common/server/bo/RandomTSpan.java | 140 ++++++++++++++++++ .../common/server/bo/SpanFactoryAssert.java | 128 ++++++++++++++++ .../common/server/bo/SpanFactoryTest.java | 53 +++++++ 4 files changed, 341 insertions(+), 12 deletions(-) create mode 100644 commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/RandomTSpan.java create mode 100644 commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryAssert.java create mode 100644 commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryTest.java diff --git a/commons-server/src/main/java/com/navercorp/pinpoint/common/server/bo/SpanFactory.java b/commons-server/src/main/java/com/navercorp/pinpoint/common/server/bo/SpanFactory.java index da6a1b2c0c62..0b7da84db4c6 100644 --- a/commons-server/src/main/java/com/navercorp/pinpoint/common/server/bo/SpanFactory.java +++ b/commons-server/src/main/java/com/navercorp/pinpoint/common/server/bo/SpanFactory.java @@ -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()); @@ -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"); } @@ -198,6 +199,21 @@ private void bind(SpanEventBo spanEvent, TSpanEvent tSpanEvent) { public SpanChunkBo buildSpanChunkBo(TSpanChunk tSpanChunk) { + final SpanChunkBo spanChunkBo = newSpanChunkBo(tSpanChunk); + + List spanEventList = tSpanChunk.getSpanEventList(); + List 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()); @@ -223,15 +239,6 @@ public SpanChunkBo buildSpanChunkBo(TSpanChunk tSpanChunk) { spanChunkBo.setSpanId(tSpanChunk.getSpanId()); spanChunkBo.setEndPoint(tSpanChunk.getEndPoint()); - - List spanEventList = tSpanChunk.getSpanEventList(); - List spanEventBoList = buildSpanEventBoList(spanChunkBo, spanEventList); - spanChunkBo.addSpanEventBoList(spanEventBoList); - - - long acceptedTime = acceptedTimeService.getAcceptedTime(); - spanChunkBo.setCollectorAcceptTime(acceptedTime); - return spanChunkBo; } @@ -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); diff --git a/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/RandomTSpan.java b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/RandomTSpan.java new file mode 100644 index 000000000000..853823fa6bd1 --- /dev/null +++ b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/RandomTSpan.java @@ -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 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 randomTAnnotationList() { + int annotationSize = RandomUtils.nextInt(0, 3); + List 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 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; + } +} diff --git a/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryAssert.java b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryAssert.java new file mode 100644 index 000000000000..b49fc4a951f5 --- /dev/null +++ b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryAssert.java @@ -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 tAnnotationList, List 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()); + + } +} diff --git a/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryTest.java b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryTest.java new file mode 100644 index 000000000000..73f4d7cbdd09 --- /dev/null +++ b/commons-server/src/test/java/com/navercorp/pinpoint/common/server/bo/SpanFactoryTest.java @@ -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); + + } + +} \ No newline at end of file