Skip to content

Commit

Permalink
feat : add cause exception in span tree
Browse files Browse the repository at this point in the history
  • Loading branch information
JiwonKKang committed Nov 12, 2024
1 parent 2490f34 commit 321c192
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.univ.tracedin.domain.span.SpanEvent;

public record StackTraceResponse(String exceptionType, String message, String stackTrace) {
public record SpanExceptionResponse(String exceptionType, String message, String stackTrace) {

public static StackTraceResponse from(SpanEvent event) {
return new StackTraceResponse(
public static SpanExceptionResponse from(SpanEvent event) {
return new SpanExceptionResponse(
event.attributes().get("exception.type").toString(),
event.attributes().get("exception.message").toString(),
event.attributes().get("exception.stacktrace").toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.univ.tracedin.api.span.dto;

import static com.univ.tracedin.domain.span.Span.ANOMALY;

import java.time.LocalDateTime;
import java.util.Map;

Expand All @@ -25,9 +23,7 @@ public record SpanResponse(
Map<String, Object> data,
Integer capacity,
Integer totalAddedValues,
boolean anomaly,
boolean error,
StackTraceResponse stackTrace) {
boolean anomaly) {

public static SpanResponse from(Span span) {
return new SpanResponse(
Expand All @@ -46,8 +42,6 @@ public static SpanResponse from(Span span) {
span.getAttributes().data(),
span.getAttributes().capacity(),
span.getAttributes().totalAddedValues(),
span.getAttributes().data().containsKey(ANOMALY),
span.isError(),
span.isError() ? StackTraceResponse.from(span.getExceptionEvent()) : null);
span.isAnomaly());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,27 @@
import com.univ.tracedin.domain.span.SpanTree;

@JsonInclude(value = NON_EMPTY)
public record SpanTreeResponse(SpanResponse span, List<SpanTreeResponse> children) {
public record SpanTreeResponse(
SpanResponse span,
List<SpanTreeResponse> children,
boolean hasAnomaly,
boolean hasError,
SpanExceptionResponse stackTrace) {

public static SpanTreeResponse from(SpanTree spanTree) {
return from(spanTree, true); // 최상위 노드 플래그 설정
}

private static SpanTreeResponse from(SpanTree spanTree, boolean isRoot) {
return new SpanTreeResponse(
SpanResponse.from(spanTree.getSpan()),
spanTree.getChildren().stream().map(SpanTreeResponse::from).toList());
spanTree.getChildren().stream()
.map(child -> from(child, false)) // 하위 노드는 최상위가 아님
.toList(),
spanTree.hasAnomalySpan(),
spanTree.hasErrorSpan(),
isRoot && spanTree.hasErrorSpan() // 최상위 노드에서만 stackTrace 설정
? SpanExceptionResponse.from(spanTree.findExceptionCause())
: null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public LocalDateTime getStartDateTime() {
return timing.startDateTime();
}

public boolean isAnomaly() {
return attributes.data().containsKey(ANOMALY);
}

public boolean isError() {
return status.isError();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.univ.tracedin.domain.span;

public record SpanException(String type, String message, String stackTrace) {}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,56 @@ public static SpanTree init(Span span) {
}

public SpanId getParentId() {
SpanId spanId = new SpanId();
return span.getParentId();
}

public void addChild(SpanTree child) {
children.add(child);
}

public SpanEvent findExceptionCause() {
final SpanEvent currentCause = span.getExceptionEvent();

SpanEvent causeException = null;
for (SpanTree child : children) {
final SpanEvent childCause = child.findExceptionCause();
if (childCause != null) {
causeException = childCause;
}
}

return causeException != null ? causeException : currentCause;
}

public boolean hasAnomalySpan() {
boolean isAnomaly = false;

if (span.isAnomaly()) {
isAnomaly = true;
} else {
for (SpanTree child : children) {
if (child.hasAnomalySpan()) {
isAnomaly = true;
break;
}
}
}
return isAnomaly;
}

public boolean hasErrorSpan() {
boolean isError = false;

if (span.isError()) {
isError = true;
} else {
for (SpanTree child : children) {
if (child.hasErrorSpan()) {
isError = true;
break;
}
}
}
return isError;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ private static Query createSpanQuery(String projectKey, SpanType spanType, SpanK
r.field("startEpochMillis")
.gte(
JsonData.of(
"now-1m"))))))
"now-5s"))))))
._toQuery();
}

Expand Down

0 comments on commit 321c192

Please sign in to comment.