0
votes

I’m trying to push stage data from Jenkins pipeline to influx db and I get issue as below

Issue: In jenkin builds console output, after each stage, I see : java.io.NotSerializableException: sun.net.www.protocol.http.HttpURLConnection

Jenkins pipeline 2.150.2 InfluxDB 1.6.2

Any suggestions would be helpful. I'm new to this topic.

Note: I have commented @NonCPS annotation. If I uncomment then it sends only first stage data and exits and fails to iterate for loop which has 20 stages data.

//Maps for Field type columns
myDataField1 = [:]
myDataField2 = [:]
myDataField3 = [:]

//Maps for Custom Field measurements
myCustomDataFields1 = [:]
myCustomDataFields2 = [:]
myCustomDataFields3 = [:]

//Maps for Tag type columns
 myDataTag1 = [:]
 myDataTag2 = [:]
 myDataTag3 = [:]

//Maps for Custom Tag measurements
myCustomDataTags1 = [:]
myCustomDataTags2 = [:]
myCustomDataTags3 = [:]

//@NonCPS
def pushStageData() {

def url_string = "${JENKINS_URL}job/ENO_ENG_TP/job/R421/13/wfapi/describe"
def get = new URL(url_string).openConnection();

get.addRequestProperty ("User-Agent","Mozilla/4.0");
get.addRequestProperty("Authorization", "Basic dZXZvceDIwMTk=");

//fetching the contents of the endpoint URL
def jsonText = get.getInputStream().getText();
//converting the text into JSON object using 
JsonSlurperClassic 
def jsonObject = new JsonSlurperClassic().parseText(jsonText)

// Extracting the details of all the stages present in that particular build number
for (int i=0; i<jsonObject.stages.size()-1; i++){ //size-1 to ignore the post stage
//populating the field type columns of InfluxDB measurements and pushing them to the map called myDataField1
def size = jsonObject.stages.size()-1
myDataField1['result'] = jsonObject.stages[i].status
myDataField1['duration'] = 
jsonObject.stages[i].durationMillis
myDataField1['stage_name'] = jsonObject.stages[i].name

//populating the tag type columns of InfluxDB 
measurements and pushing them to the map called 
myDataTag1
myDataTag1['result_tag'] = jsonObject.stages[i].status
myDataTag1['stage_name_tag'] = jsonObject.stages[i].name

//assigning field type columns to the measurement called CustomData
myCustomDataFields1['CustomData'] = myDataField1
//assigning tag type columns to the measurement called CustomData
myCustomDataTags1['CustomData'] = myDataTag1

//Push the data into influx instance
try
{ step([$class: 'InfluxDbPublisher', target: 'jenkins_data', customPrefix: null, customDataMapTags: myCustomDataTags1]) }

catch (err)
{ println ("pushStagData exception: " + err) }
}
}

Expected: I want to push each stages of jenkins pipeline data to influx db.

2
You have to use NonCPS. So, please provide the failure reason when using NonCPS.daggett
@daggett Thank you. When I use NonCPS it iterates for loop just one time(or first stages data) out of 20 stages and exits with status 'push data to influxdb is complete' with no exceptions.sridattas
Could you share what you have inside try-catch? Will it run if you replace it with println...?daggett
@daggett Thank you. If I replace with println in try it prints the output. Otherwise it prints below output with influxdb InfluxDB Plugin] Collecting data for publication in InfluxDB... [InfluxDB Plugin] Custom data map found. Writing to InfluxDB... [InfluxDB Plugin] Publishing data to: [url=walvw:8086, description=Jenkins_Data, username=, password=*****, database=KPI] [InfluxDB Plugin] Completed End of pipeline... SUCCESSsridattas

2 Answers

1
votes

Try to use jenkins' standard methods instead of creating objects manually. You can use httpRequest instead of URL. This should fix exception you are getting. You can also use readJson instead of JsonSlurperClassic (latter is optional since classic slurper is actually serializable).

1
votes

you just need to explicitly set get to null to make sure there's none non serializable object being instantiated.

def jsonText = get.getInputStream().getText();
get = null;