prompush.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2015 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. //This is a utility for prometheus pushing functionality.
  14. package framework
  15. import (
  16. "fmt"
  17. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. // Prometheus stuff: Setup metrics.
  20. var runningMetric = prometheus.NewGauge(prometheus.GaugeOpts{
  21. Name: "e2e_running",
  22. Help: "The num of running pods",
  23. })
  24. var pendingMetric = prometheus.NewGauge(prometheus.GaugeOpts{
  25. Name: "e2e_pending",
  26. Help: "The num of pending pods",
  27. })
  28. // Turn this to true after we register.
  29. var prom_registered = false
  30. // Reusable function for pushing metrics to prometheus. Handles initialization and so on.
  31. func promPushRunningPending(running, pending int) error {
  32. if TestContext.PrometheusPushGateway == "" {
  33. return nil
  34. } else {
  35. // Register metrics if necessary
  36. if !prom_registered && TestContext.PrometheusPushGateway != "" {
  37. prometheus.Register(runningMetric)
  38. prometheus.Register(pendingMetric)
  39. prom_registered = true
  40. }
  41. // Update metric values
  42. runningMetric.Set(float64(running))
  43. pendingMetric.Set(float64(pending))
  44. // Push them to the push gateway. This will be scraped by prometheus
  45. // provided you launch it with the pushgateway as an endpoint.
  46. if err := prometheus.Push(
  47. "e2e",
  48. "none",
  49. TestContext.PrometheusPushGateway, //i.e. "127.0.0.1:9091"
  50. ); err != nil {
  51. fmt.Println("failed at pushing to pushgateway ", err)
  52. return err
  53. }
  54. }
  55. return nil
  56. }