diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/DatadogBaseBuildLogic.java b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/DatadogBaseBuildLogic.java index c0a333a5..1cfee24a 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/traces/DatadogBaseBuildLogic.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/traces/DatadogBaseBuildLogic.java @@ -89,7 +89,13 @@ protected String getStageBreakdown(Run run) { return null; } - final List 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 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)); @@ -101,7 +107,14 @@ protected String getStageBreakdown(Run run) { return stagesJson; } - private List traverseStages(List 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 traverseStages(List heads, int limit) { List stages = new ArrayList<>(); Queue nodes = new ArrayDeque<>(heads); while (!nodes.isEmpty()) { @@ -126,6 +139,11 @@ private List traverseStages(List 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)