-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from gamoutatsumi/feat/recover-pending-job
Add collecting metrics for pending (queued) workflow runs
- Loading branch information
Showing
9 changed files
with
304 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package gh | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/go-github/v47/github" | ||
"github.com/whywaita/myshoes/pkg/logger" | ||
) | ||
|
||
// ActiveTargets stores targets by recently received webhook | ||
var ActiveTargets = sync.Map{} | ||
|
||
// PendingRuns stores queued / pending workflow runs | ||
var PendingRuns = sync.Map{} | ||
|
||
func listRuns(ctx context.Context, client *github.Client, owner, repo string, opts *github.ListWorkflowRunsOptions) (*github.WorkflowRuns, *github.Response, error) { | ||
runs, resp, err := client.Actions.ListRepositoryWorkflowRuns(ctx, owner, repo, opts) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to list workflow runs: %w", err) | ||
} | ||
return runs, resp, nil | ||
} | ||
|
||
// ListRuns get workflow runs that registered repository | ||
func ListRuns(owner, repo string) ([]*github.WorkflowRun, error) { | ||
if cachedRs, expiration, found := responseCache.GetWithExpiration(getRunsCacheKey(owner, repo)); found { | ||
if time.Until(expiration).Minutes() <= 1 { | ||
go updateCache(context.Background(), owner, repo) | ||
} | ||
logger.Logf(true, "found workflow runs (cache hit: expiration: %s) in %s/%s", expiration.Format("2006/01/02 15:04:05.000 -0700"), owner, repo) | ||
return cachedRs.([]*github.WorkflowRun), nil | ||
} | ||
go updateCache(context.Background(), owner, repo) | ||
return []*github.WorkflowRun{}, nil | ||
} | ||
|
||
func getRunsCacheKey(owner, repo string) string { | ||
return fmt.Sprintf("runs-owner-%s-repo-%s", owner, repo) | ||
} | ||
|
||
// ClearRunsCache clear github workflow run caches | ||
func ClearRunsCache(owner, repo string) { | ||
responseCache.Delete(getRunsCacheKey(owner, repo)) | ||
} | ||
|
||
func updateCache(ctx context.Context, owner, repo string) { | ||
var opts = &github.ListWorkflowRunsOptions{ | ||
ListOptions: github.ListOptions{ | ||
Page: 0, | ||
PerPage: 10, | ||
}, | ||
} | ||
logger.Logf(true, "get workflow runs of %s/%s, recent %d runs", owner, repo, opts.PerPage) | ||
activeTarget, ok := ActiveTargets.Load(fmt.Sprintf("%s/%s", owner, repo)) | ||
if !ok { | ||
logger.Logf(true, "%s/%s is not active target", owner, repo) | ||
return | ||
} | ||
installationID := activeTarget.(int64) | ||
client, err := NewClientInstallation(installationID) | ||
if err != nil { | ||
logger.Logf(false, "failed to list workflow runs (%s/%s): %+v", owner, repo, err) | ||
return | ||
} | ||
runs, resp, err := listRuns(ctx, client, owner, repo, opts) | ||
if err != nil { | ||
logger.Logf(false, "failed to list workflow runs (%s/%s): %+v", owner, repo, err) | ||
return | ||
} | ||
storeRateLimit(getRateLimitKey(owner, repo), resp.Rate) | ||
responseCache.Set(getRunsCacheKey(owner, repo), runs.WorkflowRuns, 15*time.Minute) | ||
logger.Logf(true, "found %d workflow runs in %s/%s", len(runs.WorkflowRuns), owner, repo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package metric | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/whywaita/myshoes/pkg/datastore" | ||
"github.com/whywaita/myshoes/pkg/gh" | ||
"github.com/whywaita/myshoes/pkg/logger" | ||
) | ||
|
||
const githubName = "github" | ||
|
||
var ( | ||
githubPendingRunsDesc = prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, githubName, "pending_runs"), | ||
"Number of pending runs", | ||
[]string{"target_id", "scope"}, nil, | ||
) | ||
) | ||
|
||
// ScraperGitHub is scraper implement for GitHub | ||
type ScraperGitHub struct{} | ||
|
||
// Name return name | ||
func (ScraperGitHub) Name() string { | ||
return githubName | ||
} | ||
|
||
// Help return help | ||
func (ScraperGitHub) Help() string { | ||
return "Collect from GitHub" | ||
} | ||
|
||
// Scrape scrape metrics | ||
func (s ScraperGitHub) Scrape(ctx context.Context, ds datastore.Datastore, ch chan<- prometheus.Metric) error { | ||
if err := scrapePendingRuns(ctx, ds, ch); err != nil { | ||
return fmt.Errorf("failed to scrape pending runs: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func scrapePendingRuns(ctx context.Context, ds datastore.Datastore, ch chan<- prometheus.Metric) error { | ||
gh.ActiveTargets.Range(func(key, value any) bool { | ||
var pendings float64 | ||
scope := key.(string) | ||
installationID := value.(int64) | ||
target, err := ds.GetTargetByScope(ctx, scope) | ||
if err != nil { | ||
logger.Logf(false, "failed to get target by scope (%s): %+v", scope, err) | ||
return true | ||
} | ||
owner, repo := target.OwnerRepo() | ||
if repo == "" { | ||
return true | ||
} | ||
runs, err := gh.ListRuns(owner, repo) | ||
if err != nil { | ||
logger.Logf(false, "failed to list pending runs: %+v", err) | ||
return true | ||
} | ||
|
||
for _, r := range runs { | ||
if r.GetStatus() == "queued" || r.GetStatus() == "pending" { | ||
if time.Since(r.CreatedAt.Time).Minutes() >= 30 { | ||
pendings++ | ||
gh.PendingRuns.Store(installationID, r) | ||
} | ||
} | ||
} | ||
ch <- prometheus.MustNewConstMetric(githubPendingRunsDesc, prometheus.GaugeValue, pendings, target.UUID.String(), target.Scope) | ||
return true | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.