github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/kube/metrics.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kube
    18  
    19  import (
    20  	"github.com/prometheus/client_golang/prometheus"
    21  )
    22  
    23  var (
    24  	prowJobs = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    25  		Name: "prowjobs",
    26  		Help: "Number of prowjobs in the system",
    27  	}, []string{
    28  		// name of the job
    29  		"job_name",
    30  		// type of the prowjob: presubmit, postsubmit, periodic, batch
    31  		"type",
    32  		// state of the prowjob: triggered, pending, success, failure, aborted, error
    33  		"state",
    34  	})
    35  )
    36  
    37  func init() {
    38  	prometheus.MustRegister(prowJobs)
    39  }
    40  
    41  // GatherProwJobMetrics gathers prometheus metrics for prowjobs.
    42  func GatherProwJobMetrics(pjs []ProwJob) {
    43  	// map of job to job type to state to count
    44  	metricMap := make(map[string]map[string]map[string]float64)
    45  
    46  	for _, pj := range pjs {
    47  		if metricMap[pj.Spec.Job] == nil {
    48  			metricMap[pj.Spec.Job] = make(map[string]map[string]float64)
    49  		}
    50  		if metricMap[pj.Spec.Job][string(pj.Spec.Type)] == nil {
    51  			metricMap[pj.Spec.Job][string(pj.Spec.Type)] = make(map[string]float64)
    52  		}
    53  		metricMap[pj.Spec.Job][string(pj.Spec.Type)][string(pj.Status.State)]++
    54  	}
    55  
    56  	// This may be racing with the prometheus server but we need to remove
    57  	// stale metrics like triggered or pending jobs that are now complete.
    58  	prowJobs.Reset()
    59  
    60  	for job, jobMap := range metricMap {
    61  		for jobType, typeMap := range jobMap {
    62  			for state, count := range typeMap {
    63  				prowJobs.WithLabelValues(job, jobType, state).Set(count)
    64  			}
    65  		}
    66  	}
    67  }