Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-70086

Splunk plugin - Event enrichment

    XMLWordPrintable

Details

    • Task
    • Status: Resolved (View Workflow)
    • Minor
    • Resolution: Fixed
    • splunk-devops-plugin
    • None
    • Jenkins 2.361.2
      Splunk Plugin 1.10.0

    Description

      I have the Splunk plugin correctly configured and events are sent to the Splunk.

      I would like to send a extra field (Jenkins env. var.) in the result event after each pipeline build.

      How to proceed ? I have tried to use "Customize Event Processing Command" without any success. See:

      https://community.splunk.com/t5/Getting-Data-In/How-to-send-few-values-from-jenkins-pipeline-to-splunk-and/m-p/384470 
      https://wiki.jenkins-ci.org/display/JENKINS/Splunk-Plugin-for-Jenkins.html 

      Attachments

        Activity

          fengxx Ted Xiao added a comment -

          You need admin access to Jenkins->Configure System->Splunk for Jenkins Configuration->Advanced ->Customize Event Processing Script

          // a map of detailed event
          def event=["foo":"bar", "foo1":"bar1"] 
          splunkins.send(event)
          

          I don't know what happened until screenshot of your config, job logs or error message provided

          fengxx Ted Xiao added a comment - You need admin access to Jenkins->Configure System->Splunk for Jenkins Configuration->Advanced ->Customize Event Processing Script // a map of detailed event def event=[ "foo" : "bar" , "foo1" : "bar1" ] splunkins.send(event) I don't know what happened until screenshot of your config, job logs or error message provided
          gbegin Gautier BEGIN added a comment -

          I found it:

          To activate the "Customize Event Processing Command", you have to activate the data sources "Build Event" and "Build Report" too.

           

          Here my code for who would like to use this plugin (more detailed than the current documentation)

          /**
           * Transform job metadata before sending to splunk
           * This script is configured in Jenkins->Configure System->Splunk for Jenkins Configuration->Advanced
           *  ->Customize Event Processing Command
           */
          
          import groovy.json.JsonSlurperClassic
          import hudson.model.*
          import com.splunk.splunkjenkins.model.CoverageMetricsAdapter
          import com.splunk.splunkjenkins.utils.LogEventHelper
          import org.apache.commons.codec.digest.DigestUtils
          import com.splunk.splunkjenkins.utils.SplunkLogService
          import com.splunk.splunkjenkins.model.EventType
          
          /**
           * @param run Jenkins job Run
           * @return the upstream job url and build number
           */
          
          def getUpStreamBuild(Run build) {
              for (CauseAction action : build.getActions(CauseAction.class)) {
                  Cause.UpstreamCause upstreamCause = action.findCause(Cause.UpstreamCause.class)
                  if (upstreamCause != null) {
                      return [upstreamCause.upstreamUrl, upstreamCause.upstreamBuild, upstreamCause.upstreamProject]
                  }
              }
              return [build.parent.url, build.number, build.parent.fullName]
          }
          
          def isRebuild(String cause) {
              return cause?.contains("Rebuilds build")
          }
          
          def hexDecode(encodedString) {
              transformedString = ""
              c = 0
              encodedString.split('%').each{
                  if ( c > 0 ) {
                      hexNumber = it.substring(0, 2)
                      remainingString = it.substring(2)
                      decodedString = (char)Integer.parseInt(hexNumber, 16)
                      newPart = decodedString.toString() + remainingString
                  } else {
                      newPart = it
                  }
                  transformedString = transformedString + newPart
                  c = c + 1
              }
              return transformedString
          }
          
          try {
              //junit report with page size set to 50, each page has maximum 50 test cases.
              //need ignore AggregatedTestResultAction since we already send downstream results
              def build = splunkins.build
              def metadata = LogEventHelper.getBuildVariables(build)
              def upStream = getUpStreamBuild(build)
              def buildEnv = LogEventHelper.getEnvironment(build)
          
              try {
                  Executor executor = build.getExecutor();
                  Owner = executor.getOwner()
                  Node execNode = Owner.getNode();
                  label = execNode.getLabelString();        hostname = Owner.hostName
                  defaultNodeName = Owner.url.split('/')[1]
                  try {
                      nodeName = Owner.getName()
                      if (StringUtils.isEmpty(nodeName)) { nodeName = defaultNodeName }
                  } catch(Exception e1) { nodeName = defaultNodeName }
                  nodeName = hexDecode(nodeName)
              } catch(Exception e1) {
                  fh1.append "ERROR build discovery: ${e1}\n"
              }
          
              // In the Case the job was launched by another job
              //metadata["root_trigger"] = upStream[0]
              //metadata["root_trigger_build_no"] = upStream[1]
              def causes = LogEventHelper.getBuildCauses(build)
              def rebuildFlag = isRebuild(causes)
              //end of metadata
          
          
              def event = [
                      "build_number"     : buildEnv["BUILD_NUMBER"],
                      "build_url"        : buildEnv["BUILD_URL"],
                      "job_name"         : buildEnv["JOB_NAME"],
                      "event_tag"        : "custom",
                      "label"            : label,
                      "metadata"         : metadata,
                      "node"             : nodeName,
                      "hostname"         : hostname,
                      "trigger_by"       : causes,
                      "job_result"       : build.result["name"],
                      "job_started_at"   : build.timestampString2,
                      "job_duration"     : LogEventHelper.getRunDuration(build),
                      "user"             : LogEventHelper.getTriggerUserName(build)
              ]
          
             result = splunkins.send(event)
          
          } catch(Exception e1) {
             println "ERROR Splunk Event: ${e1}\n"
          }
           
          gbegin Gautier BEGIN added a comment - I found it: To activate the "Customize Event Processing Command", you have to activate the data sources "Build Event" and "Build Report" too.   Here my code for who would like to use this plugin (more detailed than the current documentation) /**  * Transform job metadata before sending to splunk  * This script is configured in Jenkins->Configure System ->Splunk for Jenkins Configuration->Advanced  *  ->Customize Event Processing Command  */ import groovy.json.JsonSlurperClassic import hudson.model.* import com.splunk.splunkjenkins.model.CoverageMetricsAdapter import com.splunk.splunkjenkins.utils.LogEventHelper import org.apache.commons.codec.digest.DigestUtils import com.splunk.splunkjenkins.utils.SplunkLogService import com.splunk.splunkjenkins.model.EventType /**  * @param run Jenkins job Run  * @ return the upstream job url and build number  */ def getUpStreamBuild(Run build) {     for (CauseAction action : build.getActions(CauseAction.class)) {         Cause.UpstreamCause upstreamCause = action.findCause(Cause.UpstreamCause.class)         if (upstreamCause != null ) {             return [upstreamCause.upstreamUrl, upstreamCause.upstreamBuild, upstreamCause.upstreamProject]         }     }     return [build.parent.url, build.number, build.parent.fullName] } def isRebuild( String cause) {     return cause?.contains( "Rebuilds build" ) } def hexDecode(encodedString) {     transformedString = ""     c = 0     encodedString.split( '%' ).each{         if ( c > 0 ) {             hexNumber = it.substring(0, 2)             remainingString = it.substring(2)             decodedString = ( char ) Integer .parseInt(hexNumber, 16)             newPart = decodedString.toString() + remainingString         } else {             newPart = it         }         transformedString = transformedString + newPart         c = c + 1     }     return transformedString } try {     //junit report with page size set to 50, each page has maximum 50 test cases.     //need ignore AggregatedTestResultAction since we already send downstream results     def build = splunkins.build     def metadata = LogEventHelper.getBuildVariables(build)     def upStream = getUpStreamBuild(build)     def buildEnv = LogEventHelper.getEnvironment(build)     try {         Executor executor = build.getExecutor();         Owner = executor.getOwner()         Node execNode = Owner.getNode();         label = execNode.getLabelString();        hostname = Owner.hostName         defaultNodeName = Owner.url.split( '/' )[1]         try {             nodeName = Owner.getName()             if (StringUtils.isEmpty(nodeName)) { nodeName = defaultNodeName }         } catch (Exception e1) { nodeName = defaultNodeName }         nodeName = hexDecode(nodeName)     } catch (Exception e1) {         fh1.append "ERROR build discovery: ${e1}\n"     }     // In the Case the job was launched by another job     //metadata[ "root_trigger" ] = upStream[0]     //metadata[ "root_trigger_build_no" ] = upStream[1]     def causes = LogEventHelper.getBuildCauses(build)     def rebuildFlag = isRebuild(causes)     //end of metadata   def event = [             "build_number"     : buildEnv[ "BUILD_NUMBER" ],             "build_url"        : buildEnv[ "BUILD_URL" ],             "job_name"         : buildEnv[ "JOB_NAME" ],             "event_tag"        : "custom" ,             "label"            : label,             "metadata"         : metadata,             "node"             : nodeName,             "hostname"         : hostname,             "trigger_by"       : causes,             "job_result"       : build.result[ "name" ],             "job_started_at"   : build.timestampString2,             "job_duration"     : LogEventHelper.getRunDuration(build),             "user"             : LogEventHelper.getTriggerUserName(build)     ]  result = splunkins.send(event) } catch (Exception e1) {    println "ERROR Splunk Event: ${e1}\n" }

          People

            fengxx Ted Xiao
            gbegin Gautier BEGIN
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: