package metrics import ( "os" "strconv" "sync" "github.com/google/go-github/v47/github" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" githubclient "edge-infra.dev/pkg/f8n/devinfra/github-client" ) const ( workflowRunStatus = "github_workflow_run_status" workflowDuration = "github_workflow_run_duration_ms" workflowUsage = "github_workflow_usage_seconds" runnerStatus = "github_runner_status" runnerOrganization = "github_runner_organization_status" ) var ( org string repo string client *github.Client ) var Metrics = struct { sync.Mutex runnersGauge *prometheus.GaugeVec runnersOrganizationGauge *prometheus.GaugeVec workflowRunStatusGauge *prometheus.GaugeVec workflowRunDurationGauge *prometheus.GaugeVec workflowBillGauge *prometheus.GaugeVec }{ runnersGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: runnerStatus, Help: "runner status", }, []string{"repo", "os", "name", "id", "busy"}, ), runnersOrganizationGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: runnerOrganization, Help: "runner status", }, []string{"repo", "id", "node_id", "name", "state", "os"}, ), workflowRunStatusGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: workflowRunStatus, Help: "Workflow run status", }, []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "workflow_event", "status"}, ), workflowRunDurationGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: workflowDuration, Help: "Workflow run duration (in milliseconds)", }, []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "workflow_event", "status"}, ), workflowBillGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: workflowUsage, Help: "Number of billable seconds used by a specific workflow during the current billing cycle. Any job re-runs are also included in the usage. Only apply to workflows in private repositories that use GitHub-hosted runners.", }, []string{"repo", "id", "node_id", "name", "state", "os"}, ), } // InitMetrics - register metrics in prometheus lib and start func for monitor func InitMetrics() error { appRepo := os.Getenv("REPO_URL") privateKey := []byte(os.Getenv("GITHUB_APP_PRIVATE_KEY")) //appID := int64(96535) appID, err := strconv.ParseInt(os.Getenv("APP_ID"), 10, 64) if err != nil { return err } cfg := githubclient.Config{ Repository: appRepo, PrivateKey: privateKey, AppID: appID, } repo = cfg.Repo() org = cfg.Owner() ac, err := githubclient.NewClient(cfg) if err != nil { return err } client = ac.Client go workflowCache() Metrics.Lock() defer Metrics.Unlock() go getBillableFromGithub(Metrics.workflowBillGauge) go getRunnersFromGithub(Metrics.runnersGauge) go getRunnersOrganizationFromGithub(Metrics.runnersOrganizationGauge) go getWorkflowRunsFromGithub(Metrics.workflowRunStatusGauge) go getDurationGithub(Metrics.workflowRunDurationGauge) return nil }