Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACCDT-1238: Parametrize actions metrics histogram buckets size #3

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/actionsmetricsserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
)

const (
webhookSecretTokenEnvName = "GITHUB_WEBHOOK_SECRET_TOKEN"
webhookSecretTokenEnvName = "GITHUB_WEBHOOK_SECRET_TOKEN"
prometheusRunBucketIntervalsName = "PROMETHEUS_RUN_BUCKET_INTERVALS"
prometheusQueueBucketIntervalsName = "PROMETHEUS_QUEUE_BUCKET_INTERVALS"
)

func init() {
Expand All @@ -73,6 +75,10 @@
logFormat string

ghClient *github.Client

// List of histogram buckets that we want to see in metrics
runBucketsList actionsmetrics.BucketsSlice
queueBucketsList actionsmetrics.BucketsSlice
)

var c github.Config
Expand All @@ -83,6 +89,8 @@
}

webhookSecretTokenEnv = os.Getenv(webhookSecretTokenEnvName)
runBucketsList.Set(os.Getenv(prometheusRunBucketIntervalsName))

Check failure on line 92 in cmd/actionsmetricsserver/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `runBucketsList.Set` is not checked (errcheck)
queueBucketsList.Set(os.Getenv(prometheusQueueBucketIntervalsName))

Check failure on line 93 in cmd/actionsmetricsserver/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `queueBucketsList.Set` is not checked (errcheck)

flag.StringVar(&webhookAddr, "webhook-addr", ":8000", "The address the metric endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
Expand Down Expand Up @@ -113,6 +121,8 @@
webhookSecretToken = webhookSecretTokenEnv
}

actionsmetrics.InitializeMetrics(runBucketsList, queueBucketsList)

if webhookSecretToken == "" {
logger.Info(fmt.Sprintf("-github-webhook-secret-token and %s are missing or empty. Create one following https://docs.github.com/en/developers/webhooks-and-events/securing-your-webhooks and specify it via the flag or the envvar", webhookSecretTokenEnvName))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/actionsmetrics/event_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (reader *EventReader) ProcessWorkflowJobEvent(ctx context.Context, event in
log.Info("reading workflow_job logs")
}

githubWorkflowJobQueueDurationSeconds.With(labels).Observe(parseResult.QueueTime.Seconds())
githubWorkflowJobQueueHistogram.With(labels).Observe(parseResult.QueueTime.Seconds())

case "completed":
githubWorkflowJobsCompletedTotal.With(labels).Inc()
Expand Down Expand Up @@ -200,7 +200,7 @@ func (reader *EventReader) ProcessWorkflowJobEvent(ctx context.Context, event in
}

if runTimeSeconds != nil {
githubWorkflowJobRunDurationSeconds.With(extraLabel("job_conclusion", *e.WorkflowJob.Conclusion, labels)).Observe(*runTimeSeconds)
githubWorkflowJobRunHistogram.With(extraLabel("job_conclusion", *e.WorkflowJob.Conclusion, labels)).Observe(*runTimeSeconds)
}
}
}
Expand Down
86 changes: 65 additions & 21 deletions pkg/actionsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,48 @@
package actionsmetrics

import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"strconv"
"strings"
)

func init() {
type BucketsSlice []float64

func (i *BucketsSlice) String() string {
return fmt.Sprintf("%v", *i)
}

func (i *BucketsSlice) Set(value string) error {
valuesStr := strings.Split(value, ",")
for _, str := range valuesStr {
// Convert the string to float64.
val, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}
*i = append(*i, val)
}
return nil
}

var githubWorkflowJobQueueHistogram *prometheus.HistogramVec
var githubWorkflowJobRunHistogram *prometheus.HistogramVec

func initMetrics(runBuckets, queueBuckets []float64) {
githubWorkflowJobRunHistogram = githubWorkflowJobRunDurationSeconds(runBuckets)
githubWorkflowJobQueueHistogram = githubWorkflowJobQueueDurationSeconds(queueBuckets)

if len(runBuckets) == 0 {
githubWorkflowJobRunHistogram = githubWorkflowJobRunDurationSeconds(DefaultRuntimeBuckets)
}
if len(queueBuckets) == 0 {
githubWorkflowJobQueueHistogram = githubWorkflowJobQueueDurationSeconds(DefaultRuntimeBuckets)
}
metrics.Registry.MustRegister(
githubWorkflowJobQueueDurationSeconds,
githubWorkflowJobRunDurationSeconds,
githubWorkflowJobQueueHistogram,
githubWorkflowJobRunHistogram,
githubWorkflowJobConclusionsTotal,
githubWorkflowJobsQueuedTotal,
githubWorkflowJobsStartedTotal,
Expand All @@ -21,8 +55,34 @@ func init() {
)
}

func githubWorkflowJobQueueDurationSeconds(buckets []float64) *prometheus.HistogramVec {
return prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "github_workflow_job_queue_duration_seconds",
Help: "Queue times for workflow jobs in seconds",
Buckets: buckets,
},
metricLabels(),
)
}

func githubWorkflowJobRunDurationSeconds(buckets []float64) *prometheus.HistogramVec {
return prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "github_workflow_job_run_duration_seconds",
Help: "Run times for workflow jobs in seconds",
Buckets: buckets,
},
metricLabels("job_conclusion"),
)
}

func InitializeMetrics(runBuckets, queueBuckets []float64) {
initMetrics(runBuckets, queueBuckets)
}

var (
runtimeBuckets []float64 = []float64{
DefaultRuntimeBuckets = []float64{
0.01,
0.05,
0.1,
Expand Down Expand Up @@ -76,23 +136,7 @@ func metricLabels(extras ...string) []string {
}

var (
commonLabels = []string{"runs_on", "job_name", "organization", "repository", "repository_full_name", "owner", "workflow_name", "head_branch"}
githubWorkflowJobQueueDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "github_workflow_job_queue_duration_seconds",
Help: "Queue times for workflow jobs in seconds",
Buckets: runtimeBuckets,
},
metricLabels(),
)
githubWorkflowJobRunDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "github_workflow_job_run_duration_seconds",
Help: "Run times for workflow jobs in seconds",
Buckets: runtimeBuckets,
},
metricLabels("job_conclusion"),
)
commonLabels = []string{"runs_on", "job_name", "organization", "repository", "repository_full_name", "owner", "workflow_name", "head_branch"}
githubWorkflowJobConclusionsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "github_workflow_job_conclusions_total",
Expand Down
Loading