Skip to content

Commit

Permalink
Add the way to customize bucket size for actions-metric-server
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillbilchenko authored and sindunuragarp committed Nov 13, 2023
1 parent ed2caef commit 2574214
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 24 deletions.
9 changes: 8 additions & 1 deletion cmd/actionsmetricsserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var (
)

const (
webhookSecretTokenEnvName = "GITHUB_WEBHOOK_SECRET_TOKEN"
webhookSecretTokenEnvName = "GITHUB_WEBHOOK_SECRET_TOKEN"
prometheusBucketIntervalsName = "PROMETHEUS_BUCKET_INTERVALS"
)

func init() {
Expand All @@ -73,6 +74,9 @@ func main() {
logFormat string

ghClient *github.Client

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

var c github.Config
Expand All @@ -83,6 +87,7 @@ func main() {
}

webhookSecretTokenEnv = os.Getenv(webhookSecretTokenEnvName)
bucketsList.Set(os.Getenv(prometheusBucketIntervalsName))

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 +118,8 @@ func main() {
webhookSecretToken = webhookSecretTokenEnv
}

actionsmetrics.InitializeMetrics(bucketsList)

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
84 changes: 63 additions & 21 deletions pkg/actionsmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,46 @@
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(buckets []float64) {
if len(buckets) > 0 {
githubWorkflowJobQueueHistogram = githubWorkflowJobQueueDurationSeconds(buckets)
githubWorkflowJobRunHistogram = githubWorkflowJobRunDurationSeconds(buckets)
} else {
githubWorkflowJobQueueHistogram = githubWorkflowJobQueueDurationSeconds(DefaultRuntimeBuckets)
githubWorkflowJobRunHistogram = githubWorkflowJobRunDurationSeconds(DefaultRuntimeBuckets)
}
metrics.Registry.MustRegister(
githubWorkflowJobQueueDurationSeconds,
githubWorkflowJobRunDurationSeconds,
githubWorkflowJobQueueHistogram,
githubWorkflowJobRunHistogram,
githubWorkflowJobConclusionsTotal,
githubWorkflowJobsQueuedTotal,
githubWorkflowJobsStartedTotal,
Expand All @@ -21,8 +53,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(buckets []float64) {
initMetrics(buckets)
}

var (
runtimeBuckets []float64 = []float64{
DefaultRuntimeBuckets = []float64{
0.01,
0.05,
0.1,
Expand Down Expand Up @@ -76,23 +134,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

0 comments on commit 2574214

Please sign in to comment.