Skip to content

Commit

Permalink
[#9115] Improve legacyCompatibilityCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed Aug 10, 2022
1 parent 7facdf2 commit 0a4bcff
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static Predicate<SpanBo> agentIdFilter(String agentId) {
return new SpanAgentIdPredicate(agentId);
}

// Also used in SpanQueryBuilder
private static class SpanAgentIdPredicate implements Predicate<SpanBo> {
private final String agentId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Predicate<SpanBo> newSpanFilter(TransactionId transactionId, SpanHint spanHint)
if (spanHint.getApplicationName() != null) {
builder.addFilter(SpanFilters.applicationIdFilter(spanHint.getApplicationName()));
}
if (spanHint.getAgentId() != null) {
builder.addFilter(SpanFilters.agentIdFilter(spanHint.getAgentId()));
}

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.common.server.bo.SpanBo;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.web.dao.ApplicationTraceIndexDao;
import com.navercorp.pinpoint.web.dao.TraceDao;
Expand All @@ -11,12 +12,11 @@
import com.navercorp.pinpoint.web.util.ListListUtils;
import com.navercorp.pinpoint.web.vo.GetTraceInfo;
import com.navercorp.pinpoint.web.vo.LimitedScanResult;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.web.vo.SpanHint;
import com.navercorp.pinpoint.web.vo.scatter.Dot;
import com.navercorp.pinpoint.web.vo.scatter.DotMetaData;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand Down Expand Up @@ -98,7 +98,8 @@ private LimitedScanResult<List<DotMetaData>> legacyCompatibilityCheck(String app

List<GetTraceInfo> query = buildQuery(applicationName, dots);
final List<List<SpanBo>> selectedSpans = traceDao.selectSpans(query);
List<SpanBo> spanList = ListListUtils.toList(selectedSpans, selectedSpans.size());
//List<SpanBo> spanList = ListListUtils.toList(selectedSpans, selectedSpans.size());
List<SpanBo> spanList = pickFirst(selectedSpans);
spanService.populateAgentName(spanList);

if (dots.size() != spanList.size()) {
Expand Down Expand Up @@ -172,9 +173,22 @@ private GetTraceInfo dotToGetTraceInfo(String applicationName, Dot dot) {
TransactionId transactionId = dot.getTransactionId();

SpanHint spanHint = new SpanHint(dot.getAcceptedTime(),
dot.getElapsedTime(), applicationName);
dot.getElapsedTime(), applicationName, dot.getAgentId());

return new GetTraceInfo(transactionId, spanHint);
}

private List<SpanBo> pickFirst(List<List<SpanBo>> spanLists) {
List<SpanBo> result = new ArrayList<>(spanLists.size());
for (List<SpanBo> candidates : spanLists) {
if (candidates.size() > 0) {
result.add(candidates.get(0));

if (candidates.size() > 1 && logger.isDebugEnabled()) {
logger.debug("heuristically avoid Legacy compatibility error, spanCandidate:{}", candidates);
}
}
}
return result;
}
}
18 changes: 18 additions & 0 deletions web/src/main/java/com/navercorp/pinpoint/web/vo/SpanHint.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public final class SpanHint {
private final long collectorAcceptorTime;
private final int responseTime;
private final String applicationName;
private final String agentId;

private final boolean isSet;

public SpanHint() {
this.collectorAcceptorTime = -1;
this.responseTime = -1;
this.applicationName = null;
this.agentId = null;

this.isSet = false;
}
Expand All @@ -44,6 +46,18 @@ public SpanHint(long collectorAcceptorTime, int responseTime, String application
Assert.isTrue(responseTime >= 0, "responseTime must be 'responseTime >= 0'");
this.responseTime = responseTime;
this.applicationName = applicationName;
this.agentId = null;

this.isSet = true;
}

public SpanHint(long collectorAcceptorTime, int responseTime, String applicationName, String agentId) {
Assert.isTrue(collectorAcceptorTime > 0, "collectorAcceptorTime must be 'collectorAcceptorTime > 0'");
this.collectorAcceptorTime = collectorAcceptorTime;
Assert.isTrue(responseTime >= 0, "responseTime must be 'responseTime >= 0'");
this.responseTime = responseTime;
this.applicationName = applicationName;
this.agentId = agentId;

this.isSet = true;
}
Expand All @@ -60,6 +74,10 @@ public String getApplicationName() {
return applicationName;
}

public String getAgentId() {
return agentId;
}

public boolean isSet() {
return isSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public void spanFilter() {
Assertions.assertTrue(filter.test(span));
}

@Test
public void spanFilterWithAgentId() {

SpanHint spanHint = new SpanHint(COLLECTOR_ACCEPTOR_TIME, RESPONSE_TIME, "appName", "agentId");

SpanQueryBuilder builder = new SpanQueryBuilder();
Predicate<SpanBo> filter = builder.newSpanFilter(txId, spanHint);

SpanBo span = new SpanBo();
span.setTransactionId(txId);
span.setCollectorAcceptTime(100);
span.setElapsed(200);
span.setApplicationId("appName");
span.setAgentId("agentId");

Assertions.assertTrue(filter.test(span));
}

@Test
public void spanFilter_false() {

Expand All @@ -46,6 +64,19 @@ public void spanFilter_false() {
Assertions.assertFalse(filter.test(span));
}

@Test
public void spanFilterWithAgentId_false() {

SpanHint spanHint = new SpanHint(COLLECTOR_ACCEPTOR_TIME, RESPONSE_TIME, "appName", "agentId");

SpanQueryBuilder builder = new SpanQueryBuilder();
Predicate<SpanBo> filter = builder.newSpanFilter(txId, spanHint);

SpanBo span = new SpanBo();

Assertions.assertFalse(filter.test(span));
}

@Test
public void spanFilter_txid() {

Expand Down Expand Up @@ -78,4 +109,15 @@ public void spanFilter_responseTime() {

Assertions.assertTrue(filter.test(span));
}

@Test
public void spanFilter_agentId() {

Predicate<SpanBo> filter = SpanFilters.agentIdFilter("agentId");

SpanBo span = new SpanBo();
span.setAgentId("agentId");

Assertions.assertTrue(filter.test(span));
}
}

0 comments on commit 0a4bcff

Please sign in to comment.