Skip to content
New issue

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

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

Already on GitHub? # to your account

add custom tags property #28

Merged
merged 12 commits into from
Apr 30, 2018
53 changes: 51 additions & 2 deletions src/main/java/jenkinsci/plugins/influxdb/InfluxDbPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ public class InfluxDbPublisher extends Notifier implements SimpleBuildStep{
*/
private Map<String, Object> customData;


/**
* custom data tags, especially in pipelines, where additional information is calculated
* or retrieved by Groovy functions which should be sent to InfluxDB
* This can easily be done by calling
*
* def myDataMapTags = [:]
* myDataMapTags['myKey'] = 'myValue'
* step([$class: 'InfluxDbPublisher', target: myTarget, customPrefix: 'myPrefix', customData: myDataMap, customDataTags: myDataMapTags])
*
* inside a pipeline script
*/
private Map<String, String> customDataTags;

/**
* custom data maps, especially in pipelines, where additional information is calculated
* or retrieved by Groovy functions which should be sent to InfluxDB.
Expand All @@ -96,6 +110,25 @@ public class InfluxDbPublisher extends Notifier implements SimpleBuildStep{
*/
private Map<String, Map<String, Object>> customDataMap;

/**
* custom tags that are sent to all measurements defined in customDataMaps.
*
* Example for a pipeline script:
*
* def myCustomDataMapTags = [:]
* def myCustomTags = [:]
* myCustomTags["buildResult"] = currentBuild.result
* myCustomTags["NODE_LABELS"] = env.NODE_LABELS
* myCustomDataMapTags["series1"] = myCustomTags
* step([$class: 'InfluxDbPublisher',
* target: myTarget,
* customPrefix: 'myPrefix',
* customDataMap: myCustomDataMap,
* customDataMapTags: myCustomDataMapTags])
*/

private Map<String, Map<String, String>> customDataMapTags;

public InfluxDbPublisher() {
}

Expand Down Expand Up @@ -148,6 +181,15 @@ public Map<String, Object> getCustomData() {
return customData;
}

@DataBoundSetter
public void setCustomDataTags(Map<String, String> customDataTags) {
this.customDataTags = customDataTags;
}

public Map<String, String> getCustomDataTags() {
return customDataTags;
}

@DataBoundSetter
public void setCustomDataMap(Map<String, Map<String, Object>> customDataMap) {
this.customDataMap = customDataMap;
Expand All @@ -157,6 +199,13 @@ public Map<String, Map<String, Object>> getCustomDataMap() {
return customDataMap;
}

@DataBoundSetter
public void setCustomDataMapTags(Map<String, Map<String, String>> customDataMapTags) {
this.customDataMapTags = customDataMapTags;
}

public Map<String, Map<String, String>> getCustomDataMapTags() { return customDataMapTags; }

public Target getTarget() {
Target[] targets = DESCRIPTOR.getTargets();
if (selectedTarget == null && targets.length > 0) {
Expand Down Expand Up @@ -239,13 +288,13 @@ public Request authenticate(Route route, Response response) throws IOException {
JenkinsBasePointGenerator jGen = new JenkinsBasePointGenerator(measurementRenderer, customPrefix, build);
addPoints(pointsToWrite, jGen, listener);

CustomDataPointGenerator cdGen = new CustomDataPointGenerator(measurementRenderer, customPrefix, build, customData);
CustomDataPointGenerator cdGen = new CustomDataPointGenerator(measurementRenderer, customPrefix, build, customData, customDataTags);
if (cdGen.hasReport()) {
listener.getLogger().println("[InfluxDB Plugin] Custom data found. Writing to InfluxDB...");
addPoints(pointsToWrite, cdGen, listener);
}

CustomDataMapPointGenerator cdmGen = new CustomDataMapPointGenerator(measurementRenderer, customPrefix, build, customDataMap);
CustomDataMapPointGenerator cdmGen = new CustomDataMapPointGenerator(measurementRenderer, customPrefix, build, customDataMap, customDataMapTags);
if (cdmGen.hasReport()) {
listener.getLogger().println("[InfluxDB Plugin] Custom data map found. Writing to InfluxDB...");
addPoints(pointsToWrite, cdmGen, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ public class CustomDataMapPointGenerator extends AbstractPointGenerator {
private final Run<?, ?> build;
private final String customPrefix;
Map<String, Map<String, Object>> customDataMap;
Map<String, Map<String, String>> customDataMapTags;

public CustomDataMapPointGenerator(MeasurementRenderer<Run<?,?>> projectNameRenderer, String customPrefix, Run<?, ?> build, Map customDataMap) {
public CustomDataMapPointGenerator(MeasurementRenderer<Run<?,?>> projectNameRenderer, String customPrefix,
Run<?, ?> build, Map<String, Map<String, Object>> customDataMap,
Map<String, Map<String, String>> customDataMapTags) {
super(projectNameRenderer);
this.build = build;
this.customPrefix = customPrefix;
this.customDataMap = customDataMap;
this.customDataMapTags = customDataMapTags;
}

public boolean hasReport() {
Expand All @@ -28,10 +32,20 @@ public boolean hasReport() {
public Point[] generate() {
List<Point> customPoints = new ArrayList<Point>();
Set<String> customKeys = customDataMap.keySet();

for (String key : customKeys) {
Point point = buildPoint(measurementName(key), customPrefix, build)
.fields(customDataMap.get(key))
.build();
Point.Builder pointBuilder = buildPoint(measurementName(key), customPrefix, build)
.fields(customDataMap.get(key));

Map<String, String> customTags = customDataMapTags.get(key);
if (customTags != null) {
if (customTags.size() > 0){
pointBuilder = pointBuilder.tag(customTags);
}
}

Point point = pointBuilder.build();

customPoints.add(point);
}
return customPoints.toArray(new Point[customPoints.size()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ public class CustomDataPointGenerator extends AbstractPointGenerator {
private final Run<?, ?> build;
private final String customPrefix;
Map<String, Object> customData;
Map<String, String> customDataTags;

public CustomDataPointGenerator(MeasurementRenderer<Run<?,?>> projectNameRenderer, String customPrefix, Run<?, ?> build, Map customData) {
public CustomDataPointGenerator(MeasurementRenderer<Run<?,?>> projectNameRenderer, String customPrefix,
Run<?, ?> build, Map customData, Map<String, String> customDataTags) {
super(projectNameRenderer);
this.build = build;
this.customPrefix = customPrefix;
this.customData = customData;
this.customDataTags = customDataTags;
}

public boolean hasReport() {
Expand All @@ -29,10 +32,19 @@ public Point[] generate() {
long startTime = build.getTimeInMillis();
long currTime = System.currentTimeMillis();
long dt = currTime - startTime;
Point point = buildPoint(measurementName("jenkins_custom_data"), customPrefix, build)

Point.Builder pointBuilder = buildPoint(measurementName("jenkins_custom_data"), customPrefix, build)
.addField(BUILD_TIME, build.getDuration() == 0 ? dt : build.getDuration())
.fields(customData)
.build();
.fields(customData);

if (customDataTags != null) {
if (customDataTags.size() > 0) {
pointBuilder = pointBuilder.tag(customDataTags);
}
}

Point point = pointBuilder.build();

return new Point[] {point};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public Point[] generate() {
.addField(BUILD_AGENT_NAME, getBuildAgentName())
.addField(PROJECT_BUILD_HEALTH, build.getParent().getBuildHealth().getScore())
.addField(PROJECT_LAST_SUCCESSFUL, getLastSuccessfulBuild())
.addField(PROJECT_LAST_STABLE, getLastStableBuild());
.addField(PROJECT_LAST_STABLE, getLastStableBuild())
.tag(BUILD_RESULT, result);

if(hasTestResults(build)) {
point.addField(TESTS_FAILED, build.getAction(AbstractTestResultAction.class).getFailCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public void before() {
@Test
public void hasReportTest() {
//check with customDataMap = null
CustomDataMapPointGenerator cdmGen1 = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, null);
CustomDataMapPointGenerator cdmGen1 = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build,
null, null);
Assert.assertFalse(cdmGen1.hasReport());

//check with empty customDataMap
CustomDataMapPointGenerator cdmGen2 = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, Collections.<String, Map<String, Object>>emptyMap());
CustomDataMapPointGenerator cdmGen2 = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build,
Collections.<String, Map<String, Object>>emptyMap(), Collections.<String, Map<String, String>>emptyMap());
Assert.assertFalse(cdmGen2.hasReport());
}

Expand All @@ -65,10 +67,16 @@ public void generateTest() {
customDataMap.put("series1", customData1);
customDataMap.put("series2", customData2);

Map<String, Map<String, String>> customDataMapTags = new HashMap<String, Map<String, String>>();
Map<String, String> customTags = new HashMap<String, String>();
customTags.put("build_result", "SUCCESS");
customDataMapTags.put("series1", customTags);


List<Point> pointsToWrite = new ArrayList<Point>();

CustomDataMapPointGenerator cdmGen = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, customDataMap);
CustomDataMapPointGenerator cdmGen = new CustomDataMapPointGenerator(measurementRenderer, CUSTOM_PREFIX, build,
customDataMap, customDataMapTags);
pointsToWrite.addAll(Arrays.asList(cdmGen.generate()));

String lineProtocol1;
Expand All @@ -80,7 +88,7 @@ public void generateTest() {
lineProtocol1 = pointsToWrite.get(1).lineProtocol();
lineProtocol2 = pointsToWrite.get(0).lineProtocol();
}
Assert.assertTrue(lineProtocol1.startsWith("series1,prefix=test_prefix,project_name=test_prefix_master build_number=11i,project_name=\"test_prefix_master\",project_path=\"folder/master\",test1=11i,test2=22i"));
Assert.assertTrue(lineProtocol1.startsWith("series1,build_result=SUCCESS,prefix=test_prefix,project_name=test_prefix_master build_number=11i,project_name=\"test_prefix_master\",project_path=\"folder/master\",test1=11i,test2=22i"));
Assert.assertTrue(lineProtocol2.startsWith("series2,prefix=test_prefix,project_name=test_prefix_master build_number=11i,project_name=\"test_prefix_master\",project_path=\"folder/master\",test3=33i,test4=44i"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public void before() {
@Test
public void hasReportTest() {
//check with customDataMap = null
CustomDataPointGenerator cdGen1 = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, null);
CustomDataPointGenerator cdGen1 = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, null, null);
Assert.assertFalse(cdGen1.hasReport());

//check with empty customDataMap
CustomDataPointGenerator cdGen2 = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, Collections.<String, Map<String, Object>>emptyMap());
CustomDataPointGenerator cdGen2 = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, Collections.<String, Map<String, Object>>emptyMap(), null);
Assert.assertFalse(cdGen2.hasReport());
}

Expand All @@ -55,13 +55,17 @@ public void generateTest() {
customData.put("test1", 11);
customData.put("test2", 22);

Map<String, String> customDataTags = new HashMap<String, String>();
customDataTags.put("tag1", "myTag");


List<Point> pointsToWrite = new ArrayList<Point>();

CustomDataPointGenerator cdGen = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, customData);
CustomDataPointGenerator cdGen = new CustomDataPointGenerator(measurementRenderer, CUSTOM_PREFIX, build, customData, customDataTags);
pointsToWrite.addAll(Arrays.asList(cdGen.generate()));

String lineProtocol = pointsToWrite.get(0).lineProtocol();
Assert.assertTrue(lineProtocol.startsWith("jenkins_custom_data,prefix=test_prefix,project_name=test_prefix_master build_number=11i,build_time="));
Assert.assertTrue(lineProtocol.startsWith("jenkins_custom_data,prefix=test_prefix,project_name=test_prefix_master,tag1=myTag build_number=11i,build_time="));
Assert.assertTrue(lineProtocol.indexOf("project_name=\"test_prefix_master\",project_path=\"folder/master\",test1=11i,test2=22i")>0);
}
}