...

Source file src/edge-infra.dev/pkg/f8n/devinfra/github-actions-exporter/metrics/get_workflow_runs_from_github.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/github-actions-exporter/metrics

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/google/go-github/v47/github"
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  // getFieldValue return value from run element which corresponds to field
    13  func getFieldValue(repo string, run github.WorkflowRun, field string) string {
    14  	switch field {
    15  	case "repo":
    16  		return repo
    17  	case "id":
    18  		return strconv.FormatInt(*run.ID, 10)
    19  	case "node_id":
    20  		return *run.NodeID
    21  	case "head_branch":
    22  		return *run.HeadBranch
    23  	case "head_sha":
    24  		return *run.HeadSHA
    25  	case "run_number":
    26  		return strconv.Itoa(*run.RunNumber)
    27  	case "workflow_id":
    28  		return strconv.FormatInt(*run.WorkflowID, 10)
    29  	case "workflow":
    30  		return *workflows[repo][*run.WorkflowID].Name
    31  	case "event":
    32  		return *run.Event
    33  	case "status":
    34  		return *run.Status
    35  	}
    36  	return ""
    37  }
    38  
    39  func getRelevantFields(repo string, run *github.WorkflowRun) []string {
    40  	fields := []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "workflow,event", "status"}
    41  	result := make([]string, len(fields))
    42  	for i, field := range fields {
    43  		result[i] = getFieldValue(repo, *run, field)
    44  	}
    45  	return result
    46  }
    47  
    48  // getWorkflowRunsFromGithub - return informations and status about a workflow
    49  func getWorkflowRunsFromGithub(gauge *prometheus.GaugeVec) {
    50  	for {
    51  		opts := github.ListWorkflowRunsOptions{}
    52  		resp, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), org, repo, &opts)
    53  		if err != nil {
    54  			log.Printf("ListRepositoryWorkflowRuns error for %s: %s", repo, err.Error())
    55  		} else {
    56  			for _, run := range resp.WorkflowRuns {
    57  				conclusion := run.GetConclusion()
    58  				var state float64
    59  				switch conclusion {
    60  				case "success":
    61  					state = 1
    62  				case "skipped":
    63  					state = 2
    64  				case "in_progress":
    65  					state = 3
    66  				case "queued":
    67  					state = 4
    68  				}
    69  
    70  				fields := getRelevantFields(repo, run)
    71  				gauge.WithLabelValues(fields...).Set(state)
    72  			}
    73  		}
    74  	}
    75  }
    76  
    77  func getDurationGithub(gauge *prometheus.GaugeVec) {
    78  	for {
    79  		resp, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), org, repo, nil)
    80  		if err != nil {
    81  			log.Printf("ListRepositoryWorkflowRuns error for %s: %s", repo, err.Error())
    82  		} else {
    83  			for _, run := range resp.WorkflowRuns {
    84  				resp, _, err := client.Actions.GetWorkflowRunUsageByID(context.Background(), org, repo, *run.ID)
    85  				fields := getRelevantFields(repo, run)
    86  				if err != nil { // Fallback for Github Enterprise
    87  					created := run.CreatedAt.Time.Unix()
    88  					updated := run.UpdatedAt.Time.Unix()
    89  					elapsed := updated - created
    90  					gauge.WithLabelValues(fields...).Set(float64(elapsed * 1000))
    91  				} else {
    92  					gauge.WithLabelValues(fields...).Set(float64(resp.GetRunDurationMS()))
    93  				}
    94  			}
    95  		}
    96  	}
    97  }
    98  

View as plain text