Add metric for askpass, noop sync != success

This commit is contained in:
Tim Hockin 2020-08-01 20:40:01 -07:00
parent c4f83fdae3
commit 04c85ee1dc
1 changed files with 30 additions and 7 deletions

View File

@ -127,8 +127,19 @@ var (
syncCount = prometheus.NewCounterVec(prometheus.CounterOpts{ syncCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "git_sync_count_total", Name: "git_sync_count_total",
Help: "How many git syncs completed, partitioned by success", Help: "How many git syncs completed, partitioned by state (success, error, noop)",
}, []string{"status"}) }, []string{"status"})
askpassCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "git_sync_askpass_calls",
Help: "How many git askpass calls completed, partitioned by state (success, error)",
}, []string{"status"})
)
const (
metricKeySuccess = "success"
metricKeyError = "error"
metricKeyNoOp = "noop"
) )
// initTimeout is a timeout for initialization, like git credentials setup. // initTimeout is a timeout for initialization, like git credentials setup.
@ -143,6 +154,7 @@ const (
func init() { func init() {
prometheus.MustRegister(syncDuration) prometheus.MustRegister(syncDuration)
prometheus.MustRegister(syncCount) prometheus.MustRegister(syncCount)
prometheus.MustRegister(askpassCount)
} }
func envString(key, def string) string { func envString(key, def string) string {
@ -292,9 +304,11 @@ func main() {
if *flAskPassURL != "" { if *flAskPassURL != "" {
if err := setupGitAskPassURL(ctx); err != nil { if err := setupGitAskPassURL(ctx); err != nil {
askpassCount.WithLabelValues(metricKeyError).Inc()
fmt.Fprintf(os.Stderr, "ERROR: failed to call ASKPASS callback URL: %v\n", err) fmt.Fprintf(os.Stderr, "ERROR: failed to call ASKPASS callback URL: %v\n", err)
os.Exit(1) os.Exit(1)
} }
askpassCount.WithLabelValues(metricKeySuccess).Inc()
} }
// The scope of the initialization context ends here, so we call cancel to release resources associated with it. // The scope of the initialization context ends here, so we call cancel to release resources associated with it.
@ -355,8 +369,7 @@ func main() {
start := time.Now() start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout))
if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest, *flAskPassURL, *flSubmodules); err != nil { if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest, *flAskPassURL, *flSubmodules); err != nil {
syncDuration.WithLabelValues("error").Observe(time.Since(start).Seconds()) updateSyncMetrics(metricKeyError, start)
syncCount.WithLabelValues("error").Inc()
if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures { if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures {
// Exit after too many retries, maybe the error is not recoverable. // Exit after too many retries, maybe the error is not recoverable.
log.Error(err, "failed to sync repo, aborting") log.Error(err, "failed to sync repo, aborting")
@ -369,11 +382,14 @@ func main() {
cancel() cancel()
time.Sleep(waitTime(*flWait)) time.Sleep(waitTime(*flWait))
continue continue
} else if changed && webhook != nil { } else if changed {
webhook.Send(hash) if webhook != nil {
webhook.Send(hash)
}
updateSyncMetrics(metricKeySuccess, start)
} else {
updateSyncMetrics(metricKeyNoOp, start)
} }
syncDuration.WithLabelValues("success").Observe(time.Since(start).Seconds())
syncCount.WithLabelValues("success").Inc()
if initialSync { if initialSync {
if *flOneTime { if *flOneTime {
@ -396,6 +412,11 @@ func main() {
} }
} }
func updateSyncMetrics(key string, start time.Time) {
syncDuration.WithLabelValues(key).Observe(time.Since(start).Seconds())
syncCount.WithLabelValues(key).Inc()
}
func waitTime(seconds float64) time.Duration { func waitTime(seconds float64) time.Duration {
return time.Duration(int(seconds*1000)) * time.Millisecond return time.Duration(int(seconds*1000)) * time.Millisecond
} }
@ -662,8 +683,10 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot,
// For ASKPASS Callback URL, the credentials behind is dynamic, it needs to be // For ASKPASS Callback URL, the credentials behind is dynamic, it needs to be
// re-fetched each time. // re-fetched each time.
if err := setupGitAskPassURL(ctx); err != nil { if err := setupGitAskPassURL(ctx); err != nil {
askpassCount.WithLabelValues(metricKeyError).Inc()
return false, "", fmt.Errorf("failed to call GIT_ASKPASS_URL: %v", err) return false, "", fmt.Errorf("failed to call GIT_ASKPASS_URL: %v", err)
} }
askpassCount.WithLabelValues(metricKeySuccess).Inc()
} }
target := path.Join(gitRoot, dest) target := path.Join(gitRoot, dest)