github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/telemetry/data.go (about)

     1  package telemetry
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  )
     7  
     8  // BaseData object definition containing the base data and it's mapping information
     9  type BaseData struct {
    10  	// SWA receives the fields custom1 - custom30 and e_a, e_2 - e_30 for custom values.
    11  	ActionName      string `json:"action_name"`
    12  	EventType       string `json:"event_type"`
    13  	SiteID          string `json:"idsite"`
    14  	URL             string `json:"url"`
    15  	StepName        string `json:"e_3"` // set by step generator
    16  	StageName       string `json:"e_10"`
    17  	PipelineURLHash string `json:"e_4"`  // defaults to sha1 of provider.GetBuildURL()
    18  	BuildURLHash    string `json:"e_5"`  // defaults to sha1 of provider.GetJobURL()
    19  	Orchestrator    string `json:"e_14"` // defaults to provider.OrchestratorType()
    20  }
    21  
    22  var baseData BaseData
    23  
    24  // BaseMetaData object definition containing the labels for the base data, and it's mapping information
    25  type BaseMetaData struct {
    26  	// SWA receives the fields custom1 - custom30 and e_a, e_2 - e_30 for custom values.
    27  	StepNameLabel        string `json:"custom3"`
    28  	StageNameLabel       string `json:"custom10"`
    29  	PipelineURLHashLabel string `json:"custom4"`
    30  	BuildURLHashLabel    string `json:"custom5"`
    31  	DurationLabel        string `json:"custom11,omitempty"`
    32  	ExitCodeLabel        string `json:"custom12,omitempty"`
    33  	ErrorCategoryLabel   string `json:"custom13,omitempty"`
    34  	OrchestratorLabel    string `json:"custom14,omitempty"`
    35  	PiperCommitHashLabel string `json:"custom15,omitempty"`
    36  }
    37  
    38  // baseMetaData object containing the labels for the base data
    39  var baseMetaData BaseMetaData = BaseMetaData{
    40  	StepNameLabel:        "stepName",
    41  	StageNameLabel:       "stageName",
    42  	PipelineURLHashLabel: "pipelineUrlHash",
    43  	BuildURLHashLabel:    "buildUrlHash",
    44  	DurationLabel:        "duration",
    45  	ExitCodeLabel:        "exitCode",
    46  	ErrorCategoryLabel:   "errorCategory",
    47  	OrchestratorLabel:    "orchestrator",
    48  	PiperCommitHashLabel: "piperCommitHash",
    49  }
    50  
    51  // CustomData object definition containing the data that can be set by a step, and it's mapping information
    52  type CustomData struct {
    53  	// SWA receives the fields custom1 - custom30 and e_a, e_2 - e_30 for custom values.
    54  	// Piper uses the values custom11 - custom25 & e_11 - e_25 for library related reporting
    55  	// and custom26 - custom30 & e_26 - e_30 for step  related reporting.
    56  	Duration        string `json:"e_11,omitempty"`
    57  	ErrorCode       string `json:"e_12,omitempty"`
    58  	ErrorCategory   string `json:"e_13,omitempty"`
    59  	PiperCommitHash string `json:"e_15,omitempty"`
    60  	Custom1Label    string `json:"custom26,omitempty"`
    61  	Custom2Label    string `json:"custom27,omitempty"`
    62  	Custom3Label    string `json:"custom28,omitempty"`
    63  	Custom4Label    string `json:"custom29,omitempty"`
    64  	Custom5Label    string `json:"custom30,omitempty"`
    65  	Custom1         string `json:"e_26,omitempty"`
    66  	Custom2         string `json:"e_27,omitempty"`
    67  	Custom3         string `json:"e_28,omitempty"`
    68  	Custom4         string `json:"e_29,omitempty"`
    69  	Custom5         string `json:"e_30,omitempty"`
    70  }
    71  
    72  // StepTelemetryData definition for telemetry reporting and monitoring
    73  type StepTelemetryData struct {
    74  	StepStartTime   string                 `json:"StepStartTime"`
    75  	PipelineURLHash string                 `json:"PipelineURLHash"`
    76  	BuildURLHash    string                 `json:"BuildURLHash"`
    77  	StageName       string                 `json:"StageName"`
    78  	StepName        string                 `json:"StepName"`
    79  	ErrorCode       string                 `json:"ErrorCode"`
    80  	StepDuration    string                 `json:"StepDuration"`
    81  	ErrorCategory   string                 `json:"ErrorCategory"`
    82  	CorrelationID   string                 `json:"CorrelationID"`
    83  	PiperCommitHash string                 `json:"PiperCommitHash"`
    84  	ErrorDetail     map[string]interface{} `json:"ErrorDetail"`
    85  }
    86  
    87  // Data object definition containing all telemetry data
    88  type Data struct {
    89  	BaseData
    90  	BaseMetaData
    91  	CustomData
    92  }
    93  
    94  // toMap transfers the data object into a map using JSON tags
    95  func (d *Data) toMap() (result map[string]string) {
    96  	jsonObj, _ := json.Marshal(d)
    97  	json.Unmarshal(jsonObj, &result)
    98  	return
    99  }
   100  
   101  // toPayloadString transfers the data object into a 'key=value&..' string
   102  func (d *Data) toPayloadString() string {
   103  	parameters := url.Values{}
   104  
   105  	for key, value := range d.toMap() {
   106  		if len(value) > 0 {
   107  			parameters.Add(key, value)
   108  		}
   109  	}
   110  
   111  	return parameters.Encode()
   112  }