Skip to content
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
9 changes: 9 additions & 0 deletions doc/available_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,12 @@ Tags specific for this measurement:
| subtask_count | int | Amount of subtasks | |
| total_duration | long | Build duration in milliseconds from when it entered the queue until it was finished. | |
| waiting_time | long | Milliseconds in the queue waiting before it could be considered for execution. | |

### Git plugin

#### `git_data` (since 3.4)
| Metric | Type | Description | Introduced in |
| --- | --- | --- | --- |
| git_repository | string | URL of the Git repository used by the build | |
| git_revision | string | SHA-1 of the commit selected | |
| git_reference | string | reference of the branch | |
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ THE SOFTWARE.
<access-modifier-annotation.version>1.21</access-modifier-annotation.version>
<annotation-indexer.version>1.14</annotation-indexer.version>
<asm.version>9.1</asm.version>
<bridge-method-annotation.version>1.18</bridge-method-annotation.version>
<byte-buddy.version>1.9.10</byte-buddy.version>
<caffeine.version>2.9.2</caffeine.version>
<cobertura.version>1.12.1</cobertura.version>
Expand All @@ -58,6 +59,7 @@ THE SOFTWARE.
<configuration-as-code.version>1.55.1</configuration-as-code.version>
<credentials.version>2.6.1.1</credentials.version>
<doxia-sink-api.version>1.1.2</doxia-sink-api.version>
<git-plugin.version>3.9.1</git-plugin.version>
<gson.version>2.8.9</gson.version>
<hpi.compatibleSinceVersion>3.0</hpi.compatibleSinceVersion>
<influxdb-client-java.version>4.1.0</influxdb-client-java.version>
Expand Down Expand Up @@ -166,6 +168,11 @@ THE SOFTWARE.
<artifactId>symbol-annotation</artifactId>
<version>${symbol-annotation.version}</version>
</dependency>
<dependency>
<groupId>com.infradna.tool</groupId>
<artifactId>bridge-method-annotation</artifactId>
<version>${bridge-method-annotation.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -345,6 +352,12 @@ THE SOFTWARE.
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>${git-plugin.version}</version>
<optional>true</optional>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ public void perform(Run<?, ?> build, TaskListener listener, EnvVars env) {
logger.log(Level.FINE, "Plugin skipped: Performance");
}

try {
GitPointGenerator gitGen = new GitPointGenerator(build, listener, measurementRenderer, timestamp, jenkinsEnvParameterTag, customPrefix);
if (gitGen.hasReport()) {
listener.getLogger().println("[InfluxDB Plugin] Git data found. Writing to InfluxDB...");
addPoints(pointsToWrite, gitGen, listener);
} else {
logger.log(Level.FINE, "Plugin skipped: Git");
}
} catch (NoClassDefFoundError ignore) {
logger.log(Level.FINE, "Plugin skipped: Git");
}

JUnitPointGenerator junitGen = new JUnitPointGenerator(build, listener, measurementRenderer, timestamp, jenkinsEnvParameterTag, customPrefix, env);
if (junitGen.hasReport()) {
listener.getLogger().println("[InfluxDB Plugin] JUnit data found. Writing to InfluxDB...");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package jenkinsci.plugins.influxdb.generators;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.influxdb.client.write.Point;

import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import jenkinsci.plugins.influxdb.renderer.ProjectNameRenderer;

/**
* @author Mathieu Delrocq
*/
public class GitPointGenerator extends AbstractPointGenerator {

// Point fields names
protected static final String GIT_REPOSITORY = "git_repository";
protected static final String GIT_REVISION = "git_revision";
protected static final String GIT_REFERENCE = "git_reference";
protected static final String UNIQUE_ID = "unique_id";

private String customPrefix;
private List<BuildData> gitActions;

public GitPointGenerator(Run<?, ?> build, TaskListener listener, ProjectNameRenderer projectNameRenderer,
long timestamp, String jenkinsEnvParameterTag, String customPrefix) {
super(build, listener, projectNameRenderer, timestamp, jenkinsEnvParameterTag);
this.customPrefix = customPrefix;
gitActions = build.getActions(BuildData.class);
}

/**
* Check if git infos are presents in the build
*
* @return true if present
*/
@Override
public boolean hasReport() {
return CollectionUtils.isNotEmpty(gitActions);
}

/**
* Generates Git Points with datas in Git plugins
*
* return Array of Point
*/
@Override
public Point[] generate() {
List<Point> points = new ArrayList<>();
String sha1String = null;
String branchName = null;
BuildData gitAction = null;
for (int i = 0; i < gitActions.size(); i++) {
gitAction = gitActions.get(i);
Revision revision = gitAction.getLastBuiltRevision();
if(revision != null) {
sha1String = revision.getSha1String();
Collection<Branch> branches = revision.getBranches();
if (CollectionUtils.isNotEmpty(branches)) {
branchName = branches.iterator().next().getName();
}
}
Point point = buildPoint("git_data", customPrefix, build)
.addTag(UNIQUE_ID, String.valueOf(i+1))
.addField(GIT_REPOSITORY, !CollectionUtils.isEmpty(gitAction.getRemoteUrls()) ? gitAction.getRemoteUrls().iterator().next() : "")//
.addField(GIT_REFERENCE, branchName)
.addField(GIT_REVISION, sha1String);
points.add(point);
}
return points.toArray(new Point[0]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package jenkinsci.plugins.influxdb.generators;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.influxdb.client.write.Point;

import hudson.model.HealthReport;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import jenkins.model.Jenkins;
import jenkinsci.plugins.influxdb.renderer.ProjectNameRenderer;

/**
* @author Mathieu Delrocq
*/
public class GitPointGeneratorTest {

private static final String CUSTOM_PREFIX = "test_prefix";
private static final String JOB_NAME = "job_name";
private static final String GIT_REPOSITORY = "repository";
private static final String GIT_REFERENCE = "reference";
private static final String GIT_REVISION = "revision";

private Run<?, ?> build;
private List<BuildData> gitActions;
private BuildData gitAction1;
private BuildData gitAction2;
private TaskListener listener;
private long currTime;
private ProjectNameRenderer measurementRenderer;
private Revision revision1;
private Revision revision2;
private Branch branch1;
private Branch branch2;

@Before
public void before() throws Exception {
// Global Mocks
listener = Mockito.mock(TaskListener.class);
currTime = System.currentTimeMillis();
measurementRenderer = new ProjectNameRenderer(CUSTOM_PREFIX, null);
Job<?, ?> job = Mockito.mock(Job.class);
Mockito.when(job.getName()).thenReturn(JOB_NAME);
Mockito.when(job.getRelativeNameFrom(Mockito.nullable(Jenkins.class))).thenReturn("folder/" + JOB_NAME);
Mockito.when(job.getBuildHealth()).thenReturn(new HealthReport());

build = Mockito.mock(Run.class);
Mockito.doReturn(job).when(build).getParent();
gitAction1 = Mockito.mock(BuildData.class);
gitAction2 = Mockito.mock(BuildData.class);
gitActions = new ArrayList<>();
gitActions.add(gitAction1);
gitActions.add(gitAction2);
branch1 = Mockito.mock(Branch.class);
branch2 = Mockito.mock(Branch.class);
revision1 = Mockito.mock(Revision.class);
revision2 = Mockito.mock(Revision.class);
Mockito.when(build.getActions(BuildData.class)).thenReturn(gitActions);
Mockito.when(gitAction1.getLastBuiltRevision()).thenReturn(revision1);
Mockito.when(gitAction2.getLastBuiltRevision()).thenReturn(revision2);
List<Branch> branches1 = Arrays.asList(branch1);
List<Branch> branches2 = Arrays.asList(branch2);
Mockito.when(revision1.getBranches()).thenReturn(branches1);
Mockito.when(revision2.getBranches()).thenReturn(branches2);
Mockito.when(branch1.getName()).thenReturn(GIT_REFERENCE);
Mockito.when(branch2.getName()).thenReturn(GIT_REFERENCE + "2");
Mockito.when(revision1.getSha1String()).thenReturn(GIT_REVISION);
Mockito.when(revision2.getSha1String()).thenReturn(GIT_REVISION + "2");
Set<String> remoteUrls1 = new HashSet<>();
remoteUrls1.add(GIT_REPOSITORY);
Set<String> remoteUrls2 = new HashSet<>();
remoteUrls2.add(GIT_REPOSITORY + "2");
Mockito.when(gitAction1.getRemoteUrls()).thenReturn(remoteUrls1);
Mockito.when(gitAction2.getRemoteUrls()).thenReturn(remoteUrls2);
}

@Test
public void test_with_datas() {
GitPointGenerator gen = new GitPointGenerator(build, listener, measurementRenderer, currTime, StringUtils.EMPTY,
CUSTOM_PREFIX);
assertTrue(gen.hasReport());
Point[] points = gen.generate();
assertTrue(points != null && points.length != 0);
assertTrue(points[0].hasFields());
String lineProtocol = points[0].toLineProtocol();
assertTrue(lineProtocol.contains("git_repository=\"repository\""));
assertTrue(lineProtocol.contains("git_revision=\"revision\""));
assertTrue(lineProtocol.contains("git_reference=\"reference\""));
String lineProtocol2 = points[1].toLineProtocol();
assertTrue(lineProtocol2.contains("git_repository=\"repository2\""));
assertTrue(lineProtocol2.contains("git_revision=\"revision2\""));
assertTrue(lineProtocol2.contains("git_reference=\"reference2\""));
}

}