@@ -79,6 +79,9 @@ public List<SegmentRecord> queryByTraceId(String traceId, @Nullable Duration dur
7979
8080 @ Override
8181 public List <SegmentRecord > queryBySegmentIdList (List <String > segmentIdList , @ Nullable Duration duration ) throws IOException {
82+ if (CollectionUtils .isEmpty (segmentIdList )) {
83+ return Collections .emptyList ();
84+ }
8285 final boolean isColdStage = duration != null && duration .isColdStage ();
8386 TraceQueryResponse resp = queryTraceDebuggable (isColdStage , SegmentRecord .INDEX_NAME , getTimestampRange (duration ),
8487 new QueryBuilder <TraceQuery >() {
@@ -89,11 +92,14 @@ public void apply(TraceQuery query) {
8992 query .setOrderBy (new TraceQuery .OrderBy (SegmentRecord .START_TIME , AbstractQuery .Sort .DESC ));
9093 }
9194 });
92- return buildRecords (resp );
95+ return buildRecords (resp , segmentIdList , null , true );
9396 }
9497
9598 @ Override
9699 public List <SegmentRecord > queryByTraceIdWithInstanceId (List <String > traceIdList , List <String > instanceIdList , @ Nullable Duration duration ) throws IOException {
100+ if (CollectionUtils .isEmpty (traceIdList ) || CollectionUtils .isEmpty (instanceIdList )) {
101+ return Collections .emptyList ();
102+ }
97103 final boolean isColdStage = duration != null && duration .isColdStage ();
98104 TraceQueryResponse resp = queryTraceDebuggable (isColdStage , SegmentRecord .INDEX_NAME , getTimestampRange (duration ),
99105 new QueryBuilder <TraceQuery >() {
@@ -105,7 +111,7 @@ public void apply(TraceQuery query) {
105111 }
106112 });
107113
108- return buildRecords (resp );
114+ return buildRecords (resp , null , instanceIdList , false );
109115 }
110116
111117 @ Override
@@ -226,14 +232,22 @@ public void apply(TraceQuery query) {
226232 /**
227233 * Notice: this method not build the full SegmentRecord, only build the fields needed by ProfiledTraceSegments and ProfiledSegment
228234 */
229- private List <SegmentRecord > buildRecords (TraceQueryResponse resp ) throws IOException {
235+ private List <SegmentRecord > buildRecords (TraceQueryResponse resp ,
236+ @ Nullable List <String > segmentIdList ,
237+ @ Nullable List <String > instanceIdList ,
238+ boolean filterBySegmentId ) throws IOException {
230239 List <SegmentRecord > segmentRecords = new ArrayList <>();
231240 for (var t : resp .getTraces ()) {
232241 for (var wrapper : t .getSpansList ()) {
233242 SpanWrapper spanWrapper = SpanWrapper .parseFrom (wrapper .getSpan ());
234243 if (spanWrapper .getSource ().equals (Source .SKYWALKING )) {
235244 SegmentObject segmentObject = SegmentObject .parseFrom (spanWrapper .getSpan ());
236245 SegmentRecord segmentRecord = new SegmentRecord ();
246+ if (filterBySegmentId ) {
247+ if (segmentIdList == null || !segmentIdList .contains (segmentObject .getTraceSegmentId ())) {
248+ continue ;
249+ }
250+ }
237251 segmentRecord .setSegmentId (segmentObject .getTraceSegmentId ());
238252 segmentRecord .setTraceId (segmentObject .getTraceId ());
239253 String serviceName = Const .EMPTY_STRING ;
@@ -244,6 +258,11 @@ private List<SegmentRecord> buildRecords(TraceQueryResponse resp) throws IOExcep
244258 String serviceId = IDManager .ServiceID .buildId (serviceName , true );
245259 segmentRecord .setServiceId (serviceId );
246260 String serviceInstanceId = IDManager .ServiceInstanceID .buildId (serviceId , instanceName );
261+ if (!filterBySegmentId ) {
262+ if (instanceIdList == null || !instanceIdList .contains (serviceInstanceId )) {
263+ continue ;
264+ }
265+ }
247266 segmentRecord .setServiceInstanceId (serviceInstanceId );
248267 long startTimestamp = 0 ;
249268 long endTimestamp = 0 ;
0 commit comments