Skip to content

Commit

Permalink
Add a metrics of GitHub Apps installation
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Dec 5, 2024
1 parent 74e5929 commit 8e7a8be
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
56 changes: 56 additions & 0 deletions pkg/gh/installation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gh

import (
"context"
"fmt"
"time"

"github.com/google/go-github/v47/github"
"github.com/whywaita/myshoes/pkg/logger"
)

func listInstallations(ctx context.Context) ([]*github.Installation, error) {
if cachedRs, found := responseCache.Get(getCacheInstallationsKey()); found {
return cachedRs.([]*github.Installation), nil
}

inst, err := _listInstallations(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list installations: %w", err)
}

responseCache.Set(getCacheInstallationsKey(), inst, 1*time.Hour)

return _listInstallations(ctx)
}

func getCacheInstallationsKey() string {
return "installations"
}

func _listInstallations(ctx context.Context) ([]*github.Installation, error) {
clientApps, err := NewClientGitHubApps()
if err != nil {
return nil, fmt.Errorf("failed to create a client Apps: %w", err)
}

var opts = &github.ListOptions{
Page: 0,
PerPage: 100,
}

var installations []*github.Installation
for {
logger.Logf(true, "get installations from GitHub, page: %d, now all installations: %d", opts.Page, len(installations))
is, resp, err := clientApps.Apps.ListInstallations(ctx, opts)
if err != nil {
return nil, fmt.Errorf("failed to list installations: %w", err)
}
installations = append(installations, is...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return installations, nil
}
27 changes: 0 additions & 27 deletions pkg/gh/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,3 @@ func listAppsInstalledRepo(ctx context.Context, installationID int64) ([]*github

return repositories, nil
}

func listInstallations(ctx context.Context) ([]*github.Installation, error) {
clientApps, err := NewClientGitHubApps()
if err != nil {
return nil, fmt.Errorf("failed to create a client Apps: %w", err)
}

var opts = &github.ListOptions{
Page: 0,
PerPage: 100,
}

var installations []*github.Installation
for {
logger.Logf(true, "get installations from GitHub, page: %d, now all installations: %d", opts.Page, len(installations))
is, resp, err := clientApps.Apps.ListInstallations(ctx, opts)
if err != nil {
return nil, fmt.Errorf("failed to list installations: %w", err)
}
installations = append(installations, is...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return installations, nil
}
14 changes: 14 additions & 0 deletions pkg/metric/scrape_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ var (
"Number of targets",
[]string{"resource_type"}, nil,
)
datastoreTargetDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, datastoreName, "target_describe"),
"Target",
[]string{
"target_id",
"scope",
"resource_type",
}, nil,
)
datastoreJobDurationOldest = prometheus.NewDesc(
prometheus.BuildFQName(namespace, datastoreName, "job_duration_oldest_seconds"),
"Duration time of oldest job",
Expand Down Expand Up @@ -172,6 +181,11 @@ func scrapeTargets(ctx context.Context, ds datastore.Datastore, ch chan<- promet

result := map[string]float64{} // key: resource_type, value: number
for _, t := range targets {
ch <- prometheus.MustNewConstMetric(
datastoreTargetDesc, prometheus.GaugeValue, 1,
t.UUID.String(), t.Scope, t.ResourceType.String(),
)

result[t.ResourceType.String()]++
}
for rt, number := range result {
Expand Down
38 changes: 38 additions & 0 deletions pkg/metric/scrape_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ var (
"Number of pending runs",
[]string{"target_id", "scope"}, nil,
)
githubInstallationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, githubName, "installation"),
"installations",
[]string{
"installation_id",
"account_login",
"account_type",
"target_type",
"repository_selection",
"html_url",
},
nil,
)
)

// ScraperGitHub is scraper implement for GitHub
Expand All @@ -39,6 +52,9 @@ func (s ScraperGitHub) Scrape(ctx context.Context, ds datastore.Datastore, ch ch
if err := scrapePendingRuns(ctx, ds, ch); err != nil {
return fmt.Errorf("failed to scrape pending runs: %w", err)
}
if err := scrapeInstallation(ctx, ch); err != nil {
return fmt.Errorf("failed to scrape installations: %w", err)
}
return nil
}

Expand Down Expand Up @@ -80,3 +96,25 @@ func scrapePendingRuns(ctx context.Context, ds datastore.Datastore, ch chan<- pr
})
return nil
}

func scrapeInstallation(ctx context.Context, ch chan<- prometheus.Metric) error {
installations, err := gh.GHlistInstallations(ctx)
if err != nil {
return fmt.Errorf("failed to list installations: %w", err)
}

for _, installation := range installations {
ch <- prometheus.MustNewConstMetric(
githubInstallationDesc,
prometheus.GaugeValue,
1,
fmt.Sprint(installation.GetID()),
installation.GetAccount().GetLogin(),
installation.GetAccount().GetType(),
installation.GetTargetType(),
installation.GetRepositorySelection(),
installation.GetHTMLURL(),
)
}
return nil
}

0 comments on commit 8e7a8be

Please sign in to comment.