Skip to content

Commit

Permalink
[pinpoint-apm#9631] Receive and send it to EmptyExceptionTraceService
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Jan 17, 2023
1 parent dad8e43 commit ab08007
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.navercorp.pinpoint.collector.service;

import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.common.server.bo.exception.SpanEventExceptionBo;
import com.navercorp.pinpoint.common.trace.ServiceType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author intr3p1d
*/
@ConditionalOnMissingBean(value = ExceptionTraceService.class, ignored = EmptyExceptionTraceService.class)
@Service
public class EmptyExceptionTraceService implements ExceptionTraceService {
@Override
public void save(List<SpanEventExceptionBo> spanEventExceptionBoList, ServiceType applicationServiceType, String applicationId, String agentId, TransactionId transactionId, long spanId) {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.navercorp.pinpoint.collector.service;

import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.common.server.bo.exception.SpanEventExceptionBo;
import com.navercorp.pinpoint.common.trace.ServiceType;

import java.util.List;

/**
* @author intr3p1d
*/
public interface ExceptionTraceService {
void save(List<SpanEventExceptionBo> spanEventExceptionBoList, ServiceType applicationServiceType, String applicationId, String agentId, TransactionId transactionId, long spanId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.navercorp.pinpoint.collector.dao.ApplicationTraceIndexDao;
import com.navercorp.pinpoint.collector.dao.HostApplicationMapDao;
import com.navercorp.pinpoint.collector.dao.TraceDao;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.common.server.bo.SpanBo;
import com.navercorp.pinpoint.common.server.bo.SpanChunkBo;
import com.navercorp.pinpoint.common.server.bo.SpanEventBo;
import com.navercorp.pinpoint.common.server.bo.exception.SpanEventExceptionBo;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.trace.ServiceTypeCategory;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
Expand All @@ -32,6 +34,7 @@

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TraceService {
Expand All @@ -43,15 +46,22 @@ public class TraceService {

private final HostApplicationMapDao hostApplicationMapDao;

private final ExceptionTraceService exceptionTraceService;

private final StatisticsService statisticsService;

private final ServiceTypeRegistryService registry;

public TraceService(TraceDao traceDao, ApplicationTraceIndexDao applicationTraceIndexDao, HostApplicationMapDao hostApplicationMapDao,
StatisticsService statisticsService, ServiceTypeRegistryService registry) {
public TraceService(TraceDao traceDao,
ApplicationTraceIndexDao applicationTraceIndexDao,
HostApplicationMapDao hostApplicationMapDao,
ExceptionTraceService exceptionTraceService,
StatisticsService statisticsService,
ServiceTypeRegistryService registry) {
this.traceDao = Objects.requireNonNull(traceDao, "traceDao");
this.applicationTraceIndexDao = Objects.requireNonNull(applicationTraceIndexDao, "applicationTraceIndexDao");
this.hostApplicationMapDao = Objects.requireNonNull(hostApplicationMapDao, "hostApplicationMapDao");
this.exceptionTraceService = Objects.requireNonNull(exceptionTraceService, "exceptionTraceService");
this.statisticsService = Objects.requireNonNull(statisticsService, "statisticsService");
this.registry = Objects.requireNonNull(registry, "registry");
}
Expand All @@ -63,6 +73,7 @@ public void insertSpanChunk(final SpanChunkBo spanChunkBo) {
if (spanEventList != null) {
// TODO need to batch update later.
insertSpanEventList(spanEventList, applicationServiceType, spanChunkBo.getApplicationId(), spanChunkBo.getAgentId(), spanChunkBo.getEndPoint());
insertExceptionInfos(spanEventList, applicationServiceType, spanChunkBo.getApplicationId(), spanChunkBo.getAgentId(), spanChunkBo.getTransactionId(), spanChunkBo.getSpanId());
}
}

Expand Down Expand Up @@ -195,6 +206,7 @@ private void insertSpanEventStat(SpanBo span) {
final ServiceType applicationServiceType = getApplicationServiceType(span);
// TODO need to batch update later.
insertSpanEventList(spanEventList, applicationServiceType, span.getApplicationId(), span.getAgentId(), span.getEndPoint());
insertExceptionInfos(spanEventList, applicationServiceType, span.getApplicationId(), span.getAgentId(), span.getTransactionId(), span.getSpanId());
}

private void insertSpanEventList(List<SpanEventBo> spanEventList, ServiceType applicationServiceType, String applicationId, String agentId, String endPoint) {
Expand Down Expand Up @@ -235,6 +247,25 @@ private void insertSpanEventList(List<SpanEventBo> spanEventList, ServiceType ap
}
}

private void insertExceptionInfos(List<SpanEventBo> spanEventList, ServiceType applicationServiceType, String applicationId, String agentId, TransactionId transactionId, long spanId) {
try {
List<SpanEventExceptionBo> spanEventExceptionBos = spanEventList.stream()
.map(SpanEventBo::getFlushedException)
.filter(Objects::nonNull)
.collect(Collectors.toList());

if (spanEventExceptionBos.isEmpty()) {
return;
}
exceptionTraceService.save(spanEventExceptionBos,
applicationServiceType, applicationId, agentId,
transactionId, spanId);
} catch (Exception e) {
// ignore
logger.debug(e);
}
}

private String normalize(String spanEventApplicationName, ServiceType spanEventType) {
if (spanEventType.getCategory() == ServiceTypeCategory.DATABASE) {
// empty database id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.navercorp.pinpoint.common.server.bo;

import java.util.ArrayList;
import com.navercorp.pinpoint.common.server.bo.exception.SpanEventExceptionBo;

import java.util.List;

/**
Expand All @@ -25,7 +27,7 @@
*/
public class SpanEventBo implements Event {

// version 0 means that the type of prefix's size is int
// version 0 means that the type of prefix's size is int

private byte version = 0;

Expand All @@ -34,7 +36,7 @@ public class SpanEventBo implements Event {
private int startElapsed;
private int endElapsed;

// private String rpc;
// private String rpc;
private short serviceType;

private String destinationId;
Expand All @@ -53,13 +55,15 @@ public class SpanEventBo implements Event {
// should get exceptionClass from dao
private String exceptionClass;

private SpanEventExceptionBo flushedException;

private int nextAsyncId = -1;

@Deprecated
private int asyncId = -1;
@Deprecated
private short asyncSequence = -1;

public SpanEventBo() {
}

Expand Down Expand Up @@ -166,11 +170,11 @@ public void setAnnotationBoList(List<AnnotationBo> annotationList) {
}
this.annotationBoList = annotationList;
}

public boolean isAsync() {
return this.asyncId != -1;
}

public boolean hasException() {
return hasException;
}
Expand Down Expand Up @@ -198,6 +202,14 @@ public void setExceptionClass(String exceptionClass) {
this.exceptionClass = exceptionClass;
}

public SpanEventExceptionBo getFlushedException() {
return flushedException;
}

public void setFlushedException(SpanEventExceptionBo flushedException) {
this.flushedException = flushedException;
}

public int getNextAsyncId() {
return nextAsyncId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.navercorp.pinpoint.common.server.bo.exception;

import java.util.List;

/**
* @author intr3p1d
*/
public class SpanEventExceptionBo {
private String exceptionClassName;
private String exceptionMessage;
private List<StackTraceElementWrapperBo> stackTraceElements;
private long startTime;

public SpanEventExceptionBo() {
}

public String getExceptionClassName() {
return exceptionClassName;
}

public void setExceptionClassName(String exceptionClassName) {
this.exceptionClassName = exceptionClassName;
}

public String getExceptionMessage() {
return exceptionMessage;
}

public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
}

public List<StackTraceElementWrapperBo> getStackTraceElements() {
return stackTraceElements;
}

public void setStackTraceElements(List<StackTraceElementWrapperBo> stackTraceElements) {
this.stackTraceElements = stackTraceElements;
}

public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

@Override
public String toString() {
return "SpanEventExceptionBo{" +
"exceptionClassName='" + exceptionClassName + '\'' +
", exceptionMessage='" + exceptionMessage + '\'' +
", stackTraceElements=" + stackTraceElements +
", startTime=" + startTime +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.navercorp.pinpoint.common.server.bo.exception;

import java.util.Objects;

/**
* @author intr3p1d
*/
public class StackTraceElementWrapperBo {
private String className;
private String fileName;
private int lineNumber;
private String methodName;

public StackTraceElementWrapperBo(String className,
String fileName,
int lineNumber,
String methodName) {
this.className = Objects.requireNonNull(className, "className");
this.fileName = Objects.requireNonNull(fileName, "fileName");
this.lineNumber = lineNumber;
this.methodName = Objects.requireNonNull(methodName, "methodName");
}

public String getClassName() {
return className;
}

public void setClassName(String className) {
this.className = className;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public int getLineNumber() {
return lineNumber;
}

public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}

public String getMethodName() {
return methodName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.navercorp.pinpoint.grpc.trace.PSpan;
import com.navercorp.pinpoint.grpc.trace.PSpanChunk;
import com.navercorp.pinpoint.grpc.trace.PSpanEvent;
import com.navercorp.pinpoint.grpc.trace.PSpanEventException;
import com.navercorp.pinpoint.grpc.trace.PTransactionId;
import com.navercorp.pinpoint.io.SpanVersion;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -59,6 +60,8 @@ public class GrpcSpanBinder {

private static final AnnotationFactory<PAnnotation> annotationFactory = new AnnotationFactory<>(new GrpcAnnotationHandler());

private static final GrpcSpanEventExceptionBinder spanEventExceptionBinder = new GrpcSpanEventExceptionBinder();

public GrpcSpanBinder() {
}

Expand Down Expand Up @@ -224,6 +227,10 @@ private void bind(SpanEventBo spanEvent, PSpanEvent pSpanEvent, SpanEventBo prev
spanEvent.setExceptionInfo(exceptionInfo.getIntValue(), getExceptionMessage(exceptionInfo));
}

if(pSpanEvent.hasFlushedException()) {
final PSpanEventException pSpanEventException = pSpanEvent.getFlushedException();
spanEvent.setFlushedException(spanEventExceptionBinder.bind(pSpanEventException));
}
}

public SpanChunkBo bindSpanChunkBo(PSpanChunk pSpanChunk, Header header) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.navercorp.pinpoint.common.server.bo.grpc;

import com.navercorp.pinpoint.common.server.bo.exception.SpanEventExceptionBo;
import com.navercorp.pinpoint.common.server.bo.exception.StackTraceElementWrapperBo;
import com.navercorp.pinpoint.grpc.trace.PSpanEventException;
import com.navercorp.pinpoint.grpc.trace.PStackTraceElement;

import javax.annotation.Nullable;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author intr3p1d
*/
public class GrpcSpanEventExceptionBinder {

public GrpcSpanEventExceptionBinder() {
}

@Nullable
public SpanEventExceptionBo bind(PSpanEventException pSpanEventException) {
if (pSpanEventException == null) {
return null;
}
SpanEventExceptionBo spanEventExceptionBo = new SpanEventExceptionBo();

spanEventExceptionBo.setExceptionClassName(pSpanEventException.getExceptionClassName());
spanEventExceptionBo.setExceptionMessage(pSpanEventException.getExceptionMessage());

spanEventExceptionBo.setStackTraceElements(
getStackTraceElements(pSpanEventException.getStackTraceElementList())
);

spanEventExceptionBo.setStartTime(pSpanEventException.getStartTime());

return spanEventExceptionBo;
}

private List<StackTraceElementWrapperBo> getStackTraceElements(List<PStackTraceElement> pStackTraceElementList) {
return pStackTraceElementList.stream().map(
this::getStackTraceElement
).collect(Collectors.toList());
}

private StackTraceElementWrapperBo getStackTraceElement(PStackTraceElement pStackTraceElement) {
return new StackTraceElementWrapperBo(
pStackTraceElement.getClassName(),
pStackTraceElement.getFileName(),
pStackTraceElement.getLineNumber(),
pStackTraceElement.getMethodName()
);
}
}
2 changes: 1 addition & 1 deletion grpc/grpc-idl
Submodule grpc-idl updated 1 files
+18 −0 proto/v1/Span.proto

0 comments on commit ab08007

Please sign in to comment.