diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 01c65bffa5..7c181b032e 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -94,7 +94,7 @@ jobs: env: REGISTRY_IMAGE: ghcr.io/miroapp/actions-runner-controller BUILD_PLATFORM: linux/amd64 - BUILD_VERSION: ${{ github.sha }} + BUILD_VERSION: ${{ github.event_name != 'pull_request' && 'master' || 'branch' }}-${{ github.sha }} steps: - uses: docker/setup-buildx-action@v3 @@ -113,7 +113,7 @@ jobs: uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY_IMAGE }} - + - uses: docker/build-push-action@v5 id: build with: @@ -128,5 +128,5 @@ jobs: cache-to: type=gha,mode=max,scope=${{ github.ref_name }} platforms: ${{ env.BUILD_PLATFORM }} labels: ${{ steps.meta.outputs.labels }} - push: ${{ github.event_name != 'pull_request' }} + push: true tags: ${{ env.REGISTRY_IMAGE }}:${{ env.BUILD_VERSION }} diff --git a/pkg/actionsmetrics/metrics.go b/pkg/actionsmetrics/metrics.go index 96619f37ee..972a6f9f5e 100644 --- a/pkg/actionsmetrics/metrics.go +++ b/pkg/actionsmetrics/metrics.go @@ -5,11 +5,44 @@ package actionsmetrics import ( + "fmt" + "os" + "strconv" + "strings" + "github.com/prometheus/client_golang/prometheus" "sigs.k8s.io/controller-runtime/pkg/metrics" ) +const ( + prometheusRunBucketIntervalsName = "PROMETHEUS_RUN_BUCKET_INTERVALS" + prometheusQueueBucketIntervalsName = "PROMETHEUS_QUEUE_BUCKET_INTERVALS" +) + func init() { + queueBuckets := defaultRuntimeBuckets + if _, ok := os.LookupEnv(prometheusQueueBucketIntervalsName); ok { + buckets, err := parseBucketsString(os.Getenv(prometheusQueueBucketIntervalsName)) + if err != nil { + fmt.Fprintf(os.Stderr, "Warning: Failed to parse %s, using default buckets: %s\n", prometheusQueueBucketIntervalsName, err) + } else { + queueBuckets = buckets + } + } + + runBuckets := defaultRuntimeBuckets + if _, ok := os.LookupEnv(prometheusRunBucketIntervalsName); ok { + buckets, err := parseBucketsString(os.Getenv(prometheusRunBucketIntervalsName)) + if err != nil { + fmt.Fprintf(os.Stderr, "Warning: Failed to parse %s, using default buckets: %s\n", prometheusRunBucketIntervalsName, err) + } else { + runBuckets = buckets + } + } + + githubWorkflowJobQueueDurationSeconds = initGithubWorkflowJobQueueDurationSeconds(queueBuckets) + githubWorkflowJobRunDurationSeconds = initGithubWorkflowJobRunDurationSeconds(runBuckets) + metrics.Registry.MustRegister( githubWorkflowJobQueueDurationSeconds, githubWorkflowJobRunDurationSeconds, @@ -22,7 +55,7 @@ func init() { } var ( - runtimeBuckets []float64 = []float64{ + defaultRuntimeBuckets = []float64{ 0.01, 0.05, 0.1, @@ -75,25 +108,48 @@ func metricLabels(extras ...string) []string { return append(append([]string{}, commonLabels...), extras...) } -var ( - commonLabels = []string{"runs_on", "job_name", "organization", "repository", "repository_full_name", "owner", "workflow_name", "head_branch"} - githubWorkflowJobQueueDurationSeconds = prometheus.NewHistogramVec( +func parseBucketsString(value string) ([]float64, error) { + valuesStr := strings.Split(value, ",") + buckets := make([]float64, 0, len(valuesStr)) + + for _, str := range valuesStr { + val, err := strconv.ParseFloat(str, 64) + if err != nil { + return nil, err + } + buckets = append(buckets, val) + } + + return buckets, nil +} + +func initGithubWorkflowJobQueueDurationSeconds(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: runtimeBuckets, + Buckets: buckets, }, metricLabels(), ) - githubWorkflowJobRunDurationSeconds = prometheus.NewHistogramVec( +} + +func initGithubWorkflowJobRunDurationSeconds(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: runtimeBuckets, + Buckets: buckets, }, metricLabels("job_conclusion"), ) - githubWorkflowJobConclusionsTotal = prometheus.NewCounterVec( +} + +var ( + commonLabels = []string{"runs_on", "job_name", "organization", "repository", "repository_full_name", "owner", "workflow_name", "head_branch"} + githubWorkflowJobQueueDurationSeconds *prometheus.HistogramVec + githubWorkflowJobRunDurationSeconds *prometheus.HistogramVec + githubWorkflowJobConclusionsTotal = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "github_workflow_job_conclusions_total", Help: "Conclusions for tracked workflow jobs",