push.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Copyright (c) 2013, The Prometheus Authors
  14. // All rights reserved.
  15. //
  16. // Use of this source code is governed by a BSD-style license that can be found
  17. // in the LICENSE file.
  18. package prometheus
  19. // Push triggers a metric collection by the default registry and pushes all
  20. // collected metrics to the Pushgateway specified by addr. See the Pushgateway
  21. // documentation for detailed implications of the job and instance
  22. // parameter. instance can be left empty. You can use just host:port or ip:port
  23. // as url, in which case 'http://' is added automatically. You can also include
  24. // the schema in the URL. However, do not include the '/metrics/jobs/...' part.
  25. //
  26. // Note that all previously pushed metrics with the same job and instance will
  27. // be replaced with the metrics pushed by this call. (It uses HTTP method 'PUT'
  28. // to push to the Pushgateway.)
  29. func Push(job, instance, url string) error {
  30. return defRegistry.Push(job, instance, url, "PUT")
  31. }
  32. // PushAdd works like Push, but only previously pushed metrics with the same
  33. // name (and the same job and instance) will be replaced. (It uses HTTP method
  34. // 'POST' to push to the Pushgateway.)
  35. func PushAdd(job, instance, url string) error {
  36. return defRegistry.Push(job, instance, url, "POST")
  37. }
  38. // PushCollectors works like Push, but it does not collect from the default
  39. // registry. Instead, it collects from the provided collectors. It is a
  40. // convenient way to push only a few metrics.
  41. func PushCollectors(job, instance, url string, collectors ...Collector) error {
  42. return pushCollectors(job, instance, url, "PUT", collectors...)
  43. }
  44. // PushAddCollectors works like PushAdd, but it does not collect from the
  45. // default registry. Instead, it collects from the provided collectors. It is a
  46. // convenient way to push only a few metrics.
  47. func PushAddCollectors(job, instance, url string, collectors ...Collector) error {
  48. return pushCollectors(job, instance, url, "POST", collectors...)
  49. }
  50. func pushCollectors(job, instance, url, method string, collectors ...Collector) error {
  51. r := newRegistry()
  52. for _, collector := range collectors {
  53. if _, err := r.Register(collector); err != nil {
  54. return err
  55. }
  56. }
  57. return r.Push(job, instance, url, method)
  58. }