Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stage breakdown to Jenkins Build/Pipelines traces. #158

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.datadog.jenkins.plugins.datadog.model;

import hudson.model.InvisibleAction;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
* Keeps the Stage breakdown related information.
*/
public class StageBreakdownAction extends InvisibleAction implements Serializable {

private static final long serialVersionUID = 1L;

private final Map<String, StageData> stageDataByName;

public StageBreakdownAction() {
this.stageDataByName = new HashMap<>();
}

public Map<String, StageData> getStageDataByName() {
return stageDataByName;
}

public void put(String name, StageData stageData) {
this.stageDataByName.put(name, stageData);
}
}
112 changes: 112 additions & 0 deletions src/main/java/org/datadog/jenkins/plugins/datadog/model/StageData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.datadog.jenkins.plugins.datadog.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.Objects;

/**
* Keeps the information of a Stage to calculate the Stage breakdown.
*/
public class StageData implements Serializable, Comparable<StageData> {

private static final long serialVersionUID = 1L;

@Expose
private final String name;
@Expose(serialize = false)
private final long startTimeInMicros;
@Expose(serialize = false)
private final long endTimeInMicros;
@Expose
@SerializedName("duration")
private final long durationInNanos;

private StageData(final Builder builder) {
this.name = builder.name;
this.startTimeInMicros = builder.start;
this.endTimeInMicros = builder.end;
this.durationInNanos = (this.endTimeInMicros - this.startTimeInMicros) * 1000;
}

public String getName() {
return name;
}

public long getStartTimeInMicros() {
return startTimeInMicros;
}

public long getEndTimeInMicros() {
return endTimeInMicros;
}

public long getDurationInNanos() {
return durationInNanos;
}

public static Builder builder() {
return new Builder();
}

@Override
public int compareTo(StageData other) {
if(this.startTimeInMicros < other.getStartTimeInMicros()) {
return -1;
} else if(this.startTimeInMicros > other.getStartTimeInMicros()) {
return 1;
} else {
return 0;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StageData stageData = (StageData) o;
return startTimeInMicros == stageData.startTimeInMicros;
}

@Override
public int hashCode() {
return Objects.hash(startTimeInMicros);
}

public static class Builder {
private String name;
private long start;
private long end;

public Builder withName(final String name) {
this.name = name;
return this;
}

public Builder withStartTimeInMicros(final long start){
this.start = start;
return this;
}

public Builder withEndTimeInMicros(final long end) {
this.end = end;
return this;
}

public StageData build() {
return new StageData(this);
}
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("StageData{");
sb.append("name='").append(name).append('\'');
sb.append(", startTimeInMicros=").append(startTimeInMicros);
sb.append(", endTimeInMicros=").append(endTimeInMicros);
sb.append(", durationInMicros=").append(durationInNanos);
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class CITags {
public static final String _DD_HOSTNAME = "_dd.hostname";
public static final String _DD_CI_INTERNAL = "_dd.ci.internal";
public static final String _DD_CI_BUILD_LEVEL = "_dd.ci.build_level";
public static final String _DD_CI_STAGES = "_dd.ci.stages";

public static final String _ID = ".id";
public static final String _NAME = ".name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.datadog.jenkins.plugins.datadog.traces.GitInfoUtils.normalizeBranch;
import static org.datadog.jenkins.plugins.datadog.traces.GitInfoUtils.normalizeTag;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this package need to be added to pom.xml?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed because is inherited by the jenkins dependencies

import datadog.trace.api.DDTags;
import hudson.model.Result;
import hudson.model.Run;
Expand All @@ -13,8 +15,13 @@
import org.datadog.jenkins.plugins.datadog.DatadogUtilities;
import org.datadog.jenkins.plugins.datadog.model.BuildData;
import org.datadog.jenkins.plugins.datadog.model.BuildPipelineNode;
import org.datadog.jenkins.plugins.datadog.model.StageBreakdownAction;
import org.datadog.jenkins.plugins.datadog.model.StageData;
import org.datadog.jenkins.plugins.datadog.model.TimeInQueueAction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

Expand All @@ -26,9 +33,11 @@ public class DatadogTraceBuildLogic {
private static final Logger logger = Logger.getLogger(DatadogTraceBuildLogic.class.getName());

private final Tracer tracer;
private final Gson gson;

public DatadogTraceBuildLogic(final Tracer tracer) {
this.tracer = tracer;
this.gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}

public void startBuildTrace(final BuildData buildData, Run run) {
Expand Down Expand Up @@ -63,6 +72,9 @@ public void startBuildTrace(final BuildData buildData, Run run) {

final StepDataAction stepDataAction = new StepDataAction();
run.addAction(stepDataAction);

final StageBreakdownAction stageBreakdownAction = new StageBreakdownAction();
run.addAction(stageBreakdownAction);
}

public void finishBuildTrace(final BuildData buildData, final Run<?,?> run) {
Expand Down Expand Up @@ -168,6 +180,17 @@ public void finishBuildTrace(final BuildData buildData, final Run<?,?> run) {
}
}

// Stage breakdown
final StageBreakdownAction stageBreakdownAction = run.getAction(StageBreakdownAction.class);
if(stageBreakdownAction != null){
final Map<String, StageData> stageDataByName = stageBreakdownAction.getStageDataByName();
final List<StageData> stages = new ArrayList<>(stageDataByName.values());
Collections.sort(stages);

final String stagesJson = gson.toJson(stages);
buildSpan.setTag(CITags._DD_CI_STAGES, stagesJson);
}

// Jenkins specific
buildSpan.setTag(CITags.JENKINS_TAG, buildData.getBuildTag(""));
buildSpan.setTag(CITags.JENKINS_EXECUTOR_NUMBER, buildData.getExecutorNumber(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.datadog.jenkins.plugins.datadog.model.BuildPipelineNode;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
import org.datadog.jenkins.plugins.datadog.model.GitRepositoryAction;
import org.datadog.jenkins.plugins.datadog.model.StageBreakdownAction;
import org.datadog.jenkins.plugins.datadog.model.StageData;
import org.datadog.jenkins.plugins.datadog.util.git.GitUtils;
import org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode;
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
Expand Down Expand Up @@ -95,11 +97,12 @@ public void execute(Run run, FlowNode flowNode) {

final BuildData buildData = buildSpanAction.getBuildData();
if(!isLastNode(flowNode)){
updateBuildData(buildData, run, flowNode);
final BuildPipelineNode pipelineNode = buildPipelineNode(flowNode);
updateStageBreakdown(run, pipelineNode);
updateBuildData(buildData, run, pipelineNode, flowNode);
return;
}


final FlowEndNode flowEndNode = (FlowEndNode) flowNode;
final BuildPipeline pipeline = new BuildPipeline();

Expand All @@ -124,14 +127,7 @@ public void execute(Run run, FlowNode flowNode) {
}
}

private void updateBuildData(BuildData buildData, Run<?, ?> run, FlowNode node) {
BuildPipelineNode pipelineNode = null;
if(node instanceof BlockEndNode) {
pipelineNode = new BuildPipelineNode((BlockEndNode) node);
} else if(node instanceof StepAtomNode) {
pipelineNode = new BuildPipelineNode((StepAtomNode) node);
}

private void updateBuildData(BuildData buildData, Run<?, ?> run, BuildPipelineNode pipelineNode, FlowNode node) {
if(pipelineNode == null){
return;
}
Expand Down Expand Up @@ -260,8 +256,6 @@ private void sendTrace(final Tracer tracer, final BuildData buildData, final Bui
span.finish(current.getEndTimeMicros());
}



private Map<String, Object> buildTraceTags(final BuildPipelineNode current, final BuildData buildData) {
final String prefix = current.getType().getTagName();
final String buildLevel = current.getType().getBuildLevel();
Expand Down Expand Up @@ -441,7 +435,6 @@ private GitCommitAction buildGitCommitAction(Run<?, ?> run, BuildPipelineNode pi
}
}


private GitRepositoryAction buildGitRepositoryAction(Run<?, ?> run, BuildPipelineNode pipelineNode, FlowNode node) {
try {
final TaskListener listener = node.getExecution().getOwner().getListener();
Expand All @@ -454,4 +447,37 @@ private GitRepositoryAction buildGitRepositoryAction(Run<?, ?> run, BuildPipelin
return null;
}
}

private BuildPipelineNode buildPipelineNode(FlowNode flowNode) {
BuildPipelineNode pipelineNode = null;
if(flowNode instanceof BlockEndNode) {
pipelineNode = new BuildPipelineNode((BlockEndNode) flowNode);
} else if(flowNode instanceof StepAtomNode) {
pipelineNode = new BuildPipelineNode((StepAtomNode) flowNode);
}
return pipelineNode;
}

private void updateStageBreakdown(final Run<?,?> run, BuildPipelineNode pipelineNode) {
final StageBreakdownAction stageBreakdownAction = run.getAction(StageBreakdownAction.class);
if(stageBreakdownAction == null){
return;
}

if(pipelineNode == null){
return;
}

if(!BuildPipelineNode.NodeType.STAGE.equals(pipelineNode.getType())){
return;
}

final StageData stageData = StageData.builder()
.withName(pipelineNode.getName())
.withStartTimeInMicros(pipelineNode.getStartTimeMicros())
.withEndTimeInMicros(pipelineNode.getEndTimeMicros())
.build();

stageBreakdownAction.put(stageData.getName(), stageData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void testTraces() throws Exception {
assertNotNull(buildSpan.getTag(CITags._DD_HOSTNAME));
assertEquals("success", buildSpan.getTag(CITags.JENKINS_RESULT));
assertEquals("jenkins-buildIntegrationSuccess-1", buildSpan.getTag(CITags.JENKINS_TAG));
assertNotNull(buildSpan.getTag(CITags._DD_CI_STAGES));
assertEquals("[]", buildSpan.getTag(CITags._DD_CI_STAGES));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ public void testIntegrationNoFailureTag() throws Exception {
assertEquals("jenkins-pipelineIntegrationSuccess-1", buildSpan.getTag(CITags.JENKINS_TAG));
assertEquals(false, buildSpan.getTag(CITags._DD_CI_INTERNAL));
assertEquals(BuildPipelineNode.NodeType.PIPELINE.getBuildLevel(), buildSpan.getTag(CITags._DD_CI_BUILD_LEVEL));
assertNotNull(buildSpan.getTag(CITags._DD_CI_STAGES));
assertTrue(((String) buildSpan.getTag(CITags._DD_CI_STAGES)).contains("{\"name\":\"test\",\"duration\""));

final List<DDSpan> pipelineTrace = tracerWriter.get(1);
assertEquals(4, pipelineTrace.size());
Expand Down