metrics.go 1.9 KB

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