Skip to content

Commit

Permalink
Short-circuit StageData creation in trace generation (#430)
Browse files Browse the repository at this point in the history
Co-authored-by: Mandeep Bedi <>
  • Loading branch information
msbit01 authored Jun 3, 2024
1 parent 91e4b97 commit d6122e0
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ protected String getStageBreakdown(Run run) {
return null;
}

final List<StageData> stages = traverseStages(currentHeads);
// Limit stage count to MAX_TAG_LENGTH since each stage will contribute at least 1 to the tag length json
final List<StageData> stages = traverseStages(currentHeads, MAX_TAG_LENGTH);
if (stages == null) {
logger.warning("Skipping sending stage to Datadog; stage breakdown is too large");
return null;
}

Collections.sort(stages);

final String stagesJson = JsonUtils.toJson(new ArrayList<>(stages));
Expand All @@ -101,7 +107,14 @@ protected String getStageBreakdown(Run run) {
return stagesJson;
}

private List<StageData> traverseStages(List<FlowNode> heads) {
/**
* Find all stages associated with heads, unless the number of stages exceeds the limit, in which case we
* short-circuit and omit stage data altogether.
* @param heads
* @param limit
* @return stages or null if there are more stages than the limit
*/
private List<StageData> traverseStages(List<FlowNode> heads, int limit) {
List<StageData> stages = new ArrayList<>();
Queue<FlowNode> nodes = new ArrayDeque<>(heads);
while (!nodes.isEmpty()) {
Expand All @@ -126,6 +139,11 @@ private List<StageData> traverseStages(List<FlowNode> heads) {
continue;
}

// If adding another stage would result in exceeding the limit then return instead
if (stages.size() >= limit) {
return null;
}

StageData stageData = new StageData.Builder()
.withName(startNode.getDisplayName())
.withStartTimeInMicros(startTimeMicros)
Expand Down

0 comments on commit d6122e0

Please sign in to comment.