From 349451bc970b9af66cb9ade31a128dfa35c8c3fc Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Fri, 4 Oct 2013 11:41:03 +0000 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EC=9A=B4=EB=8D=95]=20[LUCYSUS-1744]?= =?UTF-8?q?=20span,=20spanchunk,=20spanevent=EB=A5=BC=20toThrift=EB=A5=BC?= =?UTF-8?q?=20=EA=B1=B0=EC=B3=90=20=EC=83=9D=EC=84=B1=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EA=B3=A0=20=EB=8B=A4=EC=9D=B4=EB=A0=89=ED=8A=B8?= =?UTF-8?q?=EB=A1=9C=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=B1=EB=8A=A5=20=EA=B0=9C=EC=84=A0,=20JDBCUrlParser?= =?UTF-8?q?=EA=B0=80=20url=EC=9D=84=20=EC=BA=90=EC=89=AC=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@2426 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../nhn/pinpoint/profiler/DefaultAgent.java | 28 ++- .../profiler/context/BypassStorage.java | 2 +- .../profiler/context/DefaultAsyncTrace.java | 8 +- .../profiler/context/DefaultTrace.java | 23 +- .../profiler/context/RootStackFrame.java | 6 +- .../nhn/pinpoint/profiler/context/Span.java | 52 +++-- .../pinpoint/profiler/context/SpanChunk.java | 81 +++---- .../pinpoint/profiler/context/SpanEvent.java | 214 ++++-------------- .../profiler/context/SpanEventStackFrame.java | 4 +- .../profiler/context/TimeBaseStorage.java | 9 +- .../profiler/modifier/db/JDBCUrlParser.java | 45 ++-- .../profiler/context/SpanEventTest.java | 47 ++++ 12 files changed, 231 insertions(+), 288 deletions(-) create mode 100644 src/test/java/com/nhn/pinpoint/profiler/context/SpanEventTest.java diff --git a/src/main/java/com/nhn/pinpoint/profiler/DefaultAgent.java b/src/main/java/com/nhn/pinpoint/profiler/DefaultAgent.java index 67b90d3cb..cfb943a7d 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/DefaultAgent.java +++ b/src/main/java/com/nhn/pinpoint/profiler/DefaultAgent.java @@ -24,6 +24,7 @@ import com.nhn.pinpoint.profiler.sender.DataSender; import com.nhn.pinpoint.profiler.sender.TcpDataSender; import com.nhn.pinpoint.profiler.sender.UdpDataSender; import com.nhn.pinpoint.profiler.util.NetworkUtils; +import com.nhn.pinpoint.thrift.dto.TAgentKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,8 +55,10 @@ public class DefaultAgent implements Agent { private final AgentInformation agentInformation; + private final TAgentKey tAgentKey; + // agent info는 heartbeat에서 매번 사용한다. - private TAgentInfo agentInfo; + private TAgentInfo tAgentInfo; // agent의 상태, private volatile AgentStatus agentStatus; @@ -101,8 +104,9 @@ public class DefaultAgent implements Agent { // 매핑 테이블 초기화를 위해 엑세스 // ApiMappingTable.findApiId("test", null, null); - this.agentInfo = createAgentInfo(); - this.heartBitChecker = new HeartBitChecker(tcpDataSender, profilerConfig.getHeartbeatInterval(), agentInfo); + this.tAgentInfo = createTAgentInfo(); + this.tAgentKey = createTAgentKey(); + this.heartBitChecker = new HeartBitChecker(tcpDataSender, profilerConfig.getHeartbeatInterval(), tAgentInfo); // JVM 통계 등을 주기적으로 수집하여 collector에 전송하는 monitor를 초기화한다. this.agentStatMonitor = new AgentStatMonitor(this.statDataSender, this.agentInformation.getAgentId()); @@ -111,6 +115,8 @@ public class DefaultAgent implements Agent { } + + private AgentInformation createAgentInformation() { final String machineName = NetworkUtils.getHostName(); final String agentId = getId("pinpoint.agentId", machineName, PinpointConstants.AGENT_NAME_MAX_LEN); @@ -176,7 +182,7 @@ public class DefaultAgent implements Agent { } } - private TAgentInfo createAgentInfo() { + private TAgentInfo createTAgentInfo() { final ServerInfo serverInfo = this.serverInfo; String ip = serverInfo.getHostip(); String ports = ""; @@ -199,6 +205,14 @@ public class DefaultAgent implements Agent { return agentInfo; } + private TAgentKey createTAgentKey() { + return new TAgentKey(agentInformation.getAgentId(), agentInformation.getApplicationName(), agentInformation.getStartTime()); + } + + public TAgentKey getTAgentKey() { + return tAgentKey; + } + private void changeStatus(AgentStatus status) { this.agentStatus = status; if (logger.isDebugEnabled()) { @@ -318,9 +332,9 @@ public class DefaultAgent implements Agent { public void stop() { logger.info("Stopping {} Agent.", ProductInfo.CAMEL_NAME); - agentInfo.setIsAlive(false); - this.tcpDataSender.send(agentInfo); - // TODO send agentInfo alive false후 send 메시지의 처리가 정확하지 않음 + tAgentInfo.setIsAlive(false); + this.tcpDataSender.send(tAgentInfo); + // TODO send tAgentInfo alive false후 send 메시지의 처리가 정확하지 않음 changeStatus(AgentStatus.STOPPING); this.heartBitChecker.stop(); diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/BypassStorage.java b/src/main/java/com/nhn/pinpoint/profiler/context/BypassStorage.java index 3cc71aba0..4b08d81be 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/BypassStorage.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/BypassStorage.java @@ -37,7 +37,7 @@ public class BypassStorage implements Storage { if (spanEvent == null) { throw new NullPointerException("spanEvent must not be null"); } - dataSender.send(spanEvent); + dataSender.send((Thriftable) spanEvent); } @Override diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/DefaultAsyncTrace.java b/src/main/java/com/nhn/pinpoint/profiler/context/DefaultAsyncTrace.java index 4a1197c46..887b588da 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/DefaultAsyncTrace.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/DefaultAsyncTrace.java @@ -75,7 +75,7 @@ public class DefaultAsyncTrace implements AsyncTrace { @Override public void markBeforeTime() { - spanEvent.setStartTime(System.currentTimeMillis()); + spanEvent.markStartTime(); } @Override @@ -90,7 +90,7 @@ public class DefaultAsyncTrace implements AsyncTrace { @Override public void markAfterTime() { - spanEvent.setEndTime(System.currentTimeMillis()); + spanEvent.markEndTime(); } @@ -135,7 +135,7 @@ public class DefaultAsyncTrace implements AsyncTrace { @Override public void recordServiceType(final ServiceType serviceType) { - this.spanEvent.setServiceType(serviceType); + this.spanEvent.setServiceType(serviceType.getCode()); } @Override @@ -147,7 +147,7 @@ public class DefaultAsyncTrace implements AsyncTrace { @Override public void recordDestinationId(String destinationId) { - this.spanEvent.setDestionationId(destinationId); + this.spanEvent.setDestinationId(destinationId); } // TODO: final String... endPoint로 받으면 합치는데 비용이 들어가 그냥 한번에 받는게 나을것 같음. diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/DefaultTrace.java b/src/main/java/com/nhn/pinpoint/profiler/context/DefaultTrace.java index 3377aebd5..9cb0350df 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/DefaultTrace.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/DefaultTrace.java @@ -345,7 +345,7 @@ public final class DefaultTrace implements Trace { span.setServiceType(serviceType.getCode()); } else { SpanEvent spanEvent = ((SpanEventStackFrame) currentStackFrame).getSpanEvent(); - spanEvent.setServiceType(serviceType); + spanEvent.setServiceType(serviceType.getCode()); } } @@ -370,18 +370,19 @@ public final class DefaultTrace implements Trace { StackFrame currentStackFrame = this.currentStackFrame; if (currentStackFrame instanceof SpanEventStackFrame) { SpanEvent spanEvent = ((SpanEventStackFrame) currentStackFrame).getSpanEvent(); - spanEvent.setDestionationId(destinationId); + spanEvent.setDestinationId(destinationId); } } @Override + @Deprecated public void recordDestinationAddress(List address) { // TODO API 단일화 필요. - StackFrame currentStackFrame = this.currentStackFrame; - if (currentStackFrame instanceof SpanEventStackFrame) { - SpanEvent spanEvent = ((SpanEventStackFrame) currentStackFrame).getSpanEvent(); - spanEvent.setDestinationAddress(); - } +// StackFrame currentStackFrame = this.currentStackFrame; +// if (currentStackFrame instanceof SpanEventStackFrame) { +// SpanEvent spanEvent = ((SpanEventStackFrame) currentStackFrame).getSpanEvent(); +// spanEvent.setDestinationAddress(); +// } } @Override @@ -415,13 +416,15 @@ public final class DefaultTrace implements Trace { } @Override - public void recordNextSpanId(int spanId) { + public void recordNextSpanId(int nextSpanId) { StackFrame currentStackFrame = this.currentStackFrame; if (currentStackFrame instanceof RootStackFrame) { - logger.warn("OMG. Something's going wrong. Current stackframe is root Span. nextSpanId={}", spanId); + logger.warn("OMG. Something's going wrong. Current stackframe is root Span. nextSpanId={}", nextSpanId); } else { SpanEvent spanEvent = ((SpanEventStackFrame) currentStackFrame).getSpanEvent(); - spanEvent.setNextSpanId(spanId); + if (nextSpanId != -1) { + spanEvent.setNextSpanId(nextSpanId); + } } } diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/RootStackFrame.java b/src/main/java/com/nhn/pinpoint/profiler/context/RootStackFrame.java index ad6be20ca..c9b8088ad 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/RootStackFrame.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/RootStackFrame.java @@ -39,14 +39,12 @@ public class RootStackFrame implements StackFrame { @Override public void markAfterTime() { - final long startTime = this.span.getStartTime(); - // long으로 바꿀것. - this.span.setElapsed((int)(System.currentTimeMillis() - startTime)); + this.span.markEndTime(); } @Override public long getAfterTime() { - return this.span.getElapsed() + this.span.getStartTime(); + return span.getEndTime(); } diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/Span.java b/src/main/java/com/nhn/pinpoint/profiler/context/Span.java index 9506084c3..058da5ff5 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/Span.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/Span.java @@ -8,7 +8,9 @@ import com.nhn.pinpoint.thrift.dto.TSpan; import com.nhn.pinpoint.thrift.dto.TSpanEvent; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Span represent RPC @@ -18,15 +20,14 @@ import java.util.List; public class Span extends TSpan implements Thriftable { private final TraceId traceId; - private final List traceAnnotationList = new ArrayList(5); + private List traceAnnotationList = new ArrayList(4); - private List spanEventList; - public Span(TraceId traceId) { if (traceId == null) { throw new NullPointerException("traceId must not be null"); } this.traceId = traceId; + recordTraceId(traceId); } private void recordTraceId(TraceId traceId) { @@ -42,6 +43,26 @@ public class Span extends TSpan implements Thriftable { this.setFlag(traceId.getFlags()); } + public void markBeforeTime() { + this.setStartTime(System.currentTimeMillis()); + } + + public void markEndTime() { + if (!isSetStartTime()) { + throw new RuntimeException("startTime is not set"); + } + final long startTime = this.getStartTime(); + // long으로 바꿀것. + this.setElapsed((int)(System.currentTimeMillis() - startTime)); + } + + public long getEndTime() { + if (!isSetStartTime()) { + throw new RuntimeException("startTime is not set"); + } + return this.getStartTime() + this.getElapsed(); + } + public boolean addAnnotation(TraceAnnotation traceAnnotation) { return traceAnnotationList.add(traceAnnotation); @@ -51,14 +72,8 @@ public class Span extends TSpan implements Thriftable { return traceAnnotationList.size(); } - public List getPSpanEventList() { - return spanEventList; - } - public void setPSpanEventList(List spanEventList) { - this.spanEventList = spanEventList; - } - + public int getException() { return getErr(); } @@ -73,7 +88,6 @@ public class Span extends TSpan implements Thriftable { public TSpan toThrift() { - recordTraceId(traceId); final AgentInformation agentInformation = DefaultAgent.getInstance().getAgentInformation(); this.setAgentId(agentInformation.getAgentId()); this.setApplicationName(agentInformation.getApplicationName()); @@ -86,17 +100,19 @@ public class Span extends TSpan implements Thriftable { annotationList.add(traceAnnotation.toThrift()); } this.setAnnotations(annotationList); + this.traceAnnotationList = null; - List spanEventList = this.getPSpanEventList(); - if (spanEventList != null && spanEventList.size() != 0) { - List tSpanEventList = new ArrayList(spanEventList.size()); - for (SpanEvent spanEvent : spanEventList) { - TSpanEvent tSpanEvent = spanEvent.toThrift(true); - tSpanEventList.add(tSpanEvent); + final List spanEventList = this.getSpanEventList(); + if (spanEventList != null) { + for (TSpanEvent spanEvent : spanEventList) { + if (spanEvent instanceof SpanEvent) { + ((SpanEvent)spanEvent).toThrift(true); + } } - this.setSpanEventList(tSpanEventList); } return this; } + + } diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/SpanChunk.java b/src/main/java/com/nhn/pinpoint/profiler/context/SpanChunk.java index 330e4409a..c9b545cc4 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/SpanChunk.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/SpanChunk.java @@ -14,84 +14,57 @@ import com.nhn.pinpoint.profiler.DefaultAgent; /** * */ -public class SpanChunk implements Thriftable { - - private final List spanEventList; +public class SpanChunk extends TSpanChunk implements Thriftable { public SpanChunk(List spanEventList) { if (spanEventList == null) { throw new NullPointerException("spanEventList must not be null"); } - this.spanEventList = spanEventList; + setSpanEventList((List) spanEventList); } @Override public TBase toThrift() { - TSpanChunk tSpanChunk = new TSpanChunk(); // TODO 반드시 1개 이상이라는 조건을 충족해야 된다. - SpanEvent first = spanEventList.get(0); - Span parentSpan = first.getParentSpan(); + final List spanEventList = getSpanEventList(); + TSpanEvent first = spanEventList.get(0); + if (first == null) { + throw new IllegalStateException("fist spanEvent not found"); + } + Span parentSpan = ((SpanEvent)first).getSpan(); final AgentInformation agentInformation = DefaultAgent.getInstance().getAgentInformation(); - tSpanChunk.setAgentId(agentInformation.getAgentId()); - tSpanChunk.setApplicationName(agentInformation.getApplicationName()); - tSpanChunk.setAgentStartTime(agentInformation.getStartTime()); + this.setAgentId(agentInformation.getAgentId()); + this.setApplicationName(agentInformation.getApplicationName()); + this.setAgentStartTime(agentInformation.getStartTime()); - tSpanChunk.setServiceType(parentSpan.getServiceType()); + this.setServiceType(parentSpan.getServiceType()); - tSpanChunk.setTraceAgentId(parentSpan.getTraceAgentId()); - tSpanChunk.setTraceAgentStartTime(parentSpan.getTraceAgentStartTime()); - tSpanChunk.setTraceTransactionSequence(parentSpan.getTraceTransactionSequence()); - tSpanChunk.setSpanId(parentSpan.getSpanId()); + this.setTraceAgentId(parentSpan.getTraceAgentId()); + this.setTraceAgentStartTime(parentSpan.getTraceAgentStartTime()); + this.setTraceTransactionSequence(parentSpan.getTraceTransactionSequence()); + this.setSpanId(parentSpan.getSpanId()); + + this.setEndPoint(parentSpan.getEndPoint()); - tSpanChunk.setEndPoint(parentSpan.getEndPoint()); - - List tSpanEvent = createSpanEvent(spanEventList); + List tSpanEvent = createTSpanEvent(spanEventList); - tSpanChunk.setSpanEventList(tSpanEvent); + this.setSpanEventList(tSpanEvent); - return tSpanChunk; + return this; } - private List createSpanEvent(List spanEventList) { - List result = new ArrayList(spanEventList.size()); - for (SpanEvent spanEvent : spanEventList) { - TSpanEvent tSpanEvent = new TSpanEvent(); - + private List createTSpanEvent(List spanEventList) { + for (TSpanEvent tSpanEvent : spanEventList) { // tSpanEvent.setAgentId(Agent.getInstance().getAgentId()); // tSpanEvent.setApplicationName(Agent.getInstance().getApplicationName()); // tSpanEvent.setAgentIdentifier(Agent.getInstance().getPid()); - - long parentSpanStartTime = spanEvent.getParentSpan().getStartTime(); - tSpanEvent.setStartElapsed((int) (spanEvent.getStartTime() - parentSpanStartTime)); - tSpanEvent.setEndElapsed((int) (spanEvent.getEndTime() - spanEvent.getStartTime())); - - tSpanEvent.setSequence(spanEvent.getSequence()); - - tSpanEvent.setRpc(spanEvent.getRpc()); - tSpanEvent.setServiceType(spanEvent.getServiceType().getCode()); - tSpanEvent.setDestinationId(spanEvent.getDestionationId()); - - tSpanEvent.setEndPoint(spanEvent.getEndPoint()); - - // 여기서 데이터 인코딩을 하자. - List annotationList = new ArrayList(spanEvent.getAnnotationSize()); - for (TraceAnnotation traceAnnotation : spanEvent.getTraceAnnotationList()) { - annotationList.add(traceAnnotation.toThrift()); + if (tSpanEvent instanceof SpanEvent) { + ((SpanEvent)tSpanEvent).toThrift(true); } - - if (spanEvent.getDepth() != -1) { - tSpanEvent.setDepth(spanEvent.getDepth()); - } - - if (spanEvent.getNextSpanId() != -1) { - tSpanEvent.setNextSpanId(spanEvent.getNextSpanId()); - } - - tSpanEvent.setAnnotations(annotationList); - result.add(tSpanEvent); } - return result; + // type check 무시. + return spanEventList; } diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/SpanEvent.java b/src/main/java/com/nhn/pinpoint/profiler/context/SpanEvent.java index bf481d42b..a28218036 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/SpanEvent.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/SpanEvent.java @@ -1,11 +1,10 @@ package com.nhn.pinpoint.profiler.context; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import com.nhn.pinpoint.profiler.AgentInformation; import com.nhn.pinpoint.profiler.DefaultAgent; -import com.nhn.pinpoint.common.ServiceType; import com.nhn.pinpoint.thrift.dto.TAgentKey; import com.nhn.pinpoint.thrift.dto.TAnnotation; import com.nhn.pinpoint.thrift.dto.TSpanEvent; @@ -15,211 +14,86 @@ import com.nhn.pinpoint.thrift.dto.TSpanEvent; * * @author netspider */ -public class SpanEvent implements Thriftable { +public class SpanEvent extends TSpanEvent implements Thriftable { - private final Span parentSpan; + private final Span span; - private short sequence; + private List traceAnnotationList; - private long startTime; - private long endTime; - private String rpc; - private ServiceType serviceType; - - private String endPoint; - - private String destionationId; - private List destinationAddress; - - private final List traceAnnotationList = new ArrayList(4); - - private int nextSpanId = -1; - private int depth = -1; - - public SpanEvent(Span parentSpan) { - this.parentSpan = parentSpan; + public SpanEvent(Span span) { + if (span == null) { + throw new NullPointerException("span must not be null"); + } + this.span = span; } - public Span getParentSpan() { - return parentSpan; - } - - public short getSequence() { - return sequence; - } - - public void setSequence(short sequence) { - this.sequence = sequence; + public Span getSpan() { + return span; } public boolean addAnnotation(TraceAnnotation traceAnnotation) { + if (traceAnnotationList == null) { + this.traceAnnotationList = new ArrayList(4); + } return traceAnnotationList.add(traceAnnotation); } - public List getTraceAnnotationList() { - return traceAnnotationList; - } - public int getAnnotationSize() { - return traceAnnotationList.size(); - } - - public String getEndPoint() { - return this.endPoint; - } - - - public String getRpc() { - return rpc; - } - - public void setRpc(String rpc) { - this.rpc = rpc; - } - - public void setEndPoint(String endPoint) { - this.endPoint = endPoint; - } - - public String getDestionationId() { - return destionationId; - } - - public void setDestionationId(String destionationId) { - this.destionationId = destionationId; - } - - public List getDestinationAddress() { - return destinationAddress; - } - - public void setDestinationAddress(List destinationAddress) { - this.destinationAddress = destinationAddress; - } - - public void setStartTime(long startTime) { - this.startTime = startTime; + public void markStartTime() { +// spanEvent.setStartElapsed((int) (startTime - parentSpanStartTime)); + final int startElapsed = (int)(System.currentTimeMillis() - span.getStartTime()); + this.setStartElapsed(startElapsed); } public long getStartTime() { - return startTime; + return span.getStartTime() + getStartElapsed(); } - public void setEndTime(long endTime) { - this.endTime = endTime; + public void markEndTime() { +// spanEvent.setEndElapsed((int) (endTime - startTime)); + final int endElapsed = (int)(System.currentTimeMillis() - getStartTime()); + this.setEndElapsed(endElapsed); } public long getEndTime() { - return endTime; + return span.getStartTime() + getStartElapsed() + getEndElapsed(); } - public ServiceType getServiceType() { - return serviceType; - } - - public void setServiceType(ServiceType serviceType) { - this.serviceType = serviceType; - } - - - - public int getDepth() { - return depth; - } - - public void setDepth(int depth) { - this.depth = depth; - } - - public int getNextSpanId() { - return nextSpanId; - } - - public void setNextSpanId(int nextSpanId) { - this.nextSpanId = nextSpanId; - } - - public String toString() { - StringBuilder sb = new StringBuilder(256); - - sb.append("{"); - sb.append("\n\t Depth = ").append(depth); - sb.append("\n\t NextSpanid=").append(nextSpanId); - sb.append("\n\t ParentTraceID=").append(parentSpan.getTraceId()); - sb.append("\n\t Sequence=").append(sequence); - sb.append(",\n\t StartTime=").append(startTime); - sb.append(", EndTime=").append(endTime); - sb.append(",\n\t Name=").append(rpc); - sb.append(", ServiceType=").append(serviceType); - sb.append(", EndPoint=").append(endPoint); - sb.append(", Seq=").append(sequence); - sb.append(",\n\t Annotations = {"); - for (TraceAnnotation a : traceAnnotationList) { - sb.append("\n\t\t").append(a); - } - sb.append("\n\t}"); - - sb.append("}"); - - return sb.toString(); - } public TSpanEvent toThrift() { return toThrift(false); } public TSpanEvent toThrift(boolean child) { - TSpanEvent spanEvent = new TSpanEvent(); - - long parentSpanStartTime = parentSpan.getStartTime(); - spanEvent.setStartElapsed((int) (startTime - parentSpanStartTime)); - spanEvent.setEndElapsed((int) (endTime - startTime)); - - spanEvent.setSequence(sequence); // Span내부의 SpanEvent로 들어가지 않을 경우 if (!child) { - TAgentKey agentKey = new TAgentKey(); - final AgentInformation agentInformation = DefaultAgent.getInstance().getAgentInformation(); - agentKey.setAgentId(agentInformation.getAgentId()); - agentKey.setApplicationName(agentInformation.getApplicationName()); - agentKey.setAgentStartTime(agentInformation.getStartTime()); + final TAgentKey tAgentKey = DefaultAgent.getInstance().getTAgentKey(); + this.setAgentKey(tAgentKey); - spanEvent.setAgentKey(agentKey); + // span 데이터 셋은 child일때만 한다. + this.setParentServiceType(span.getServiceType()); // added + this.setParentEndPoint(span.getEndPoint()); // added - spanEvent.setParentServiceType(parentSpan.getServiceType()); // added - spanEvent.setParentEndPoint(parentSpan.getEndPoint()); // added - - spanEvent.setTraceAgentId(parentSpan.getTraceAgentId()); - spanEvent.setTraceAgentStartTime(parentSpan.getTraceAgentStartTime()); - spanEvent.setTraceTransactionSequence(parentSpan.getTraceTransactionSequence()); - spanEvent.setSpanId(parentSpan.getSpanId()); + this.setTraceAgentId(span.getTraceAgentId()); + this.setTraceAgentStartTime(span.getTraceAgentStartTime()); + this.setTraceTransactionSequence(span.getTraceTransactionSequence()); + this.setSpanId(span.getSpanId()); } - spanEvent.setRpc(rpc); - spanEvent.setServiceType(serviceType.getCode()); - - spanEvent.setEndPoint(endPoint); - spanEvent.setDestinationId(this.destionationId); - // 여기서 데이터 인코딩을 하자. - List annotationList = new ArrayList(traceAnnotationList.size()); - for (TraceAnnotation traceAnnotation : traceAnnotationList) { - annotationList.add(traceAnnotation.toThrift()); + if (traceAnnotationList != null) { + List annotationList = new ArrayList(traceAnnotationList.size()); + for (TraceAnnotation traceAnnotation : traceAnnotationList) { + annotationList.add(traceAnnotation.toThrift()); + } + this.setAnnotations(annotationList); + this.traceAnnotationList = null; } - spanEvent.setAnnotations(annotationList); - if (depth != -1) { - spanEvent.setDepth(depth); - } - - if (nextSpanId != -1) { - spanEvent.setNextSpanId(nextSpanId); - } - - return spanEvent; + return this; } - public void setDestinationAddress() { - //To change body of created methods use File | Settings | File Templates. - } + + + } diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/SpanEventStackFrame.java b/src/main/java/com/nhn/pinpoint/profiler/context/SpanEventStackFrame.java index 598d0880e..f89040aff 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/SpanEventStackFrame.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/SpanEventStackFrame.java @@ -26,7 +26,7 @@ public class SpanEventStackFrame implements StackFrame { @Override public void markBeforeTime() { - spanEvent.setStartTime(System.currentTimeMillis()); + spanEvent.markStartTime(); } @Override @@ -36,7 +36,7 @@ public class SpanEventStackFrame implements StackFrame { @Override public void markAfterTime() { - spanEvent.setEndTime(System.currentTimeMillis()); + spanEvent.markEndTime(); } @Override diff --git a/src/main/java/com/nhn/pinpoint/profiler/context/TimeBaseStorage.java b/src/main/java/com/nhn/pinpoint/profiler/context/TimeBaseStorage.java index 532b084e1..dec082952 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/context/TimeBaseStorage.java +++ b/src/main/java/com/nhn/pinpoint/profiler/context/TimeBaseStorage.java @@ -1,7 +1,6 @@ package com.nhn.pinpoint.profiler.context; import com.nhn.pinpoint.profiler.sender.DataSender; -import org.apache.thrift.TBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,11 +74,11 @@ public class TimeBaseStorage implements Storage { } if (!add) { // add가 실패하였을 경우는 이미 span이 flush된 상태이다. - dataSender.send(spanEvent); + dataSender.send((Thriftable)spanEvent); return; } if (flushData != null) { - dataSender.send(new SpanChunk(flushData)); + dataSender.send((Thriftable) new SpanChunk(flushData)); } } } @@ -97,7 +96,7 @@ public class TimeBaseStorage implements Storage { } private boolean checkLimit(SpanEvent spanEvent) { - return checkLimit(spanEvent.getParentSpan()); + return checkLimit(spanEvent.getSpan()); } private boolean checkLimit(Span span) { @@ -136,7 +135,7 @@ public class TimeBaseStorage implements Storage { this.storage = null; } if (spanEventList != null && spanEventList.size() != 0) { - span.setPSpanEventList(spanEventList); + span.setSpanEventList((List)spanEventList); } dataSender.send((Thriftable)span); } diff --git a/src/main/java/com/nhn/pinpoint/profiler/modifier/db/JDBCUrlParser.java b/src/main/java/com/nhn/pinpoint/profiler/modifier/db/JDBCUrlParser.java index 0c8bd91cd..f5caad333 100644 --- a/src/main/java/com/nhn/pinpoint/profiler/modifier/db/JDBCUrlParser.java +++ b/src/main/java/com/nhn/pinpoint/profiler/modifier/db/JDBCUrlParser.java @@ -10,6 +10,8 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -18,25 +20,23 @@ import java.util.regex.Pattern; */ public class JDBCUrlParser { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final ConcurrentMap cache = new ConcurrentHashMap(); public DefaultDatabaseInfo parse(String url) { - // jdbc 체크 - String lowCaseURL = url.toLowerCase().trim(); - if (!lowCaseURL.startsWith("jdbc:")) { - return createUnknownDataBase(url); + final DefaultDatabaseInfo hit = cache.get(url); + if (hit != null) { + logger.debug("database url cache hit:{} {}", url, hit); + return hit; } - if (driverTypeCheck(lowCaseURL, "mysql")) { - return parseMysql(url); + final DefaultDatabaseInfo databaseInfo = doParse(url); + final DefaultDatabaseInfo old = cache.putIfAbsent(url, databaseInfo); + if (old != null) { + return old; } - if (driverTypeCheck(lowCaseURL, "oracle")) { - return parseOracle(url); - } - if (driverTypeCheck(lowCaseURL, "cubrid")) { - return parseCubrid(url); - } - return createUnknownDataBase(url); + return databaseInfo; + // else if (url.indexOf("jdbc:oracle") >= 0) { // maker.lower().after("jdbc:oracle:").after(':'); // info.type = TYPE.ORACLE; @@ -84,6 +84,25 @@ public class JDBCUrlParser { // return null; } + private DefaultDatabaseInfo doParse(String url) { + // jdbc 체크 + String lowCaseURL = url.toLowerCase().trim(); + if (!lowCaseURL.startsWith("jdbc:")) { + return createUnknownDataBase(url); + } + + if (driverTypeCheck(lowCaseURL, "mysql")) { + return parseMysql(url); + } + if (driverTypeCheck(lowCaseURL, "oracle")) { + return parseOracle(url); + } + if (driverTypeCheck(lowCaseURL, "cubrid")) { + return parseCubrid(url); + } + return createUnknownDataBase(url); + } + private boolean driverTypeCheck(String lowCaseURL, String type) { final int jdbcNextIndex = 5; return lowCaseURL.startsWith(type, jdbcNextIndex); diff --git a/src/test/java/com/nhn/pinpoint/profiler/context/SpanEventTest.java b/src/test/java/com/nhn/pinpoint/profiler/context/SpanEventTest.java new file mode 100644 index 000000000..546ee3a6e --- /dev/null +++ b/src/test/java/com/nhn/pinpoint/profiler/context/SpanEventTest.java @@ -0,0 +1,47 @@ +package com.nhn.pinpoint.profiler.context; + +import junit.framework.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class SpanEventTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Test + public void testMarkStartTime() throws Exception { + final DefaultTraceId traceId = new DefaultTraceId("agentTime", 0, 0); + Span span = new Span(traceId); + span.markBeforeTime(); + Thread.sleep(10); + span.markEndTime(); + logger.debug("span:{}", span); + + final SpanEvent spanEvent = new SpanEvent(span); + spanEvent.markStartTime(); + Thread.sleep(10); + spanEvent.markEndTime(); + logger.debug("spanEvent:{}", spanEvent); + + Assert.assertEquals("startTime", span.getStartTime() + spanEvent.getStartElapsed(), spanEvent.getStartTime()); + Assert.assertEquals("endTime", span.getStartTime() + spanEvent.getStartElapsed() + spanEvent.getEndElapsed(), spanEvent.getEndTime()); + } + + @Test + public void testGetStartTime() throws Exception { + + } + + @Test + public void testMarkEndTime() throws Exception { + + } + + @Test + public void testGetEndTime() throws Exception { + + } +}