Skip to content

Commit

Permalink
Merge pull request #222 from whywaita/feat/add-scrape-github-installa…
Browse files Browse the repository at this point in the history
…tion

Add a metrics of GitHub Apps installation
  • Loading branch information
whywaita authored Dec 12, 2024
2 parents 74e5929 + f1aa081 commit 5d80441
Show file tree
Hide file tree
Showing 4 changed files with 120 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
}
26 changes: 26 additions & 0 deletions pkg/metric/scrape_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ 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,
)
datastoreTargetTokenExpiresDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, datastoreName, "target_token_expires_seconds"),
"Token expires time",
[]string{"target_id"}, nil,
)
datastoreJobDurationOldest = prometheus.NewDesc(
prometheus.BuildFQName(namespace, datastoreName, "job_duration_oldest_seconds"),
"Duration time of oldest job",
Expand Down Expand Up @@ -170,9 +184,21 @@ func scrapeTargets(ctx context.Context, ds datastore.Datastore, ch chan<- promet
return fmt.Errorf("failed to list targets: %w", err)
}

now := time.Now()
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()]++

ch <- prometheus.MustNewConstMetric(
datastoreTargetTokenExpiresDesc, prometheus.GaugeValue,
t.TokenExpiredAt.Sub(now).Seconds(),
t.UUID.String(),
)
}
for rt, number := range result {
ch <- prometheus.MustNewConstMetric(
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.GetAccount().GetHTMLURL(),
)
}
return nil
}

0 comments on commit 5d80441

Please sign in to comment.