Add per-repo CPU stats (#562)

Co-authored-by: Azeem Shaikh <azeems@google.com>
This commit is contained in:
Azeem Shaikh 2021-06-09 14:18:40 -07:00 committed by GitHub
parent 8c87ab7647
commit 88f645bb76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 8 deletions

View File

@ -46,13 +46,18 @@ func (l *logger) Logf(s string, f ...interface{}) {
l.messages = append(l.messages, fmt.Sprintf(s, f...))
}
func logStats(ctx context.Context, startTime time.Time) {
runTimeInSecs := time.Now().Unix() - startTime.Unix()
opencensusstats.Record(ctx, stats.CheckRuntimeInSec.M(runTimeInSecs))
}
func (r *Runner) Run(ctx context.Context, f CheckFn) CheckResult {
ctx, err := tag.New(ctx, tag.Upsert(stats.CheckName, r.CheckName))
if err != nil {
panic(err)
}
defer logStats(ctx, time.Now())
startTime := time.Now().Unix()
var res CheckResult
var l logger
for retriesRemaining := checkRetries; retriesRemaining > 0; retriesRemaining-- {
@ -68,8 +73,6 @@ func (r *Runner) Run(ctx context.Context, f CheckFn) CheckResult {
break
}
res.Details = l.messages
runTimeInSecs := time.Now().Unix() - startTime
opencensusstats.Record(ctx, stats.CPURuntimeInSec.M(runTimeInSecs))
return res
}

View File

@ -31,6 +31,7 @@ var errorUndefinedExporter = errors.New("unsupported exporterType")
type exporterType string
const (
stackdriverMetricPrefix = "scorecard-cron"
stackdriverTimeSeriesQuota = 200
stackdriverTimeoutMinutes = 10
stackDriver exporterType = "stackdriver"
@ -66,7 +67,7 @@ func newStackDriverExporter() (*stackdriver.Exporter, error) {
}
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: projectID,
MetricPrefix: "scorecard-cron",
MetricPrefix: stackdriverMetricPrefix,
MonitoredResource: gcp.Autodetect(),
Timeout: stackdriverTimeoutMinutes * time.Minute,
// Stackdriver specific quotas based on https://cloud.google.com/monitoring/quotas

View File

@ -16,17 +16,26 @@ package pkg
import (
"context"
"log"
"net/http"
"sync"
"time"
"github.com/google/go-github/v32/github"
"github.com/shurcooL/githubv4"
opencensusstats "go.opencensus.io/stats"
"go.opencensus.io/tag"
"github.com/ossf/scorecard/checker"
"github.com/ossf/scorecard/repos"
"github.com/ossf/scorecard/stats"
)
func logStats(ctx context.Context, startTime time.Time) {
runTimeInSecs := time.Now().Unix() - startTime.Unix()
opencensusstats.Record(ctx, stats.RepoRuntimeInSec.M(runTimeInSecs))
}
func runEnabledChecks(ctx context.Context,
repo repos.RepoURL, checksToRun checker.CheckNameToFnMap,
httpClient *http.Client, githubClient *github.Client, graphClient *githubv4.Client,
@ -64,6 +73,12 @@ func RunScorecards(ctx context.Context,
httpClient *http.Client,
githubClient *github.Client,
graphClient *githubv4.Client) repos.RepoResult {
ctx, err := tag.New(ctx, tag.Upsert(stats.Repo, repo.URL()))
if err != nil {
log.Panicf("error during tag.New: %v. Not logging stats", err)
}
defer logStats(ctx, time.Now())
ret := repos.RepoResult{
Repo: repo.URL(),
Date: time.Now().Format("2006-01-02"),

View File

@ -17,8 +17,12 @@ package stats
import "go.opencensus.io/stats"
var (
// CPURuntimeInSec measures the CPU runtime in seconds.
CPURuntimeInSec = stats.Int64("CPURuntimeInSec", "Measures the CPU runtime in seconds", stats.UnitSeconds)
// CheckRuntimeInSec measures the CPU runtime in seconds per check.
CheckRuntimeInSec = stats.Int64("CheckRuntimeInSec", "Measures the CPU runtime in seconds for a check",
stats.UnitSeconds)
// RepoRuntimeInSec measures the CPU runtime in seconds per repo.
RepoRuntimeInSec = stats.Int64("RepoRuntimeInSec", "Measures the CPU runtime in seconds for a repo",
stats.UnitSeconds)
// HTTPRequests measures the count of HTTP requests.
HTTPRequests = stats.Int64("HTTPRequests", "Measures the count of HTTP requests", stats.UnitDimensionless)
)

View File

@ -19,6 +19,8 @@ import "go.opencensus.io/tag"
var (
// CheckName is the tag key for the check name.
CheckName = tag.MustNewKey("checkName")
// Repo is the tag key for the repo name.
Repo = tag.MustNewKey("repo")
// RequestTag is the tag key for the request type.
RequestTag = tag.MustNewKey("requestTag")
)

View File

@ -20,11 +20,11 @@ import (
)
var (
// CheckRuntime tracks CPU runtime stats.
// CheckRuntime tracks CPU runtime stats for checks.
CheckRuntime = view.View{
Name: "CheckRuntime",
Description: "CPU runtime stats per check",
Measure: CPURuntimeInSec,
Measure: CheckRuntimeInSec,
TagKeys: []tag.Key{CheckName},
//nolint:gomnd
Aggregation: view.Distribution(
@ -45,6 +45,31 @@ var (
1<<15),
}
// RepoRuntime tracks CPU runtime stats for repos.
RepoRuntime = view.View{
Name: "RepoRuntime",
Description: "CPU runtime stats per repo",
Measure: RepoRuntimeInSec,
TagKeys: []tag.Key{Repo},
//nolint:gomnd
Aggregation: view.Distribution(
0,
1<<2,
1<<3,
1<<4,
1<<5,
1<<6,
1<<7,
1<<8,
1<<9,
1<<10,
1<<11,
1<<12,
1<<13,
1<<14,
1<<15),
}
// OutgoingHTTPRequests tracks HTTPRequests made.
OutgoingHTTPRequests = view.View{
Name: "OutgoingHTTPRequests",